diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 5bb56528..02980c38 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -88,6 +88,9 @@ impl Args { while let Some(a) = it.next() { if a == "--sample" { positional.push(a.clone()); + } else if let Some((k, v)) = a.strip_prefix("--").and_then(|f| f.split_once('=')) { + // `--flag=value`, as MCP client configurations often write the roots. + flags.push((k.to_string(), v.to_string())); } else if let Some(k) = a.strip_prefix("--") { let v = it.next().ok_or_else(|| format!("flag --{k} needs a value"))?; flags.push((k.to_string(), v.clone())); @@ -846,6 +849,9 @@ pub(crate) mod tests { assert_eq!(read.len(), 2); assert!(write.is_absolute()); assert!(automation_folders(&Args::parse(&[]).unwrap()).unwrap().is_none(), "serve without roots works as before"); + let eq = Args::parse(&[format!("--automation-read-root={work}"), format!("--automation-write-root={out}")]).unwrap(); + assert_eq!(automation_folders(&eq).unwrap().map(|(r, _)| r.len()), Some(1), "--flag=value"); + assert_eq!(exit_code(&run(&["mcp", "--automation-read-root=", &format!("--automation-write-root={out}")]).unwrap_err()), USAGE_EXIT); } #[test] diff --git a/crates/engine/src/access.rs b/crates/engine/src/access.rs index d7476bee..b0f0f5c9 100644 --- a/crates/engine/src/access.rs +++ b/crates/engine/src/access.rs @@ -123,8 +123,33 @@ impl Roots { } } + /// Whether `path` (absolute) may be looked at on disk at all. On Windows, resolving a path + /// on another computer (`\\server\share\…`) contacts that computer and offers it the + /// person's sign-in, and a device path (`\\.\…`) is no file: both are refused by their + /// spelling, before the file system is asked. A share is allowed when a root is on it. + fn reachable(&self, abs: &Path) -> bool { + use std::path::Prefix; + let share = |p: &Path| match p.components().next() { + Some(Component::Prefix(x)) => match x.kind() { + Prefix::UNC(server, share) | Prefix::VerbatimUNC(server, share) => Some(Some((server.to_ascii_lowercase(), share.to_ascii_lowercase()))), + Prefix::Disk(_) | Prefix::VerbatimDisk(_) => None, + Prefix::Verbatim(_) | Prefix::DeviceNS(_) => Some(None), + }, + _ => None, + }; + match share(abs) { + None => true, + Some(None) => false, + Some(there) => self.read.iter().chain(&self.write).any(|r| share(r) == Some(there.clone())), + } + } + /// `path` resolved, when this policy allows `access` to it; the error names the roots. pub fn permit(&self, path: &Path, access: Access) -> Result { + let abs = std::path::absolute(path).map_err(|e| format!("{}: {e}", path.display()))?; + if !self.reachable(&abs) { + return Err(self.refusal(&abs, access)); + } let resolved = resolve(path).map_err(|e| format!("{}: {e}", path.display()))?; if self.inside(&resolved, access) { Ok(resolved) } else { Err(self.refusal(&resolved, access)) } } @@ -510,6 +535,12 @@ mod tests { assert!(r.permit(&slashes, Access::Read).is_ok(), "forward slashes"); // A device name inside the root is the device, not a file there. assert!(r.permit(&s.0.join("out").join("NUL"), Access::Write).is_err()); + // Another computer is refused by its spelling: asking it would offer it a sign-in. + // (`.invalid` never resolves, so a regression here fails slowly rather than leaks.) + for there in [r"\\anicraft-test.invalid\share\a.png", r"\\?\UNC\anicraft-test.invalid\share\a.png", "//anicraft-test.invalid/share/a.png", r"\\.\PhysicalDrive0", r"\\?\GLOBALROOT\Device\x"] { + let e = r.permit(Path::new(there), Access::Probe).unwrap_err(); + assert!(e.contains("outside the folders automation may use"), "{there}: {e}"); + } assert!(!display(&r.read()[0]).starts_with(r"\\?\")); } diff --git a/docs/adr/0124-automation-roots.md b/docs/adr/0124-automation-roots.md index 5c3072e4..b8438e32 100644 --- a/docs/adr/0124-automation-roots.md +++ b/docs/adr/0124-automation-roots.md @@ -52,7 +52,10 @@ inside a root; each Craft app has its own. PhotoCraft's flags are the model for is exact for folders that aren't there. A link that points nowhere is refused, since writing through it would create its target. The result must start with a resolved root, component by component. So case-insensitivity on Windows comes from the file system's own answer, - not from our folding of names. + not from our folding of names. Before any of that, on Windows, a path on another computer + (`\\server\share\…`) or a device path (`\\.\…`) is refused by its spelling alone, unless a + root is on that same share: resolving it would contact the server and offer it the + person's sign-in. (A mapped drive letter can't be told from a local one.) 4. **Where the check sits: in the engine, in three places**, under one scoped, per-thread policy (`access::confine(roots)`, in force until its guard drops): @@ -124,7 +127,7 @@ inside a root; each Craft app has its own. PhotoCraft's flags are the model for * Roots saved in the app's preferences, and a switch in the window (craft-libs `AgentSettings`), so the desktop app can cap what any bridge asks for. * A shared `craft-libs` helper for resolving and checking roots, so the Craft apps stop - carrying one each (proposed there; `engine::access::resolve` and `Roots` are written to + carrying one each (proposed in storytold/craft-libs#2527 and #45; `engine::access::resolve` and `Roots` are written to move as they are). * `ANICRAFT_AUTOMATION_*_ROOT` environment variables.