package mapatest import ( "bytes" "encoding/binary" "hash/crc32" "image" "image/png" "math/rand" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "testing" ) // IMG2 through a real server (Fable H1 on 49d8df3): an upload over // 1 MiB, the size past which ParseMultipartForm would spill a part to // a temporary file, leaves nothing on disk, by POST /images and by the // Markdown screen's Upload and insert. A part spilled to $TMPDIR was // never removed, and held the picture as sent, metadata and all. // bigCanaryPNG is a PNG of about 1.5 MiB of noise, carrying a canary in // a tEXt chunk. func bigCanaryPNG(t *testing.T) []byte { t.Helper() canvas := image.NewNRGBA(image.Rect(0, 0, 700, 560)) rand.New(rand.NewSource(7)).Read(canvas.Pix) var out bytes.Buffer if err := png.Encode(&out, canvas); err != nil { t.Fatal(err) } raw := out.Bytes() data := []byte("Comment\x00CANARY-SPILLED-METADATA") chunk := append([]byte("tEXt"), data...) text := binary.BigEndian.AppendUint32(nil, uint32(len(data))) text = append(text, chunk...) text = binary.BigEndian.AppendUint32(text, crc32.ChecksumIEEE(chunk)) iend := len(raw) - 12 big := append(append(append([]byte{}, raw[:iend]...), text...), raw[iend:]...) if len(big) <= 1<<20 { t.Fatalf("the picture is only %d bytes", len(big)) } return big } func TestAnUploadSpillsNothingToDisk(t *testing.T) { spill := t.TempDir() t.Setenv("TMPDIR", spill) rig := newRig(t) server := httptest.NewServer(rig.built.As(Ana)) defer server.Close() picture := bigCanaryPNG(t) for _, target := range []string{"/images", "/scratch?upload=1"} { body, kind := multipartOf(t, url.Values{"body": {"Kept text"}}, "photo.png", picture) request, _ := http.NewRequest(http.MethodPost, server.URL+target, body) request.Header.Set("Content-Type", kind) request.Header.Set("Origin", testOrigin) request.Header.Set("Accept", "application/json") response, err := http.DefaultClient.Do(request) if err != nil { t.Fatal(err) } response.Body.Close() if response.StatusCode != http.StatusOK { t.Fatalf("%s: %d", target, response.StatusCode) } } entries, err := os.ReadDir(spill) if err != nil { t.Fatal(err) } for _, entry := range entries { raw, _ := os.ReadFile(filepath.Join(spill, entry.Name())) t.Errorf("%s was left on disk (%d bytes, canary %v)", entry.Name(), len(raw), bytes.Contains(raw, []byte("CANARY-SPILLED"))) } } // Fable N3 on 4c95863: every other route parses its form with // PostFormValue, which spills a file part over 32 MiB to $TMPDIR, the // sign-in door's included. Only the upload routes take multipart; every // other body is refused as 415 before anything reads it, and bounded. func TestOnlyUploadRoutesTakeMultipart(t *testing.T) { spill := t.TempDir() t.Setenv("TMPDIR", spill) rig := newRig(t) server := httptest.NewServer(rig.built.As(Ana)) defer server.Close() picture := bigCanaryPNG(t) // ?upload=1 makes an upload only where an upload is read (round 5). for _, target := range []string{"/auth/begin", "/pages", "/settings", "/scratch", "/auth/begin?upload=1", "/pages?upload=1"} { body, kind := multipartOf(t, url.Values{"title": {"x"}}, "a.png", picture) request, _ := http.NewRequest(http.MethodPost, server.URL+target, body) request.Header.Set("Content-Type", kind) request.Header.Set("Origin", testOrigin) response, err := http.DefaultClient.Do(request) if err != nil { t.Fatal(err) } response.Body.Close() if response.StatusCode != http.StatusUnsupportedMediaType { t.Errorf("%s took multipart: %d", target, response.StatusCode) } } if entries, _ := os.ReadDir(spill); len(entries) != 0 { t.Errorf("%d files spilled", len(entries)) } }