mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Gate Pixel Replacement paths in data-set export
`image.applyDataSet` judges the Pixel Replacement image paths a data set names with the session's automation gate. `export_data_sets_with` (doc.exportDataSets / doc_export_data_sets) now does the same: the check is factored out of `apply_data_set` and runs for every planned data set before anything is written. Test in automation rpc tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6649b78526
commit
25def299d7
2 files changed
+49
-4
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user