From d795d23b31f72a5b5983cd7ffa4f01a71b048e07 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:08:39 -0500 Subject: [PATCH] Bound malformed subtitle scans and escape mixed-case SubViewer breaks --- crates/captions/src/microdvd.rs | 37 +++++++++++++++++++++----------- crates/captions/src/sami.rs | 17 +++++++++++++-- crates/captions/src/subviewer.rs | 20 ++++++++++++++++- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/crates/captions/src/microdvd.rs b/crates/captions/src/microdvd.rs index 7e3d17962a..a5b606367e 100644 --- a/crates/captions/src/microdvd.rs +++ b/crates/captions/src/microdvd.rs @@ -67,21 +67,23 @@ fn split_controls(line: &str) -> (String, Vec<(char, String)>) { while let Some(open) = rest.find('{') { text.push_str(rest.get(..open).unwrap_or("")); let after = rest.get(open + 1..).unwrap_or(""); - let control = after.split_once('}').and_then(|(body, tail)| { - let (name, value) = body.split_once(':')?; - let mut chars = name.chars(); - let c = chars.next().filter(|c| c.is_ascii_alphabetic())?; - chars.next().is_none().then(|| (c, value.to_string(), tail)) - }); - match control { - Some((c, value, tail)) => { - controls.push((c, value)); + // Check the fixed-size control prefix before scanning its body. Invalid opening + // braces must not repeatedly scan the same suffix for a closing brace. + let name = after.as_bytes().first().copied().filter(u8::is_ascii_alphabetic); + if let Some(name) = name.filter(|_| after.as_bytes().get(1) == Some(&b':')) { + if let Some((body, tail)) = after.get(2..).and_then(|s| s.split_once('}')) { + controls.push((char::from(name), body.to_string())); rest = tail; - } - None => { + } else { + // No later control can close either: preserve the entire remaining text. text.push('{'); - rest = after; + text.push_str(after); + rest = ""; + break; } + } else { + text.push('{'); + rest = after; } } text.push_str(rest); @@ -207,6 +209,17 @@ mod tests { assert_eq!(cue_text("{f:Arial}{s:20}a & b {x"), "a & b {x"); } + #[test] + fn malformed_controls_inside_valid_cues_are_preserved() { + for raw in ["{".repeat(32_768), "{y:".repeat(16_384)] { + let doc = parse(&format!("{{0}}{{25}}{raw}")).unwrap(); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].text, raw); + } + assert_eq!(cue_text("{{y:i}hello"), "{hello"); + assert_eq!(cue_text("{xx:no}{y:b}hello"), "{xx:no}hello"); + } + #[test] fn rate_header() { let d = parse("{1}{1}25.000\n{25}{50}x\n").unwrap(); diff --git a/crates/captions/src/sami.rs b/crates/captions/src/sami.rs index 3329cc53e0..056cc1c6d8 100644 --- a/crates/captions/src/sami.rs +++ b/crates/captions/src/sami.rs @@ -72,8 +72,12 @@ fn text_run(s: &str) -> String { if let Some((_, len)) = crate::entity_at(rest) { out.push_str(rest.get(..len).unwrap_or("")); advance = len; - } else if let Some((body, _)) = rest.get(1..).and_then(|r| r.split_once(';')).filter(|(b, _)| b.len() <= 8) - && let Some(ch) = numeric_reference(body) + } else if let Some(body) = rest.get(1..).and_then(|r| { + // Numeric entities have at most eight body bytes. Never scan the + // unbounded remainder again for each malformed ampersand. + let end = r.bytes().take(9).position(|b| b == b';')?; + r.get(..end) + }) && let Some(ch) = numeric_reference(body) { out.push_str(&escape_markup(&ch.to_string())); advance = body.len() + 2; @@ -241,6 +245,15 @@ mod tests { assert_eq!(d.cues[1].end, last_cue_end(ms_to_tick(4000))); } + #[test] + fn malformed_entities_inside_valid_cues_are_bounded() { + let raw = "&".repeat(32_768); + let doc = parse(&format!("

{raw}

 ")).unwrap(); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].text, "&".repeat(32_768)); + assert_eq!(text_run("A A A &é;"), "A &#00000065; A &é;"); + } + #[test] fn only_the_first_class_is_read() { let d = parse("

en

kr

 ").unwrap(); diff --git a/crates/captions/src/subviewer.rs b/crates/captions/src/subviewer.rs index 337788f320..5b94ceedd0 100644 --- a/crates/captions/src/subviewer.rs +++ b/crates/captions/src/subviewer.rs @@ -91,7 +91,15 @@ pub fn write(doc: &Document) -> String { } let end = if clock(c.end) == clock(c.start) { Tick(c.end.0.saturating_add(crate::TICKS_PER_MS * 10)) } else { c.end }; out.push_str(&format!("{},{}\n", clock(c.start), clock(end))); - let lines: Vec = lines.iter().map(|l| l.replace("[br]", "\u{ff3b}br\u{ff3d}").replace("[BR]", "\u{ff3b}BR\u{ff3d}")).collect(); + let lines: Vec = lines + .iter() + .map(|l| { + l.replace("[br]", "\u{ff3b}br\u{ff3d}") + .replace("[BR]", "\u{ff3b}BR\u{ff3d}") + .replace("[Br]", "\u{ff3b}Br\u{ff3d}") + .replace("[bR]", "\u{ff3b}bR\u{ff3d}") + }) + .collect(); out.push_str(&lines.join("[br]")); out.push_str("\n\n"); } @@ -109,6 +117,16 @@ mod tests { assert_eq!(d.cues[0].end, crate::text::ms_to_tick(3500)); } + #[test] + fn writer_escapes_every_case_of_literal_break_tokens() { + let doc = Document { + cues: vec![Cue { start: Tick(0), end: crate::text::ms_to_tick(1000), text: "a[br]b[BR]c[Br]d[bR]é".into(), ..Default::default() }], + ..Default::default() + }; + let decoded = parse(&write(&doc)).unwrap(); + assert_eq!(decoded.cues[0].text, "a[br]b[BR]c[Br]d[bR]é"); + } + #[test] fn rejects_other_text() { assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").is_err());