| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "hash/crc32" |
| 7 | "image" |
| 8 | "image/png" |
| 9 | "math/rand" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "net/url" |
| 13 | "os" |
| 14 | "path/filepath" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
| 18 | // IMG2 through a real server (Fable H1 on 49d8df3): an upload over |
| 19 | // 1 MiB, the size past which ParseMultipartForm would spill a part to |
| 20 | // a temporary file, leaves nothing on disk, by POST /images and by the |
| 21 | // Markdown screen's Upload and insert. A part spilled to $TMPDIR was |
| 22 | // never removed, and held the picture as sent, metadata and all. |
| 23 | |
| 24 | // bigCanaryPNG is a PNG of about 1.5 MiB of noise, carrying a canary in |
| 25 | // a tEXt chunk. |
| 26 | func bigCanaryPNG(t *testing.T) []byte { |
| 27 | t.Helper() |
| 28 | canvas := image.NewNRGBA(image.Rect(0, 0, 700, 560)) |
| 29 | rand.New(rand.NewSource(7)).Read(canvas.Pix) |
| 30 | var out bytes.Buffer |
| 31 | if err := png.Encode(&out, canvas); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | raw := out.Bytes() |
| 35 | data := []byte("Comment\x00CANARY-SPILLED-METADATA") |
| 36 | chunk := append([]byte("tEXt"), data...) |
| 37 | text := binary.BigEndian.AppendUint32(nil, uint32(len(data))) |
| 38 | text = append(text, chunk...) |
| 39 | text = binary.BigEndian.AppendUint32(text, crc32.ChecksumIEEE(chunk)) |
| 40 | iend := len(raw) - 12 |
| 41 | big := append(append(append([]byte{}, raw[:iend]...), text...), |
| 42 | raw[iend:]...) |
| 43 | if len(big) <= 1<<20 { |
| 44 | t.Fatalf("the picture is only %d bytes", len(big)) |
| 45 | } |
| 46 | return big |
| 47 | } |
| 48 | |
| 49 | func TestAnUploadSpillsNothingToDisk(t *testing.T) { |
| 50 | spill := t.TempDir() |
| 51 | t.Setenv("TMPDIR", spill) |
| 52 | rig := newRig(t) |
| 53 | server := httptest.NewServer(rig.built.As(Ana)) |
| 54 | defer server.Close() |
| 55 | picture := bigCanaryPNG(t) |
| 56 | for _, target := range []string{"/images", "/scratch?upload=1"} { |
| 57 | body, kind := multipartOf(t, url.Values{"body": {"Kept text"}}, |
| 58 | "photo.png", picture) |
| 59 | request, _ := http.NewRequest(http.MethodPost, server.URL+target, body) |
| 60 | request.Header.Set("Content-Type", kind) |
| 61 | request.Header.Set("Origin", testOrigin) |
| 62 | request.Header.Set("Accept", "application/json") |
| 63 | response, err := http.DefaultClient.Do(request) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | response.Body.Close() |
| 68 | if response.StatusCode != http.StatusOK { |
| 69 | t.Fatalf("%s: %d", target, response.StatusCode) |
| 70 | } |
| 71 | } |
| 72 | entries, err := os.ReadDir(spill) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | for _, entry := range entries { |
| 77 | raw, _ := os.ReadFile(filepath.Join(spill, entry.Name())) |
| 78 | t.Errorf("%s was left on disk (%d bytes, canary %v)", entry.Name(), |
| 79 | len(raw), bytes.Contains(raw, []byte("CANARY-SPILLED"))) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Fable N3 on 4c95863: every other route parses its form with |
| 84 | // PostFormValue, which spills a file part over 32 MiB to $TMPDIR, the |
| 85 | // sign-in door's included. Only the upload routes take multipart; every |
| 86 | // other body is refused as 415 before anything reads it, and bounded. |
| 87 | func TestOnlyUploadRoutesTakeMultipart(t *testing.T) { |
| 88 | spill := t.TempDir() |
| 89 | t.Setenv("TMPDIR", spill) |
| 90 | rig := newRig(t) |
| 91 | server := httptest.NewServer(rig.built.As(Ana)) |
| 92 | defer server.Close() |
| 93 | picture := bigCanaryPNG(t) |
| 94 | // ?upload=1 makes an upload only where an upload is read (round 5). |
| 95 | for _, target := range []string{"/auth/begin", "/pages", "/settings", |
| 96 | "/scratch", "/auth/begin?upload=1", "/pages?upload=1"} { |
| 97 | body, kind := multipartOf(t, url.Values{"title": {"x"}}, "a.png", |
| 98 | picture) |
| 99 | request, _ := http.NewRequest(http.MethodPost, server.URL+target, body) |
| 100 | request.Header.Set("Content-Type", kind) |
| 101 | request.Header.Set("Origin", testOrigin) |
| 102 | response, err := http.DefaultClient.Do(request) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | response.Body.Close() |
| 107 | if response.StatusCode != http.StatusUnsupportedMediaType { |
| 108 | t.Errorf("%s took multipart: %d", target, response.StatusCode) |
| 109 | } |
| 110 | } |
| 111 | if entries, _ := os.ReadDir(spill); len(entries) != 0 { |
| 112 | t.Errorf("%d files spilled", len(entries)) |
| 113 | } |
| 114 | } |
| 115 | |