AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Bug] imageEditMask decodes arbitrary image dimensions without DecodeConfig bounds, allowing tiny PNG masks to trigger multi-GB OOM crash

Open photobrand opened this issue 1 week ago • 0 comments

发生了什么 / What happened

Summary

In v0.3.14, decodeGIFFrame (internal/api/media.go:824-829) was hardened with image.DecodeConfig + maxGIFCanvasPixels = 64_000_000 to prevent OOM crashes from tiny abnormal-canvas images.

However, imageEditMask (internal/api/image_edits.go:159-179), used by both POST /v1/images/edits (mask) and /v1/responses (image_generation input_image_mask), still calls image.Decode(bytes.NewReader(data)) and image.NewGray(bounds) with no image.DecodeConfig dimension check:

func imageEditMask(data []byte) ([]byte, error) {
	source, _, err := image.Decode(bytes.NewReader(data))
	if err != nil {
		return nil, fmt.Errorf("mask must be a PNG image: %w", err)
	}
	bounds := source.Bounds()
	mask := image.NewGray(bounds)

Trigger & Consequence

Uploading a tiny (~1 KB) valid solid-color 65535 x 65535 PNG as mask to POST /v1/images/edits (or input_image_mask on /v1/responses) passes ParseMultipartForm, then causes image.Decode + image.NewGray(bounds) to allocate ~8.6 GiB (4.3 GiB decoded image + 4.3 GiB gray mask), crashing the Go process with OOM.

Suggested Fix

Check image.DecodeConfig(bytes.NewReader(data)) before image.Decode and reject masks with non-positive dimensions or pixel counts exceeding maxGIFCanvasPixels (64_000_000).

版本 / Version

v0.3.14

photobrand avatar Nov 22 '26 14:11 photobrand