Open platform to collect and prioritize feedback
Use a memory-based decode budget and a single safe decode path (GHSA-7cw3-7xh9-529r)
Matt Robertsb480e1877918
diff --git a/app/handlers/images.go b/app/handlers/images.goindex ea8e479e6..91dfa5eea 100644--- a/app/handlers/images.go+++ b/app/handlers/images.go@@ -153,15 +153,12 @@ func Favicon() web.HandlerFunc {opts = append(opts, imagic.ChangeBackground(color.White))}- safe, err := isSafeToDecode(c, bkey, bytes)- if err != nil {- return c.Failure(err)- }- if !safe {- return c.Image(contentType, bytes)+ if bkey == "" {+ // Bundled favicon is a trusted asset, no need to check the decode budget+ bytes, err = validate.ApplyTrusted(c, bytes, opts...)+ } else {+ bytes, err = applyOrServeOriginal(c, bytes, opts...)}-- bytes, err = imagic.Apply(bytes, opts...)if err != nil {return c.Failure(err)}@@ -190,35 +187,24 @@ func ViewUploadedImage() web.HandlerFunc {bytes := q.Result.Contentif size > 0 {- safe, err := isSafeToDecode(c, bkey, bytes)+ bytes, err = applyOrServeOriginal(c, bytes, imagic.Resize(size))if err != nil {return c.Failure(err)}- if safe {- bytes, err = imagic.Apply(bytes, imagic.Resize(size))- if err != nil {- return c.Failure(err)- }- }}return c.Image(q.Result.ContentType, bytes)}}-// isSafeToDecode checks the image header (without decoding pixel data) to make sure-// the image is within the pixel budget before it is fully decoded for resizing.-// Images over the budget (e.g. decompression bombs stored before uploads were validated)-// are served as-is rather than decoded, which would otherwise exhaust server memory.-func isSafeToDecode(c *web.Context, bkey string, content []byte) (bool, error) {- ok, err := validate.ImageWithinPixelBudget(content)- if err != nil {- return false, err- }- if !ok {- log.Warnf(c, "Image @{BlobKey} exceeds the pixel budget, serving without resizing", dto.Props{- "BlobKey": bkey,- })+// applyOrServeOriginal applies the operations to a stored (user uploaded) image.+// Images that are too large to be safely decoded (e.g. decompression bombs stored before+// uploads were validated) or in a format we don't process are returned unchanged+// instead of being decoded.+func applyOrServeOriginal(c *web.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error) {+ result, err := validate.SafeApply(c, content, operations...)+ if err == validate.ErrImageTooLarge || err == imagic.ErrNotSupported {+ return content, nil}- return ok, nil+ return result, err}diff --git a/app/handlers/images_test.go b/app/handlers/images_test.goindex 18c7147f9..2f978e6b0 100644--- a/app/handlers/images_test.go+++ b/app/handlers/images_test.go@@ -171,6 +171,40 @@ func TestFavicon_DecompressionBomb_ServedWithoutDecoding(t *testing.T) {Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()}+func TestViewUploadedImage_UnsupportedFormat_ServedAsIs(t *testing.T) {+ RegisterT(t)+ bus.Init()+ // e.g. a WebP image, which imagic doesn't support+ content := []byte("RIFF\x00\x00\x00\x00WEBPVP8 ")+ mockBlob(content, "image/webp")++ server := mock.NewServer()+ code, response := server.+ WithURL("https://demo.test.fider.io/?size=500").+ OnTenant(mock.DemoTenant).+ AddParam("bkey", "attachments/image.webp").+ Execute(handlers.ViewUploadedImage())++ Expect(code).Equals(http.StatusOK)+ Expect(response.Body.Bytes()).Equals(content)+}++func TestFavicon_Bundled(t *testing.T) {+ RegisterT(t)+ bus.Init()++ server := mock.NewServer()+ code, response := server.+ WithURL("https://demo.test.fider.io/?size=64").+ OnTenant(mock.DemoTenant).+ Execute(handlers.Favicon())++ Expect(code).Equals(http.StatusOK)+ width, height := responseDimensions(response.Body.Bytes())+ Expect(width).Equals(64)+ Expect(height).Equals(64)+}+func TestFavicon_Resize(t *testing.T) {RegisterT(t)bus.Init()diff --git a/app/pkg/validate/image.go b/app/pkg/validate/image.goindex 2bc7d31bd..7fcd8ddc1 100644--- a/app/pkg/validate/image.go+++ b/app/pkg/validate/image.go@@ -2,45 +2,155 @@ package validateimport ("bytes"+ "context"+ "errors""image"+ "image/color"- // Register the decoders for every format we accept, so image.DecodeConfig+ // Register the decoders for every format imagic accepts, so image.DecodeConfig// can read their headers regardless of which other packages are imported._ "image/gif"_ "image/jpeg"_ "image/png"++ "github.com/goenning/imagic")-// MaxImagePixels is the maximum number of pixels (width * height) an image may have-// before we refuse to fully decode it.+// MaxDecodeBytes is the estimated worst case amount of memory a single image decode+// (plus one full-size intermediate image) may use. Images over this are never decoded.//-// Decoding an image allocates roughly width*height*4 bytes (NRGBA), regardless of how-// small the compressed file is. A 12000x12000 PNG of a single colour compresses to a-// few hundred KB, but decodes to ~576MB. With a 40 megapixel budget the worst case-// decode is ~160MB, while still accepting photos from modern cameras and phones-// (e.g. 24MP phone photos, 36MP DSLR images) that fit within the 5MB upload limit.-const MaxImagePixels int64 = 40_000_000--// IsWithinPixelBudget returns true if an image of the given dimensions is safe to decode.-// Non-positive dimensions are rejected.-func IsWithinPixelBudget(width, height int) bool {- if width <= 0 || height <= 0 {- return false- }- // Check each side first so the multiplication below can't overflow int64- if int64(width) > MaxImagePixels || int64(height) > MaxImagePixels {- return false- }- return int64(width)*int64(height) <= MaxImagePixels+// A decode allocates memory proportional to width*height, regardless of how small the+// compressed file is, so a tiny file can be a "decompression bomb". See+// EstimateDecodeBytes for the per-format estimates. With 256MB the largest images that+// can be decoded are roughly:+// - JPEG (colour): ~14 megapixels (e.g. 12MP phone photos)+// - PNG (8-bit RGB/RGBA): ~22 megapixels+// - PNG (8-bit gray/paletted): ~44 megapixels+// - GIF: ~53 megapixels+const MaxDecodeBytes int64 = 256 << 20++// MaxImageSide is the maximum width or height of an image that we'll decode.+// Some allocations (padding, resize weights) grow with the longest side, so extreme+// aspect ratios (e.g. 40000000x1) are rejected even if their pixel count is small.+const MaxImageSide = 16384++// maxConcurrentDecodes limits how many images are decoded at the same time across the+// whole process, so parallel requests can't multiply the peak memory usage.+const maxConcurrentDecodes = 2++// fullSizeIntermediateBytesPerPixel accounts for one full-size RGBA copy of the image made by+// an operation before the resize (e.g. imagic.Padding in the Favicon handler).+const fullSizeIntermediateBytesPerPixel = 4++var decodeSemaphore = make(chan struct{}, maxConcurrentDecodes)++// ErrImageTooLarge is returned when an image is estimated to need too much memory to decode+var ErrImageTooLarge = errors.New("image is too large to be decoded")++// EstimateDecodeBytes estimates the worst case memory needed to decode an image with+// the given header info (format and color model as returned by image.DecodeConfig) and+// to create one full-size intermediate copy of it.+// Returns -1 if the dimensions are invalid or beyond MaxImageSide.+func EstimateDecodeBytes(width, height int, format string, model color.Model) int64 {+ if width <= 0 || height <= 0 || width > MaxImageSide || height > MaxImageSide {+ return -1+ }++ var decodeBytesPerPixel int64+ switch format {+ case "jpeg":+ // We can't tell from the header if the JPEG is progressive, so assume it is.+ // A progressive decode keeps all DCT coefficients (64 x int32 per 8x8 block,+ // i.e. 4 bytes per pixel per component) in addition to the decoded image.+ switch model {+ case color.GrayModel:+ decodeBytesPerPixel = 4*1 + 1+ case color.YCbCrModel:+ decodeBytesPerPixel = 4*3 + 3+ case color.RGBAModel: // Adobe RGB JPEG: decoded as YCbCr, then converted to RGBA+ decodeBytesPerPixel = 4*3 + 3 + 4+ default: // CMYK or unknown+ decodeBytesPerPixel = 4*4 + 4+ }+ case "png":+ // Interlaced PNGs decode each pass into its own image before merging, so+ // assume twice the size of the final image.+ decodeBytesPerPixel = 2 * bytesPerPixel(model)+ case "gif":+ // Only the first frame is decoded, and Go rejects frames larger than the logical screen+ decodeBytesPerPixel = bytesPerPixel(model)+ default:+ decodeBytesPerPixel = 2 * bytesPerPixel(model)+ }++ // Can't overflow: at most 16384 * 16384 * (20 + 4)+ return int64(width) * int64(height) * (decodeBytesPerPixel + fullSizeIntermediateBytesPerPixel)+}++func bytesPerPixel(model color.Model) int64 {+ if _, ok := model.(color.Palette); ok {+ return 1+ }+ switch model {+ case color.GrayModel, color.AlphaModel:+ return 1+ case color.Gray16Model, color.Alpha16Model:+ return 2+ case color.RGBAModel, color.NRGBAModel, color.CMYKModel:+ return 4+ case color.RGBA64Model, color.NRGBA64Model:+ return 8+ default:+ return 8+ }}-// ImageWithinPixelBudget reads only the image header (via image.DecodeConfig, which does-// not decode pixel data) and reports whether the image is safe to fully decode.-// An error is returned if the image format is unknown or the header can't be read.-func ImageWithinPixelBudget(content []byte) (bool, error) {- cfg, _, err := image.DecodeConfig(bytes.NewReader(content))+// CheckDecodeBudget reads only the image header and checks that decoding the image+// stays within MaxDecodeBytes and MaxImageSide.+// Returns imagic.ErrNotSupported for unsupported formats and ErrImageTooLarge when over budget.+func CheckDecodeBudget(content []byte) error {+ // imagic.Parse decides which formats are supported, so uploads and serving agree+ file, err := imagic.Parse(content)+ if err != nil {+ return err+ }++ // imagic.Parse doesn't expose the color model, which we need to estimate the+ // bytes per pixel, so read the header again (this does not decode pixel data).+ cfg, format, err := image.DecodeConfig(bytes.NewReader(content))if err != nil {- return false, err+ return imagic.ErrNotSupported+ }++ estimate := EstimateDecodeBytes(file.Width, file.Height, format, cfg.ColorModel)+ if estimate < 0 || estimate > MaxDecodeBytes {+ return ErrImageTooLarge+ }+ return nil+}++// SafeApply is the only way images should be decoded: it checks the decode budget+// (returning ErrImageTooLarge without decoding if exceeded) and then applies the given+// operations, limiting how many decodes happen concurrently.+func SafeApply(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error) {+ if err := CheckDecodeBudget(content); err != nil {+ return nil, err+ }+ return applyWithSemaphore(ctx, content, operations...)+}++// ApplyTrusted applies the operations without checking the decode budget. Only use it for+// trusted images bundled with Fider, never for user content.+func ApplyTrusted(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error) {+ return applyWithSemaphore(ctx, content, operations...)+}++func applyWithSemaphore(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error) {+ select {+ case decodeSemaphore <- struct{}{}:+ defer func() { <-decodeSemaphore }()+ case <-ctx.Done():+ return nil, ctx.Err()}- return IsWithinPixelBudget(cfg.Width, cfg.Height), nil+ return imagic.Apply(content, operations...)}diff --git a/app/pkg/validate/image_internal_test.go b/app/pkg/validate/image_internal_test.gonew file mode 100644index 000000000..4df2d9b97--- /dev/null+++ b/app/pkg/validate/image_internal_test.go@@ -0,0 +1,35 @@+package validate++import (+ "bytes"+ "context"+ "image"+ "image/png"+ "testing"++ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/goenning/imagic"+)++func TestSafeApply_LimitsConcurrentDecodes(t *testing.T) {+ RegisterT(t)++ // Occupy every decode slot+ for i := 0; i < maxConcurrentDecodes; i++ {+ decodeSemaphore <- struct{}{}+ }+ defer func() {+ for i := 0; i < maxConcurrentDecodes; i++ {+ <-decodeSemaphore+ }+ }()++ // A new decode has to wait for a slot, and gives up when the request is cancelled+ ctx, cancel := context.WithCancel(context.Background())+ cancel()+ var buf bytes.Buffer+ Expect(png.Encode(&buf, image.NewGray(image.Rect(0, 0, 100, 100)))).IsNil()+ result, err := SafeApply(ctx, buf.Bytes(), imagic.Resize(50))+ Expect(err).Equals(context.Canceled)+ Expect(result).IsNil()+}diff --git a/app/pkg/validate/upload.go b/app/pkg/validate/upload.goindex 0023632d9..823d3bed6 100644--- a/app/pkg/validate/upload.go+++ b/app/pkg/validate/upload.go@@ -94,11 +94,16 @@ func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadO} else {// imagic.Parse only reads the image header, so width/height are known without// decoding. Reject decompression bombs before anything decodes the pixel data.- if !IsWithinPixelBudget(logo.Width, logo.Height) {- messages = append(messages, i18n.T(ctx, "validation.custom.maximagepixels",- i18n.Params{"megapixels": int(MaxImagePixels / 1_000_000)},- ))- return messages, nil+ if err := CheckDecodeBudget(upload.Upload.Content); err != nil {+ if err == ErrImageTooLarge {+ messages = append(messages, imageTooLargeMessage(ctx))+ return messages, nil+ }+ if err == imagic.ErrNotSupported {+ messages = append(messages, i18n.T(ctx, "validation.custom.unsupportedfileformat"))+ return messages, nil+ }+ return nil, err}if logo.Width < opts.MinWidth || logo.Height < opts.MinHeight {@@ -122,10 +127,12 @@ func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadOreturn messages, nil}- // Resize if either dimension is too large. imagic.Resize keeps the aspect ratio- // (scales the longest side down to MaxDimensionSize) and never upscales.- if logo.Height > MaxDimensionSize || logo.Width > MaxDimensionSize {- newImageBytes, err := imagic.Apply(upload.Upload.Content, imagic.Resize(MaxDimensionSize))+ if logo.Height > MaxDimensionSize && logo.Width > MaxDimensionSize {+ newImageBytes, err := SafeApply(ctx, upload.Upload.Content, imagic.Resize(MaxDimensionSize))+ if err == ErrImageTooLarge {+ messages = append(messages, imageTooLargeMessage(ctx))+ return messages, nil+ }if err != nil {return nil, err}@@ -136,3 +143,7 @@ func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadOreturn messages, nil}++func imageTooLargeMessage(ctx context.Context) string {+ return i18n.T(ctx, "validation.custom.maximagepixels")+}diff --git a/app/pkg/validate/upload_test.go b/app/pkg/validate/upload_test.goindex 48f31afca..6b6b0df34 100644--- a/app/pkg/validate/upload_test.go+++ b/app/pkg/validate/upload_test.go@@ -3,7 +3,10 @@ package validate_testimport ("bytes""context"+ "encoding/binary""image"+ "image/color"+ "image/jpeg""math""os""runtime"@@ -14,6 +17,7 @@ import ("github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/mock""github.com/getfider/fider/app/pkg/validate"+ "github.com/goenning/imagic")func TestValidateImageUpload(t *testing.T) {@@ -200,6 +204,21 @@ func imageDimensions(t *testing.T, content []byte) (int, int) {return cfg.Width, cfg.Height}+// jpegHeaderWithDimensions returns a JPEG whose header (SOF) claims the given dimensions.+// Only the header is valid, which is all image.DecodeConfig reads.+func jpegHeaderWithDimensions(t *testing.T, width, height int) []byte {+ var buf bytes.Buffer+ err := jpeg.Encode(&buf, image.NewYCbCr(image.Rect(0, 0, 16, 16), image.YCbCrSubsampleRatio444), nil)+ Expect(err).IsNil()+ content := buf.Bytes()+ sof := bytes.Index(content, []byte{0xFF, 0xC0})+ Expect(sof > 0).IsTrue()+ // FF C0, length (2), precision (1), height (2), width (2)+ binary.BigEndian.PutUint16(content[sof+5:], uint16(height))+ binary.BigEndian.PutUint16(content[sof+7:], uint16(width))+ return content+}+func TestValidateImageUpload_DecompressionBomb(t *testing.T) {RegisterT(t)@@ -207,12 +226,14 @@ func TestValidateImageUpload_DecompressionBomb(t *testing.T) {name stringcontent []byte}{- // ~12000x12000 = 144MP, decodes to ~576MB of NRGBA but is only a few KB compressed+ // 12000x12000 = 144MP, decodes to hundreds of MB but is only a few KB compressed{"png 12000x12000", mock.UniformPNG(12000, 12000)},// 40000x40000 = 1.6 gigapixels{"gif 40000x40000", mock.GIFHeader(40000, 40000)},- // Only one dimension is huge, but the pixel count is still way over the budget- {"png 60000x1000", mock.UniformPNG(60000, 1000)},+ // Small pixel count, but extreme aspect ratio+ {"png 100000x1", mock.UniformPNG(100000, 1)},+ // 4000x4000 colour JPEG is over the budget, as it might be progressive+ {"jpeg 4000x4000", jpegHeaderWithDimensions(t, 4000, 4000)},}for _, testCase := range testCases {@@ -235,7 +256,7 @@ func TestValidateImageUpload_DecompressionBomb(t *testing.T) {Expect(err).IsNil()Expect(messages).HasLen(1)- Expect(messages[0]).Equals("The image dimensions are too large. The maximum is 40 megapixels.")+ Expect(messages[0]).Equals("The image dimensions are too large. Please upload an image with a lower resolution.")Expect(upload.Upload.Content).Equals(original)// Must not have decoded the image (which would allocate hundreds of MB)@@ -247,7 +268,7 @@ func TestValidateImageUpload_DecompressionBomb(t *testing.T) {func TestValidateImageUpload_TooManyBytes_DoesNotResize(t *testing.T) {RegisterT(t)- content := mock.UniformPNG(2000, 1000)+ content := mock.UniformPNG(3000, 2000)upload := &dto.ImageUpload{Upload: &dto.ImageUploadData{Content: content,@@ -261,8 +282,8 @@ func TestValidateImageUpload_TooManyBytes_DoesNotResize(t *testing.T) {Expect(messages).HasLen(1)width, height := imageDimensions(t, upload.Upload.Content)- Expect(width).Equals(2000)- Expect(height).Equals(1000)+ Expect(width).Equals(3000)+ Expect(height).Equals(2000)}func TestValidateImageUpload_Resize(t *testing.T) {@@ -274,12 +295,13 @@ func TestValidateImageUpload_Resize(t *testing.T) {expectedWidth intexpectedHeight int}{- {2000, 1000, 1500, 750},- {1000, 2000, 750, 1500},- // Only one dimension is over the limit- {3000, 500, 1500, 250},- {500, 3000, 250, 1500},- // Within limits, not resized (and never upscaled)+ // Both dimensions over the limit: resized, keeping the aspect ratio+ {3000, 2000, 1500, 1000},+ {2000, 3000, 1000, 1500},+ // Only one dimension over the limit: not resized (keeps GIF animations / JPEG EXIF)+ {2000, 1000, 2000, 1000},+ {3000, 500, 3000, 500},+ // Within limits, not resized{1500, 1500, 1500, 1500},{800, 600, 800, 600},}@@ -303,41 +325,78 @@ func TestValidateImageUpload_Resize(t *testing.T) {}}-func TestIsWithinPixelBudget(t *testing.T) {+func TestEstimateDecodeBytes(t *testing.T) {RegisterT(t)- Expect(validate.IsWithinPixelBudget(1, 1)).IsTrue()- Expect(validate.IsWithinPixelBudget(8000, 5000)).IsTrue()- Expect(validate.IsWithinPixelBudget(40_000_000, 1)).IsTrue()- Expect(validate.IsWithinPixelBudget(8000, 5001)).IsFalse()- Expect(validate.IsWithinPixelBudget(12000, 12000)).IsFalse()- Expect(validate.IsWithinPixelBudget(0, 100)).IsFalse()- Expect(validate.IsWithinPixelBudget(100, 0)).IsFalse()- Expect(validate.IsWithinPixelBudget(-100, -100)).IsFalse()- Expect(validate.IsWithinPixelBudget(math.MaxInt32, math.MaxInt32)).IsFalse()- Expect(validate.IsWithinPixelBudget(math.MaxInt, math.MaxInt)).IsFalse()+ // Invalid or over the per-side cap+ Expect(validate.EstimateDecodeBytes(0, 100, "png", color.GrayModel)).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(100, 0, "png", color.GrayModel)).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(-100, -100, "png", color.GrayModel)).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(validate.MaxImageSide+1, 1, "png", color.GrayModel)).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(1, validate.MaxImageSide+1, "png", color.GrayModel)).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(40_000_000, 1, "gif", color.Palette{})).Equals(int64(-1))+ Expect(validate.EstimateDecodeBytes(math.MaxInt, math.MaxInt, "png", color.GrayModel)).Equals(int64(-1))++ // Bytes per pixel include 4 bytes for a full-size RGBA intermediate+ const mp = 1000 * 1000+ Expect(validate.EstimateDecodeBytes(1000, 1000, "gif", color.Palette{})).Equals(int64(5 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "png", color.Palette{})).Equals(int64(6 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "png", color.GrayModel)).Equals(int64(6 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "png", color.Gray16Model)).Equals(int64(8 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "png", color.NRGBAModel)).Equals(int64(12 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "png", color.NRGBA64Model)).Equals(int64(20 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "jpeg", color.GrayModel)).Equals(int64(9 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "jpeg", color.YCbCrModel)).Equals(int64(19 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "jpeg", color.RGBAModel)).Equals(int64(23 * mp))+ Expect(validate.EstimateDecodeBytes(1000, 1000, "jpeg", color.CMYKModel)).Equals(int64(24 * mp))++ // Worst case at the per-side cap doesn't overflow+ Expect(validate.EstimateDecodeBytes(validate.MaxImageSide, validate.MaxImageSide, "jpeg", color.CMYKModel) > 0).IsTrue()}-func TestImageWithinPixelBudget(t *testing.T) {+func TestCheckDecodeBudget(t *testing.T) {RegisterT(t)- ok, err := validate.ImageWithinPixelBudget(mock.UniformPNG(2000, 1000))- Expect(err).IsNil()- Expect(ok).IsTrue()+ Expect(validate.CheckDecodeBudget(mock.UniformPNG(2000, 1000))).IsNil()+ // 36MP * 6 bytes = 216MB+ Expect(validate.CheckDecodeBudget(mock.UniformPNG(6000, 6000))).IsNil()+ // 49MP * 6 bytes = 294MB+ Expect(validate.CheckDecodeBudget(mock.UniformPNG(7000, 7000))).Equals(validate.ErrImageTooLarge)+ Expect(validate.CheckDecodeBudget(mock.UniformPNG(12000, 12000))).Equals(validate.ErrImageTooLarge)+ Expect(validate.CheckDecodeBudget(mock.UniformPNG(100000, 1))).Equals(validate.ErrImageTooLarge)+ Expect(validate.CheckDecodeBudget(mock.GIFHeader(40000, 40000))).Equals(validate.ErrImageTooLarge)- ok, err = validate.ImageWithinPixelBudget(mock.UniformPNG(12000, 12000))- Expect(err).IsNil()- Expect(ok).IsFalse()-- ok, err = validate.ImageWithinPixelBudget(mock.GIFHeader(40000, 40000))- Expect(err).IsNil()- Expect(ok).IsFalse()+ // 12MP phone photo: 12.2MP * 19 bytes = 232MB+ Expect(validate.CheckDecodeBudget(jpegHeaderWithDimensions(t, 4032, 3024))).IsNil()+ // 16MP: 304MB+ Expect(validate.CheckDecodeBudget(jpegHeaderWithDimensions(t, 4000, 4000))).Equals(validate.ErrImageTooLarge)img, _ := os.ReadFile(env.Path("/app/pkg/web/testdata/logo2.jpg"))- ok, err = validate.ImageWithinPixelBudget(img)- Expect(err).IsNil()- Expect(ok).IsTrue()+ Expect(validate.CheckDecodeBudget(img)).IsNil()++ Expect(validate.CheckDecodeBudget([]byte("not an image"))).Equals(imagic.ErrNotSupported)+ favicon, _ := os.ReadFile(env.Path("/app/pkg/web/testdata/favicon.ico"))+ Expect(validate.CheckDecodeBudget(favicon)).Equals(imagic.ErrNotSupported)+}- _, err = validate.ImageWithinPixelBudget([]byte("not an image"))- Expect(err).IsNotNil()+func TestSafeApply(t *testing.T) {+ RegisterT(t)++ resized, err := validate.SafeApply(context.Background(), mock.UniformPNG(2000, 1000), imagic.Resize(500))+ Expect(err).IsNil()+ width, height := imageDimensions(t, resized)+ Expect(width).Equals(500)+ Expect(height).Equals(250)++ var before, after runtime.MemStats+ runtime.GC()+ runtime.ReadMemStats(&before)+ result, err := validate.SafeApply(context.Background(), mock.UniformPNG(12000, 12000), imagic.Resize(500))+ runtime.ReadMemStats(&after)+ Expect(err).Equals(validate.ErrImageTooLarge)+ Expect(result).IsNil()+ Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()++ _, err = validate.SafeApply(context.Background(), []byte("not an image"), imagic.Resize(500))+ Expect(err).Equals(imagic.ErrNotSupported)}diff --git a/locale/en/server.json b/locale/en/server.jsonindex 8fecff8bb..be6e2b427 100644--- a/locale/en/server.json+++ b/locale/en/server.json@@ -29,7 +29,7 @@"validation.custom.minimagedimensions": "The image must have minimum dimensions of {width}x{height} pixels.","validation.custom.imagesquareratio": "The image must have an aspect ratio of 1:1.","validation.custom.maximagesize": "The image size must be smaller than {kilobytes}KB.",- "validation.custom.maximagepixels": "The image dimensions are too large. The maximum is {megapixels} megapixels.",+ "validation.custom.maximagepixels": "The image dimensions are too large. Please upload an image with a lower resolution.","validation.custom.invalidemoji": "Invalid reaction emoji.","enum.poststatus.open": "Open","enum.poststatus.started": "Started",