Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Gate Pixel Replacement paths in data-set export

James Chapman25def299d7d0
diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs
index c0369c917..14fb82bfe 100644
--- a/crates/automation/src/rpc.rs
+++ b/crates/automation/src/rpc.rs
@@ -836,6 +836,41 @@ mod tests {
std::fs::remove_dir_all(base).unwrap();
}
+ #[test]
+ fn doc_export_data_sets_judges_pixel_replacement_paths_like_apply() {
+ let base = std::env::temp_dir().join(format!("pc-doc-data-set-pixels-{}", std::process::id()));
+ let root = base.join("workspace");
+ std::fs::create_dir_all(&root).unwrap();
+ let red = photocraft_codecs::Image::from_u8(4, 4, photocraft_codecs::ChannelLayout::Rgb, [255, 0, 0].repeat(16)).unwrap();
+ let photo = base.join("photo.png");
+ std::fs::write(&photo, photocraft_codecs::encode(&red, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap();
+ let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap());
+ h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "card"})).unwrap();
+ let layer = h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {}})).unwrap()["layer"].clone();
+ h.handle(
+ "engine.execute",
+ json!({"command": "image.variables.define", "params": {"defs": [{"name": "photo", "layer": layer, "type": "pixelReplacement"}]}}),
+ )
+ .unwrap();
+ // Storing the path is allowed; reading it is what the gate judges.
+ h.handle(
+ "engine.execute",
+ json!({"command": "image.variables.dataSets", "params": {"dataSets": [
+ {"name": "swap", "values": [{"variable": "photo", "kind": "pixels", "value": photo.to_string_lossy()}]},
+ ]}}),
+ )
+ .unwrap();
+ let applied = h.handle("engine.execute", json!({"command": "image.applyDataSet", "params": {"name": "swap"}})).unwrap_err().to_string();
+ assert!(applied.contains("ambient filesystem paths"), "{applied}");
+ let sets = root.join("sets");
+ let exported = h.handle("doc.exportDataSets", json!({"dir": sets.to_string_lossy(), "format": "png"})).unwrap_err().to_string();
+ assert!(exported.contains("ambient filesystem paths"), "{exported}");
+ assert_eq!(std::fs::read_dir(&sets).map_or(0, Iterator::count), 0, "nothing of the refused batch is written");
+ // 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/variables_cmds.rs b/crates/engine/src/variables_cmds.rs
index 0560a9d59..10eb9b613 100644
--- a/crates/engine/src/variables_cmds.rs
+++ b/crates/engine/src/variables_cmds.rs
@@ -243,10 +243,9 @@ fn replace_pixels(doc: &mut Document, layer: LayerId, path: &str, method: PixelM
Ok(())
}
-fn apply_data_set(s: &mut Session, p: &Value) -> Result<Value> {
- let (set, idx) = resolve_set(s, p, "image.applyDataSet")?;
- let vars = s.active().ok_or(EngineError::NoDocument)?.doc.variables.clone();
- // Pixel Replacement reads image files, so an untrusted session's gate judges each path.
+/// Pixel Replacement reads image files, so an untrusted session's gate judges each path a data
+/// set would read: whether it is applied or exported, the path is document data.
+fn authorize_pixel_paths(s: &Session, set: &DataSet) -> Result<()> {
if let Some(auth) = s.authorize {
for dv in &set.values {
if let VarValue::Pixels(path) = &dv.value {
@@ -254,6 +253,13 @@ fn apply_data_set(s: &mut Session, p: &Value) -> Result<Value> {
}
}
}
+ Ok(())
+}
+
+fn apply_data_set(s: &mut Session, p: &Value) -> Result<Value> {
+ let (set, idx) = resolve_set(s, p, "image.applyDataSet")?;
+ let vars = s.active().ok_or(EngineError::NoDocument)?.doc.variables.clone();
+ authorize_pixel_paths(s, &set)?;
s.edit(&format!("Apply Data Set \"{}\"", set.name), |doc, _| apply_to_doc(doc, &vars, &set))?;
set_vars(s, |v| v.active = Some(idx))?;
Ok(json!({"applied": set.name, "index": idx}))
@@ -409,6 +415,10 @@ pub fn export_data_sets_with(s: &Session, p: &Value, mut write: impl FnMut(&str,
if sets.is_empty() {
return Err(bad("file.export.dataSetsAsFiles", "no data sets to export"));
}
+ // Judge every image path before anything is written, as Apply Data Set does.
+ for set in &sets {
+ authorize_pixel_paths(s, set)?;
+ }
// Filename template: `{name}` (sanitised data-set name), `{index}` (1-based). Default `{name}`.
let template = p.get("naming").and_then(Value::as_str).unwrap_or("{name}");
let doc_stem = original.name.rsplit_once('.').map_or(original.name.as_str(), |(a, _)| a).to_string();