| 1 | // Package imagefile reads an uploaded image (images spec §5): its type |
| 2 | // from its bytes, its size from the format's own decoder, and a copy |
| 3 | // with its metadata taken out by a container walker that never decodes |
| 4 | // a pixel. The claimed Content-Type and the file name are never read. |
| 5 | // |
| 6 | // Each walker drops named kinds of metadata and keeps the image's own |
| 7 | // structure, so a segment it does not list is never lost when the |
| 8 | // picture needs it, and ends the file where the format ends it. A file |
| 9 | // a walker cannot parse is refused, never passed through: location, |
| 10 | // camera serials, authors, edit history, thumbnails and trailers never |
| 11 | // reach the bucket. |
| 12 | package imagefile |
| 13 | |
| 14 | import ( |
| 15 | "bytes" |
| 16 | "errors" |
| 17 | "image" |
| 18 | _ "image/gif" // DecodeConfig for GIF |
| 19 | _ "image/jpeg" // DecodeConfig for JPEG |
| 20 | _ "image/png" // DecodeConfig for PNG |
| 21 | |
| 22 | _ "golang.org/x/image/webp" // DecodeConfig for WebP |
| 23 | ) |
| 24 | |
| 25 | // The limits (§5): at most 10,000 px on a side and 50 megapixels in |
| 26 | // all, read from the header before any further work. Mapa never decodes |
| 27 | // pixels, so a decompression bomb costs it nothing. |
| 28 | const ( |
| 29 | MaxSide = 10000 |
| 30 | MaxPixels = 50_000_000 |
| 31 | ) |
| 32 | |
| 33 | // The four types Mapa takes. |
| 34 | const ( |
| 35 | PNG = "image/png" |
| 36 | JPEG = "image/jpeg" |
| 37 | GIF = "image/gif" |
| 38 | WebP = "image/webp" |
| 39 | ) |
| 40 | |
| 41 | // Refusals. ErrType is anything but the four formats, SVG, HEIC and |
| 42 | // HTML included; ErrDimensions is a picture past the limits; |
| 43 | // ErrMalformed is a file of one of the four that its walker or decoder |
| 44 | // cannot read. |
| 45 | var ( |
| 46 | ErrType = errors.New("imagefile: not a PNG, JPEG, GIF or WebP") |
| 47 | ErrDimensions = errors.New("imagefile: the picture is too large") |
| 48 | ErrMalformed = errors.New("imagefile: the file cannot be read") |
| 49 | ) |
| 50 | |
| 51 | // Info is what Mapa keeps of a picture, sealed: its type, and its width |
| 52 | // and height as shown, after a JPEG's orientation. |
| 53 | type Info struct { |
| 54 | Type string `json:"type"` |
| 55 | Width, Height int |
| 56 | } |
| 57 | |
| 58 | // Sniff is the type the bytes say, or "" for anything else. |
| 59 | func Sniff(raw []byte) string { |
| 60 | switch { |
| 61 | case bytes.HasPrefix(raw, []byte("\x89PNG\r\n\x1a\n")): |
| 62 | return PNG |
| 63 | case bytes.HasPrefix(raw, []byte{0xff, 0xd8, 0xff}): |
| 64 | return JPEG |
| 65 | case bytes.HasPrefix(raw, []byte("GIF87a")), |
| 66 | bytes.HasPrefix(raw, []byte("GIF89a")): |
| 67 | return GIF |
| 68 | case len(raw) >= 12 && bytes.HasPrefix(raw, []byte("RIFF")) && |
| 69 | string(raw[8:12]) == "WEBP": |
| 70 | return WebP |
| 71 | } |
| 72 | return "" |
| 73 | } |
| 74 | |
| 75 | // walkers strip each format. Each returns the cleaned file and, for a |
| 76 | // JPEG, its EXIF orientation (1 for upright or unknown). |
| 77 | var walkers = map[string]func([]byte) ([]byte, int, error){ |
| 78 | PNG: cleanPNG, JPEG: cleanJPEG, GIF: cleanGIF, WebP: cleanWebP, |
| 79 | } |
| 80 | |
| 81 | // formats is image.DecodeConfig's name for each type. |
| 82 | var formats = map[string]string{ |
| 83 | PNG: "png", JPEG: "jpeg", GIF: "gif", WebP: "webp", |
| 84 | } |
| 85 | |
| 86 | // Clean checks raw and returns it with its metadata taken out. |
| 87 | func Clean(raw []byte) ([]byte, Info, error) { |
| 88 | kind := Sniff(raw) |
| 89 | if kind == "" { |
| 90 | return nil, Info{}, ErrType |
| 91 | } |
| 92 | config, err := checkConfig(raw, kind) |
| 93 | if err != nil { |
| 94 | return nil, Info{}, err |
| 95 | } |
| 96 | cleaned, orientation, err := walkers[kind](raw) |
| 97 | if err != nil { |
| 98 | return nil, Info{}, err |
| 99 | } |
| 100 | after, err := checkConfig(cleaned, kind) |
| 101 | if err != nil || after != config { |
| 102 | return nil, Info{}, ErrMalformed |
| 103 | } |
| 104 | info := Info{Type: kind, Width: config.Width, Height: config.Height} |
| 105 | if orientation >= 5 { |
| 106 | info.Width, info.Height = info.Height, info.Width |
| 107 | } |
| 108 | return cleaned, info, nil |
| 109 | } |
| 110 | |
| 111 | // checkConfig reads the header with the format's own decoder, which |
| 112 | // must agree on the type, and holds it to the limits. |
| 113 | func checkConfig(raw []byte, kind string) (image.Config, error) { |
| 114 | config, format, err := image.DecodeConfig(bytes.NewReader(raw)) |
| 115 | if err != nil || format != formats[kind] { |
| 116 | return config, ErrMalformed |
| 117 | } |
| 118 | if config.Width <= 0 || config.Height <= 0 { |
| 119 | return config, ErrMalformed |
| 120 | } |
| 121 | if config.Width > MaxSide || config.Height > MaxSide || |
| 122 | int64(config.Width)*int64(config.Height) > MaxPixels { |
| 123 | return config, ErrDimensions |
| 124 | } |
| 125 | return image.Config{Width: config.Width, Height: config.Height}, nil |
| 126 | } |
| 127 | |