Merge pull request #2630

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
aiongg
2026-12-04 20:05:43 +00:00
committed by autocraft-bot[bot]
co-authored by autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
4 changed files with 49 additions and 9 deletions
+5 -4
View File
@@ -81,10 +81,10 @@ pub fn decode(bytes: &Bytes) -> Result<Pcm> {
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<u64> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mss = MediaSourceStream::new(Box::new(Cursor::new(Shared(bytes.clone()))), MediaSourceStreamOptions::default());
+11
View File
@@ -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<u64> {
+7 -5
View File
@@ -52,7 +52,11 @@ pub fn probe(bytes: &Bytes) -> Result<MediaInfo> {
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<MediaInfo> {
};
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()
+26
View File
@@ -434,3 +434,29 @@ 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");
// 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");
}