| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | ) |
| 9 | |
| 10 | // Reading an upload (Fable H1 on 49d8df3). ParseMultipartForm writes a |
| 11 | // part over its memory bound to a temporary file, and chi hands each |
| 12 | // handler a copy of the request, so net/http's cleanup of those files |
| 13 | // never ran: a picture over 1 MiB stayed in $TMPDIR as sent, metadata |
| 14 | // and all, which is plaintext at rest. Mapa reads the parts itself, |
| 15 | // into memory bounded by MaxBytesReader, and nothing reaches the disk. |
| 16 | // The fields come before the file in every form, so text typed on the |
| 17 | // Markdown screen is kept even when the file is refused (Fable L5). |
| 18 | |
| 19 | // errUnread is an upload not read at all: every UploadReads slot was |
| 20 | // taken. Its form's text never arrived, so the Markdown screen is not |
| 21 | // shown again (it would be empty): a 503 page, and Back keeps the text. |
| 22 | var errUnread = errors.New("mapa: the upload was not read") |
| 23 | |
| 24 | // errUnreadable is a multipart body that is not one: a broken part, not |
| 25 | // a picture of the wrong kind (Fable N8). |
| 26 | var errUnreadable = errors.New("mapa: the form could not be read") |
| 27 | |
| 28 | // errNoFile is an upload with no file chosen. |
| 29 | var errNoFile = errors.New("Choose an image file first.") |
| 30 | |
| 31 | // uploadParts is one multipart request: its text fields, and the file's |
| 32 | // bytes, or why the file was refused. |
| 33 | type uploadParts struct { |
| 34 | fields url.Values |
| 35 | file []byte |
| 36 | err error |
| 37 | } |
| 38 | |
| 39 | // readUpload reads request's parts, the text ones up to text bytes in |
| 40 | // all and the file up to MaxImageBytes. It sets PostForm, so the |
| 41 | // handler reads the fields as from any form. |
| 42 | func readUpload(writer http.ResponseWriter, request *http.Request, |
| 43 | text int64) uploadParts { |
| 44 | request.Body = http.MaxBytesReader(writer, request.Body, |
| 45 | text+MaxImageBytes+uploadSlack) |
| 46 | read := uploadParts{fields: url.Values{}, err: errNoFile} |
| 47 | request.PostForm, request.Form = read.fields, read.fields |
| 48 | parts, err := request.MultipartReader() |
| 49 | if err != nil { |
| 50 | read.err = ErrImageType |
| 51 | return read |
| 52 | } |
| 53 | left := text + uploadSlack |
| 54 | for { |
| 55 | part, err := parts.NextPart() |
| 56 | if errors.Is(err, io.EOF) { |
| 57 | return read |
| 58 | } |
| 59 | if err != nil { |
| 60 | read.err = sizeOr(err, errUnreadable) |
| 61 | return read |
| 62 | } |
| 63 | if part.FormName() == "file" { |
| 64 | read.takeFile(part) |
| 65 | continue |
| 66 | } |
| 67 | value, err := io.ReadAll(io.LimitReader(part, left+1)) |
| 68 | left -= int64(len(value)) |
| 69 | if err != nil { |
| 70 | // Over the request's bound, or a body that is not a form. |
| 71 | read.err = sizeOr(err, errUnreadable) |
| 72 | return read |
| 73 | } |
| 74 | if left < 0 { |
| 75 | read.err = ErrImageTooLarge |
| 76 | return read |
| 77 | } |
| 78 | read.fields.Add(part.FormName(), string(value)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // takeFile reads the file part: none chosen, too large, or its bytes. |
| 83 | func (read *uploadParts) takeFile(part io.Reader) { |
| 84 | data, err := io.ReadAll(io.LimitReader(part, MaxImageBytes+1)) |
| 85 | switch { |
| 86 | case err != nil: |
| 87 | read.err = sizeOr(err, ErrImageType) |
| 88 | case len(data) > MaxImageBytes: |
| 89 | read.err = ErrImageTooLarge |
| 90 | case len(data) == 0: |
| 91 | read.err = errNoFile |
| 92 | default: |
| 93 | read.file, read.err = data, nil |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // sizeOr is ErrImageTooLarge for a request over its bound, else other. |
| 98 | func sizeOr(err, other error) error { |
| 99 | var tooLarge *http.MaxBytesError |
| 100 | if errors.As(err, &tooLarge) { |
| 101 | return ErrImageTooLarge |
| 102 | } |
| 103 | return other |
| 104 | } |
| 105 | |
| 106 | // receiveImage reads an upload and stores its file. |
| 107 | // It holds one of the UploadReads slots while it reads and stores |
| 108 | // (Fable N7, round 5), and hands the bytes on without another copy. |
| 109 | func (app *app) receiveImage(writer http.ResponseWriter, |
| 110 | request *http.Request, text int64) (Image, error) { |
| 111 | release, err := app.store.AcquireRead(request.Context()) |
| 112 | if err != nil { |
| 113 | return Image{}, errors.Join(errUnread, err) |
| 114 | } |
| 115 | // Held until the image is stored, so the bodies waiting for a blob |
| 116 | // slot are bounded too (Fable, round 5). |
| 117 | defer release() |
| 118 | read := readUpload(writer, request, text) |
| 119 | if read.err != nil { |
| 120 | return Image{}, read.err |
| 121 | } |
| 122 | address, _ := app.identity(request) |
| 123 | return app.store.UploadBytes(request.Context(), address, read.file) |
| 124 | } |
| 125 | |