Scripts: script.grants.revoke removes only the grant named as given

script.grants.revoke matched a path both as given and with its links
followed, so revoking a grant whose path is now a link to another
granted path removed both grants. The spellings are now tried in order,
the path as given first, and only the first script and path pair that
some grant holds is removed.
This commit is contained in:
dotCooCoo committed 2026-11-25 15:13:55 -08:00
1 parent 0947c9a780
commit 6ad8c0476a
4 files changed
+57 -8

No files matched your search

+3 -3
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 given and as script.grants lists it, after its links are followed (a path outside the automation read roots is matched as given only); 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 given, as script.grants lists it, or after its links are followed when no grant holds it as given (a path outside the automation read roots is matched as given only); refused with the busy error while a script runs, as the run keeps the grants it started with → {removed: how many}",
always,
grants_revoke
),
@@ -384,8 +384,8 @@ fn grants(s: &mut Session, _: &Value) -> Result<Value> {
fn grants_revoke(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "script.grants.revoke";
// A path is matched as given, and also with its links followed, so a grant whose path a link
// has since taken the place of is still removed. A path the request may not read (outside the
// A path is matched as given, or with its links followed when no grant holds it as given, so a
// grant whose path a link has since taken the place of is still removed. 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.
let path_of = |key: &str| match p.get(key) {
None => Ok(None),
+18 -4
View File
@@ -1079,15 +1079,29 @@ pub(crate) fn stored_grants(data_dir: &Path) -> std::io::Result<Vec<serde_json::
}
/// Removes from the grants file beside `data_dir` the grants of the script `script` and of the path
/// `path` (each matched by any of its spellings), of both when both are given and of every grant
/// when neither is (`script.grants.revoke`). → how many were removed.
/// `path`, of both when both are given and of every grant when neither is (`script.grants.revoke`).
/// The spellings of each are tried in order, the script's first, and the first pair that some grant
/// holds is the one removed, so a grant kept under the spellings given first is removed without the
/// grants of later ones. → how many were removed.
pub(crate) fn revoke_grants(data_dir: &Path, script: Option<&[String]>, path: Option<&[String]>) -> std::io::Result<usize> {
let store = data_dir.with_file_name(GRANTS_FILE);
let _lock = lock_store(&store)?;
let grants = try_read_store(&store)?;
let before = grants.len();
let matches = |value: &serde_json::Value, names: Option<&[String]>| names.is_none_or(|names| names.iter().any(|n| value == n.as_str()));
let kept: Vec<serde_json::Value> = grants.into_iter().filter(|g| !(matches(&g["script"], script) && matches(&g["path"], path))).collect();
// `None` matches any value; `Some(name)` only that name.
fn spellings(names: Option<&[String]>) -> Vec<Option<&str>> {
match names {
None => vec![None],
Some(names) => names.iter().map(|n| Some(n.as_str())).collect(),
}
}
let matches = |g: &serde_json::Value, (script, path): (Option<&str>, Option<&str>)| {
script.is_none_or(|s| g["script"] == s) && path.is_none_or(|p| g["path"] == p)
};
let pairs = spellings(script).into_iter().flat_map(|s| spellings(path).into_iter().map(move |p| (s, p)));
let pair = pairs.into_iter().find(|&pair| grants.iter().any(|g| matches(g, pair)));
let Some(pair) = pair else { return Ok(0) };
let kept: Vec<serde_json::Value> = grants.into_iter().filter(|g| !matches(g, pair)).collect();
let removed = before - kept.len();
if removed > 0 {
write_store(&store, kept)?;
+35
View File
@@ -674,6 +674,41 @@ fn a_grant_whose_path_is_now_a_link_is_revoked_by_its_listed_path() {
let _ = std::fs::remove_dir_all(base);
}
/// A grant kept for a path that is now a link to another granted path is revoked alone: the grant of
/// the link's target stays.
#[cfg(unix)]
#[test]
fn revoking_a_grant_whose_path_now_links_to_another_grant_keeps_the_other() {
let base = folder("grants-relinked-both");
let real = std::fs::canonicalize(&base).unwrap();
let (kept, other) = (real.join("kept.txt"), real.join("other.txt"));
write(&other, b"other");
let (kept_path, other_path) = (kept.to_string_lossy().into_owned(), other.to_string_lossy().into_owned());
let grant = |path: &str| json!({"script": "/s.jsx", "sha256": "0", "code": [], "path": path, "write": false, "folder": false});
let stored = json!({"version": 1, "grants": [grant(&kept_path), grant(&other_path)]});
write(&base.join("settings/Script Grants.json"), stored.to_string().as_bytes());
std::os::unix::fs::symlink(&other, &kept).unwrap();
let mut s = with_data(&base);
let r = s.execute("script.grants.revoke", &json!({"script": "/s.jsx", "path": kept_path})).unwrap();
assert_eq!(r["removed"], 1, "{r}");
let left = s.execute("script.grants", &json!({})).unwrap();
let paths: Vec<Value> = left["grants"].as_array().into_iter().flatten().map(|g| g["path"].clone()).collect();
assert_eq!(paths, [json!(other_path)], "{left}");
// Another script's grant of the path as given does not keep this script's grant of the link's
// target from being removed.
let stored = json!({"version": 1, "grants": [
{"script": "/t.jsx", "sha256": "0", "code": [], "path": kept_path, "write": false, "folder": false},
grant(&other_path)
]});
write(&base.join("settings/Script Grants.json"), stored.to_string().as_bytes());
let r = s.execute("script.grants.revoke", &json!({"script": "/s.jsx", "path": kept_path})).unwrap();
assert_eq!(r["removed"], 1, "{r}");
let left = s.execute("script.grants", &json!({})).unwrap();
let scripts: Vec<Value> = left["grants"].as_array().into_iter().flatten().map(|g| g["script"].clone()).collect();
assert_eq!(scripts, [json!("/t.jsx")], "{left}");
let _ = std::fs::remove_dir_all(base);
}
/// Reading and changing the grants file wait for a lock another handle holds only for a bounded
/// number of tries: then a listing and a change fail, with the file unchanged. A lock
/// released while it waits is taken.
+1 -1
View File
@@ -3161,7 +3161,7 @@ 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}`. A path is
matched as given and after its links are followed; under automation read roots, a path outside them is matched as given only. While a script
matched as given, or after its links are followed when no grant holds it as given; under automation read roots, a path outside them is matched as given only. 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, a link to a missing file included, is left as it is: `script.grants` and `script.grants.revoke` fail, and a grant picked with Always is
not kept. A missing grants file lists no grants, and `script.grants` leaves out a grant in the file that is not well