diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 216e27f677..19c1a37409 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -1844,11 +1844,16 @@ fn rename(host: &Host, a: &[Data]) -> Result { if std::fs::rename(&from, &to).is_err() { return Ok(error(WRITE_FAILED)); } - // Open objects of the file, or of files in the folder, write to the new path. - for file in disk.open.values_mut() { - if let Some(rest) = file.target.as_deref().and_then(|t| t.strip_prefix(&from).ok()) { - file.target = Some(if rest.as_os_str().is_empty() { to.clone() } else { to.join(rest) }); + // Open objects of the file, or of files in the folder, write to the new path, and the rules for + // reading apply to them there. + let moved = |p: &mut Option| { + if let Some(rest) = p.as_deref().and_then(|t| t.strip_prefix(&from).ok()) { + *p = Some(if rest.as_os_str().is_empty() { to.clone() } else { to.join(rest) }); } + }; + for file in disk.open.values_mut() { + moved(&mut file.target); + moved(&mut file.read_from); } // The old path no longer holds the file, so the report names it as well as the new one. disk.wrote(&from); diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 4463c19e97..1943a11bab 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -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.