From 0091d8c95e80b65c652c9010f1007c29f0b7e9d2 Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:28:14 +0000 Subject: [PATCH 1/2] Check MP4 sample tables before probe() demuxes for a duration Check MP4 sample tables before probe() demuxes for a duration `probe()` no longer hands an MP4 with damaged sample tables to Symphonia. When a file's header gives no length, `probe()` now runs the MP4 table check before the demux that adds up packet lengths. A crafted `stsz` gets `MediaError::Corrupt` instead of a 4 GB allocation that aborts the app. That is what happened before the regression. The bug is in this app's own code, not in craft-libs, so there was nothing to repin. Changes (followed the plan): - `crates/media/src/mp4guard.rs`: new `check(container, bytes)`. It returns Corrupt when the file is sniffed as Mp4, Unknown or Mp3 and `probe_fits` fails. This is the test that `audio::decode` and `probe_headers` each wrote out themselves before. - `crates/media/src/probe.rs`: `probe()` sniffs the container once and calls `mp4guard::check` before `demux_duration_ms`. `probe_headers` uses the same helper, and how it behaves is unchanged. - `crates/media/src/audio.rs`: `decode` uses the helper for its Symphonia fallback, with the same behaviour. A doc line on `demux_duration_ms` says callers must run the check first. Test: `probe_rejects_damaged_stsz_without_header_duration` sits next to the damaged-MP4 test in `crates/media/tests/it/decode.rs`. It zeroes the mvhd, tkhd and mdhd durations in `damaged/stsz-size.m4a` (I confirmed those offsets in the fixture). It then checks three things: `probe_headers` still describes decodable audio with no length, `probe` returns Corrupt, and an intact `tone.m4a` still probes with a length. Checks: the test fails with the check removed from `probe()` and passes with it. `cargo test -p deckcraft-media` passes (all unit and integration tests), and so do `cargo clippy -p deckcraft-media --all-targets -- -D warnings` and `cargo fmt --all --check`. I did not run `cargo xtask wasm` or the workspace-wide `cargo xtask ci`. --- crates/media/src/audio.rs | 9 +++++---- crates/media/src/mp4guard.rs | 11 +++++++++++ crates/media/src/probe.rs | 12 +++++++----- crates/media/tests/it/decode.rs | 17 +++++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/crates/media/src/audio.rs b/crates/media/src/audio.rs index 43d8dac48..0b271d00d 100644 --- a/crates/media/src/audio.rs +++ b/crates/media/src/audio.rs @@ -81,10 +81,10 @@ pub fn decode(bytes: &Bytes) -> Result { Some(r) => r, None => contained(|| decode_symphonia(bytes, "mkv")), }, - Container::Mp4 | Container::Unknown | Container::Mp3 if !crate::mp4guard::probe_fits(bytes) => { - Err(MediaError::Corrupt("the MP4's sample tables do not fit the file".into())) + c => { + crate::mp4guard::check(c, bytes)?; + contained(|| decode_symphonia(bytes, c.name())) } - c => contained(|| decode_symphonia(bytes, c.name())), } } @@ -297,7 +297,8 @@ pub(crate) fn probe_symphonia(bytes: &Bytes, ext: &str) -> Option<(String, u32, } /// Length in milliseconds of the track playback decodes, summed from its packet durations: the -/// file is demuxed but not decoded. `None` when the demuxer reports no durations. +/// file is demuxed but not decoded. `None` when the demuxer reports no durations. Callers must +/// pass [`crate::mp4guard::check`] first: the demuxer reads each sample whole at its `stsz` size. pub(crate) fn demux_duration_ms(bytes: &Bytes, ext: &str) -> Option { std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let mss = MediaSourceStream::new(Box::new(Cursor::new(Shared(bytes.clone()))), MediaSourceStreamOptions::default()); diff --git a/crates/media/src/mp4guard.rs b/crates/media/src/mp4guard.rs index 45d62f522..8510c1624 100644 --- a/crates/media/src/mp4guard.rs +++ b/crates/media/src/mp4guard.rs @@ -35,6 +35,17 @@ pub(crate) fn probe_fits(b: &[u8]) -> bool { } } +/// The check every path into symphonia's demuxer runs first: `Corrupt` when symphonia would read +/// `b` (sniffed as `c`) as an MP4 whose tables do not fit the file. Other known containers skip +/// it (a WAV's audio bytes can spell an `ftyp`, but symphonia opens it as a WAV). +pub(crate) fn check(c: crate::Container, b: &[u8]) -> crate::Result<()> { + use crate::Container; + if matches!(c, Container::Mp4 | Container::Unknown | Container::Mp3) && !probe_fits(b) { + return Err(crate::MediaError::Corrupt("the MP4's sample tables do not fit the file".into())); + } + Ok(()) +} + use craft_media::isobmff::boxes::BoxIter; fn be32(b: &[u8], at: usize) -> Option { diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 03b7b4f64..a150a1381 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -52,7 +52,11 @@ pub fn probe(bytes: &Bytes) -> Result { if info.duration_ms == 0 && info.audio.as_ref().is_some_and(|a| a.decodable) && info.video.is_none() { // No length in the header (raw MP3 / ADTS without an index): add up the packet // lengths. This only demuxes, so a long file neither allocates its PCM nor stalls the caller. - if let Some(d) = crate::audio::demux_duration_ms(bytes, Container::sniff(bytes).name()) { + // Our MP4 parser can describe a track whose `stsz` claims a sample symphonia would read + // whole, so the tables are checked first here too. + let c = Container::sniff(bytes); + crate::mp4guard::check(c, bytes)?; + if let Some(d) = crate::audio::demux_duration_ms(bytes, c.name()) { info.duration_ms = d; } } @@ -72,10 +76,8 @@ pub fn probe_headers(bytes: &Bytes) -> Result { }; if info.audio.is_none() && info.video.is_none() || info.audio.as_ref().is_some_and(|a| a.sample_rate == 0) { // Plain audio files (and anything our demuxers didn't describe): symphonia's probe. - // Symphonia opens an MP4 wherever its probe finds an `ftyp` (after leading ID3v2 tags too); other known containers (a WAV's audio bytes can spell an `ftyp`) skip the check. - if matches!(c, Container::Mp4 | Container::Unknown | Container::Mp3) && !crate::mp4guard::probe_fits(bytes) { - return Err(MediaError::Corrupt("the MP4's sample tables do not fit the file".into())); - } + // Symphonia opens an MP4 wherever its probe finds an `ftyp` (after leading ID3v2 tags too). + crate::mp4guard::check(c, bytes)?; let (codec, rate, channels, frames) = std::panic::catch_unwind(|| crate::audio::probe_symphonia(bytes, c.name())) .ok() .flatten() diff --git a/crates/media/tests/it/decode.rs b/crates/media/tests/it/decode.rs index 863aa4d3a..fd6427ddf 100644 --- a/crates/media/tests/it/decode.rs +++ b/crates/media/tests/it/decode.rs @@ -434,3 +434,20 @@ fn mp4_sample_tables_that_overrun_the_file_are_corrupt() { assert!(audio::decode(&fixture(name)).is_ok() || name.starts_with("clip"), "{name} still decodes"); } } + +/// `stsz-size.m4a` with its mvhd, tkhd and mdhd durations zeroed: `probe_headers` describes the +/// AAC track but finds no length, so `probe` demuxes it. The 4 GB sample must not reach symphonia. +#[test] +fn probe_rejects_damaged_stsz_without_header_duration() { + let mut b = fixture("damaged/stsz-size.m4a").to_vec(); + // The duration fields of mvhd, tkhd and mdhd (all version 0). + for at in [4538, 4658, 4790] { + b[at..at + 4].copy_from_slice(&[0; 4]); + } + let b: Bytes = Arc::new(b); + let head = probe::probe_headers(&b).expect("our MP4 parser still accepts the file"); + assert_eq!(head.duration_ms, 0); + assert!(head.audio.as_ref().is_some_and(|a| a.decodable && a.sample_rate > 0)); + assert!(matches!(probe::probe(&b), Err(MediaError::Corrupt(_))), "probe must reject the damaged sample table"); + assert!(probe::probe(&fixture("tone.m4a")).is_ok_and(|i| i.duration_ms > 0), "an intact MP4 still probes"); +} From 65debf460c8e4a9951a9bfc9402b079fb8be922a Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Fri, 4 Dec 2026 20:05:43 +0000 Subject: [PATCH 2/2] Part 1: I replaced the last assertion in `probe_rejects_damaged_stsz_wit Part 1: I replaced the last assertion in `probe_rejects_damaged_stsz_without_header_duration` (`crates/media/tests/it/decode.rs`). The old one probed the unmodified `tone.m4a`, which has a header duration, so it never took the demux path. The new control copies `tone.m4a` and zeroes the same mvhd, tkhd and mdhd duration fields (offsets 4538, 4658 and 4790). It then asserts three things. `probe_headers` reports `duration_ms == 0`. `probe` returns Ok, so `mp4guard::check` doesn't reject the intact file. The returned `duration_ms` is greater than 0, so the demux still finds a length. `demux_duration_ms` is `pub(crate)`, so the test checks the demux length through `probe`'s result. In the rest of `deckcraft-media`, I ran only the changed test, which passes. `cargo clippy -p deckcraft-media --all-targets -- -D warnings` passes and `cargo fmt --all --check` is clean. I did not run the full `deckcraft-media` test suite. My attempts to run it failed when the runner restarted. --- crates/media/tests/it/decode.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/media/tests/it/decode.rs b/crates/media/tests/it/decode.rs index fd6427ddf..4c0b2853d 100644 --- a/crates/media/tests/it/decode.rs +++ b/crates/media/tests/it/decode.rs @@ -449,5 +449,14 @@ fn probe_rejects_damaged_stsz_without_header_duration() { assert_eq!(head.duration_ms, 0); assert!(head.audio.as_ref().is_some_and(|a| a.decodable && a.sample_rate > 0)); assert!(matches!(probe::probe(&b), Err(MediaError::Corrupt(_))), "probe must reject the damaged sample table"); - assert!(probe::probe(&fixture("tone.m4a")).is_ok_and(|i| i.duration_ms > 0), "an intact MP4 still probes"); + // Control: the intact file with the same fields zeroed takes the same demux path, and the + // table check must let it through, with the length coming from the demux. + let mut intact = fixture("tone.m4a").to_vec(); + for at in [4538, 4658, 4790] { + intact[at..at + 4].copy_from_slice(&[0; 4]); + } + let intact: Bytes = Arc::new(intact); + assert_eq!(probe::probe_headers(&intact).expect("headers parse").duration_ms, 0, "the header gives no length"); + let info = probe::probe(&intact).expect("an intact MP4 without a header duration still probes"); + assert!(info.duration_ms > 0, "the demux still finds a length"); }