From 567de6c7a7265a586de3b4e65a09c9663f69d421 Mon Sep 17 00:00:00 2001 From: PerchAnton Date: Fri, 16 Oct 2026 11:14:57 +0300 Subject: [PATCH 1/4] fix(automation): accept canonical and configured root paths --- crates/automation/src/workspace.rs | 210 +++++++++++++++++++++++++---- docs/control-protocol.md | 13 +- 2 files changed, 195 insertions(+), 28 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index f73a702385..fc1880e8e7 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::{Read, Write}; -use std::path::{Component, Path, PathBuf}; +use std::path::{Component, Components, Path, PathBuf, Prefix}; use std::sync::Arc; use cap_std::ambient_authority; @@ -16,16 +16,18 @@ const DENIED: &str = "automation filesystem access is not granted"; #[derive(Clone)] struct RootCapability { dir: Arc, - /// The root as an absolute path (trusted launch configuration), to recognise absolute request + /// The canonical root path (trusted launch configuration), to recognise absolute request /// paths that name a file beneath it. path: PathBuf, + /// The trusted launch spelling also names the same held capability, even through a link. + configured_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, or absolute paths whose text lies beneath the root +/// held directory handle, or absolute paths beneath the canonical or configured root spelling /// (translated to relative ones, see [`request_path`]). Parent traversal, /// alternate separators, Windows prefixes and empty components are rejected /// before I/O. @@ -140,35 +142,51 @@ fn open_root(path: Option<&Path>, authority: &str) -> Result) -> Result { - 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, + let path = Path::new(raw); + let Some(root) = root.filter(|_| path.is_absolute()) else { return relative_path(raw) }; + // Prefer the trusted launch spelling: on Unix it can itself contain `..`. Only that + // exact granted prefix is exempt; traversal in the remaining request still fails below. + let request = path_beneath_root(path, &root.configured_path) + .or_else(|| path_beneath_root(path, &root.path)) + .ok_or_else(|| path_error(raw, "absolute paths must be inside the automation root"))?; + let rest = request.map(|c| c.as_os_str().to_str().ok_or_else(|| path_error(raw, "path is not UTF-8"))).collect::, _>>()?; + relative_path(&rest.join("/")) +} + +fn path_beneath_root<'a>(path: &'a Path, root: &Path) -> Option> { + let mut request = path.components(); + for want in root.components() { + let same = match (request.next()?, want) { + (Component::Prefix(a), Component::Prefix(b)) => match (a.kind(), b.kind()) { + // std::fs::canonicalize uses verbatim paths on Windows. Accept their ordinary + // drive/UNC spelling too, without treating a device prefix as a disk prefix. + (Prefix::Disk(a) | Prefix::VerbatimDisk(a), Prefix::Disk(b) | Prefix::VerbatimDisk(b)) => a.eq_ignore_ascii_case(&b), + (Prefix::UNC(a, b) | Prefix::VerbatimUNC(a, b), Prefix::UNC(c, d) | Prefix::VerbatimUNC(c, d)) => { + a.eq_ignore_ascii_case(c) && b.eq_ignore_ascii_case(d) + } + _ => a.as_os_str().eq_ignore_ascii_case(b.as_os_str()), + }, + (got, want) => got == want, }; if !same { - return Err(path_error(raw, "absolute paths must be inside the automation root")); + return None; } } - let rest: Vec<&str> = request.map(|c| c.as_os_str().to_str().unwrap_or("\u{FFFD}")).collect(); - relative_path(&rest.join("/")) + Some(request) } fn relative_path(raw: &str) -> Result { @@ -966,6 +984,150 @@ mod tests { assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); } + #[cfg(windows)] + #[test] + fn canonical_windows_prefixes_match_without_ambient_resolution() { + for (request, root) in + [(r"c:\root\new.bin", r"\\?\C:\root"), (r"\\?\C:\root\new.bin", r"C:\root"), (r"\\server\share\root\new.bin", r"\\?\UNC\SERVER\SHARE\root")] + { + let remaining = path_beneath_root(Path::new(request), Path::new(root)).unwrap(); + assert_eq!(remaining.as_path(), Path::new("new.bin")); + } + for request in [r"C:\root-sibling\new.bin", r"D:\root\new.bin", r"\\.\C:\root\new.bin", r"\\other\share\root\new.bin"] { + assert!(path_beneath_root(Path::new(request), Path::new(r"\\?\C:\root")).is_none()); + } + } + + #[test] + fn configured_parent_components_accept_resolved_absolute_paths() { + let (inside, outside, _) = roots("resolved-parent-root"); + let configured = inside.join("..").join("inside"); + let workspace = AuthorizedWorkspace::new(Some(&configured), Some(&configured)).unwrap(); + let resolved = std::fs::canonicalize(&inside).unwrap(); + workspace.write(&resolved.join("new.bin").to_string_lossy(), b"new").unwrap(); + assert_eq!(workspace.read(&resolved.join("new.bin").to_string_lossy()).unwrap(), b"new"); + #[cfg(unix)] + workspace.write(&configured.join("launch-spelling.bin").to_string_lossy(), b"trusted prefix").unwrap(); + assert!(workspace.write(&outside.join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("blocked.bin").exists()); + } + + #[test] + fn relative_configured_root_accepts_resolved_absolute_paths() { + let relative = PathBuf::from("target").join(format!("automation-relative-root-{}", std::process::id())); + std::fs::create_dir_all(&relative).unwrap(); + let workspace = AuthorizedWorkspace::new(Some(&relative), Some(&relative)).unwrap(); + let resolved = std::fs::canonicalize(&relative).unwrap(); + workspace.write(&resolved.join("new.bin").to_string_lossy(), b"relative").unwrap(); + assert_eq!(workspace.read(&resolved.join("new.bin").to_string_lossy()).unwrap(), b"relative"); + std::fs::remove_file(resolved.join("new.bin")).unwrap(); + drop(workspace); + std::fs::remove_dir(resolved).unwrap(); + } + + #[cfg(unix)] + #[test] + fn trusted_root_names_are_not_validated_as_relative_request_names() { + let (inside, outside, _) = roots("trusted-root-names"); + let root = inside.join("aux"); + std::fs::create_dir(&root).unwrap(); + let alias = outside.join("CON"); + link_directory(&root, &alias); + let workspace = AuthorizedWorkspace::new(Some(&alias), Some(&alias)).unwrap(); + for spelling in [&root, &alias] { + workspace.write(&spelling.join("new.bin").to_string_lossy(), b"trusted").unwrap(); + assert_eq!(workspace.read(&spelling.join("new.bin").to_string_lossy()).unwrap(), b"trusted"); + assert!(workspace.write(&spelling.join("NUL").to_string_lossy(), b"no").is_err()); + } + assert!(!root.join("NUL").exists()); + remove_directory_link(&alias); + } + + // Windows junctions exercise the same root alias without requiring Developer Mode. + #[cfg(any(unix, windows))] + fn link_directory(target: &Path, link: &Path) { + #[cfg(unix)] + std::os::unix::fs::symlink(target, link).unwrap(); + #[cfg(windows)] + assert!(std::process::Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(link).arg(target).output().unwrap().status.success()); + } + + #[cfg(any(unix, windows))] + fn remove_directory_link(link: &Path) { + #[cfg(unix)] + std::fs::remove_file(link).unwrap(); + #[cfg(windows)] + std::fs::remove_dir(link).unwrap(); + } + + #[cfg(any(unix, windows))] + #[test] + fn linked_root_accepts_both_spellings_and_new_nested_exports() { + let (inside, outside, _) = roots("linked-root"); + let link = outside.join("root-link"); + link_directory(&inside, &link); + let workspace = AuthorizedWorkspace::new(Some(&link), Some(&link)).unwrap(); + let resolved = std::fs::canonicalize(&inside).unwrap(); + for (i, spelling) in [resolved, link.clone()].iter().enumerate() { + let name = format!("new-{i}.bin"); + workspace.write(&spelling.join(&name).to_string_lossy(), b"new").unwrap(); + assert_eq!(workspace.read(&spelling.join(&name).to_string_lossy()).unwrap(), b"new"); + assert_eq!(workspace.read(&name).unwrap(), b"new"); + let mut export = workspace.export_sequence(&spelling.join(format!("frames-{i}")).join("nested").to_string_lossy()).unwrap(); + export.write("frame.png", b"frame").unwrap(); + export.commit(); + assert_eq!(std::fs::read(inside.join(format!("frames-{i}/nested/frame.png"))).unwrap(), b"frame"); + } + remove_directory_link(&link); + } + + #[cfg(any(unix, windows))] + #[test] + fn ungranted_absolute_aliases_and_link_escapes_are_refused() { + let (inside, outside, workspace) = roots("request-alias"); + let alias = outside.join("inside-alias"); + link_directory(&inside, &alias); + workspace.write("new.bin", b"inside").unwrap(); + assert!(workspace.read(&alias.join("new.bin").to_string_lossy()).is_err()); + assert!(workspace.write(&alias.join("ungranted.bin").to_string_lossy(), b"no").is_err()); + assert!(!inside.join("ungranted.bin").exists()); + std::fs::write(outside.join("secret.bin"), b"outside").unwrap(); + let escape = inside.join("escape"); + link_directory(&outside, &escape); + assert!(workspace.read(&escape.join("secret.bin").to_string_lossy()).is_err()); + assert!(workspace.write(&escape.join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(workspace.export_sequence(&escape.join("frames").join("nested").to_string_lossy()).is_err()); + assert!(!outside.join("blocked.bin").exists()); + assert!(!outside.join("frames").exists()); + assert_eq!(std::fs::read(outside.join("secret.bin")).unwrap(), b"outside"); + let read_only = AuthorizedWorkspace::new(Some(&alias), Some(&outside)).unwrap(); + assert_eq!(read_only.read(&inside.join("new.bin").to_string_lossy()).unwrap(), b"inside"); + assert!(read_only.write(&alias.join("denied.bin").to_string_lossy(), b"no").is_err()); + assert!(!inside.join("denied.bin").exists()); + remove_directory_link(&escape); + remove_directory_link(&alias); + } + + #[cfg(any(unix, windows))] + #[test] + fn retargeting_root_alias_does_not_regrant_the_held_authority() { + let (inside, outside, _) = roots("retargeted-root"); + let alias = outside.join("root-alias"); + link_directory(&inside, &alias); + let workspace = AuthorizedWorkspace::new(Some(&alias), Some(&alias)).unwrap(); + remove_directory_link(&alias); + link_directory(&outside, &alias); + std::fs::write(outside.join("secret.bin"), b"secret").unwrap(); + workspace.write("held.bin", b"held").unwrap(); + assert_eq!(workspace.read(&inside.join("held.bin").to_string_lossy()).unwrap(), b"held"); + assert!(workspace.read(&alias.join("secret.bin").to_string_lossy()).is_err()); + workspace.write(&alias.join("held-alias.bin").to_string_lossy(), b"original grant").unwrap(); + assert_eq!(std::fs::read(inside.join("held-alias.bin")).unwrap(), b"original grant"); + assert!(!outside.join("held-alias.bin").exists()); + assert_eq!(std::fs::read(outside.join("secret.bin")).unwrap(), b"secret"); + remove_directory_link(&alias); + } + /// 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() { diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 14d416f19b..d0fdcdb99b 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -87,7 +87,7 @@ The transport is `apps/photocraft/src/control_server.rs`, and the handlers are i base64 PNG data; a path is relative to the automation write root. Raises the window first (default) because occluded macOS windows stop rendering - `ui.focus`: bring the main window to the front -- `app.open {path}` / `app.save {path?, tgaBits?}`: relative file I/O through the configured automation roots (`app.open` reads under the read root, `app.save` writes under the write root; absolute paths, `..` and paths escaping the root are refused, and both fail closed when no root was granted). Both reply with `warnings` (import/export notes such as "adjustment layer flattened"; `[]` when none), also shown to the user in the status bar and as a notice (`notices` in `ui.inspect`); `app.open` also returns the `path` and document `name` (directory `.pcraft` — the native on-disk form — loads through the read root's capability; the single-file (zip) form takes the bytes path), `app.save` the `path` written. `app.save` without `path` writes back only to the document's own PSD, PSB or `.pcraft` file, like File › Save. `tgaBits` (24 or 32) sets a TGA's bits per pixel: 32 writes the alpha channel (or else the transparency) as its alpha, 24 none; without it, 32 with an alpha channel or transparency, else 24. Automation saves never show TGA or TIFF options. Automation opens and saves never fire script events. Use these two rather than `file.open`, `file.save`, `file.saveAs` or `file.saveACopy`, which the control channel refuses (see [Engine commands](#engine-commands)) +- `app.open {path}` / `app.save {path?, tgaBits?}`: relative file I/O through the configured automation roots (`app.open` reads under the read root, `app.save` writes under the write root; absolute paths beneath the canonical or configured root are accepted, while `..` and paths escaping the root are refused, and both fail closed when no root was granted). Both reply with `warnings` (import/export notes such as "adjustment layer flattened"; `[]` when none), also shown to the user in the status bar and as a notice (`notices` in `ui.inspect`); `app.open` also returns the `path` and document `name` (directory `.pcraft` — the native on-disk form — loads through the read root's capability; the single-file (zip) form takes the bytes path), `app.save` the `path` written. `app.save` without `path` writes back only to the document's own PSD, PSB or `.pcraft` file, like File › Save. `tgaBits` (24 or 32) sets a TGA's bits per pixel: 32 writes the alpha channel (or else the transparency) as its alpha, 24 none; without it, 32 with an alpha channel or transparency, else 24. Automation saves never show TGA or TIFF options. Automation opens and saves never fire script events. Use these two rather than `file.open`, `file.save`, `file.saveAs` or `file.saveACopy`, which the control channel refuses (see [Engine commands](#engine-commands)) - `app.quit` ## Engine commands @@ -343,9 +343,14 @@ the desktop `photocraft` process, but not by headless `photocraft-cli mcp` or `s for those CLI modes, supply the flags explicitly. In MCP bridge mode, configure roots on the desktop process rather than on the bridging CLI. Request paths must be 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 +beneath it. Configured roots are canonicalized at startup, so relative roots, `..` in trusted +configuration and directory links grant the resolved directory. Absolute requests may use that +canonical root or the original configured spelling; both refer to the capability opened at +startup, even if a launch-time link is later retargeted. Only that root prefix is translated; +request paths are not resolved through the ambient filesystem. The remaining components are +validated and all file effects use the held directory capability, including new output files +and nested export directories. Other absolute aliases outside those roots, parent +traversal beyond the granted root prefix, 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 From 66a1195212eca9ebce06de0880d2c2142f0c5866 Mon Sep 17 00:00:00 2001 From: PerchAnton Date: Mon, 19 Oct 2026 09:55:02 +0300 Subject: [PATCH 2/4] test(automation): handle aliased Windows TEMP in root tests --- .github/workflows/ci.yml | 5 +++-- crates/automation/src/workspace.rs | 32 ++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4303f0aac4..f265a9a01c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,8 @@ concurrency: jobs: # Which OSes the test job runs on. main, manual runs and PRs labelled `full-ci` get Linux and # Windows. Other PRs get Linux, plus Windows when they touch code whose behaviour differs by - # platform: the app shell, UI, text and fonts, GPU, pen input, build scripts or the lockfile. + # platform: the app shell, filesystem automation, UI, text and fonts, GPU, pen input, build + # scripts or the lockfile. # macOS is tested only by release.yml. platforms: timeout-minutes: 10 @@ -78,7 +79,7 @@ jobs: else echo "web=false" >> "$GITHUB_OUTPUT" fi - if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(ui-egui|text|gpu|tablet|heif)/|Cargo\.lock$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then + if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(automation|ui-egui|text|gpu|tablet|heif)/|Cargo\.lock$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then echo "legs=$all" >> "$GITHUB_OUTPUT" else echo "legs=[$linux]" >> "$GITHUB_OUTPUT" diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index fc1880e8e7..862a562b17 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -1081,10 +1081,37 @@ mod tests { remove_directory_link(&link); } + /// TEMP can name the same directory differently from canonicalize (8.3 on Windows). + /// A junction makes that mismatch deterministic even when 8.3 names are disabled. + #[cfg(windows)] + #[test] + fn root_alias_tests_work_with_an_aliased_windows_temp() { + let (inside, outside, _) = roots("aliased-temp"); + let temp_alias = outside.join("temp-alias"); + link_directory(&inside, &temp_alias); + let exe = std::env::current_exe().unwrap(); + for test in [ + "workspace::tests::ungranted_absolute_aliases_and_link_escapes_are_refused", + "workspace::tests::retargeting_root_alias_does_not_regrant_the_held_authority", + ] { + // Set TEMP only in the child; changing the process environment races other tests. + let output = + std::process::Command::new(&exe).args(["--exact", test, "--nocapture"]).env("TEMP", &temp_alias).env("TMP", &temp_alias).output().unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(output.status.success(), "{test}: {stdout}\n{stderr}"); + assert!(stdout.contains("1 passed;"), "child must run the requested test: {stdout}"); + } + remove_directory_link(&temp_alias); + } + #[cfg(any(unix, windows))] #[test] fn ungranted_absolute_aliases_and_link_escapes_are_refused() { let (inside, outside, workspace) = roots("request-alias"); + // TEMP may use an 8.3 name or a junction. A grant through `alias` below recognises + // that configured alias and the canonical target, not every other name for `inside`. + let resolved_inside = std::fs::canonicalize(&inside).unwrap(); let alias = outside.join("inside-alias"); link_directory(&inside, &alias); workspace.write("new.bin", b"inside").unwrap(); @@ -1101,7 +1128,7 @@ mod tests { assert!(!outside.join("frames").exists()); assert_eq!(std::fs::read(outside.join("secret.bin")).unwrap(), b"outside"); let read_only = AuthorizedWorkspace::new(Some(&alias), Some(&outside)).unwrap(); - assert_eq!(read_only.read(&inside.join("new.bin").to_string_lossy()).unwrap(), b"inside"); + assert_eq!(read_only.read(&resolved_inside.join("new.bin").to_string_lossy()).unwrap(), b"inside"); assert!(read_only.write(&alias.join("denied.bin").to_string_lossy(), b"no").is_err()); assert!(!inside.join("denied.bin").exists()); remove_directory_link(&escape); @@ -1112,6 +1139,7 @@ mod tests { #[test] fn retargeting_root_alias_does_not_regrant_the_held_authority() { let (inside, outside, _) = roots("retargeted-root"); + let resolved_inside = std::fs::canonicalize(&inside).unwrap(); let alias = outside.join("root-alias"); link_directory(&inside, &alias); let workspace = AuthorizedWorkspace::new(Some(&alias), Some(&alias)).unwrap(); @@ -1119,7 +1147,7 @@ mod tests { link_directory(&outside, &alias); std::fs::write(outside.join("secret.bin"), b"secret").unwrap(); workspace.write("held.bin", b"held").unwrap(); - assert_eq!(workspace.read(&inside.join("held.bin").to_string_lossy()).unwrap(), b"held"); + assert_eq!(workspace.read(&resolved_inside.join("held.bin").to_string_lossy()).unwrap(), b"held"); assert!(workspace.read(&alias.join("secret.bin").to_string_lossy()).is_err()); workspace.write(&alias.join("held-alias.bin").to_string_lossy(), b"original grant").unwrap(); assert_eq!(std::fs::read(inside.join("held-alias.bin")).unwrap(), b"original grant"); From d53cbb6c9fd16da033d2cfd370d935538f96b9bc Mon Sep 17 00:00:00 2001 From: PerchAnton Date: Wed, 21 Oct 2026 16:47:02 +0300 Subject: [PATCH 3/4] fix(automation): accept Windows root ASCII case variants --- crates/automation/src/workspace.rs | 41 +++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index 72fd76f199..ae21a66b49 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -184,6 +184,8 @@ fn request_path(raw: &str, root: Option<&RootCapability>) -> Result(path: &'a Path, root: &Path) -> Option> { let mut request = path.components(); for want in root.components() { @@ -197,6 +199,8 @@ fn path_beneath_root<'a>(path: &'a Path, root: &Path) -> Option> } _ => a.as_os_str().eq_ignore_ascii_case(b.as_os_str()), }, + #[cfg(windows)] + (Component::Normal(a), Component::Normal(b)) => a.eq_ignore_ascii_case(b), (got, want) => got == want, }; if !same { @@ -1027,12 +1031,18 @@ mod tests { assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); } + /// Equivalent drive, UNC and ASCII-case root spellings need no ambient path resolution. #[cfg(windows)] #[test] fn canonical_windows_prefixes_match_without_ambient_resolution() { - for (request, root) in - [(r"c:\root\new.bin", r"\\?\C:\root"), (r"\\?\C:\root\new.bin", r"C:\root"), (r"\\server\share\root\new.bin", r"\\?\UNC\SERVER\SHARE\root")] - { + for (request, root) in [ + (r"c:\root\new.bin", r"\\?\C:\root"), + (r"\\?\C:\root\new.bin", r"C:\root"), + (r"\\server\share\root\new.bin", r"\\?\UNC\SERVER\SHARE\root"), + (r"c:\ROOT\new.bin", r"\\?\C:\root"), + (r"\\?\C:\ROOT\new.bin", r"c:\root"), + (r"\\server\share\ROOT\new.bin", r"\\?\UNC\SERVER\SHARE\root"), + ] { let remaining = path_beneath_root(Path::new(request), Path::new(root)).unwrap(); assert_eq!(remaining.as_path(), Path::new("new.bin")); } @@ -1041,6 +1051,31 @@ mod tests { } } + /// Case variants of the granted Windows prefix still use its held directory capability. + #[cfg(windows)] + #[test] + fn windows_root_case_variants_keep_io_inside_the_grant() { + let (inside, outside, workspace) = roots("root-case"); + let configured = PathBuf::from(inside.to_string_lossy().to_ascii_uppercase()); + let canonical = PathBuf::from(std::fs::canonicalize(&inside).unwrap().to_string_lossy().to_ascii_uppercase()); + for spelling in [&configured, &canonical] { + let path = spelling.join("new.bin"); + workspace.write(&path.to_string_lossy(), b"inside").unwrap(); + assert_eq!(workspace.read(&path.to_string_lossy()).unwrap(), b"inside"); + assert!(workspace.write(&spelling.join("..").join("outside").join("blocked.bin").to_string_lossy(), b"no").is_err()); + } + assert_eq!(std::fs::read(inside.join("new.bin")).unwrap(), b"inside"); + assert!(workspace.write(&configured.with_file_name("INSIDE-SIBLING").join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("blocked.bin").exists()); + } + + /// Unix roots with different case must not become aliases of the granted prefix. + #[cfg(unix)] + #[test] + fn unix_root_prefixes_remain_case_sensitive() { + assert!(path_beneath_root(Path::new("/Root/new.bin"), Path::new("/root")).is_none()); + } + #[test] fn configured_parent_components_accept_resolved_absolute_paths() { let (inside, outside, _) = roots("resolved-parent-root"); From 8b566e3334b20f726377c6a04be0813c34015fb0 Mon Sep 17 00:00:00 2001 From: PerchAnton Date: Fri, 23 Oct 2026 10:48:07 +0300 Subject: [PATCH 4/4] fix(automation): retain parent components in an absolute configured root --- crates/automation/src/workspace.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index fd368289da..4c79104902 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -159,8 +159,13 @@ fn open_root(path: Option<&Path>, authority: &str) -> Result, authority: &str) -> Result) -> Result { let path = Path::new(raw); let Some(root) = root.filter(|_| path.is_absolute()) else { return relative_path(raw) }; - // Prefer the trusted launch spelling: on Unix it can itself contain `..`. Only that + // Prefer the trusted launch spelling: it can itself contain `..`. Only that // exact granted prefix is exempt; traversal in the remaining request still fails below. let request = path_beneath_root(path, &root.configured_path) .or_else(|| path_beneath_root(path, &root.path)) @@ -1117,8 +1122,11 @@ mod tests { let resolved = std::fs::canonicalize(&inside).unwrap(); workspace.write(&resolved.join("new.bin").to_string_lossy(), b"new").unwrap(); assert_eq!(workspace.read(&resolved.join("new.bin").to_string_lossy()).unwrap(), b"new"); - #[cfg(unix)] workspace.write(&configured.join("launch-spelling.bin").to_string_lossy(), b"trusted prefix").unwrap(); + assert_eq!(workspace.read(&configured.join("launch-spelling.bin").to_string_lossy()).unwrap(), b"trusted prefix"); + assert_eq!(workspace.read(&resolved.join("launch-spelling.bin").to_string_lossy()).unwrap(), b"trusted prefix"); + assert!(workspace.write(&configured.join("..").join("outside/escaped.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("escaped.bin").exists()); assert!(workspace.write(&outside.join("blocked.bin").to_string_lossy(), b"no").is_err()); assert!(!outside.join("blocked.bin").exists()); }