Skip to content

Return 404 when Active Storage image variants cannot be processed - #172

Merged
thibaudgg merged 1 commit into
masterfrom
cursor/activestorage-vips-representation-c57b
Sep 1, 2026
Merged

Return 404 when Active Storage image variants cannot be processed#172
thibaudgg merged 1 commit into
masterfrom
cursor/activestorage-vips-representation-c57b

Conversation

@thibaudgg

@thibaudgg thibaudgg commented Sep 1, 2026

Copy link
Copy Markdown
Member

Why

Production AppSignal exception incident #458 is a Vips::Error from ActiveStorage::Representations::RedirectController#show:

VipsForeignLoad: "/tmp/ActiveStorage-….png" is not a known file format

Recent traces (tenant ragedevert, blobs ~9548 and ~9549) 500 while transforming the blob to the newsletter/Trix JPEG variant (resize_to_limit: [1024, 768], matching app/views/active_storage/blobs/_blob.html.erb).

The blob is stored/named as an image, so Active Storage treats it as representable, but the bytes are not a format libvips can load (corrupt, truncated, or not actually an image). Rails already returns 404 for an invalid variation signature; this extends that to failed image processing.

Upload-time validation alone would not stop the 500s: these blobs already exist, Trix only checks the browser-reported MIME type, and org logos already validate bytes with Vips. This keeps the existing images in place and makes their representation URLs fail closed.

What

  • Reopen ActiveStorage::Representations::BaseController#set_representation after boot and rescue Vips::Error / ImageProcessing::Error next to the existing InvalidSignature handler (head :not_found).
  • after_initialize (not to_prepare + prepend): the gem controller is not loaded while initializers run, and gem controllers do not reload.
  • Cover the newsletter JPEG variant: corrupt PNG header → 404; valid PNG → disk redirect.

Do not merge or deploy from this PR. Leave the AppSignal incident open.

Open in Web Open in Cursor 

@thibaudgg
thibaudgg marked this pull request as ready for review September 1, 2026 11:57
Blobs can be stored as PNG/JPEG while the bytes are not a format
libvips can load. Variant generation then raised Vips::Error from
the representations redirect, turning newsletter/Trix image URLs
into 500s (AppSignal #458). Rescue that like an unusable blob.

Co-authored-by: Thibaud Guillaume-Gentil <thibaud@thibaud.gg>
@cursor
cursor Bot force-pushed the cursor/activestorage-vips-representation-c57b branch from e561725 to acb3c7f Compare September 1, 2026 12:29
@thibaudgg
thibaudgg merged commit 22a8346 into master Sep 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants