Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Prevent image decompression bomb DoS (GHSA-7cw3-7xh9-529r)

Matt Robertsede8ede5d6ec
diff --git a/app/handlers/images.go b/app/handlers/images.go
index 77e910e33..ea8e479e6 100644
--- a/app/handlers/images.go
+++ b/app/handlers/images.go
@@ -18,6 +18,7 @@ import (
"github.com/getfider/fider/app/pkg/crypto"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/log"
+ "github.com/getfider/fider/app/pkg/validate"
"github.com/getfider/fider/app/pkg/web"
"github.com/goenning/imagic"
"github.com/goenning/letteravatar"
@@ -152,6 +153,14 @@ 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)
+ }
+
bytes, err = imagic.Apply(bytes, opts...)
if err != nil {
return c.Failure(err)
@@ -181,12 +190,35 @@ func ViewUploadedImage() web.HandlerFunc {
bytes := q.Result.Content
if size > 0 {
- bytes, err = imagic.Apply(bytes, imagic.Resize(size))
+ safe, err := isSafeToDecode(c, bkey, bytes)
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,
+ })
+ }
+ return ok, nil
+}
diff --git a/app/handlers/images_test.go b/app/handlers/images_test.go
index 501ec11d0..18c7147f9 100644
--- a/app/handlers/images_test.go
+++ b/app/handlers/images_test.go
@@ -1,12 +1,16 @@
package handlers_test
import (
+ "bytes"
"context"
+ "image"
"io"
"net/http"
+ "runtime"
"testing"
"github.com/getfider/fider/app"
+ "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
@@ -85,6 +89,106 @@ func TestGravatarNotFound_LetterAvatarHandler(t *testing.T) {
Expect(bytes).Equals(expectedAvatar)
}
+func mockBlob(content []byte, contentType string) {
+ bus.AddHandler(func(ctx context.Context, q *query.GetBlobByKey) error {
+ q.Result = &dto.Blob{
+ Content: content,
+ ContentType: contentType,
+ Size: int64(len(content)),
+ }
+ return nil
+ })
+}
+
+func responseDimensions(body []byte) (int, int) {
+ cfg, _, err := image.DecodeConfig(bytes.NewReader(body))
+ Expect(err).IsNil()
+ return cfg.Width, cfg.Height
+}
+
+func TestViewUploadedImage_Resize(t *testing.T) {
+ RegisterT(t)
+ bus.Init()
+ mockBlob(mock.UniformPNG(2000, 1000), "image/png")
+
+ server := mock.NewServer()
+ code, response := server.
+ WithURL("https://demo.test.fider.io/?size=500").
+ OnTenant(mock.DemoTenant).
+ AddParam("bkey", "attachments/image.png").
+ Execute(handlers.ViewUploadedImage())
+
+ Expect(code).Equals(http.StatusOK)
+ width, height := responseDimensions(response.Body.Bytes())
+ Expect(width).Equals(500)
+ Expect(height).Equals(250)
+}
+
+func TestViewUploadedImage_DecompressionBomb_ServedWithoutDecoding(t *testing.T) {
+ RegisterT(t)
+ bus.Init()
+ bomb := mock.UniformPNG(12000, 12000)
+ mockBlob(bomb, "image/png")
+
+ server := mock.NewServer()
+
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ code, response := server.
+ WithURL("https://demo.test.fider.io/?size=500").
+ OnTenant(mock.DemoTenant).
+ AddParam("bkey", "attachments/bomb.png").
+ Execute(handlers.ViewUploadedImage())
+ runtime.ReadMemStats(&after)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(response.Body.Bytes()).Equals(bomb)
+ // Decoding would allocate hundreds of MB
+ Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()
+}
+
+func TestFavicon_DecompressionBomb_ServedWithoutDecoding(t *testing.T) {
+ RegisterT(t)
+ bus.Init()
+ bomb := mock.UniformPNG(12000, 12000)
+ mockBlob(bomb, "image/png")
+
+ server := mock.NewServer()
+
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ code, response := server.
+ WithURL("https://demo.test.fider.io/?size=100&bg=white").
+ OnTenant(mock.DemoTenant).
+ AddParam("bkey", "logos/bomb.png").
+ Execute(handlers.Favicon())
+ runtime.ReadMemStats(&after)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(response.Body.Bytes()).Equals(bomb)
+ Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()
+}
+
+func TestFavicon_Resize(t *testing.T) {
+ RegisterT(t)
+ bus.Init()
+ mockBlob(mock.UniformPNG(400, 400), "image/png")
+
+ server := mock.NewServer()
+ code, response := server.
+ WithURL("https://demo.test.fider.io/?size=100").
+ OnTenant(mock.DemoTenant).
+ AddParam("bkey", "logos/logo.png").
+ Execute(handlers.Favicon())
+
+ Expect(code).Equals(http.StatusOK)
+ width, height := responseDimensions(response.Body.Bytes())
+ Expect(width).Equals(100)
+ Expect(height).Equals(100)
+}
+
func TestUnknownUser_LetterAvatarHandler(t *testing.T) {
RegisterT(t)
bus.Init(httpclient.Service{})
diff --git a/app/pkg/mock/image.go b/app/pkg/mock/image.go
new file mode 100644
index 000000000..ef5395dbb
--- /dev/null
+++ b/app/pkg/mock/image.go
@@ -0,0 +1,70 @@
+package mock
+
+import (
+ "bytes"
+ "compress/zlib"
+ "encoding/binary"
+ "hash/crc32"
+ "sync"
+)
+
+var uniformPNGCache sync.Map
+
+// UniformPNG returns a valid 1-bit grayscale PNG of the given dimensions where every pixel is black.
+// It is streamed row by row, so generating it never allocates the full image in memory.
+// Large dimensions compress extremely well, which makes this useful for building
+// "decompression bomb" images in tests (e.g. 12000x12000 is only a few KB).
+// Results are cached, callers must not modify the returned slice.
+func UniformPNG(width, height int) []byte {
+ key := [2]int{width, height}
+ if cached, ok := uniformPNGCache.Load(key); ok {
+ return cached.([]byte)
+ }
+ content := uniformPNG(width, height)
+ uniformPNGCache.Store(key, content)
+ return content
+}
+
+func uniformPNG(width, height int) []byte {
+ var idat bytes.Buffer
+ zw, _ := zlib.NewWriterLevel(&idat, zlib.BestCompression)
+ row := make([]byte, 1+(width+7)/8) // filter byte (0 = none) + packed 1-bit pixels
+ for i := 0; i < height; i++ {
+ _, _ = zw.Write(row)
+ }
+ _ = zw.Close()
+
+ ihdr := make([]byte, 13)
+ binary.BigEndian.PutUint32(ihdr[0:4], uint32(width))
+ binary.BigEndian.PutUint32(ihdr[4:8], uint32(height))
+ ihdr[8] = 1 // bit depth
+ ihdr[9] = 0 // color type: grayscale
+
+ var out bytes.Buffer
+ out.Write([]byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'})
+ writePNGChunk(&out, "IHDR", ihdr)
+ writePNGChunk(&out, "IDAT", idat.Bytes())
+ writePNGChunk(&out, "IEND", nil)
+ return out.Bytes()
+}
+
+func writePNGChunk(out *bytes.Buffer, name string, data []byte) {
+ _ = binary.Write(out, binary.BigEndian, uint32(len(data)))
+ crc := crc32.NewIEEE()
+ _, _ = crc.Write([]byte(name))
+ _, _ = crc.Write(data)
+ out.WriteString(name)
+ out.Write(data)
+ _ = binary.Write(out, binary.BigEndian, crc.Sum32())
+}
+
+// GIFHeader returns the header of a GIF with the given logical screen dimensions (max 65535).
+// It contains no image data: enough for image.DecodeConfig, but not for image.Decode.
+func GIFHeader(width, height int) []byte {
+ b := []byte("GIF89a")
+ b = binary.LittleEndian.AppendUint16(b, uint16(width))
+ b = binary.LittleEndian.AppendUint16(b, uint16(height))
+ b = append(b, 0x80, 0, 0) // global color table present (2 entries), bg color, aspect ratio
+ b = append(b, 0, 0, 0, 255, 255, 255) // global color table
+ return b
+}
diff --git a/app/pkg/validate/image.go b/app/pkg/validate/image.go
new file mode 100644
index 000000000..2bc7d31bd
--- /dev/null
+++ b/app/pkg/validate/image.go
@@ -0,0 +1,46 @@
+package validate
+
+import (
+ "bytes"
+ "image"
+
+ // Register the decoders for every format we accept, so image.DecodeConfig
+ // can read their headers regardless of which other packages are imported.
+ _ "image/gif"
+ _ "image/jpeg"
+ _ "image/png"
+)
+
+// MaxImagePixels is the maximum number of pixels (width * height) an image may have
+// before we refuse to fully decode it.
+//
+// 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
+}
+
+// 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))
+ if err != nil {
+ return false, err
+ }
+ return IsWithinPixelBudget(cfg.Width, cfg.Height), nil
+}
diff --git a/app/pkg/validate/upload.go b/app/pkg/validate/upload.go
index 8f392d73d..0023632d9 100644
--- a/app/pkg/validate/upload.go
+++ b/app/pkg/validate/upload.go
@@ -30,7 +30,7 @@ type ImageUploadOpts struct {
MaxKilobytes int
}
-//MultiImageUpload validates multiple image uploads
+// MultiImageUpload validates multiple image uploads
func MultiImageUpload(ctx context.Context, currentAttachments []string, uploads []*dto.ImageUpload, opts MultiImageUploadOpts) ([]string, error) {
if currentAttachments == nil {
currentAttachments = []string{}
@@ -71,7 +71,7 @@ func MultiImageUpload(ctx context.Context, currentAttachments []string, uploads
return []string{}, nil
}
-//ImageUpload validates given image upload
+// ImageUpload validates given image upload
func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadOpts) ([]string, error) {
messages := []string{}
@@ -92,6 +92,14 @@ func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadO
return nil, err
}
} 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 logo.Width < opts.MinWidth || logo.Height < opts.MinHeight {
messages = append(messages, i18n.T(ctx, "validation.custom.minimagedimensions",
@@ -109,7 +117,14 @@ func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadO
))
}
- if logo.Height > MaxDimensionSize && logo.Width > MaxDimensionSize {
+ // The upload is rejected anyway, so don't spend time/memory decoding it.
+ if len(messages) > 0 {
+ return 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 err != nil {
return nil, err
diff --git a/app/pkg/validate/upload_test.go b/app/pkg/validate/upload_test.go
index f4a2c0fb2..48f31afca 100644
--- a/app/pkg/validate/upload_test.go
+++ b/app/pkg/validate/upload_test.go
@@ -1,13 +1,18 @@
package validate_test
import (
+ "bytes"
"context"
+ "image"
+ "math"
"os"
+ "runtime"
"testing"
"github.com/getfider/fider/app/models/dto"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/mock"
"github.com/getfider/fider/app/pkg/validate"
)
@@ -188,3 +193,151 @@ func TestValidateMultiImageUpload_Existing(t *testing.T) {
Expect(messages).HasLen(0)
Expect(err).IsNil()
}
+
+func imageDimensions(t *testing.T, content []byte) (int, int) {
+ cfg, _, err := image.DecodeConfig(bytes.NewReader(content))
+ Expect(err).IsNil()
+ return cfg.Width, cfg.Height
+}
+
+func TestValidateImageUpload_DecompressionBomb(t *testing.T) {
+ RegisterT(t)
+
+ var testCases = []struct {
+ name string
+ content []byte
+ }{
+ // ~12000x12000 = 144MP, decodes to ~576MB of NRGBA 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)},
+ }
+
+ for _, testCase := range testCases {
+ Expect(len(testCase.content) < 100*1024).IsTrue()
+ original := append([]byte{}, testCase.content...)
+
+ upload := &dto.ImageUpload{
+ Upload: &dto.ImageUploadData{
+ Content: testCase.content,
+ },
+ }
+
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ messages, err := validate.ImageUpload(context.Background(), upload, validate.ImageUploadOpts{
+ MaxKilobytes: 5120,
+ })
+ runtime.ReadMemStats(&after)
+
+ Expect(err).IsNil()
+ Expect(messages).HasLen(1)
+ Expect(messages[0]).Equals("The image dimensions are too large. The maximum is 40 megapixels.")
+ Expect(upload.Upload.Content).Equals(original)
+
+ // Must not have decoded the image (which would allocate hundreds of MB)
+ allocated := after.TotalAlloc - before.TotalAlloc
+ Expect(allocated < 10*1024*1024).IsTrue()
+ }
+}
+
+func TestValidateImageUpload_TooManyBytes_DoesNotResize(t *testing.T) {
+ RegisterT(t)
+
+ content := mock.UniformPNG(2000, 1000)
+ upload := &dto.ImageUpload{
+ Upload: &dto.ImageUploadData{
+ Content: content,
+ },
+ }
+
+ messages, err := validate.ImageUpload(context.Background(), upload, validate.ImageUploadOpts{
+ MaxKilobytes: 0,
+ })
+ Expect(err).IsNil()
+ Expect(messages).HasLen(1)
+
+ width, height := imageDimensions(t, upload.Upload.Content)
+ Expect(width).Equals(2000)
+ Expect(height).Equals(1000)
+}
+
+func TestValidateImageUpload_Resize(t *testing.T) {
+ RegisterT(t)
+
+ var testCases = []struct {
+ width int
+ height int
+ expectedWidth int
+ expectedHeight 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)
+ {1500, 1500, 1500, 1500},
+ {800, 600, 800, 600},
+ }
+
+ for _, testCase := range testCases {
+ upload := &dto.ImageUpload{
+ Upload: &dto.ImageUploadData{
+ Content: mock.UniformPNG(testCase.width, testCase.height),
+ },
+ }
+
+ messages, err := validate.ImageUpload(context.Background(), upload, validate.ImageUploadOpts{
+ MaxKilobytes: 5120,
+ })
+ Expect(err).IsNil()
+ Expect(messages).HasLen(0)
+
+ width, height := imageDimensions(t, upload.Upload.Content)
+ Expect(width).Equals(testCase.expectedWidth)
+ Expect(height).Equals(testCase.expectedHeight)
+ }
+}
+
+func TestIsWithinPixelBudget(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()
+}
+
+func TestImageWithinPixelBudget(t *testing.T) {
+ RegisterT(t)
+
+ ok, err := validate.ImageWithinPixelBudget(mock.UniformPNG(2000, 1000))
+ Expect(err).IsNil()
+ Expect(ok).IsTrue()
+
+ 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()
+
+ img, _ := os.ReadFile(env.Path("/app/pkg/web/testdata/logo2.jpg"))
+ ok, err = validate.ImageWithinPixelBudget(img)
+ Expect(err).IsNil()
+ Expect(ok).IsTrue()
+
+ _, err = validate.ImageWithinPixelBudget([]byte("not an image"))
+ Expect(err).IsNotNil()
+}
diff --git a/locale/en/server.json b/locale/en/server.json
index f57e2c57e..8fecff8bb 100644
--- a/locale/en/server.json
+++ b/locale/en/server.json
@@ -29,6 +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.invalidemoji": "Invalid reaction emoji.",
"enum.poststatus.open": "Open",
"enum.poststatus.started": "Started",