Automation: errors name a path outside the read roots by its file name

Under --automation-read-root, an error about a file or folder the client
did not give now names it whole only when it is inside the read roots.
Outside them it names the file or folder by its file name, or as "a file
or folder outside the read roots" when it has none, and keeps the roots
and the reason. A path the client gives stays whole.

This covers a save without a path (the document's own file and the files
written beside it, also inside action.batch, and the refusal to write
over a file read with losses), file.revert, script.listInstalled,
plugin.reload without a path and plugin.list's folder report, and
links.relink with copy (the document's folder, its error when the
document's path has no folder, and the copies in the reply). Export for
Screens' refusal for an export preset's folder uses the same helper.
This commit is contained in:
dotCooCoo
2026-11-12 06:05:56 -08:00
parent 71b889605d
commit baa7ada7a1
14 changed files with 342 additions and 55 deletions
+50
View File
@@ -728,6 +728,56 @@ fn relink_each_with_copy_copies_nothing_when_a_file_cant_be_read() {
assert_eq!(link_path(&s, a), one);
}
/// With copy, for a client whose read roots don't hold the document's folder: the reply names the
/// copy by its file name, and a copy refused there keeps the write root and names the folder and
/// the file by their names.
#[test]
fn relink_with_copy_names_the_documents_folder_by_its_name_outside_the_read_roots() {
use crate::file_access::{AutomationRoots, confine};
let dir = Folder::new("relink-copy-roots");
let one = dir.file("doc/one.png");
write(&one, &png(60, 30, RED));
let mut s = session();
let a = place(&mut s, &one);
save(&mut s, &dir.file("doc/poster.vectorcraft"));
let x = dir.file("new/x.png");
write(&x, &png(60, 30, BLUE));
let (new, doc) = (Path::new(&x).parent().unwrap().to_path_buf(), Path::new(&one).parent().unwrap().to_path_buf());
let hidden = doc.to_string_lossy().into_owned();
let p = json!({"ids": [a.0], "path": x, "copy": true});
let writes_there = AutomationRoots::new(&[new.as_path()], &[doc.as_path()]).unwrap();
let r = confine(writes_there.as_ref(), || s.execute("links.relink", &p)).unwrap();
assert_eq!(r["copied"], json!([{"from": x, "to": "x.png"}]), "{r}");
assert!(!r.to_string().contains(&hidden), "{r}");
std::fs::remove_file(dir.file("doc/x.png")).unwrap();
let elsewhere = AutomationRoots::new(&[new.as_path()], &[new.as_path()]).unwrap();
let root = crate::cmd::findfiles::plain(std::fs::canonicalize(&new).unwrap()).display().to_string();
let e = confine(elsewhere.as_ref(), || s.execute("links.relink", &p)).unwrap_err().to_string();
assert_eq!(e, format!("doc: automation path rejected: outside the write root {root}: x.png"));
assert!(!Path::new(&dir.file("doc/x.png")).exists());
}
/// With copy, for a document whose path has no folder: outside the read roots the error does not
/// name the path, and without roots it names it whole.
#[test]
fn relink_with_copy_names_a_document_path_without_a_folder_as_the_request_may_see_it() {
use crate::file_access::{AutomationRoots, confine};
let dir = Folder::new("relink-copy-no-folder");
let mut s = session();
s.doc_mut().unwrap().path = Some("/".into());
let p = json!({"path": dir.file("x.png"), "copy": true});
let roots = AutomationRoots::new(&[dir.0.as_path()], &[dir.0.as_path()]).unwrap();
let e = confine(roots.as_ref(), || s.execute("links.relink", &p)).unwrap_err().to_string();
assert_eq!(e, "invalid parameters for `links.relink`: a file or folder outside the read roots has no folder");
let whole = s.execute("links.relink", &p).unwrap_err().to_string();
let doc = crate::cmd::fileio::absolute_path("/");
assert_eq!(whole, format!("invalid parameters for `links.relink`: {doc} has no folder"));
}
/// A scope or paths that doesn't fit is refused, and nothing is relinked.
#[test]
fn relink_refuses_scopes_and_paths_that_dont_fit() {
+31
View File
@@ -1433,6 +1433,37 @@ fn plugins_folder_set_again_after_clearing_installs_its_plugins() {
run(&mut s, "plugin.remove", json!({"id": id}));
}
/// The Additional Plug-ins Folder, seen by a client whose read roots don't hold it: `plugin.list`
/// names the folder and the files that failed by their names, and `plugin.reload` without a path is
/// refused naming the folder by its name and keeping the root. A folder the client gives stays whole.
#[test]
fn a_plugins_folder_outside_the_read_roots_is_named_by_its_name() {
use crate::file_access::{AutomationRoots, confine};
let _folder = PREFS_FOLDER.lock().unwrap_or_else(|e| e.into_inner());
let dir = crate::tests_links::Folder::new("plugin-folder-hidden");
let work = crate::tests_links::Folder::new("plugin-folder-work");
std::fs::write(dir.0.join("broken.wasm"), b"\0asm?").unwrap();
let mut s = session();
let shown = dir.0.display().to_string();
let name = dir.0.file_name().unwrap().to_string_lossy().into_owned();
run(&mut s, "prefs.set", json!({"key": "pluginsFolder", "value": shown}));
let roots = AutomationRoots::new(&[work.0.as_path()], &[]).unwrap();
let root = crate::cmd::findfiles::plain(std::fs::canonicalize(&work.0).unwrap()).display().to_string();
let list = confine(roots.as_ref(), || run(&mut s, "plugin.list", json!({})));
assert_eq!(list["folder"]["folder"], json!(name), "{list}");
assert_eq!(list["folder"]["failed"][0]["file"], json!("broken.wasm"), "{list}");
assert!(!list.to_string().contains(&shown), "{list}");
let e = confine(roots.as_ref(), || s.execute("plugin.reload", &json!({}))).unwrap_err().to_string();
assert_eq!(e, format!("automation path rejected: outside the read root {root}: {name}"));
let e = confine(roots.as_ref(), || s.execute("plugin.reload", &json!({"path": shown}))).unwrap_err().to_string();
assert_eq!(e, format!("automation path rejected: outside the read root {root}: {shown}"));
// Unconfined, the report names the folder whole.
assert_eq!(run(&mut s, "plugin.list", json!({}))["folder"]["folder"], json!(shown));
run(&mut s, "prefs.set", json!({"key": "pluginsFolder", "value": ""}));
}
/// #2541: a plug-in installed in a session with a plug-in store is installed again in the next
/// session that attaches the store, and Remove Plug-in takes it out of the store.
#[test]
+49
View File
@@ -84,6 +84,55 @@ fn a_confined_save_leaves_out_paths_outside_the_read_root() {
let _ = std::fs::remove_dir_all(d);
}
/// A save without a path, and Revert, refused because the document's own file is outside a confined
/// client's roots: the error keeps the root and the reason and names the file by its file name, also
/// for the files a save writes beside it. A path the client gives stays whole in the error.
#[test]
fn pathless_save_rejection_hides_document_folder() {
use crate::file_access::{AutomationRoots, confine};
let d = dir("roots-error");
let (inside, outside) = (d.join("in"), d.join("kept-out"));
for f in [&inside, &outside] {
std::fs::create_dir_all(f).unwrap();
}
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 100, "artboards": 2})).unwrap();
rect(&mut s);
let own = path(&outside, "doc.vectorcraft");
s.execute("document.save", &json!({"path": own})).unwrap();
rect(&mut s);
let roots = AutomationRoots::new(&[inside.as_path()], &[inside.as_path()]).unwrap();
let root = crate::cmd::findfiles::plain(std::fs::canonicalize(&inside).unwrap()).display().to_string();
let hidden = outside.to_string_lossy().into_owned();
let mut refused = |id: &str, p: Value| confine(roots.as_ref(), || s.execute(id, &p)).unwrap_err().to_string();
let e = refused("document.save", json!({}));
assert_eq!(e, format!("automation path rejected: outside the write root {root}: doc.vectorcraft"));
let e = refused("document.save", json!({"separateArtboards": true}));
assert!(e.starts_with(&format!("automation path rejected: outside the write root {root}: ")) && !e.contains(&hidden), "{e}");
let e = refused("file.revert", json!({}));
assert_eq!(e, format!("automation path rejected: outside the read root {root}: doc.vectorcraft"));
// Given by the client: whole.
let e = refused("document.save", json!({"path": own}));
assert_eq!(e, format!("automation path rejected: outside the write root {root}: {own}"));
// Without read roots, a write refusal still names the file by its file name.
let write_only = AutomationRoots::new(&[], &[inside.as_path()]).unwrap();
let e = confine(write_only.as_ref(), || s.execute("document.save", &json!({}))).unwrap_err().to_string();
assert_eq!(e, format!("automation path rejected: outside the write root {root}: doc.vectorcraft"));
assert!(s.doc().unwrap().is_dirty(), "nothing was saved");
// Read with losses from its own file: the refusal to write over it names the file by its file name.
let st = s.doc_mut().unwrap();
st.imported_from = Some(own.clone());
st.import_losses = vec!["hidden text".into()];
let e = confine(roots.as_ref(), || s.execute("document.save", &json!({}))).unwrap_err().to_string();
assert!(e.starts_with("invalid parameters for `document.save`: doc.vectorcraft is the file this document was read from"), "{e}");
assert!(!e.contains(&hidden), "{e}");
let e = confine(roots.as_ref(), || s.execute("document.save", &json!({"path": own}))).unwrap_err().to_string();
assert!(e.contains(&format!("{own} is the file this document was read from")), "{e}");
let _ = std::fs::remove_dir_all(d);
}
#[test]
fn an_svg_path_writes_svg_and_save_writes_it_back() {
let d = dir("svg");
+3 -1
View File
@@ -311,7 +311,9 @@ fn the_automation_read_root_keeps_an_agent_out_of_the_scripts_folder() {
s.attach_scripts_dir(root.clone());
let roots = crate::file_access::AutomationRoots::new(&[&work], &[]).unwrap().unwrap();
let e = crate::file_access::confine(Some(&roots), || s.execute("script.listInstalled", &json!({}))).unwrap_err();
assert!(e.to_string().contains("outside the read root"), "{e}");
// The Scripts folder is the app's: the error keeps the root and names the folder by its name.
let shown_root = crate::cmd::findfiles::plain(std::fs::canonicalize(&work).unwrap()).display().to_string();
assert_eq!(e.to_string(), format!("automation path rejected: outside the read root {shown_root}: Scripts"));
let e = crate::file_access::confine(Some(&roots), || s.installed_script(&script.to_string_lossy())).unwrap_err();
assert!(e.to_string().contains("outside the read root"), "{e}");
// A read root that holds the folder lets both through.