Scripts: an open file that is renamed is read under its new path's rules

`rename` moved the write path of open File objects but left the path they
were read from at the old name. When new code then withdrew the kept grant
for the folder, a grant the run still held for the old path let the object
go on giving the file's bytes. The read path now moves with the write path.
This commit is contained in:
dotCooCoo
2026-11-14 12:42:21 -08:00
parent cefaeefca9
commit d435ae1a27
2 changed files with 38 additions and 4 deletions
+29
View File
@@ -1913,6 +1913,35 @@ fn script_files_open_under_a_withdrawn_grant_are_not_read() {
let _ = std::fs::remove_dir_all(base);
}
/// A file opened for reading and then renamed is read under the rules for its new path: when new code
/// withdraws the kept grant for its folder, a grant the run still holds for its old path does not let
/// the object give more of it.
#[test]
fn script_files_renamed_while_open_are_read_under_their_new_path() {
let base = folder("grantwithdrawnrenamed");
let dir = base.join("picked");
let file = dir.join("in.txt");
write(&file, b"secret");
let text = format!(
"var f = new File({:?});\n\
if (!f.open('r')) vc.log(['asked', !!Folder.selectDialog('p')]);\n\
else {{ var a = f.readch(); File.openDialog('p'); var n = new File(f.fsName).rename('r.txt'); eval('1');\n\
var r = f.read(), e = f.error; vc.log([a, n, r, e]); }}",
file.to_string_lossy()
);
let mut s = with_data(&base);
start_framed(&mut s, &base, &text);
assert!(pump_to_picker(&mut s).is_none());
assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![dir.to_string_lossy().into_owned()], always: true }));
assert_eq!(pump_to_picker(&mut s).unwrap().unwrap()["log"], json!([["asked", true]]));
start_framed(&mut s, &base, &text);
assert!(pump_to_picker(&mut s).is_none());
assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![file.to_string_lossy().into_owned()], always: false }));
let r = pump_to_picker(&mut s).unwrap().unwrap();
assert_eq!(r["log"], json!([["s", true, "", DENIED]]));
let _ = std::fs::remove_dir_all(base);
}
/// The folder that holds the settings is kept out of every grant: a pick there gives `null` with the
/// error, a grant written into the grants file for it is not taken, and the script cannot write the
/// grants file through its data folder.