Skip to content

Decode auxiliary alpha stored as a grid - #19

Closed
laurens-pilot wants to merge 6 commits into
mainfrom
feat/grid-alpha
Closed

laurens-pilot wants to merge 6 commits into
mainfrom
feat/grid-alpha

Conversation

@laurens-pilot

@laurens-pilot laurens-pilot commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Transparent HEICs from Apple's ImageIO store their alpha as a grid item, 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:

  • An alpha grid whose size differs from the primary is skipped before any of its tiles are copied.
  • Tiles that reference the same item share one copy of its payload. HeicGridTileItemData::payload becomes an Arc<Vec<u8>>.
  • Tile payloads and ICC profiles that are actually copied are capped at four times the file size, with no 128 MiB floor. The existing per-reference budget still applies.

Peak memory on crafted files, before → after:

  • 132 KB file from the review, 65,025 references to one 2 KB tile: 142 MB → 3.5 MB (7 MB as a primary grid)
  • 536 KB file, 256 distinct tiles over the same bytes: 139 MB → 6.5 MB, on either grid path
  • 908 KB file, one ICC profile across 120 tiles: 114 MB → 9.7 MB

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).

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.
@laurens-pilot

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T12:47:37.763959Z c8a7a37 Manual request
🔒 Security Review ✅ Completed 2026-11-03T12:49:31.455684Z c8a7a37 Manual request

Security findings

Advisory findings (2)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 4e4a497302

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ 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.

Comment thread src/lib.rs Outdated
expected: (u32, u32),
guardrails: &DecodeGuardrails,
) -> Result<Option<HeicAuxiliaryAlphaPlane>, DecodeHeicError> {
let Ok(grid) = isobmff::extract_heic_grid_item_data(input, source, meta, resolved, location)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

P2 Badge 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.
@laurens-pilot

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: c91a2ba87f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ 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.

Comment thread src/lib.rs
if (descriptor.output_width, descriptor.output_height) != expected {
return Ok(None);
}
let Ok(grid) = isobmff::extract_heic_grid_item_data(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

P2 Badge 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.
@laurens-pilot

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: c8a7a37a14

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: c8a7a37a14

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ 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.

@laurens-pilot

Copy link
Copy Markdown
Owner Author

Superseded by ente#32.

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