GLM-5.3 · Heavy

feature-automatically-downsize-large-images-discourse-cursor

Discourse · Ruby · S · 30–99 lines · 3 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Haiku 5.5.

Gold bugs found
4 of 8
Comments published
6
Comments matching a bug
4
Comments not matching
2
Cost of this PR
$2.730

Published comments (6)

criticalsecurityapp/controllers/uploads_controller.rb:67

Shell injection: unsanitized type param reaches shell via tempfile path — An API request with shell metacharacters in the unsanitized `type` param achieves arbitrary command execution: the `FileHelper.download` tempfile basename embeds `type` and reaches `convert_with`'s interpolated shell string via `OptimizedImage.downsize`. Whitelist `type` and use array-form process spawning.

highbugapp/models/optimized_image.rb:149-151

Duplicate downsize definition shadows the 5-arg version and breaks ResizeEmoji — Custom emoji are never resized: `Jobs::ResizeEmoji`'s 5-argument call raises `ArgumentError` against the surviving 4-parameter `downsize`. Delete the duplicate definition and pass a dimensions string from `resize_emoji.rb`.

highbugapp/assets/javascripts/discourse/lib/utilities.js:182

Hardcoded 10MB limit ignores per-type site settings for file size validation — The hardcoded `maxSizeKB` lets attachments above `max_attachment_size_kb` pass client validation only to fail server-side in `Validators::UploadValidator#maximum_file_size`, and blocks valid uploads when admins raise limits past 10MB. Read `Discourse.SiteSettings['max_' + type + '_size_kb']` instead. **Also found in:** - `app/assets/javascripts/discourse/lib/utilities.js:244-248`

mediumperformanceapp/controllers/uploads_controller.rb:65-69

Downsize retry loop ignores failures and wastes CPU on intermediate ImageOptim passes — Large uploads now spend tens of seconds of sequential CPU: each iteration's `ImageOptim` pass is overwritten by the next `convert`, and a failed `downsize` still burns all 5 attempts. Break on failure and optimize only once, after the loop.

mediumbugapp/controllers/uploads_controller.rb:67

Browser-uploaded animated GIFs lose animation when auto-downsized — Rack multipart tempfiles lack the `.gif` extension, so `optimize`'s animation check never matches and browser-uploaded animated GIFs are flattened to a static first frame despite `allow_animation`. Base the animated decision on the upload filename rather than the tempfile path.

mediumbugapp/controllers/uploads_controller.rb:63-69

Downsize loop capped at 5 iterations may not reach the allowed image size — Five 80% passes leave a 10MB image near 3.3MB when `max_image_size_kb` is 1024, so `Upload.create_for` still rejects it despite the comment's promise. Loop until under the limit, breaking when the size stops shrinking.

Gold bugs found (4)

highdownsize is defined twice in OptimizedImage; the new (from, to, dimensions, opts) version overrides the (max_width, max_height) one, so existing callers such as ResizeEmoji that pass width and height now raise ArgumentError.
highThe client-side upload size check hardcodes 10 MB instead of the per-type max_<type>_size_kb setting, so attachment limits diverge from the server: larger allowed attachments are blocked and smaller limits are only enforced server-side.
mediumAuto-downsizing passes the upload tempfile path, which lacks a .gif extension, so the animated branch is never chosen and convert keeps only the first frame, flattening oversized animated GIFs.
criticalThe upload tempfile path, which for API URL uploads embeds the request-controlled type param, is now passed to OptimizedImage.downsize and run unescaped in a shell via backticks, allowing command injection through type.

Gold bugs missed (4)

highThe upload downsizing passes a percentage ('80%') as dimensions, but for animated GIFs the gifsicle path uses --resize-fit, which expects WxH geometry, so downsizing silently fails.
highClaims the downsize loop checking tempfile.size may never terminate because of a stale cached size, causing infinite loops.
highOversized SVG uploads go through the new ImageMagick downsize loop because SVG counts as an image, so the vector file is rasterized or corrupted in place before Upload.create_for parses it as SVG.
mediumThe 413 upload error message states a hard-coded 10 MB maximum that may not match the limit the server or web server actually enforces.