Scripts: script.grants.revoke does not look up a path outside the automation read roots

This commit is contained in:
dotCooCoo
2026-11-13 10:55:29 -08:00
parent b153a3d7a6
commit 3ef3ed568a
3 changed files with 28 additions and 7 deletions
+9 -4
View File
@@ -104,7 +104,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Revoke Script File Permissions",
[],
None,
"{script?: a script file's path, path?: an allowed file or folder's path, all?: true (exactly one of all and the other two)} remove the kept file access of that script, of that path, of both together, or every one; a path is matched as script.grants lists it, after its links are followed; refused with the busy error while a script runs, as the run keeps the grants it started with → {removed: how many}",
"{script?: a script file's path, path?: an allowed file or folder's path, all?: true (exactly one of all and the other two)} remove the kept file access of that script, of that path, of both together, or every one; a path is matched as script.grants lists it, after its links are followed (a path outside the automation read roots is matched as given); refused with the busy error while a script runs, as the run keeps the grants it started with → {removed: how many}",
always,
grants_revoke
),
@@ -358,9 +358,14 @@ fn grants_revoke(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "script.grants.revoke";
let path_of = |key: &str| match p.get(key) {
None => Ok(None),
Some(Value::String(t)) if !t.is_empty() => {
Ok(Some(std::fs::canonicalize(t).map_or_else(|_| t.clone(), |real| crate::cmd::findfiles::plain(real).to_string_lossy().into_owned())))
}
// A path the request may not read (outside the automation read roots, a network path among
// them) is compared as written, without looking it up.
Some(Value::String(t)) if !t.is_empty() => Ok(Some(
crate::file_access::check_read(t)
.ok()
.and_then(|()| std::fs::canonicalize(t).ok())
.map_or_else(|| t.clone(), |real| crate::cmd::findfiles::plain(real).to_string_lossy().into_owned()),
)),
Some(_) => Err(bad(C, format!("`{key}` must be a path"))),
};
let (script_path, path) = (path_of("script")?, path_of("path")?);
+17 -2
View File
@@ -1492,8 +1492,23 @@ fn script_file_dialog_grants_are_kept_for_the_same_script_text() {
assert!(s.script_pick(crate::script::files::PickAnswer::Deny));
assert!(pump_to_picker(&mut s).is_some(), "the run ends");
let removed = s.execute("script.grants.revoke", &json!({"path": out.to_string_lossy()})).unwrap();
assert_eq!(removed["removed"], 1);
// Under automation read roots, a path outside them is not looked up: a link there to the
// granted file does not name it.
#[cfg(unix)]
{
let link = base.join("link.txt");
std::os::unix::fs::symlink(&out, &link).unwrap();
let roots = AutomationRoots::new(&[&base.join("main")], &[]).unwrap();
let r = confine(roots.as_ref(), || s.execute("script.grants.revoke", &json!({"path": link.to_string_lossy()}))).unwrap();
assert_eq!(r["removed"], 0, "{r}");
let r = s.execute("script.grants.revoke", &json!({"path": link.to_string_lossy()})).unwrap();
assert_eq!(r["removed"], 1, "without roots the link is followed: {r}");
}
#[cfg(not(unix))]
{
let removed = s.execute("script.grants.revoke", &json!({"path": out.to_string_lossy()})).unwrap();
assert_eq!(removed["removed"], 1);
}
assert_eq!(s.execute("script.grants", &json!({})).unwrap()["grants"], json!([]));
assert!(s.execute("script.grants.revoke", &json!({})).is_err());
assert!(!base.join("settings/Script Grants.json.part").exists());
+2 -1
View File
@@ -2348,7 +2348,8 @@ grant, which covers everything in the folder (a file's grant covers its own path
takes its place), and `file` the grants
file's path, or `null` without a settings folder. Under automation read roots, a `script`, `path` or `file` outside them
is given as its file name alone. `script.grants.revoke {script?, path?, all?}` removes the grants of
a script file, of a path, of both together, or with `all: true` every grant, and returns `{removed}`. While a script
a script file, of a path, of both together, or with `all: true` every grant, and returns `{removed}`. A path is
matched after its links are followed; under automation read roots, a path outside them is matched as given. While a script
runs it is refused with the busy error, as the run keeps the grants it started with. A grants file that is there but
cannot be read is left as it is: `script.grants.revoke` fails, and a grant picked with Always is not kept. A script
cannot run either command.