diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index be813e3a74..216e27f677 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -569,7 +569,7 @@ impl Disk { /// Adds grants of `paths` (with `write`, folders when `folder`) for this script to the grants /// file, with the code the run has evaluated so far, written whole to a file beside it that then - /// takes its place. + /// takes its place. The grants the run kept before are given the same code. fn keep(&self, paths: &[std::path::PathBuf], write: bool, folder: bool) -> Result<(), &'static str> { let (Some((script, hash)), Some(store)) = (&self.script, self.store_path()) else { return Err(NO_ALWAYS) }; // A grants file that is there but cannot be read is left as it is. @@ -577,6 +577,16 @@ impl Disk { let mut grants = try_read_store(&store).map_err(|_| NOT_SAVED)?; let script = shown(script); let code: Vec<&String> = self.ran.iter().collect(); + // The grants this run kept before take the code evaluated since, as the run's end would + // have given them. + for g in &mut grants { + if g["script"] == *script + && g["sha256"] == *hash + && self.kept.iter().any(|(p, w, f)| g["path"] == *shown(p) && g["write"] == *w && g["folder"] == *f) + { + g["code"] = serde_json::json!(code); + } + } for p in paths { let path = shown(p); grants diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index ae782692f3..4463c19e97 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -1766,6 +1766,41 @@ fn script_file_grants_keep_the_code_evaluated_after_the_dialog() { let _ = std::fs::remove_dir_all(base); } +/// A grant kept with Always before a second one is kept in the same run also keeps the code the +/// run evaluated between the two, so the next run that evaluates it keeps both grants. +#[test] +fn script_file_grants_kept_earlier_in_a_run_keep_the_code_evaluated_before_a_later_grant() { + let base = folder("grantbetween"); + let first = base.join("picked/a.txt"); + let second = base.join("picked/b.txt"); + write(&first, b"a"); + write(&second, b"b"); + let text = format!( + "var a = new File({:?}); var ka = a.open('r');\n\ + if (!ka) File.openDialog('a');\n\ + var between = eval('new File(a.fsName).open(\"r\")');\n\ + var b = new File({:?}); var kb = b.open('r');\n\ + if (!kb) File.openDialog('b');\n\ + vc.log([ka, between, kb, new File(a.fsName).open('r')]);", + first.to_string_lossy(), + second.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![first.to_string_lossy().into_owned()], always: true })); + assert!(pump_to_picker(&mut s).is_none()); + assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![second.to_string_lossy().into_owned()], always: true })); + assert_eq!(pump_to_picker(&mut s).unwrap().unwrap()["log"], json!([[false, true, false, true]])); + start_framed(&mut s, &base, &text); + assert_eq!( + pump_to_picker(&mut s).unwrap().unwrap()["log"], + json!([[true, true, true, true]]), + "the first grant keeps the code evaluated before the second" + ); + let _ = std::fs::remove_dir_all(base); +} + /// Code another script left in a named engine withdraws a script's kept grants in that engine. #[test] fn script_file_grants_are_withdrawn_by_code_another_script_left_in_the_engine() {