Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Raise image decode budget to 320MB so 12MP progressive JPEGs pass (GHSA-7cw3-7xh9-529r)

Matt Roberts6e929e3764e2
diff --git a/app/pkg/validate/image.go b/app/pkg/validate/image.go
index 03aa6b640..14cfa697f 100644
--- a/app/pkg/validate/image.go
+++ b/app/pkg/validate/image.go
@@ -23,14 +23,16 @@ import (
//
// 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
-// ImageHeader.EstimateBytes for the per-format estimates. With 256MB and a single
+// ImageHeader.EstimateBytes for the per-format estimates. With 320MB and a single
// operation (e.g. a resize) the largest images that can be decoded are roughly:
-// - JPEG (colour, baseline): ~24 megapixels
-// - JPEG (colour, progressive): ~11.7 megapixels
-// - PNG (8-bit RGB/RGBA/gray): ~22 megapixels
-// - PNG (paletted): ~44 megapixels
-// - GIF: ~53 megapixels
-const MaxDecodeBytes int64 = 256 << 20
+// - JPEG (colour, baseline): ~30 megapixels
+// - JPEG (colour, progressive): ~14.6 megapixels (so 12MP phone photos pass)
+// - PNG (8-bit RGB/RGBA/gray): ~28 megapixels
+// - PNG (paletted): ~56 megapixels
+// - GIF: ~67 megapixels
+//
+// Together with maxConcurrentDecodes this bounds the worst case at ~640MB.
+const MaxDecodeBytes int64 = 320 << 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
diff --git a/app/pkg/validate/upload_test.go b/app/pkg/validate/upload_test.go
index 8942b919e..124fad1b9 100644
--- a/app/pkg/validate/upload_test.go
+++ b/app/pkg/validate/upload_test.go
@@ -482,10 +482,12 @@ func TestCheckDecodeBudget(t *testing.T) {
// 16MP * 12 bytes = 192MB
Expect(check(mock.UniformPNG(4000, 4000), 1)).IsNil()
- // ... but not with 3 operations (Favicon with background): 16MP * 20 bytes = 320MB
- Expect(check(mock.UniformPNG(4000, 4000), 3)).Equals(validate.ErrImageTooLarge)
+ // ... but not with 3 operations (Favicon with background): 20.25MP * 20 bytes = 405MB
+ Expect(check(mock.UniformPNG(4500, 4500), 3)).Equals(validate.ErrImageTooLarge)
// 25MP * 12 bytes = 300MB
- Expect(check(mock.UniformPNG(5000, 5000), 1)).Equals(validate.ErrImageTooLarge)
+ Expect(check(mock.UniformPNG(5000, 5000), 1)).IsNil()
+ // 36MP * 12 bytes = 432MB
+ Expect(check(mock.UniformPNG(6000, 6000), 1)).Equals(validate.ErrImageTooLarge)
Expect(check(mock.UniformPNG(12000, 12000), 1)).Equals(validate.ErrImageTooLarge)
Expect(check(mock.UniformPNG(100000, 1), 1)).Equals(validate.ErrImageTooLarge)
Expect(check(mock.GIFHeader(40000, 40000), 1)).Equals(validate.ErrImageTooLarge)
@@ -502,7 +504,9 @@ func TestCheckDecodeBudget(t *testing.T) {
// 9MP progressive: 3008x3008 * 23 bytes = 208MB
Expect(check(jpegHeaderWithDimensions(t, 3000, 3000, true), 1)).IsNil()
// 12MP progressive phone photo: 4032x3040 * 23 bytes = 282MB
- Expect(check(jpegHeaderWithDimensions(t, 4032, 3024, true), 1)).Equals(validate.ErrImageTooLarge)
+ Expect(check(jpegHeaderWithDimensions(t, 4032, 3024, true), 1)).IsNil()
+ // 16MP progressive: 4608x3456 * 23 bytes = 366MB
+ Expect(check(jpegHeaderWithDimensions(t, 4608, 3456, true), 1)).Equals(validate.ErrImageTooLarge)
// 40MP baseline: 8000x5024 * 11 bytes = 442MB
Expect(check(jpegHeaderWithDimensions(t, 8000, 5000, false), 1)).Equals(validate.ErrImageTooLarge)