From 9563f22969b3cb158d57a45a1e63a9166ed0b10c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 06:28:59 -0500 Subject: [PATCH] security(pipeline): one dab budget per brush mask; linear-time defringe min/max * security(pipeline): one dab budget per brush mask; linear-time defringe min/max * address review: drop min_filter transpose buffers once consumed * address review: an over-budget brush mask fails the render instead of dropping later strokes * merge main into fix/sec-pipeline-render-work; the web worker rejects an over-budget brush mask * address review: validate brush budgets on the mask's own frame; an exactly filled budget is not over it --- apps/lightcraft-web/src/wire.rs | 17 ++++++++ crates/engine/src/media/render.rs | 15 +++++++ crates/pipeline/src/lib.rs | 21 ++++++++++ crates/pipeline/src/masks.rs | 34 +++++++++++++++- crates/pipeline/src/masks/tests.rs | 45 +++++++++++++++++++++ crates/pipeline/src/optics.rs | 62 ++++++++++++++++++++--------- crates/pipeline/src/optics/tests.rs | 31 +++++++++++++++ 7 files changed, 205 insertions(+), 20 deletions(-) diff --git a/apps/lightcraft-web/src/wire.rs b/apps/lightcraft-web/src/wire.rs index e7202bbc59..9854798673 100644 --- a/apps/lightcraft-web/src/wire.rs +++ b/apps/lightcraft-web/src/wire.rs @@ -210,6 +210,8 @@ impl WorkerCore { None => src, }; let info = src.info_or(job.info()); + // an over-budget brush mask is an error for the caller, not a preview missing its later strokes + lightcraft_engine::pipeline::check_brush_budget(&src.image, &info, &job.settings, &job.request())?; let mut rendered = match &job.stages { Some(view) => { let st = self.stages.entry(view.clone()).or_default().clone(); @@ -442,6 +444,21 @@ mod tests { assert!(max.is_some_and(|m| m <= 3), "max channel difference {max:?}"); } + // an over-budget brush mask is an error for the caller, as on native + #[test] + fn an_over_budget_brush_mask_fails_the_render() { + use lightcraft_engine::develop::{BrushStroke, Mask, MaskComponent, MaskShape}; + let mut s = Session::with_demo(); + let id = s.visible_cloned()[0]; + let mut wire = WireJob::from_job(&s.thumb_job(id, 128).unwrap(), false, "Thumb"); + let points = serde_json::from_value(serde_json::json!([{"x": -4000.0, "y": 0.5}, {"x": 4000.0, "y": 0.5}])).unwrap(); + let far = BrushStroke { points, size: 0.05, ..Default::default() }; + let component = MaskComponent { name: None, op: Default::default(), invert: false, shape: MaskShape::Brush { strokes: vec![far; 10] } }; + Arc::make_mut(&mut wire.settings).masks.push(Mask { visible: true, components: vec![component], ..Default::default() }); + let err = WorkerCore::default().render(&wire, None).err().unwrap_or_default(); + assert!(err.contains("dabs"), "{err}"); + } + // a job from before windows existed has none #[test] fn a_job_without_a_window_field_has_no_window() { diff --git a/crates/engine/src/media/render.rs b/crates/engine/src/media/render.rs index bd8ee5292f..af7d10e268 100644 --- a/crates/engine/src/media/render.rs +++ b/crates/engine/src/media/render.rs @@ -356,6 +356,21 @@ impl RenderJob { // converted for the display below let clip_view = self.request.overlay.is_clipping(); let request = if clip_view { RenderRequest { display: None, ..self.request } } else { self.request }; + if let Err(e) = lightcraft_pipeline::check_brush_budget(src, &info, &self.settings, &request) { + return RenderResult { + request_id: self.request_id, + source_key: self.source_key, + photo: self.photo, + level: self.level, + key: self.key, + rendered: Err(e), + gpu_fallback: None, + loaded: None, + quick: None, + display: None, + frame: None, + }; + } let pixels_needed = self.cache.is_some() || self.display.is_some() || (self.view_cache.is_some() && self.request.quality == Quality::Full); let frame = (self.on_screen && !pixels_needed && gpu) diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index ef2d2359ca..7ca709ac3f 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -202,6 +202,27 @@ pub fn check_output_size(max_w: usize, max_h: usize) -> Result<(), String> { Ok(()) } +/// Fails when a visible brush mask holds more dabs than its budget ([`masks::MAX_STROKE_DABS`]) at this render's +/// output size: the render would otherwise skip its later strokes and show a wrong mask. +pub fn check_brush_budget(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest) -> Result<(), String> { + let brush = + |m: &lightcraft_develop::Mask| m.visible && m.components.iter().any(|c| matches!(c.shape, lightcraft_develop::MaskShape::Brush { .. })); + if !s.masks.iter().any(brush) { + return Ok(()); + } + let p = plan(src, info, s, req); + for m in s.masks.iter().filter(|m| brush(m)) { + for c in &m.components { + if let lightcraft_develop::MaskShape::Brush { strokes } = &c.shape + && masks::brush_over_budget(strokes, &p.frame.for_mask(m.on_photo), p.w, p.h) + { + return Err(format!("mask \"{}\": the brush strokes hold more than {} dabs at this size", m.name, masks::MAX_STROKE_DABS)); + } + } + } + Ok(()) +} + impl RenderRequest { pub fn fit(max_w: usize, max_h: usize) -> Self { Self { diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs index dfbf00326a..84bbdadde1 100644 --- a/crates/pipeline/src/masks.rs +++ b/crates/pipeline/src/masks.rs @@ -819,6 +819,31 @@ fn guided_cross_fast(guide: &Plane, p: &mut Plane, sigma: f32, eps: f32, weight: }); } +/// Whether the strokes of one brush mask hold more dabs than the mask's shared budget ([`MAX_STROKE_DABS`]), so that +/// rasterizing it would leave later strokes (an erase among them) unapplied. Callers that can fail check this and +/// report an error rather than render a wrong mask. +pub fn brush_over_budget(strokes: &[BrushStroke], frame: &Frame, w: usize, h: usize) -> bool { + let mut room = MAX_STROKE_DABS; + for s in strokes { + if s.points.is_empty() { + continue; + } + if room == 0 { + return true; + } + let n = brush_dabs_within(s, frame, w, h, room).dabs.len(); + if n >= room { + // the budget is exactly filled, or the stroke was cut off at it: one more dab says which (a stroke + // already at its own cap [`MAX_STROKE_DABS`] is cut off, whatever it would have held) + if room >= MAX_STROKE_DABS || brush_dabs_within(s, frame, w, h, room + 1).dabs.len() > room { + return true; + } + } + room -= n; + } + false +} + /// Stamp brush strokes into `out` (max-combine; erase strokes subtract). Auto Mask strokes weight /// each dab by [`auto_similarity`] to the pixel under its centre and are refined with /// [`guided_cross`] on log luminance. @@ -826,8 +851,15 @@ fn rasterize_brush(strokes: &[BrushStroke], frame: &Frame, w: usize, h: usize, i if w == 0 || h == 0 { return; } + // one dab budget for the whole mask, as the GPU path keeps: a stroke's own cap alone let a + // thousand far-flung two-point strokes build and stamp a billion dabs + let mut room = MAX_STROKE_DABS; for s in strokes { - let bd = brush_dabs(s, frame, w, h); + if room == 0 { + break; + } + let bd = brush_dabs_within(s, frame, w, h, room); + room = room.saturating_sub(bd.dabs.len()); let (dens, erase, auto) = (bd.density, bd.erase, bd.auto); let mut stroke_alpha = Plane::new(w, h); for (k, d) in bd.dabs.iter().enumerate() { diff --git a/crates/pipeline/src/masks/tests.rs b/crates/pipeline/src/masks/tests.rs index b57f9f6688..8b96e92696 100644 --- a/crates/pipeline/src/masks/tests.rs +++ b/crates/pipeline/src/masks/tests.rs @@ -630,6 +630,51 @@ fn people_and_subject_use_person_mattes() { assert_eq!(eval(&lips), shape_alpha(&lips, &f, w, h, &img, &l, 0.0, None)); } +/// A thousand two-point strokes, each a sweep far past the photo that lays most of a stroke's +/// own budget: the mask shares one budget across its strokes, so it renders in moments rather +/// than building and visiting most of a billion dabs. +#[test] +fn many_far_flung_strokes_share_one_dab_budget() { + let (w, h) = (64, 64); + let s = BrushStroke { points: vec![Point::new(-4000.0, 0.5), Point::new(4000.0, 0.5)], size: 0.05, ..Default::default() }; + assert!(brush_dabs(&s, &frame(w, h), w, h).dabs.len() > 500_000, "each stroke alone is large"); + let strokes = vec![s; 1000]; + let t = std::time::Instant::now(); + let mut out = Plane::new(w, h); + rasterize_brush(&strokes, &frame(w, h), w, h, &Rgb32f::new(w, h), &Plane::new(w, h), &mut out); + assert!(t.elapsed().as_secs() < 30, "took {:?}", t.elapsed()); + assert!(out.data.iter().any(|&v| v > 0.0), "the strokes crossing the photo still paint it"); +} + +/// Past the shared dab budget a brush mask is an error for the caller, not a mask missing its later (erase) strokes. +#[test] +fn an_over_budget_brush_mask_is_reported() { + let (w, h) = (64, 64); + let far = BrushStroke { points: vec![Point::new(-4000.0, 0.5), Point::new(4000.0, 0.5)], size: 0.05, ..Default::default() }; + let near = BrushStroke { points: vec![Point::new(0.1, 0.1), Point::new(0.9, 0.9)], size: 0.05, ..Default::default() }; + assert!(!brush_over_budget(std::slice::from_ref(&near), &frame(w, h), w, h)); + assert!(brush_over_budget(&vec![far; 10], &frame(w, h), w, h)); +} + +/// Strokes that fill the budget exactly are not over it; one more dab is. +#[test] +fn a_brush_mask_that_exactly_fills_the_budget_is_not_over_it() { + let (w, h) = (64, 64); + let stroke = |x: f64| BrushStroke { points: vec![Point::new(0.0, 0.5), Point::new(x, 0.5)], size: 0.05, ..Default::default() }; + // a stroke whose dab count divides the budget evenly + let (s, m) = (0..14_000) + .map(|i| stroke(6.0 + f64::from(i) * 0.001)) + .map(|s| { + let m = brush_dabs(&s, &frame(w, h), w, h).dabs.len(); + (s, m) + }) + .find(|(_, m)| *m >= 500 && MAX_STROKE_DABS.is_multiple_of(*m)) + .expect("a stroke whose dabs divide the budget"); + let k = MAX_STROKE_DABS / m; + assert!(!brush_over_budget(&vec![s.clone(); k], &frame(w, h), w, h), "{k} strokes of {m} dabs fill the budget exactly"); + assert!(brush_over_budget(&vec![s; k + 1], &frame(w, h), w, h), "one more stroke is over it"); +} + #[test] fn intersect_first_component_selects_its_shape() { let f = frame(100, 100); diff --git a/crates/pipeline/src/optics.rs b/crates/pipeline/src/optics.rs index 883be6730f..e8dc284c36 100644 --- a/crates/pipeline/src/optics.rs +++ b/crates/pipeline/src/optics.rs @@ -681,31 +681,55 @@ pub fn defringe(img: &mut Rgb32f, s: &DevelopSettings, scale: f64) { }); } -/// Separable square min (or max) filter of radius `r`. +/// Separable square min (or max) filter of radius `r`, linear in the pixels whatever `r` is (a +/// crop can make the radius as wide as the output, and a scan of every neighbourhood was then +/// cubic in the side). fn min_filter(p: &Plane, r: usize, max: bool) -> Plane { let (w, h) = (p.width, p.height); - let pick = |a: f32, b: f32| if max { a.max(b) } else { a.min(b) }; let mut tmp = Plane::new(w, h); - for_rows(&mut tmp.data, w, |y, row| { - let src = &p.data[y * w..(y + 1) * w]; - for (x, v) in row.iter_mut().enumerate() { - let (a, b) = (x.saturating_sub(r), (x + r).min(w - 1)); - *v = src[a..=b].iter().copied().fold(src[x], pick); - } - }); + for_rows(&mut tmp.data, w, |y, row| extreme_1d(&p.data[y * w..(y + 1) * w], r, max, row)); + // the vertical pass runs along the rows of the transposed plane + let mut t = vec![0f32; w * h]; + for (i, v) in tmp.data.iter().enumerate() { + t[(i % w) * h + i / w] = *v; + } + // each buffer goes once the next is built, so no more than three planes are alive at once + drop(tmp); + let mut tt = vec![0f32; w * h]; + for_rows(&mut tt, h, |x, col| extreme_1d(&t[x * h..(x + 1) * h], r, max, col)); + drop(t); let mut out = Plane::new(w, h); - for_rows(&mut out.data, w, |y, row| { - let (a, b) = (y.saturating_sub(r), (y + r).min(h - 1)); - for (x, v) in row.iter_mut().enumerate() { - let mut m = tmp.data[y * w + x]; - for yy in a..=b { - m = pick(m, tmp.data[yy * w + x]); - } - *v = m; - } - }); + for (i, v) in tt.iter().enumerate() { + out.data[(i % h) * w + i / h] = *v; + } out } +/// `out[x]` = the min (or max) of `src[x - r ..= x + r]` clipped to the slice, by the van Herk / +/// Gil–Werman blocks: three passes over the line however wide the window. +fn extreme_1d(src: &[f32], r: usize, max: bool, out: &mut [f32]) { + let n = src.len(); + if n == 0 { + return; + } + // past the line's length a wider window takes in nothing more + let r = r.min(n); + let id = if max { f32::NEG_INFINITY } else { f32::INFINITY }; + let pick = |a: f32, b: f32| if max { a.max(b) } else { a.min(b) }; + let (k, m) = (2 * r + 1, n + 2 * r); + let pad = |i: usize| if i < r || i >= r + n { id } else { src[i - r] }; + let mut g = vec![id; m]; + let mut hh = vec![id; m]; + for i in 0..m { + g[i] = if i % k == 0 { pad(i) } else { pick(g[i - 1], pad(i)) }; + } + for i in (0..m).rev() { + hh[i] = if i % k == k - 1 || i == m - 1 { pad(i) } else { pick(hh[i + 1], pad(i)) }; + } + for (x, o) in out.iter_mut().enumerate().take(n) { + *o = pick(hh[x], g[x + k - 1]); + } +} + #[cfg(test)] mod tests; diff --git a/crates/pipeline/src/optics/tests.rs b/crates/pipeline/src/optics/tests.rs index a404222e82..28518981ea 100644 --- a/crates/pipeline/src/optics/tests.rs +++ b/crates/pipeline/src/optics/tests.rs @@ -475,3 +475,34 @@ fn defringe_amounts_past_the_sliders_act_as_their_end() { assert_eq!(at(1e300, 1.0).data, at(20.0, 1.0).data); assert_eq!(at(f64::MAX, 1e300).data.len(), 800, "a huge scale too"); } + +#[test] +fn the_min_max_filter_matches_a_brute_force_scan_and_is_linear_in_its_radius() { + let (w, h) = (37, 23); + let mut p = Plane::new(w, h); + for (i, v) in p.data.iter_mut().enumerate() { + *v = ((i * 7919) % 101) as f32 / 101.0; + } + for r in [0, 1, 2, 5, 22, 40] { + for max in [false, true] { + let got = min_filter(&p, r, max); + for y in 0..h { + for x in 0..w { + let mut m = p.data[y * w + x]; + for yy in y.saturating_sub(r)..=(y + r).min(h - 1) { + for xx in x.saturating_sub(r)..=(x + r).min(w - 1) { + let v = p.data[yy * w + xx]; + m = if max { m.max(v) } else { m.min(v) }; + } + } + assert_eq!(got.data[y * w + x], m, "r {r} max {max} at {x},{y}"); + } + } + } + } + // a radius as wide as a large output: was ~4 L³ comparisons + let big = Plane::new(2000, 2000); + let t = std::time::Instant::now(); + let _ = min_filter(&big, 2000, true); + assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed()); +}