From 9811cafc25bc27f07de793413ea0872556bb506d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 15:39:59 -0800 Subject: [PATCH] Scripts: copy reads its source under the rules for reading copy read the source's real path as it was when looked up, so a file replaced by a link before the read could copy a file from outside the script's places, and a link given as the source that was pointed elsewhere copied the old target. copy now reads the path as given through the same checks open uses and refuses the copy with Script file access denied when the path no longer leads to the file it was looked up as. --- crates/engine/src/script/files.rs | 29 ++++++++++--- crates/engine/src/tests/script_io.rs | 63 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index ba2f5ec4dc..6340912cb8 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -1663,12 +1663,19 @@ fn copy(host: &Host, a: &[Data]) -> Result { if let Err(m) = ids.iter().try_for_each(|id| disk.flush(*id)) { return Ok(error(m)); } - // The bytes are read once, no more than the limit, and those bytes are what is charged and - // written: a source that grows after it is read does not copy more. - let copied = match read_head(&source, MAX_FILE_BYTES) { - Ok(bytes) if bytes.len() > MAX_FILE_BYTES => return Ok(error(TOO_LARGE)), - Ok(bytes) => bytes, - Err(_) => return Ok(error(READ_FAILED)), + #[cfg(test)] + if let Some(hook) = BEFORE_COPY_READ.with(|h| h.borrow_mut().take()) { + hook(Path::new(&from), &source); + } + // The bytes are read once, no more than the limit, under the rules for reading, and those bytes + // are what is charged and written: a source that grows after it is read does not copy more, + // and a path that leads somewhere else than when it was looked up is refused. + let copied = match disk.scope.read(Path::new(&from), MAX_FILE_BYTES) { + Ok((at, bytes)) if at == source => bytes, + Ok(_) | Err(ScriptRead::Refused) => return Ok(error(ACCESS_DENIED)), + Err(ScriptRead::TooLarge) => return Ok(error(TOO_LARGE)), + Err(ScriptRead::Missing) => return Ok(error(NOT_FOUND)), + Err(ScriptRead::Failed(_)) => return Ok(error(READ_FAILED)), }; // The target's open objects each take a copy of the bytes, which count as written too, as does // the copy itself when it is written. @@ -2195,6 +2202,16 @@ pub(super) fn install(realm: &mut Realm, link: &super::Link) -> Result<(), RunEr realm.define_file_classes(items) } +/// Tests: what the next `copy` does with its source's path, as given and as its real path, after +/// looking it up and before reading it. +#[cfg(test)] +pub(crate) type CopyHook = Box; + +#[cfg(test)] +thread_local! { + pub(crate) static BEFORE_COPY_READ: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 75abff6956..fdd75c8c57 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -513,6 +513,69 @@ fn script_files_write_in_the_temporary_and_data_folders() { let _ = std::fs::remove_dir_all(base); } +/// `copy` reads its source under the rules for reading: a source that a link to a file outside the +/// script's places replaced after it was looked up is refused, and nothing is copied. +#[cfg(unix)] +#[test] +fn a_copy_whose_source_becomes_a_link_outside_copies_nothing() { + let base = folder("copy-link"); + let mut s = with_data(&base); + let secret = base.join("secret.txt"); + write(&secret, b"secret"); + crate::script::files::BEFORE_COPY_READ.with(|h| { + *h.borrow_mut() = Some(Box::new(move |_: &Path, source: &Path| { + std::fs::remove_file(source).unwrap(); + std::os::unix::fs::symlink(&secret, source).unwrap(); + })); + }); + let r = run_in( + &mut s, + &base, + "var d = Folder.userData.fsName, a = new File(d + '/a.txt'); a.open('w'); a.write('mine'); a.close();\n\ + vc.log([a.copy(d + '/b.txt'), a.error, new File(d + '/b.txt').exists]);", + ) + .unwrap(); + crate::script::files::BEFORE_COPY_READ.with(|h| h.borrow_mut().take()); + assert_eq!(r["log"], json!([[false, "Script file access denied", false]])); + let _ = std::fs::remove_dir_all(base); +} + +/// `copy` of a link whose target changes after it was looked up is refused, also when both files +/// are in the script's places. +#[cfg(unix)] +#[test] +fn a_copy_of_a_link_that_leads_elsewhere_after_it_was_looked_up_copies_nothing() { + let base = folder("copy-relink"); + let mut s = with_data(&base); + let first = run_in( + &mut s, + &base, + "var d = Folder.userData.fsName;\n\ + ['a', 'c'].forEach(function (n) { var f = new File(d + '/' + n + '.txt'); f.open('w'); f.write(n); f.close(); });\n\ + vc.log(d);", + ) + .unwrap(); + let data = PathBuf::from(first["log"][0].as_str().unwrap()); + std::os::unix::fs::symlink(data.join("a.txt"), data.join("link.txt")).unwrap(); + let other = data.join("c.txt"); + crate::script::files::BEFORE_COPY_READ.with(|h| { + *h.borrow_mut() = Some(Box::new(move |given: &Path, _: &Path| { + std::fs::remove_file(given).unwrap(); + std::os::unix::fs::symlink(&other, given).unwrap(); + })); + }); + let r = run_in( + &mut s, + &base, + "var d = Folder.userData.fsName, l = new File(d + '/link.txt');\n\ + vc.log([l.copy(d + '/b.txt'), l.error, new File(d + '/b.txt').exists]);", + ) + .unwrap(); + crate::script::files::BEFORE_COPY_READ.with(|h| h.borrow_mut().take()); + assert_eq!(r["log"], json!([[false, "Script file access denied", false]])); + let _ = std::fs::remove_dir_all(base); +} + /// `rename` of a file an earlier run wrote names both its old and its new path in `written`. #[test] fn a_renamed_file_names_its_old_and_new_paths_in_written() {