FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
security(captions): bound burn-in overlays (#2941)
Earl Coca3ee0084502
diff --git a/crates/captions/src/burn.rs b/crates/captions/src/burn.rsindex 463e9737b..e29c3a5c3 100644--- a/crates/captions/src/burn.rs+++ b/crates/captions/src/burn.rs@@ -12,6 +12,12 @@ use filmcraft_time::Tick;use filmcraft_text::{ParagraphStyle, TextStyle, layout, render};+/// Caption overlays are transient render resources, not source images. Keep one cue from+/// materialising an unbounded mask or RGBA buffer.+const MAX_OVERLAY_PIXELS: usize = 16 << 20;+const MAX_CAPTION_SIZE_1080: f32 = 256.0;+const MAX_OUTLINE_RADIUS: f32 = 64.0;+/// A rendered caption: premultiplied linear-light RGBA f32 pixels at `(x, y)` in the frame.#[derive(Clone, Debug, PartialEq)]pub struct Overlay {@@ -110,7 +116,7 @@ pub fn render_caption(c: &Caption, style: &CaptionStyle, w: usize, h: usize) ->return None;}let scale = h as f32 / 1080.0;- let px = (style.size * scale).max(4.0);+ let px = (style.size.clamp(4.0, MAX_CAPTION_SIZE_1080) * scale).clamp(4.0, h as f32);let max_w = w as f32 * 0.9;let ts = caption_style(&style.font, px);let mut lines: Vec<String> = Vec::new();@@ -124,7 +130,7 @@ pub fn render_caption(c: &Caption, style: &CaptionStyle, w: usize, h: usize) ->}let vm = filmcraft_text::fonts::face(filmcraft_text::resolve(&ts.family, &ts.style).face).metrics(px);let (asc, desc) = (vm.ascent, vm.descent);- let lh = px * style.line_spacing.max(0.8);+ let lh = px * style.line_spacing.clamp(0.8, 4.0);let pad_x = (px * 0.3).round();let block_h = lh * lines.len() as f32;let (line_override, align_override) = vtt_overrides(&c.settings);@@ -148,15 +154,24 @@ pub fn render_caption(c: &Caption, style: &CaptionStyle, w: usize, h: usize) ->CaptionAlign::Center => (w as f32 - lw) / 2.0,}).collect();- let outline = (style.outline * scale).max(0.0);+ let outline = (style.outline * scale).clamp(0.0, MAX_OUTLINE_RADIUS);let grow = outline.ceil() + 1.0;let bx0 = xs.iter().zip(&widths).map(|(x, _)| x - pad_x - grow).fold(f32::MAX, f32::min).floor();let bx1 = xs.iter().zip(&widths).map(|(x, lw)| x + lw + pad_x + grow).fold(f32::MIN, f32::max).ceil();let by0 = (top - grow).floor();let by1 = (top + block_h + grow).ceil();- let (ox, oy) = (bx0 as i32, by0 as i32);- let ow = (bx1 - bx0).max(1.0) as usize;- let oh = (by1 - by0).max(1.0) as usize;+ // Text can be wider than the frame (an unbroken imported WebVTT word, for example). Raster+ // only the visible intersection; `draw` receives the original coordinates and clips into it.+ let (left, top) = (bx0.clamp(0.0, w as f32), by0.clamp(0.0, h as f32));+ let (right, bottom) = (bx1.clamp(0.0, w as f32), by1.clamp(0.0, h as f32));+ if right <= left || bottom <= top {+ return None;+ }+ let (ox, oy) = (left as i32, top as i32);+ let (ow, oh) = ((right - left) as usize, (bottom - top) as usize);+ if ow.checked_mul(oh).is_none_or(|pixels| pixels > MAX_OVERLAY_PIXELS) {+ return None;+ }let mut cover = vec![0.0f32; ow * oh];let mut bg = vec![0.0f32; ow * oh];for (i, line) in lines.iter().enumerate() {@@ -319,6 +334,21 @@ mod tests {assert!(canvas.chunks(4).any(|p| p[0] > 0.9));}+ #[test]+ fn overlay_is_clipped_to_the_frame_for_an_unbroken_word() {+ let o = render_caption(&cap(&"W".repeat(10_000)), &CaptionStyle::default(), 640, 360).unwrap();+ assert!(o.x >= 0 && o.y >= 0);+ assert!(o.w <= 640 && o.h <= 360, "{}x{}", o.w, o.h);+ assert!(o.w.checked_mul(o.h).unwrap() <= MAX_OVERLAY_PIXELS);+ }++ #[test]+ fn hostile_outline_is_bounded() {+ let st = CaptionStyle { outline: 1_000_000.0, ..Default::default() };+ let o = render_caption(&cap("Safe"), &st, 640, 360).unwrap();+ assert!(o.w <= 640 && o.h <= 360, "{}x{}", o.w, o.h);+ }+#[test]fn outline_draws_around_text() {let st = CaptionStyle { background: false, outline: 4.0, outline_color: [255, 0, 0, 255], ..Default::default() };