Files
craft-ai-integrations/tools/mutation/mutation_jpeg.py
T
Jeremy a717de4a88 fix(ai-media, ai-engine-candle): cap Extended XMP fragments before every JPEG decode (#187)
* test(ai-native): read access lists in-process in the Windows tests

The owner-only tests read each file's access list back with PowerShell's
Get-Acl. That call failed for every test on the windows-latest CI runner
after #184 merged, while the library code itself was not involved. The
tests now read the owner and list with GetNamedSecurityInfoW, through a
test-only helper in the named unsafe module, and the owner parser no
longer assumes a group section.

* test(ai-native): accept the LA alias for the built-in Administrator

CI runners use the machine's built-in Administrator account (RID 500),
which SDDL writes as LA instead of its full SID. The owner-only tests
compared against the full SID and failed on windows-latest; the files
themselves were correct.

* fix(ai-media, ai-engine-candle): cap Extended XMP fragments before every JPEG decode

The JPEG decoder re-scans every Extended XMP fragment collected so far after
each header segment, so a file made of thousands of tiny fragments costs it
quadratic time. rten already refused more than 1,024 fragments; the returned-
picture check in ai-media and the candle engine did not.

The fragment counter moves from rten into craft-ai-core (`jpeg` module), the
one crate all three decoders already depend on. rten, candle and
`decoded_result` (which also checks Gemini reference pictures) now refuse an
over-cap file before the decoder or the shared decode budget is reached.

Tests build a file one fragment over the cap for each decoder, and the new
`jx` mutation group drops each caller's check and breaks the counter.

* fix(ai-core): count every Extended XMP namespace, not only those a header walk reaches

The decoder runs in a lenient mode that skips stray bytes between header
segments and keeps parsing Extended XMP fragments. The header walk stopped at
the first stray byte, so a file with one stray byte before an over-cap
fragment chain passed the guard and still reached the quadratic work in the
rten and candle engines.

The count is now every occurrence of the Extended XMP namespace anywhere in
the file. The decoder only keeps a fragment whose APP1 body starts with that
namespace, so the count can never be lower than what it collects.

Each decoder test now also puts a stray byte before the fragments. rten and
candle check that the at-cap file with the stray byte still decodes (so the
decoder does skip it) and that the over-cap one is refused. ai-media already
refused the stray byte in its end-marker walk, before the decoder.
2026-11-14 23:53:02 +00:00

45 lines
2.1 KiB
Python

"""The Extended XMP fragment cap every JPEG decoder checks before `image`/`zune-jpeg` sees a file.
The counter lives in `craft-ai-core` (`jpeg.rs`); the rten and candle engines and the returned-
picture check in `craft-ai-media` each call it first. Each case breaks the counter or drops one
caller's check, and the test that builds a file one fragment over the cap must notice. The
decoder tests also put a stray byte before the fragments, which the decoder skips; the old
header walk stopped there and under-counted.
"""
CORE = 'crates/ai-core/src/jpeg.rs'
RTEN = 'crates/ai-engine-rten/src/picture.rs'
CANDLE = 'crates/ai-engine-candle/src/jpeg.rs'
MEDIA = 'crates/ai-media/src/result_image.rs'
FILTER = 'a_jpeg_made_of_tiny_xmp_fragments_is_refused_before_decoding'
JX = [
('jpeg-cap-off-by-one', CORE,
'extended_xmp_fragments(data) > MAX_XMP_FRAGMENTS',
'extended_xmp_fragments(data) > MAX_XMP_FRAGMENTS + 1',
'craft-ai-core', 'lib', 'the_cap_is_inclusive'),
('jpeg-cap-refuses-the-limit', CORE,
'extended_xmp_fragments(data) > MAX_XMP_FRAGMENTS',
'extended_xmp_fragments(data) >= MAX_XMP_FRAGMENTS',
'craft-ai-core', 'lib', 'the_cap_is_inclusive'),
('jpeg-fragments-not-counted', CORE,
'.filter(|w| *w == EXTENDED_XMP_PREFIX)',
'.filter(|w| w.starts_with(b"http://ns.adobe.com/xap/"))',
'craft-ai-core', 'lib', 'counts_each_fragment'),
('jpeg-count-stops-at-a-stray-byte', CORE,
'.filter(|w| *w == EXTENDED_XMP_PREFIX)',
'.take_while(|w| w.first() != Some(&0x42))\n .filter(|w| *w == EXTENDED_XMP_PREFIX)',
'craft-ai-core', 'lib', 'after_stray_bytes'),
('rten-cap-not-checked', RTEN,
'if too_many_xmp_fragments(data) {',
'if false {',
'craft-ai-engine-rten', 'lib', FILTER),
('candle-cap-not-checked', CANDLE,
'if too_many_xmp_fragments(data) {',
'if false {',
'craft-ai-engine-candle', 'lib', FILTER),
('result-cap-not-checked', MEDIA,
'!too_many_xmp_fragments(data)\n && reaches_eoi(data)',
'reaches_eoi(data)',
'craft-ai-media', 'lib', FILTER),
]