Repository navigation
Decode auxiliary alpha stored as a grid - #19
laurens-pilot wants to merge 6 commits into
Conversation
ImageIO stores the alpha of every transparent image with a side of 1024 px or more as a grid item tiled like the primary. The decoder only accepted coded alpha items, so all such images decoded fully opaque without an error. Extract a grid alpha item through the shared grid tile extraction, preflight it with the caller's guardrails, compose it with the existing grid decoder and use its luma plane as the alpha. Alpha decoded from ImageIO files between 1024x700 and 4096x4096 now matches ImageIO's own decode exactly. Only the first grid candidate that is an alpha item gets extracted, so a file listing thousands of crafted grid alpha candidates costs one tile budget instead of one per candidate: 3,000 candidates in a 2 MB file took 6.3 s and now take 30 ms. Coded alpha candidates keep their existing fallback.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
Security findingsAdvisory findings (2)
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Here are some automated security review suggestions for this pull request.
Reviewed commit: 4e4a497302
ℹ️ About Codex security reviews in GitHub
This is an experimental Codex feature. Security reviews are triggered when:
- You comment "@codex security review"
- A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review
Once complete, Codex will leave suggestions, or a comment if no findings are found.
| expected: (u32, u32), | ||
| guardrails: &DecodeGuardrails, | ||
| ) -> Result<Option<HeicAuxiliaryAlphaPlane>, DecodeHeicError> { | ||
| let Ok(grid) = isobmff::extract_heic_grid_item_data(input, source, meta, resolved, location) |
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Security: Validate alpha grids before materializing their tiles
When an RGBA caller decodes attacker-controlled HEIC, validate the auxiliary grid before this extraction. A 132,588-byte file can declare a mismatched 255x255 alpha grid whose 65,025 dimg entries all reference one 2,048-byte tile. The extractor clones and retains each payload up to its 128 MiB floor; only afterward do the dimension and max_pixels checks reject the grid. Thus even max_input_bytes=1,000,000 and max_pixels=256 permit about 127 MiB of transient allocation, which can terminate a constrained process. Check expected geometry/guardrails before tile extraction and share payloads for repeated item IDs.
Useful? React with 👍 / 👎.
A grid can reference the same tile item many times. Each reference got its own copy of the tile's data, so a 132 KB file whose 65,025 grid cells all point at one 2 KB tile made the decoder hold about 127 MiB of identical copies. Tiles that reference the same item now share one copy of its payload, as they already share its hvcC, colour and transform properties. The extracted buffer is moved into the Arc rather than copied, so peak memory for a single large tile does not grow. The tile data budget still counts every reference, so the set of accepted files does not change. HeicGridTileItemData::payload becomes an Arc<Vec<u8>>.
A grid alpha item of the wrong size was only rejected after all of its tiles had been copied. The grid descriptor is now read first, and an alpha grid whose output size differs from the primary image is skipped before any tile is extracted. The primary's size has already passed the caller's max_pixels limit, so this also holds the alpha grid to that limit before anything is copied.
A grid alpha item that fails to decode is dropped, so the image is returned without alpha, while running out of memory is reported as an error like other allocation failures. Nothing tested either half of this on the grid path.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Here are some automated security review suggestions for this pull request.
Reviewed commit: c91a2ba87f
ℹ️ About Codex security reviews in GitHub
This is an experimental Codex feature. Security reviews are triggered when:
- You comment "@codex security review"
- A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review
Once complete, Codex will leave suggestions, or a comment if no findings are found.
| if (descriptor.output_width, descriptor.output_height) != expected { | ||
| return Ok(None); | ||
| } | ||
| let Ok(grid) = isobmff::extract_heic_grid_item_data( |
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Security: Preflight alpha grids before copying distinct tile extents
When an application decodes attacker-supplied HEIC to RGBA, preflight the alpha grid before this extraction. A 729,536-byte 16×16 file can declare 16,384 distinct tile IDs whose iloc entries overlap one 8 KiB range. The ID-keyed Arc cache misses every entry, and the 128 MiB floor accepts exactly 128 MiB of retained copies before preflight_grid rejects the 128×128 layout; max_input_bytes=1,000,000 and max_pixels=256 still reached 143,948 KiB RSS. Fresh evidence beyond the prior comment is that distinct IDs evade the new cache even with overlapping extents. Preflight cheap geometry first, deduplicate normalized extents, or stream tiles under a caller-derived budget.
Useful? React with 👍 / 👎.
The grid tile data budget is max(4 x file size, 128 MiB). The 128 MiB floor let a small file make the decoder copy far more data than it holds: distinct tile items whose locations overlap the same bytes each got their own copy, and one large ICC profile was copied once per tile colour combination. A 536 KB file reached about 139 MB of tile copies before preflight could reject the grid, on both the primary and the alpha path. Tile payloads and colour profiles that are actually copied are now also held to four times the file size, with no floor. Repeated references to one tile share a copy, so they do not count against this cap, and the existing per-reference budget still applies. The 536 KB file now peaks at 6.5 MB, and a 908 KB file repeating one ICC profile across 120 tiles drops from 114 MB to 9.7 MB.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Superseded by ente#32. |
Transparent HEICs from Apple's ImageIO store their alpha as a
griditem, tiled like the primary, whenever a side is 1024 px or more. The decoder only accepted single coded alpha items, so these images decoded fully opaque without an error.Grid alpha now goes through the shared grid tile extraction, is preflighted with the caller's guardrails, and is composed by the existing grid decoder; its luma plane becomes the alpha. Only the first grid candidate that is an alpha item is extracted, so a crafted file listing thousands of candidates costs one tile budget.
The security review's note is addressed for both grid paths:
HeicGridTileItemData::payloadbecomes anArc<Vec<u8>>.Peak memory on crafted files, before → after:
On 23 ImageIO transparent files from 512×512 to 4096×4096, RGBA output now matches libheif 1.21.2 pixel for pixel. Across 305 corpus and ImageIO files, only the 20 files with grid alpha change output. A transparent 4096×4096 image takes about 30 MB more peak memory, roughly the size of the alpha plane.
Alpha planes whose size differs from the primary are still ignored (see
FUTURE_WORK.md).