mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 18:32:23 +00:00
Scripts: a document opened from a linked file stays read-only when its path changes
The gate now records each path a script's command reads only because it led to a file the open documents link to. A document whose file is such a path is taken out of the open documents' files for the rest of the run, without looking at where the path leads now or what the script may read now. Before, the gate decided that again for each command from the path's current target, so a path pointed at another file by a link, or a read grant given later, let the run read and write through the document.
This commit is contained in:
@@ -286,19 +286,52 @@ pub(crate) struct ScriptGate {
|
||||
pub(crate) write: ScriptScope,
|
||||
/// The paths of the open documents' files, as the documents hold them.
|
||||
pub(crate) documents: Vec<String>,
|
||||
/// The real paths of the files the documents open when the run began link to, as they were
|
||||
/// when the run's first command was run ([`ScriptGate::real_files`]): readable, not writable.
|
||||
/// A path that leads to another file now is not one of them.
|
||||
pub(crate) linked: Arc<[PathBuf]>,
|
||||
/// The files the documents open when the run began link to: readable, not writable.
|
||||
pub(crate) linked: LinkedFiles,
|
||||
/// Another gate a path must pass as well ([`ScopeHandle::within_current`]).
|
||||
pub(crate) also: Option<Arc<ScriptGate>>,
|
||||
}
|
||||
|
||||
/// The files the commands a script runs may only read because the documents open when the run
|
||||
/// began link to them ([`ScriptGate::linked`]), and the paths a command read through them alone.
|
||||
/// Clones share the record of those paths.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct LinkedFiles {
|
||||
/// The real paths of the files, as they were when the run's first command was run, sorted. A
|
||||
/// path that leads to another file now is not one of them.
|
||||
files: Arc<[PathBuf]>,
|
||||
/// The paths, absolute as a document holds its file, that a command read only because they led
|
||||
/// to one of `files` when it read them. A document opened from one of them stays read-only for
|
||||
/// the rest of the run, wherever the path leads later.
|
||||
read: Arc<std::sync::Mutex<Vec<String>>>,
|
||||
}
|
||||
|
||||
impl LinkedFiles {
|
||||
/// Is `real`, a real path, one of the files?
|
||||
fn has(&self, real: &Path) -> bool {
|
||||
self.files.binary_search_by(|f| f.as_path().cmp(real)).is_ok()
|
||||
}
|
||||
|
||||
/// The paths read through the files alone so far.
|
||||
fn read_paths(&self) -> Vec<String> {
|
||||
self.read.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone()
|
||||
}
|
||||
|
||||
/// Notes that `path` was read through the files alone.
|
||||
fn note_read(&self, path: &str) {
|
||||
let path = crate::cmd::fileio::absolute_path(path);
|
||||
let mut read = self.read.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if !read.contains(&path) {
|
||||
read.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ScriptGate {
|
||||
/// The real paths of the files at `paths` that are files now, each once, for
|
||||
/// [`ScriptGate::linked`]. A path refused by its name alone, and one that cannot be resolved,
|
||||
/// is left out.
|
||||
pub(crate) fn real_files(paths: impl IntoIterator<Item = String>) -> Arc<[PathBuf]> {
|
||||
pub(crate) fn real_files(paths: impl IntoIterator<Item = String>) -> LinkedFiles {
|
||||
let mut real: Vec<PathBuf> = paths
|
||||
.into_iter()
|
||||
.filter(|p| !ScriptScope::refused_by_name(Path::new(p)))
|
||||
@@ -307,18 +340,20 @@ impl ScriptGate {
|
||||
.collect();
|
||||
real.sort();
|
||||
real.dedup();
|
||||
real.into()
|
||||
LinkedFiles { files: real.into(), read: Arc::default() }
|
||||
}
|
||||
|
||||
/// This gate without the documents whose files only [`ScriptGate::linked`] lets it read, unless
|
||||
/// they are among `before`, the files of the documents open when the run began: a document the
|
||||
/// run opened from such a file is read, never written.
|
||||
/// run opened from such a file is read, never written. A document counts as one when a command
|
||||
/// read its file through the linked files alone, whatever the path leads to or the script may
|
||||
/// read now, and when its file is one of them now that nothing else lets the gate read.
|
||||
pub(crate) fn keeping_linked_read_only(mut self, before: &[String]) -> Self {
|
||||
let plain = ScriptGate { documents: vec![], linked: Arc::new([]), also: None, ..self.clone() };
|
||||
let linked = self.linked.clone();
|
||||
let plain = ScriptGate { documents: vec![], linked: LinkedFiles::default(), also: None, ..self.clone() };
|
||||
let (linked, read) = (self.linked.clone(), self.linked.read_paths());
|
||||
let only_linked = |d: &String| {
|
||||
let real = if ScriptScope::refused_by_name(Path::new(d)) { None } else { resolve(d).ok() };
|
||||
real.is_some_and(|real| linked.binary_search(&real).is_ok()) && plain.check(d, Access::Read).is_err()
|
||||
read.contains(d) || (real.is_some_and(|real| linked.has(&real)) && plain.check(d, Access::Read).is_err())
|
||||
};
|
||||
self.documents.retain(|d| before.contains(d) || !only_linked(d));
|
||||
self
|
||||
@@ -342,10 +377,17 @@ impl ScriptGate {
|
||||
let real = if ScriptScope::refused_by_name(Path::new(path)) { None } else { resolve(path).ok() };
|
||||
let allowed = real.is_some_and(|real| {
|
||||
let scoped = match access {
|
||||
Access::Read => self.read.allows(&real) || self.write.allows(&real) || self.linked.binary_search(&real).is_ok(),
|
||||
Access::Read => self.read.allows(&real) || self.write.allows(&real),
|
||||
Access::Write => self.write.allows(&real),
|
||||
};
|
||||
scoped || self.documents.iter().any(|d| resolve(d).is_ok_and(|d| d == real))
|
||||
if scoped || self.documents.iter().any(|d| resolve(d).is_ok_and(|d| d == real)) {
|
||||
return true;
|
||||
}
|
||||
let linked = matches!(access, Access::Read) && self.linked.has(&real);
|
||||
if linked {
|
||||
self.linked.note_read(path);
|
||||
}
|
||||
linked
|
||||
});
|
||||
if allowed { Ok(()) } else { Err(format!("Script file access denied: {path}")) }
|
||||
}
|
||||
@@ -957,7 +999,7 @@ mod tests {
|
||||
read: ScriptScope::of_folder(&base.join(dir)),
|
||||
write: ScriptScope::default(),
|
||||
documents: vec![],
|
||||
linked: Arc::new([]),
|
||||
linked: LinkedFiles::default(),
|
||||
also: None,
|
||||
};
|
||||
let (inside, other) = (s(base.join("in/a.svg")), s(base.join("outside/secret.svg")));
|
||||
@@ -986,7 +1028,7 @@ mod tests {
|
||||
read: ScriptScope::of_folder(dir),
|
||||
write: ScriptScope::default(),
|
||||
documents: vec![],
|
||||
linked: Arc::new([]),
|
||||
linked: LinkedFiles::default(),
|
||||
also: None,
|
||||
};
|
||||
let (inside, other) = (s(base.join("in/a.svg")), s(base.join("outside/secret.svg")));
|
||||
@@ -1013,7 +1055,7 @@ mod tests {
|
||||
std::fs::write(&other, "<svg/>").unwrap();
|
||||
let later = base.join("outside/later.svg");
|
||||
let linked = ScriptGate::real_files([s(secret.clone()), s(later.clone()), s(base.join("outside"))]);
|
||||
assert_eq!(linked.len(), 1, "a missing file and a folder are left out: {linked:?}");
|
||||
assert_eq!(linked.files.len(), 1, "a missing file and a folder are left out: {linked:?}");
|
||||
let gate =
|
||||
ScriptGate { read: ScriptScope::of_folder(&base.join("in")), write: ScriptScope::default(), documents: vec![], linked, also: None };
|
||||
std::fs::write(&later, "<svg/>").unwrap();
|
||||
@@ -1053,6 +1095,73 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A document opened from a file a command read through the linked files alone stays
|
||||
/// read-only after the script is allowed to read that file's folder, and after the file is
|
||||
/// replaced by another one.
|
||||
#[test]
|
||||
fn a_document_read_through_a_linked_file_stays_read_only_after_a_read_grant() {
|
||||
let (base, _) = folders("linked-granted");
|
||||
let secret = s(base.join("outside/secret.svg"));
|
||||
let linked = ScriptGate::real_files([secret.clone()]);
|
||||
let gate = |read: &str, documents: Vec<String>| ScriptGate {
|
||||
read: ScriptScope::of_folder(&base.join(read)),
|
||||
write: ScriptScope::default(),
|
||||
documents,
|
||||
linked: linked.clone(),
|
||||
also: None,
|
||||
};
|
||||
assert!(gate("in", vec![]).check(&secret, Access::Read).is_ok(), "the command that opens it reads it through the linked file");
|
||||
let granted = gate("outside", vec![secret.clone()]).keeping_linked_read_only(&[]);
|
||||
assert!(granted.check(&secret, Access::Read).is_ok());
|
||||
assert!(granted.check(&secret, Access::Write).is_err(), "a read grant made later does not make it writable");
|
||||
std::fs::remove_file(&secret).unwrap();
|
||||
std::fs::write(&secret, "<svg><rect/></svg>").unwrap();
|
||||
let replaced = gate("in", vec![secret.clone()]).keeping_linked_read_only(&[]);
|
||||
assert!(replaced.check(&secret, Access::Write).is_err(), "replaced by another file");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A document opened from a file a command read through the linked files alone gives no
|
||||
/// access to the file its path leads to after the path is pointed elsewhere by a link.
|
||||
#[test]
|
||||
fn a_document_read_through_a_linked_file_gives_nothing_once_its_path_leads_elsewhere() {
|
||||
let (base, _) = folders("linked-redirected");
|
||||
#[cfg(unix)]
|
||||
let made = |target: &Path, link: &Path| std::os::unix::fs::symlink(target, link).is_ok();
|
||||
#[cfg(windows)]
|
||||
let made = |target: &Path, link: &Path| std::os::windows::fs::symlink_dir(target, link).is_ok();
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
let made = |_: &Path, _: &Path| false;
|
||||
let (first, second, via) = (base.join("outside/first"), base.join("outside/second"), base.join("outside/via"));
|
||||
for dir in [&first, &second] {
|
||||
std::fs::create_dir_all(dir).unwrap();
|
||||
std::fs::write(dir.join("a.svg"), "<svg/>").unwrap();
|
||||
}
|
||||
// Windows needs Developer Mode or elevation to make links: nothing to test without them.
|
||||
if !made(&first, &via) {
|
||||
eprintln!("skipped: symbolic links can't be made here");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
return;
|
||||
}
|
||||
let (doc, elsewhere) = (s(via.join("a.svg")), s(second.join("a.svg")));
|
||||
let linked = ScriptGate::real_files([doc.clone()]);
|
||||
let gate = |documents: Vec<String>| ScriptGate {
|
||||
read: ScriptScope::default(),
|
||||
write: ScriptScope::default(),
|
||||
documents,
|
||||
linked: linked.clone(),
|
||||
also: None,
|
||||
};
|
||||
assert!(gate(vec![]).check(&doc, Access::Read).is_ok(), "the command that opens it reads it through the linked file");
|
||||
std::fs::remove_dir(&via).or_else(|_| std::fs::remove_file(&via)).unwrap();
|
||||
assert!(made(&second, &via));
|
||||
let opened = gate(vec![doc.clone()]).keeping_linked_read_only(&[]);
|
||||
for (path, access) in [(&doc, Access::Read), (&doc, Access::Write), (&elsewhere, Access::Read), (&elsewhere, Access::Write)] {
|
||||
assert!(opened.check(path, access).is_err(), "{path} {access:?}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A link to a linked file read through the gate counts only while it leads to that file.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
|
||||
@@ -73,9 +73,8 @@
|
||||
//! - In a wasm build every lookup fails with [`NO_WEB`].
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use vectorcraft_js::{Data, HostError, HostItem, Realm, RunError};
|
||||
@@ -459,7 +458,7 @@ impl Disk {
|
||||
/// The gate for a command the script runs: the places it reads and writes now, the files of the
|
||||
/// open documents, `documents`, and the files it may only read because the documents open when
|
||||
/// the run began link to them, `linked`.
|
||||
pub(crate) fn gate(&self, documents: Vec<String>, linked: Arc<[PathBuf]>) -> crate::file_access::ScriptGate {
|
||||
pub(crate) fn gate(&self, documents: Vec<String>, linked: crate::file_access::LinkedFiles) -> crate::file_access::ScriptGate {
|
||||
crate::file_access::ScriptGate { read: self.scope.clone(), write: self.writes.clone(), documents, linked, also: None }
|
||||
}
|
||||
|
||||
|
||||
@@ -806,9 +806,9 @@ struct Host {
|
||||
/// The files of the documents open when the run began
|
||||
/// ([`crate::file_access::ScriptGate::keeping_linked_read_only`]).
|
||||
opened_before: Vec<String>,
|
||||
/// The real paths of the files the documents open when the run began link to
|
||||
/// ([`crate::file_access::ScriptGate::linked`]).
|
||||
linked: std::cell::OnceCell<Arc<[std::path::PathBuf]>>,
|
||||
/// The files the documents open when the run began link to, and the paths the run's commands
|
||||
/// read through them alone ([`crate::file_access::ScriptGate::linked`]).
|
||||
linked: std::cell::OnceCell<crate::file_access::LinkedFiles>,
|
||||
}
|
||||
|
||||
/// A document open when a run began, with its own file and the file it was read from.
|
||||
@@ -817,8 +817,9 @@ type RunDoc = (Arc<crate::Document>, Option<String>, Option<String>);
|
||||
impl Host {
|
||||
/// The files the commands the run runs may read because the documents open when the run began
|
||||
/// link to them, found once, from those documents as they were then: a link the run adds or
|
||||
/// changes, and a document it opens, give it no file to read.
|
||||
fn linked(&self) -> Arc<[std::path::PathBuf]> {
|
||||
/// changes, and a document it opens, give it no file to read. Every command's gate shares the
|
||||
/// record of the paths read through them.
|
||||
fn linked(&self) -> crate::file_access::LinkedFiles {
|
||||
self.linked
|
||||
.get_or_init(|| {
|
||||
let docs = std::mem::take(&mut *self.run_docs.borrow_mut());
|
||||
|
||||
+3
-1
@@ -3023,7 +3023,9 @@ VectorCraft writes files by these rules of its own:
|
||||
Such a command also reads, and does not write, the files that the documents open when the run began link to
|
||||
(linked images, placed files, the plate files of a DCS image and the images their data sets name), so an export,
|
||||
save, print or package of them includes the linked art. A document the script opens from such a file is not
|
||||
saved over it. The files are taken as those links led at the run's
|
||||
saved over it for the rest of the run, also after the script is allowed to read that file's folder, and when its
|
||||
path leads to another file later, the document does not let the script read or write that file. The files are
|
||||
taken as those links led at the run's
|
||||
first `vc.run`, and a path that leads to another file since is refused. A link the script adds or points
|
||||
elsewhere, and a document it opens, does not let it read any file it could not read before. The automation
|
||||
read roots still apply.
|
||||
|
||||
Reference in New Issue
Block a user