PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Gate linked smart-object reads on re-render
James Chapmana2fe5b2458a1
diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rsindex 64f67f9d9..2a8ce8366 100644--- a/crates/automation/src/rpc.rs+++ b/crates/automation/src/rpc.rs@@ -904,6 +904,37 @@ mod tests {std::fs::remove_dir_all(base).unwrap();}+ #[test]+ fn allowed_filters_do_not_rerender_a_smart_object_from_its_linked_file() {+ use photocraft_engine::doc::{LayerContent, SmartSource};++ let base = std::env::temp_dir().join(format!("pc-linked-rerender-{}", std::process::id()));+ let root = base.join("workspace");+ std::fs::create_dir_all(&root).unwrap();+ let red = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, [255, 0, 0].repeat(64)).unwrap();+ let outside = base.join("red.png");+ std::fs::write(&outside, photocraft_codecs::encode(&red, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap();+ // A document whose white smart object names a file outside the roots as its source.+ let mut session = photocraft_engine::Session::new();+ session.execute("file.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap();+ session.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();+ let state = session.active_mut().unwrap();+ let LayerContent::Smart(smart) = &mut std::sync::Arc::make_mut(&mut state.doc).layers[0].content else { panic!("smart object") };+ smart.source = SmartSource::Linked { path: outside.to_string_lossy().into_owned() };+ std::fs::write(root.join("p.pcraft"), photocraft_engine::smart_cmds::encode_source(&state.doc).unwrap()).unwrap();++ let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap());+ h.handle("doc.open", json!({"path": "p.pcraft"})).unwrap();+ h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 0.5}})).unwrap();+ let doc = &h.session.active().unwrap().doc;+ let LayerContent::Smart(smart) = &doc.layers[0].content else { panic!("smart object") };+ assert_eq!(smart.smart_filters.len(), 1, "the filter is still recorded");+ assert_eq!(smart.cache.as_ref().unwrap().rgba(4, 4), [1.0, 1.0, 1.0, 1.0], "the linked file outside the roots is not read");+ // Release directory capabilities before cleanup on Windows.+ drop(h);+ std::fs::remove_dir_all(base).unwrap();+ }+#[test]fn trusted_local_render_keeps_full_size_behavior() {let mut h = Headless::trusted_local();diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rsindex fdd69e456..556ad5c55 100644--- a/crates/engine/src/smart_cmds.rs+++ b/crates/engine/src/smart_cmds.rs@@ -79,10 +79,16 @@ fn base_name(path: &str) -> String {/// A linked file's bytes; `None` when it is missing or its path is not followed/// ([`crate::doc_path::resolve`]).+///+/// The path is document data, not a request parameter, so every read rechecks it with the gate+/// of the session the work is done for: a filter, transform or refresh re-renders from here,+/// not only the contents commands that judge it up front. A refused path is handled like a+/// missing file: the layer keeps its last rendering.fn read_file(path: &str) -> Option<Vec<u8>> {if path.is_empty() {return None;}+ crate::jobs::authorize_render_command("layer.smartObjects.updateModifiedContent", &json!({"path": path})).ok()?;photocraft_format::read_file(&crate::doc_path::resolve(path).ok()?).ok()}