← Back to Pull requests

Bound decoded image size so a received thumbnail cannot OOM the app #1231

Open
unknown opened this pull request with 544 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 29, 2026 05:03 UTC
Author

What

A received message's inline jpeg_thumbnail is fully controlled by the sender and is drawn the moment the chat opens, through egui_extras' ImageCrateLoader. That loader decodes with no size limit: it hands the bytes straight to image::DynamicImage::from_decoder, which does not enforce even the image crate's own default 512 MiB allocation ceiling (that ceiling is only applied by ImageReader::decode, a path the loader never takes). egui's max_texture_side clamp happens only after the ColorImage is already fully allocated, so it does not help.

Result: a thumbnail of a few dozen bytes that declares huge dimensions (e.g. a PNG header saying 50000x50000) makes the loader reserve ~10 GB and the process is killed, with no action from the reader beyond opening the chat. That is a zero-click denial of service. The downloaded full photo, shown as a tile through the same loader, has the same exposure (a <= 64 MiB file declaring absurd dimensions).

The project already knows the right guard (image::Limits in animation.rs's poster decode and in link_preview.rs), but the received-thumbnail / displayed-image path through egui never applied it.

Reproduce (offline, no live account)

Feed a crafted PNG whose IHDR declares 50000x50000 (a valid header, a few bytes) to the same decode the loader uses (DynamicImage::from_decoder): it reserves gigabytes rather than erroring. Before this change, drawing such a thumbnail aborts the app; after it, the thumbnail is rejected and the rest of the chat renders.

Fix

A small size-bounded image loader (src/bounded_image.rs) installed on top of egui_extras' loaders. egui tries image loaders in reverse insertion order, so the last one added wins the still-raster path. It decodes through image::Limits, rejecting an oversized header or allocation before any buffer is reserved, exactly as animation.rs already does for posters:

  • Inline thumbnails (bytes://) get a tight bound (4096 px per side, 64 MiB), matching animation.rs. This is the pure zero-click vector.

  • Downloaded photos (file://) get the image crate's own default 512 MiB ceiling (the one this path skipped) plus a generous per-side cap that also blocks an absurdly thin, long image that would slip under the allocation ceiling.

  • Routing is decided by the bytes' own format, never by the URI extension or MIME, so a bomb cannot be smuggled past the limit by mislabelling it (e.g. a PNG named .gif). Only genuine GIF, WebP and SVG are deferred to egui_extras' own GifLoader/WebPLoader/SvgLoader, which keep rendering those unchanged.
  • Threading and caching mirror ImageCrateLoader, so behaviour is identical apart from the limit.

    Known limitation

    GIF, WebP and SVG still go through egui_extras' own loaders, which have no limit of their own, so a pixel bomb of those formats drawn via egui stays as exposed as before (no regression). In practice ZapFast draws animated GIF/WebP through animation.rs (already bounded), so only still JPEG/PNG reach this path. Bounding the animated loaders too can be a follow-up.

    Tests

    src/bounded_image.rs adds:

  • a 50000x50000 pixel-bomb thumbnail is rejected before allocating, under both the thumbnail and the photo bound;

  • a normal thumbnail still decodes to the right size;

  • routing is by content: a PNG is never deferred (so it cannot reach an unbounded loader), while a real GIF/WebP/SVG is;

  • end-to-end through the loader: the bomb never becomes a texture, a real thumbnail does.
  • Platform coverage

    Built and tested on Linux (fmt, clippy with and without --all-features, test x2, doc all pass). The code is platform-independent (no cfg branches); Windows and macOS compile the same path.

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Image loading now applies size and file limits based on the image source, helping prevent oversized images from consuming excessive resources.
    GIF, WebP, and SVG images continue to use their existing support, while other image types are decoded with bounded limits.
    Images that exceed decoding limits or cannot be decoded now show a loading error.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 544 lines (+408 / -136) across 6 files Reviewer: greptile-apps[bot] Review duration: 15m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.48) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 544 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1231"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48% (MEDIUM) · Higher = larger blast radius
    48%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.4% · Safe to merge
    27.4%
    Repository