package mapatest import ( "bytes" "io" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "strings" "sync" "testing" "amadan.net/oficina/mapa/internal/mapa" ) // IMG2, the plaintext file scan for images (images spec ยง6), extending // gate 4. A PNG and a JPEG carry canaries in tEXt, EXIF, XMP and a // trailer after EOI, in the file name, and in the alt text. Afterwards // the database (WAL included) and the bucket hold no canary and no alt // text; no object starts with a PNG or JPEG magic number; the bucket // was told application/octet-stream; and every object is a multiple of // 64 KiB plus the seal's constant. const altCanary = "CANARYALT a whiteboard of salaries" // fakeS3 is a bucket over HTTP, path-style, as blobs.S3 speaks to an // endpoint: it keeps each object as a file and records each PUT's // Content-Type. type fakeS3 struct { dir string mutex sync.Mutex types []string } func (bucket *fakeS3) ServeHTTP(writer http.ResponseWriter, request *http.Request) { path := filepath.Join(bucket.dir, filepath.Base(request.URL.Path)) switch request.Method { case http.MethodPut: raw, _ := io.ReadAll(request.Body) bucket.mutex.Lock() bucket.types = append(bucket.types, request.Header.Get("Content-Type")) bucket.mutex.Unlock() _ = os.WriteFile(path, raw, 0o600) case http.MethodGet: raw, err := os.ReadFile(path) if err != nil { http.NotFound(writer, request) return } _, _ = writer.Write(raw) case http.MethodDelete: _ = os.Remove(path) writer.WriteHeader(http.StatusNoContent) } } // s3Rig is a rig whose images go to a fake S3. func s3Rig(t *testing.T) (*rig, *fakeS3) { t.Helper() bucket := &fakeS3{dir: t.TempDir()} server := httptest.NewServer(bucket) t.Cleanup(server.Close) // Through the config a CARLOS store arrives as, so the Put is the // one production makes. env := testEnv(Ana + "," + Bo) env["CARLOS_STORE_BUCKET"], env["CARLOS_STORE_REGION"] = "mapa", "eu-west-1" env["CARLOS_STORE_ENDPOINT"] = server.URL env["CARLOS_STORE_ACCESS_KEY"], env["CARLOS_STORE_SECRET_KEY"] = "key", "secret" delivered, err := mapa.LoadConfig(func(key string) string { return env[key] }) if err != nil || delivered.Images.Objects == nil { t.Fatalf("the store was not bound: %v", err) } rig := newRigWith(t, func(config *mapa.Config, _ string) { config.Images = delivered.Images }) return rig, bucket } func fixture(t *testing.T, name string) []byte { t.Helper() raw, err := os.ReadFile(filepath.Join("..", "mapa", "imagefile", "testdata", name)) if err != nil { t.Fatal(err) } if !bytes.Contains(raw, []byte("CANARY-")) { t.Fatalf("%s carries no canary", name) } return raw } func TestImagesReachTheDiskSealed(t *testing.T) { rig, bucket := s3Rig(t) var body []string for _, name := range []string{"meta.png", "baseline.jpg"} { reply := rig.sendFile(t, Ana, "/images", nil, "CANARY-FILENAME-"+name, fixture(t, name)) if reply.Code != http.StatusOK { t.Fatalf("%s: %d %s", name, reply.Code, reply.Body.String()) } src := between(reply.Body.String(), `"src":"`, `"`) body = append(body, "!["+altCanary+"]("+src+")") } rig.post(t, Ana, "/scratch", url.Values{"body": { strings.Join(body, "\n\n")}}) if err := rig.db.Close(); err != nil { t.Fatal(err) } looking := []string{"CANARY", altCanary} for _, hit := range scan(t, rig.dir, looking) { t.Errorf("plaintext in the database: %s", hit) } for _, hit := range scan(t, bucket.dir, looking) { t.Errorf("plaintext in the bucket: %s", hit) } checkObjects(t, bucket) } // checkObjects holds each object to IMG2's shape. func checkObjects(t *testing.T, bucket *fakeS3) { t.Helper() entries, err := os.ReadDir(bucket.dir) if err != nil || len(entries) != 2 { t.Fatalf("%d objects: %v", len(entries), err) } for _, entry := range entries { raw, _ := os.ReadFile(filepath.Join(bucket.dir, entry.Name())) if bytes.HasPrefix(raw, []byte("\x89PNG")) || bytes.HasPrefix(raw, []byte{0xff, 0xd8, 0xff}) { t.Errorf("%s starts with an image's magic number", entry.Name()) } if (len(raw)-28)%(64<<10) != 0 { t.Errorf("%s is %d bytes: not 64 KiB steps plus the seal", entry.Name(), len(raw)) } } for _, kind := range bucket.types { if kind != "application/octet-stream" { t.Errorf("the bucket was told %q", kind) } } }