PhotoCraft: An open-source, photo editor written by scratch in pure Rust
automation: accept absolute paths beneath the automation roots (#2176) (#2216)
Books7198a94164ff
diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rsindex 7165636af..66efd5d3b 100644--- a/crates/automation/src/server.rs+++ b/crates/automation/src/server.rs@@ -48,7 +48,7 @@ pub struct DocIndex {#[derive(Debug, Deserialize, JsonSchema)]#[serde(deny_unknown_fields)]pub struct OpenParams {- /// Forward-slash relative path beneath the configured automation read root.+ /// Path beneath the configured automation read root: relative (forward slashes) or absolute.pub path: String,}@@ -75,7 +75,7 @@ pub struct NewParams {#[derive(Debug, Default, Deserialize, JsonSchema)]#[serde(deny_unknown_fields)]pub struct SaveParams {- /// Forward-slash relative target beneath the configured automation write root.+ /// Target beneath the configured automation write root: relative (forward slashes) or absolute./// The extension selects the format. Omit to write back to the document's own file, which/// works only for a PSD, PSB or .pcraft file kept in its own format; any other save needs/// `path`, so a flattened or converted copy never replaces the opened file.diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rsindex 1e13eef0b..930e26f57 100644--- a/crates/automation/src/workspace.rs+++ b/crates/automation/src/workspace.rs@@ -1,7 +1,7 @@//! Capability-based filesystem policy for untrusted automation paths.use std::io::Write;-use std::path::{Path, PathBuf};+use std::path::{Component, Path, PathBuf};use std::sync::Arc;use cap_std::ambient_authority;@@ -16,14 +16,19 @@ const DENIED: &str = "automation filesystem access is not granted";#[derive(Clone)]struct RootCapability {dir: Arc<Dir>,+ /// The root as an absolute path (trusted launch configuration), to recognise absolute request+ /// paths that name a file beneath it.+ path: PathBuf,}/// Separate directory capabilities for automation reads and writes.////// Root paths are trusted launch-time configuration. Request paths are always/// untrusted, forward-slash relative paths resolved by `cap-std` beneath the-/// held directory handle. Absolute paths, parent traversal, alternate-/// separators, Windows prefixes and empty components are rejected before I/O.+/// held directory handle, or absolute paths whose text lies beneath the root+/// (translated to relative ones, see [`request_path`]). Parent traversal,+/// alternate separators, Windows prefixes and empty components are rejected+/// before I/O.#[derive(Clone, Default)]pub struct AuthorizedWorkspace {read: Option<RootCapability>,@@ -39,7 +44,7 @@ impl AuthorizedWorkspace {/// Read one regular file below the configured read root.pub fn read(&self, path: &str) -> Result<Vec<u8>, AutomationError> {- let relative = relative_path(path)?;+ let relative = request_path(path, self.read.as_ref())?;let root = self.read.as_ref().ok_or_else(|| AutomationError::BadRequest(format!("{DENIED}: read authority is absent")))?;let mut file = root.dir.open(&relative).map_err(|e| file_error("read", path, e))?;let metadata = file.metadata().map_err(|e| file_error("read", path, e))?;@@ -59,7 +64,7 @@ impl AuthorizedWorkspace {/// resolution and file creation relative to the held directory handle, so/// a non-existent final target is supported without ambient path access.pub fn write(&self, path: &str, bytes: &[u8]) -> Result<(), AutomationError> {- let relative = relative_path(path)?;+ let relative = request_path(path, self.write.as_ref())?;let root = self.write.as_ref().ok_or_else(|| AutomationError::BadRequest(format!("{DENIED}: write authority is absent")))?;let leaf = relative.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();let parent = relative.parent().map(Path::to_path_buf).unwrap_or_default();@@ -95,7 +100,33 @@ fn open_root(path: Option<&Path>, authority: &str) -> Result<Option<RootCapabili}let dir = Dir::open_ambient_dir(path, ambient_authority()).map_err(|e| AutomationError::Io(format!("cannot open automation {authority} root `{}`: {e}", path.display())))?;- Ok(Some(RootCapability { dir: Arc::new(dir) }))+ // Textual (no link resolution), like the request paths it is compared with.+ let absolute =+ std::path::absolute(path).map_err(|e| AutomationError::Io(format!("cannot resolve automation {authority} root `{}`: {e}", path.display())))?;+ Ok(Some(RootCapability { dir: Arc::new(dir), path: absolute }))+}++/// A request path as the relative path the root's capability resolves. An absolute path is+/// accepted only when its text names a file beneath `root`: the root's components are+/// stripped (the drive letter compared without case) and the rest must pass+/// [`relative_path`], so `..`, prefixes and device names are refused as before. Nothing is+/// resolved on the filesystem; the held directory handle still does all the opening, so+/// links cannot escape the root.+fn request_path(raw: &str, root: Option<&RootCapability>) -> Result<PathBuf, AutomationError> {+ let Some(root) = root.filter(|_| Path::new(raw).is_absolute()) else { return relative_path(raw) };+ let mut request = Path::new(raw).components();+ for want in root.path.components() {+ let same = match (request.next(), want) {+ (Some(Component::Prefix(a)), Component::Prefix(b)) => a.as_os_str().eq_ignore_ascii_case(b.as_os_str()),+ (Some(got), want) => got == want,+ (None, _) => false,+ };+ if !same {+ return Err(path_error(raw, "absolute paths must be inside the automation root"));+ }+ }+ let rest: Vec<&str> = request.map(|c| c.as_os_str().to_str().unwrap_or("\u{FFFD}")).collect();+ relative_path(&rest.join("/"))}fn relative_path(raw: &str) -> Result<PathBuf, AutomationError> {@@ -302,7 +333,7 @@ pub(crate) struct ExportSequence {impl AuthorizedWorkspace {pub(crate) fn export_sequence(&self, path: &str) -> Result<ExportSequence, AutomationError> {- let relative = relative_path(path)?;+ let relative = request_path(path, self.write.as_ref())?;let root = self.write.as_ref().ok_or_else(|| AutomationError::BadRequest(format!("{DENIED}: write authority is absent")))?;root.dir.create_dir_all(&relative).map_err(|e| file_error("mkdir", path, e))?;let dir = root.dir.open_dir(&relative).map_err(|e| file_error("open export directory", path, e))?;@@ -753,4 +784,47 @@ mod tests {headless.command_run("file.new", serde_json::json!({"width": 5, "height": 5})).unwrap();assert!(headless.session.file_menu.event_log.is_empty());}++ /// #2176: an absolute path beneath the root names the same file as its relative path; every+ /// other absolute path is refused before any file effect.+ #[test]+ fn absolute_paths_beneath_the_root_are_translated_and_others_refused() {+ let (inside, outside, workspace) = roots("absolute-paths");+ let abs = |p: &Path| p.to_string_lossy().into_owned();++ workspace.write(&abs(&inside.join("a.bin")), b"one").unwrap();+ assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one");+ assert_eq!(workspace.read(&abs(&inside.join("a.bin"))).unwrap(), b"one");+ // Forward slashes too (on Windows the native spelling uses `\`).+ assert_eq!(workspace.read(&abs(&inside.join("a.bin")).replace('\\', "/")).unwrap(), b"one");+ std::fs::create_dir(inside.join("sub")).unwrap();+ workspace.write(&abs(&inside.join("sub").join("b.bin")), b"two").unwrap();+ assert_eq!(std::fs::read(inside.join("sub/b.bin")).unwrap(), b"two");+ assert_eq!(workspace.read("sub/b.bin").unwrap(), b"two", "relative paths still work");++ let refused = |path: String| {+ let error = workspace.write(&path, b"x").unwrap_err().to_string();+ assert!(error.contains("automation path rejected"), "{path}: {error}");+ };+ refused(abs(&outside.join("escape.bin")));+ refused(abs(&inside.join("..").join("outside").join("traversal.bin")));+ refused(abs(&inside));+ refused(abs(&inside.join("CON")));+ refused(format!("{}:hidden", abs(&inside.join("a.bin"))));+ assert!(!outside.join("escape.bin").exists());+ assert!(!outside.join("traversal.bin").exists());+ assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one");+ }++ /// Each authority is checked against its own root, and none granted refuses absolute paths.+ #[test]+ fn absolute_paths_are_checked_against_the_authority_s_own_root() {+ let (inside, outside, _) = roots("absolute-authority");+ let workspace = AuthorizedWorkspace::new(Some(&outside), Some(&inside)).unwrap();+ let target = inside.join("written.bin");+ workspace.write(&target.to_string_lossy(), b"w").unwrap();+ assert!(workspace.read(&target.to_string_lossy()).is_err(), "not under the read root");+ let none = AuthorizedWorkspace::default();+ assert!(none.read(&target.to_string_lossy()).is_err());+ }}diff --git a/crates/automation/tests/mcp.rs b/crates/automation/tests/mcp.rsindex 143c10cff..21353a0fe 100644--- a/crates/automation/tests/mcp.rs+++ b/crates/automation/tests/mcp.rs@@ -545,6 +545,9 @@ async fn filesystem_policy_rejects_absolute_and_escaping_paths_before_effects()let client = connect(headless_in(&root)).await;assert_ne!(call(&client, "doc_open", json!({"path": "inside.png"})).await.is_error, Some(true));+ // An absolute path beneath the root is the same file (#2176).+ let inside = call(&client, "doc_open", json!({"path": root.join("inside.png").to_string_lossy()})).await;+ assert_ne!(inside.is_error, Some(true), "{}", text(&inside));let absolute = outside.join("absolute.png");let response = call(&client, "doc_save", json!({"path": absolute.to_string_lossy()})).await;assert_eq!(response.is_error, Some(true));diff --git a/docs/control-protocol.md b/docs/control-protocol.mdindex 0debdc070..6f7b58232 100644--- a/docs/control-protocol.md+++ b/docs/control-protocol.md@@ -272,12 +272,14 @@ Filesystem access fails closed unless launch-time read and/or write roots are grthe desktop `photocraft` process, but not by headless `photocraft-cli mcp` or `serve`;for those CLI modes, supply the flags explicitly. In MCP bridge mode, configure roots on thedesktop process rather than on the bridging CLI. Request paths must be-non-empty, forward-slash relative paths beneath the applicable root. Absolute paths, parent-traversal, alternate separators, drive/device/stream prefixes, malformed components, and link-escapes are rejected before file effects. Read and write authority are independent; the parent of-a new output file must already exist. Engine commands that still use ambient filesystem paths are-disabled for automation until they are migrated to the same capability interface. Interactive-desktop file pickers retain normal user-selected access.+non-empty, forward-slash relative paths beneath the applicable root, or absolute paths that lie+beneath it. An absolute path is compared with the root as text and the rest is checked like a+relative path; nothing is resolved on the filesystem. Absolute paths outside the root, parent+traversal, alternate separators in relative paths, drive/device/stream prefixes, malformed+components, and link escapes are rejected before file effects. Read and write authority are+independent; the parent of a new output file must already exist. Engine commands that still use+ambient filesystem paths are disabled for automation until they are migrated to the same+capability interface. Interactive desktop file pickers retain normal user-selected access.## Headless serverdiff --git a/docs/mcp.md b/docs/mcp.mdindex bb820a74a..b3e46f67c 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -6,9 +6,9 @@ PhotoCraft follows the same conventions as FilmCraft #28. Start a headless sessiphotocraft-cli mcp --automation-read-root /work/project --automation-write-root /work/project```-Paths are relative to the corresponding root; without a root that filesystem authority is-absent. Bridge mode uses `--bridge` and `--control-token-file` as described in-[development.md](development.md). UI tools require a running desktop app.+Paths are relative to the corresponding root, or absolute paths beneath it; without a root that+filesystem authority is absent. Bridge mode uses `--bridge` and `--control-token-file` as+described in [development.md](development.md). UI tools require a running desktop app.## Tools and resources