mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 07:49:26 +00:00
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.
This commit is contained in:
@@ -1663,12 +1663,19 @@ fn copy(host: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
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<dyn FnOnce(&Path, &Path)>;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(crate) static BEFORE_COPY_READ: std::cell::RefCell<Option<CopyHook>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user