FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
security(interchange): harden FCPXML cycle detection, XML DTD entities, and imported formats (#3079)
Dylan M. Taylor15ca07300bde
diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rsindex 6ee6e8043..aa19bb615 100644--- a/crates/format/src/lib.rs+++ b/crates/format/src/lib.rs@@ -234,9 +234,14 @@ fn validate_loaded(mut loaded: Loaded) -> Result<Loaded, FormatError> {for item in loaded.project.items.values_mut() {let name = item.name.clone();match &mut item.kind {- filmcraft_project::ItemKind::Media(m) if m.info.audio_streams.len() > max => {- log::warn!("media `{name}`: {} audio streams, keeping the first {max}", m.info.audio_streams.len());- m.info.audio_streams.truncate(max);+ filmcraft_project::ItemKind::Media(m) => {+ if let Some(video) = &m.info.video {+ filmcraft_project::validate_frame_size(video.width, video.height).map_err(|e| FormatError::Corrupt(format!("media `{name}`: {e}")))?;+ }+ if m.info.audio_streams.len() > max {+ log::warn!("media `{name}`: {} audio streams, keeping the first {max}", m.info.audio_streams.len());+ m.info.audio_streams.truncate(max);+ }}filmcraft_project::ItemKind::Sequence(sequence) => {let sequence = std::sync::Arc::make_mut(sequence);diff --git a/crates/format/tests/fixtures.rs b/crates/format/tests/fixtures.rsindex 6f1c9922a..fa1b7027d 100644--- a/crates/format/tests/fixtures.rs+++ b/crates/format/tests/fixtures.rs@@ -85,6 +85,20 @@ fn hostile_audio_stream_counts_and_indices_are_repaired_not_rejected() {assert_eq!(loaded.item(ItemId(5)).unwrap().as_media().unwrap().info.audio_streams.len(), filmcraft_media::MAX_AUDIO_STREAMS);}+/// Persisted media metadata is used to allocate synthetic and offline source frames, so it must+/// have the same bounded dimensions as a sequence before the project is opened.+#[test]+fn hostile_media_video_dimensions_are_rejected() {+ let mut p = decode(&fixture("v1-edit.fcproj")).unwrap().project;+ let media = p.item_mut(ItemId(5)).unwrap().as_media_mut().unwrap();+ let video = media.info.video.as_mut().unwrap();+ video.width = u32::MAX;+ video.height = 1;++ let err = decode(&encode(&p, false)).unwrap_err();+ assert!(matches!(err, FormatError::Corrupt(ref message) if message.contains("media `Ocean_Sunset.mp4`") && message.contains("frame size")), "{err}");+}+/// Schema 8 (before M3.10): no time interpolation / Hold Filters / Field Options / source channels/// on clips, no audio channel map, subclips without Restrict Trims.#[test]diff --git a/crates/interchange/src/fcpxml.rs b/crates/interchange/src/fcpxml.rsindex 438e22fbc..c75bdd651 100644--- a/crates/interchange/src/fcpxml.rs+++ b/crates/interchange/src/fcpxml.rs@@ -14,7 +14,7 @@//! clip markers. Transitions are supported on the primary storyline (and read from secondary//! storylines); transitions elsewhere are reported.-use std::collections::HashMap;+use std::collections::{HashMap, HashSet};use filmcraft_geom::Vec2;use filmcraft_media::MediaKind;@@ -132,6 +132,7 @@ struct Imp<'a, 'i, 'r> {assets: HashMap<String, Asset>,media_nodes: HashMap<String, Node<'a, 'i>>,media_items: HashMap<String, ItemId>,+ media_active: HashSet<String>,effects: HashMap<String, String>,}@@ -163,6 +164,7 @@ pub(crate) fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Rassets: HashMap::new(),media_nodes: HashMap::new(),media_items: HashMap::new(),+ media_active: HashSet::new(),effects: HashMap::new(),};if let Some(res) = child(root, "resources") {@@ -281,9 +283,15 @@ impl<'a, 'i> Imp<'a, 'i, '_> {}let n = *self.media_nodes.get(id)?;let s = child(n, "sequence")?;- // Reserve first so recursion terminates.+ if !self.media_active.insert(id.into()) {+ self.report.warn(format!("recursive FCPXML media resource {id:?} ignored"));+ return None;+ }+ // Mark the resource before descending: a compound clip can reference itself through a+ // ref-clip before a completed sequence is available to cache.let name = n.attribute("name").unwrap_or("Compound Clip").to_string();let item = self.sequence(s, &name, None);+ self.media_active.remove(id);self.media_items.insert(id.into(), item);Some(item)}diff --git a/crates/interchange/src/xml.rs b/crates/interchange/src/xml.rsindex 4d227be49..3bc1120bb 100644--- a/crates/interchange/src/xml.rs+++ b/crates/interchange/src/xml.rs@@ -159,6 +159,17 @@ pub(crate) fn num(v: f64) -> String {}pub(crate) fn parse(text: &str) -> Result<roxmltree::Document<'_>, String> {+ // FCP7 XML commonly carries a plain `<!DOCTYPE xmeml>` declaration, which+ // roxmltree accepts only with DTD parsing enabled. Internal subsets are not+ // needed by any supported interchange format and can expand entities far+ // beyond the input size, so reject them before handing the document to the+ // parser. External entities remain unresolved because no resolver is set.+ if let Some(start) = text.find("<!DOCTYPE") {+ let declaration = &text[start..text[start..].find('>').map_or(text.len(), |end| start + end)];+ if declaration.contains('[') {+ return Err("internal DTD subsets are not supported".into());+ }+ }let opts = roxmltree::ParsingOptions { allow_dtd: true, ..Default::default() };roxmltree::Document::parse_with_options(text, opts).map_err(|e| e.to_string())}@@ -196,4 +207,10 @@ mod tests {assert_eq!(a.attribute("name"), Some("x\ny\tz\r"));assert_eq!(child(a, "b").and_then(|b| b.text()), Some("a\nb"));}++ #[test]+ fn rejects_internal_dtd_entity_expansion() {+ let text = "<!DOCTYPE root [<!ENTITY word 'expanded'>]><root>&word;</root>";+ assert_eq!(parse(text).unwrap_err(), "internal DTD subsets are not supported");+ }}diff --git a/crates/interchange/tests/it/fcpxml.rs b/crates/interchange/tests/it/fcpxml.rsindex 2bf6f3c46..065c341fd 100644--- a/crates/interchange/tests/it/fcpxml.rs+++ b/crates/interchange/tests/it/fcpxml.rs@@ -98,6 +98,24 @@ fn doc() -> String {)}+#[test]+fn self_referencing_compound_media_is_ignored_without_recursing() {+ let xml = r#"<fcpxml version="1.10">+ <resources>+ <format id="r1" frameDuration="1/24s" width="1920" height="1080"/>+ <media id="r2" name="Loop"><sequence format="r1"><spine>+ <ref-clip ref="r2" offset="0s" start="0s" duration="1s"/>+ </spine></sequence></media>+ </resources>+ <library><event><project name="P"><sequence format="r1"><spine>+ <ref-clip ref="r2" offset="0s" start="0s" duration="1s"/>+ </spine></sequence></project></event></library>+ </fcpxml>"#;+ let (imported, report) = import(xml.as_bytes(), Format::Fcpxml, None).unwrap();+ assert!(!imported.sequences.is_empty());+ assert!(report.mentions("recursive FCPXML media"), "{report:?}");+}+#[test]fn golden_connected_clips() {let d = doc();