Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

security(project): scan malformed caption markup once (#2885)

Earl Cof30c6be22866
diff --git a/crates/project/src/caption.rs b/crates/project/src/caption.rs
index d8236e8bd..30c279d26 100644
--- a/crates/project/src/caption.rs
+++ b/crates/project/src/caption.rs
@@ -239,25 +239,36 @@ pub fn plain_text(s: &str) -> String {
while let Some(c) = chars.next() {
match c {
'<' => {
- // a tag runs to the next '>' (if none, keep the '<' literally)
- let rest: String = chars.clone().collect();
- if let Some(end) = rest.find('>') {
- for _ in 0..rest[..=end].chars().count() {
- chars.next();
+ // A tag runs to the next '>'. Scan forward once; preserving an unterminated
+ // tag as literal text must not rescan every later opening bracket.
+ let mut literal = String::new();
+ let mut closed = false;
+ for next in chars.by_ref() {
+ if next == '>' {
+ closed = true;
+ break;
}
- } else {
+ literal.push(next);
+ }
+ if !closed {
out.push('<');
+ out.push_str(&literal);
}
}
'{' if chars.peek() == Some(&'\\') => {
// SSA-style override blocks some SRT files carry: {\an8}
- let rest: String = chars.clone().collect();
- if let Some(end) = rest.find('}') {
- for _ in 0..rest[..=end].chars().count() {
- chars.next();
+ let mut literal = String::new();
+ let mut closed = false;
+ for next in chars.by_ref() {
+ if next == '}' {
+ closed = true;
+ break;
}
- } else {
+ literal.push(next);
+ }
+ if !closed {
out.push('{');
+ out.push_str(&literal);
}
}
'&' => {
@@ -300,6 +311,14 @@ mod tests {
assert_eq!(plain_text("<v Bob>Hi</v>"), "Hi");
}
+ #[test]
+ fn malformed_caption_markup_is_preserved_without_repeated_suffix_scans() {
+ let angle = "<".repeat(10_000);
+ let ssa = format!("{{\\{}", "{".repeat(10_000));
+ assert_eq!(plain_text(&angle), angle);
+ assert_eq!(plain_text(&ssa), ssa);
+ }
+
#[test]
fn caption_at() {
let mut t = CaptionTrack::new(TrackId(1), "Subtitle".into(), CaptionFormat::Subtitle);