From 7cd3fac330ce19cd2a2f456a528145a33f961b4f Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Wed, 14 Oct 2026 16:20:20 -0600 Subject: [PATCH] Review: carry render authority through Session::edit and surface denials Session::edit is the engine's universal mutation entry (linked-file refresh, transforms, place); renders reached through it ran with no authorization scope installed, so stored smart-filter params read ambiently there even after the execute-path fix. Install the scope in Session::edit as well, keyed on the session's authorize gate. apply_filter_to_surface also conflated a denied filter with an unknown one: authorize_render_command's Result was discarded with .ok()?, so a denial silently rendered unfiltered pixels and the stage entered the process-wide stack cache. The signature becomes Result>: Err for denials (callers propagate), None for ids that aren't pixel filters (pass-through semantics unchanged), and an unreadable map keeps its deliberate pass-through. apply_smart_filter and apply_smart_filters propagate instead of cur.clone(). Two tests: a refresh driven outside command execution (Session::edit) is denied and leaves the document unchanged; a denied render caches no stage. Nine authorization tests pass, engine suite and strict Clippy clean, panic hunt green. --- .../tests/smart_filter_authorization.rs | 43 +++++++++++++++++++ crates/engine/src/filters.rs | 25 ++++++----- crates/engine/src/jobs.rs | 9 +++- crates/engine/src/lib.rs | 3 ++ crates/engine/src/smart_cmds.rs | 33 ++++++++------ .../src/smart_cmds/tests/stack_cache.rs | 2 +- 6 files changed, 89 insertions(+), 26 deletions(-) diff --git a/crates/automation/tests/smart_filter_authorization.rs b/crates/automation/tests/smart_filter_authorization.rs index c90f5e4c57..a5c1681a52 100644 --- a/crates/automation/tests/smart_filter_authorization.rs +++ b/crates/automation/tests/smart_filter_authorization.rs @@ -216,3 +216,46 @@ fn trusted_parameter_edits_still_read_maps_and_luts_after_a_denied_request() { assert_eq!(renders[0], renders[2], "trusted rendering remains deterministic"); } } + +#[test] +fn a_refresh_outside_command_execution_surfaces_the_denial_instead_of_rendering_unfiltered() { + // link-watch and transform paths re-render through Session::edit, not Session::execute or + // jobs::run; the gate must reach them and the denial must not silently pass pixels through + // into the process-wide stack cache. + for lookup in [false, true] { + let f = Fixture::new(); + let (doc, layer) = f.saved_filter(lookup); + let mut h = f.headless(); + h.open(Path::new("stored.pcraft")).unwrap(); + let before = h.session.active().unwrap().doc.clone(); + // Session::edit is the outside-execute mutation path (an edit closure the UI or a + // linked-file change runs); the stored filter must be denied there too. + assert_denied(h.session.edit("Re-render", |d, _| { + match smart_cmds::refresh(d, layer) { + Ok(_) => Ok(()), + Err(e) => Err(e), + } + })); + assert!(Arc::ptr_eq(&before, &h.session.active().unwrap().doc)); + // A trusted session (no gate) still renders the same document. + let mut trusted = Session::new(); + trusted.add_document(doc, None); + let mut d = (*trusted.active().unwrap().doc).clone(); + assert!(smart_cmds::refresh(&mut d, layer).is_ok()); + } +} + +#[test] +fn an_ambient_map_or_lut_never_renders_unfiltered_pixels_into_the_stack_cache() { + // The denial path must fail the render before any stage is cached process-wide. + for lookup in [false, true] { + let f = Fixture::new(); + let (doc, layer) = f.saved_filter(lookup); + let mut s = Session::new(); + s.add_document(doc, None); + s.authorize = Some(authorize_desktop_engine_step); + let bytes = smart_cmds::stack_cache_bytes(); + assert_denied(s.execute("layer.smartFilter.setVisible", json!({"layer": layer.0, "visible": true}))); + assert_eq!(bytes, smart_cmds::stack_cache_bytes(), "a denied render cached no stage"); + } +} diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs index 5bddd2807a..e40be7b6ac 100644 --- a/crates/engine/src/filters.rs +++ b/crates/engine/src/filters.rs @@ -174,8 +174,9 @@ pub fn params_for(id: &str, p: &Value) -> Option { /// Runs filter command `id` with JSON `params` on `surf` (the pixel function behind every /// `filter.*` command; smart filters re-run it on re-render). `bounds` frames distortions; /// `selection` limits the result. Like layer filters, neighbourhood filters repeat the edge pixels -/// of `canvas` ∪ the surface's content instead of fading in transparency, and the output is -/// clipped to it. `None` for ids that aren't pixel filters. +/// `None` for ids that aren't pixel filters. `Err` when the requesting session's authority denies +/// the filter's params (an ambient path the session may not read): the caller must surface the +/// error rather than render unfiltered pixels. pub fn apply_filter_to_surface( id: &str, params: &Value, @@ -183,34 +184,36 @@ pub fn apply_filter_to_surface( bounds: photocraft_geom::Rect, selection: Option<&photocraft_raster::Surface>, canvas: photocraft_geom::Rect, -) -> Option { +) -> Result> { // Replays bypass command dispatch; nested paths must still obey the requesting session's // authority, including adjustment LUTs and plug-ins, before any resolver can read them. - crate::jobs::authorize_render_command(id, params).ok()?; + crate::jobs::authorize_render_command(id, params)?; // Liquify / Puppet Warp / Perspective Warp smart filters (distort_cmds). if let Some(out) = crate::distort_cmds::apply_to_surface(id, params, surf, canvas) { - return Some(out); + return Ok(Some(out)); } // Lens Correction / Adaptive Wide Angle / Camera Raw smart filters (lens_cmds). if let Some(out) = crate::lens_cmds::apply_to_surface(id, params, surf, canvas) { - return Some(out); + return Ok(Some(out)); } // Image › Adjustments recorded as smart filters (Levels, Curves, Shadows/Highlights… from PSD). if let Some(kind) = id.strip_prefix("image.adjustments.") { - return crate::adjust_cmds::adjust_as_filter(kind, params, surf); + return Ok(crate::adjust_cmds::adjust_as_filter(kind, params, surf)); } // WebAssembly plug-in smart filters (plugin_cmds). if id == crate::plugin_cmds::RUN { - return crate::plugin_cmds::apply_to_surface(id, params, surf, selection, canvas); + return Ok(crate::plugin_cmds::apply_to_surface(id, params, surf, selection, canvas)); } - let mut fp = params_for(id, params)?; + let Some(mut fp) = params_for(id, params) else { return Ok(None) }; // A map that can no longer be read (moved or deleted since) leaves the pixels alone, like an // unknown filter; the command itself already refused an unreadable map when it was added. - crate::filters_ext::resolve_map_path(&mut fp, params).ok()?; + if crate::filters_ext::resolve_map_path(&mut fp, params).is_err() { + return Ok(None); + } let sel_bounds = selection.map(photocraft_raster::Surface::content_bounds); let content = surf.content_bounds(); let area = algo::output_area(&fp, content, bounds, sel_bounds); - Some(algo::apply_in(surf, &fp, area, bounds, selection, canvas.union(&content))) + Ok(Some(algo::apply_in(surf, &fp, area, bounds, selection, canvas.union(&content)))) } pub(crate) fn has_filterable_layer(s: &Session) -> std::result::Result<(), String> { diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index c88aeae60b..de2afd1556 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -37,7 +37,7 @@ thread_local! { static RENDER_AUTHORIZATION: std::cell::Cell> = const { std::cell::Cell::new(None) }; } -struct AuthorizationScope(Option); +pub(crate) struct AuthorizationScope(Option); impl AuthorizationScope { fn enter(gate: Option) -> Self { @@ -51,6 +51,13 @@ impl Drop for AuthorizationScope { } } +/// [`AuthorizationScope`] entered with `gate` (or the one already installed, so trusted work on +/// this thread keeps ambient paths usable). [`Session::edit`] and the job paths use this to carry +/// the requesting session's authority into every smart-object re-render. +pub(crate) fn render_authorization_scope(gate: Option) -> AuthorizationScope { + AuthorizationScope::enter(gate) +} + /// Recheck a stored filter's command and parameters before evaluating it or using its cache. pub(crate) fn authorize_render_command(id: &str, params: &Value) -> Result<()> { match RENDER_AUTHORIZATION.with(std::cell::Cell::get) { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index f13d61b979..b8d6167c3d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -779,6 +779,9 @@ impl Session { /// Apply an undoable edit to the active document. pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + // Every mutation path (linked-file refresh, transform, place) re-renders smart objects + // through this entry; the requesting session's authority must reach those renders. + let _authorization = crate::jobs::render_authorization_scope(self.authorize); let restrict = self.color_restrict; let sampler_map = self.sampler_map.take(); let st = self.active_mut().ok_or(EngineError::NoDocument)?; diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs index a768203c17..6ffc89e67d 100644 --- a/crates/engine/src/smart_cmds.rs +++ b/crates/engine/src/smart_cmds.rs @@ -298,28 +298,35 @@ fn blend_surfaces(base: &Surface, top: &Surface, mode: BlendMode, opacity: f32) out } -/// Runs the smart filter stack on the placed (unfiltered) content. -pub fn apply_smart_filters(placed: &Surface, sm: &SmartObject, canvas: Rect) -> Surface { +/// Runs the smart filter stack on the placed (unfiltered) content. A filter the session's +/// authority denies surfaces its error (the trusted compositor path has no gate installed, so +/// this reads ambiently only when the caller established one and it refuses). +pub fn apply_smart_filters(placed: &Surface, sm: &SmartObject, canvas: Rect) -> Result { if !sm.filters_enabled || !sm.smart_filters.iter().any(|f| f.visible) { - return placed.clone(); + return Ok(placed.clone()); } let mut cur = placed.clone(); // Edge pixels repeat at the canvas ∪ placed-content edge, as for layer filters. let extent = canvas.union(&placed.content_bounds()); for f in sm.smart_filters.iter().filter(|f| f.visible) { - cur = apply_smart_filter(f, &cur, canvas, extent); + cur = apply_smart_filter(f, &cur, canvas, extent)?; } - match sm.filter_mask.as_ref().filter(|m| m.enabled) { + Ok(match sm.filter_mask.as_ref().filter(|m| m.enabled) { Some(m) => photocraft_compose::filter_mask::mix(placed, &cur, m), None => cur, - } + }) } -/// One visible smart filter over `cur`, blended with its blend mode and opacity. -fn apply_smart_filter(f: &SmartFilter, cur: &Surface, canvas: Rect, extent: Rect) -> Surface { - // Unknown ids (e.g. Photoshop filters we don't implement) leave the pixels alone. - let Some(out) = crate::filters::apply_filter_to_surface(&f.command, &f.params, cur, canvas, None, extent) else { return cur.clone() }; - if f.blend == BlendMode::Normal && f.opacity >= 1.0 { out } else { blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0)) } +/// One visible smart filter over `cur`, blended with its blend mode and opacity. `Err` when the +/// requesting session's authority denies the filter's params: callers surface the error instead of +/// rendering (and caching) unfiltered pixels. +fn apply_smart_filter(f: &SmartFilter, cur: &Surface, canvas: Rect, extent: Rect) -> Result { + let out = match crate::filters::apply_filter_to_surface(&f.command, &f.params, cur, canvas, None, extent)? { + // Unknown ids (e.g. Photoshop filters we don't implement) leave the pixels alone. + Some(out) => out, + None => cur.clone(), + }; + if f.blend == BlendMode::Normal && f.opacity >= 1.0 { Ok(out) } else { Ok(blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0))) } } /// The source placed in document pixels, before any smart filter. @@ -415,7 +422,7 @@ fn render_in(doc: &Document, sm: &SmartObject, ctx: &JobCtx) -> Result Surface { let doc = &s.active().unwrap().doc; let sm = active_smart(s); let (name, bytes) = source_bytes(&doc.metadata, &sm.source).unwrap(); - apply_smart_filters(&place(&name, &bytes, doc.pixel_format(), &sm).unwrap(), &sm, doc.bounds()) + apply_smart_filters(&place(&name, &bytes, doc.pixel_format(), &sm).unwrap(), &sm, doc.bounds()).unwrap() } #[test]