From 491773ae4a67da88c548c7e54d5df1d2410bb1b7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 18:04:54 -0800 Subject: [PATCH] craft-mcp: Roots applies the Windows name rules to the absolute path too std::path::absolute turns //?/ and //./ into \?\ and \.\, so a path such as //?/GLOBALROOT/Device/Mup/server/share/x passed the name rules as written and reached the file system. Roots::check and Roots::new now apply the rules again to the absolute form before any file system call. --- crates/mcp/src/roots.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/mcp/src/roots.rs b/crates/mcp/src/roots.rs index f47525ebb7..d28fb73781 100644 --- a/crates/mcp/src/roots.rs +++ b/crates/mcp/src/roots.rs @@ -185,7 +185,10 @@ fn open_all(paths: &[PathBuf], access: Access) -> Result, AccessErr return Err(bad("an empty path names no folder".into())); } if cfg!(windows) { - check_windows_names(&without_verbatim_prefix(p).map_err(|e| bad(e.to_string()))?).map_err(|e| bad(e.to_string()))?; + let plain = without_verbatim_prefix(p).map_err(|e| bad(e.to_string()))?; + check_windows_names(&plain).map_err(|e| bad(e.to_string()))?; + let absolute = std::path::absolute(&plain).map_err(|e| bad(e.to_string()))?; + check_windows_names(&absolute).map_err(|e| bad(e.to_string()))?; } let resolved = std::fs::canonicalize(p).map_err(|e| bad(e.to_string()))?; if !resolved.is_dir() { @@ -326,6 +329,8 @@ fn check_share(path: &Path, access: Access, roots: &[PathBuf]) -> Result<(), Acc let plain = without_verbatim_prefix(path)?; check_windows_names(&plain)?; let absolute = std::path::absolute(&plain).map_err(|e| AccessError::Unresolved { path: path.to_path_buf(), reason: e.to_string() })?; + // `absolute` turns `//?/` and `//./` into `\\?\` and `\\.\`, so the rules run on its result too. + check_windows_names(&absolute)?; let Some((server, share)) = network_share(&absolute) else { return Ok(()); }; @@ -608,6 +613,9 @@ mod tests { r"\??\C:\work\x.json", r"\\?\GLOBALROOT\Device\Mup\server\share\x.json", r"\\?\pipe\x", + "//?/GLOBALROOT/Device/Mup/server/share/x.json", + "//./pipe/x", + r"//?\GLOBALROOT\Device\Mup\server\share\x.json", ] { for roots in [&local[..], &on_share[..]] { assert!(matches!(check(p, roots), Err(AccessError::Verbatim { .. })), "{p}"); @@ -620,6 +628,8 @@ mod tests { assert!(matches!(e, AccessError::Verbatim { .. }), "{e}"); let e = Roots::new(&[PathBuf::from(r"\??\UNC\craft-roots-test.invalid\share")], &[]).unwrap_err(); assert!(matches!(&e, AccessError::BadRoot { reason, .. } if reason.contains(r"`\??\`")), "a root is checked before it is opened: {e}"); + let e = Roots::new(&[PathBuf::from("//?/GLOBALROOT/Device/Mup/craft-roots-test.invalid/share")], &[]).unwrap_err(); + assert!(matches!(&e, AccessError::BadRoot { reason, .. } if reason.contains(r"`\\?\`")), "{e}"); let _ = std::fs::remove_dir_all(base); }