Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[Bug] gifJPEG allocates image.NewRGBA directly from unvalidated Logical Screen Width/Height (35-byte GIF triggers 16 GiB OOM crash or 0-byte JPEG) #64

Closed
Mag1cFall/AIStudio2API
#292

Description

@photobrand

发生了什么 / What happened

Summary

In internal/api/files.go:444-458 (also reachable via internal/api/gemini_files.go:466), multipartStreamMIME decodes the first frame of uploaded GIFs and draws it onto an RGBA canvas sized by config.Width x config.Height:

	if frame, config, ok := decodeGIFFrame(data); ok {
		data, mediaType = gifJPEG(frame, config), "image/jpeg"
	}
	return mediaType, bytes.NewReader(data), nil
}

func gifJPEG(frame *image.Paletted, config image.Config) []byte {
	canvas := image.NewRGBA(image.Rect(0, 0, config.Width, config.Height))
	draw.Draw(canvas, canvas.Bounds(), image.White, image.Point{}, draw.Src)
	draw.Draw(canvas, frame.Bounds(), frame, frame.Bounds().Min, draw.Over)
	var buf bytes.Buffer
	_ = jpeg.Encode(&buf, canvas, &jpeg.Options{Quality: 92})
	return buf.Bytes()
}

Concrete Triggers

  1. 16 GiB OOM crash from a ~35-byte GIF: Uploading a valid GIF89a with Logical Screen Descriptor 65535 x 65535 and a 1 x 1 image descriptor to POST /v1/files or POST /upload/v1beta/files passes gif.Decode (which only allocates the 1 x 1 frame), then gifJPEG calls image.NewRGBA(image.Rect(0, 0, 65535, 65535)), attempting a 17.18 GB allocation (65535 * 65535 * 4) and crashing the Go process with OOM.
  2. Silent 0-byte JPEG upload: A valid 0 x 0 GIF passes decodeGIFFrame, creates an empty canvas, fails jpeg.Encode (invalid image size: 0x0, error discarded with _ =), and returns 0 bytes labeled image/jpeg.

Suggested Fix

Validate config.Width > 0 && config.Height > 0 and cap int64(config.Width)*int64(config.Height) (e.g., <= 64_000_000) in gifJPEG, and check the jpeg.Encode error before replacing data and mediaType.

版本 / Version

v0.3.13

Activity

  1. Mag1cFall commented on Nov 22, 2026

    @Mag1cFall
    Owner

    Fixed in v0.3.14, GIFs with zero dimensions or canvas over 64M pixels are left untouched in both file upload and inline normalization instead of allocating a huge RGBA canvas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions