From b53f2dcac75537423fff4beaef4b84299353b406 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 13:26:53 +0100 Subject: [PATCH] Automation: refuse every Windows device name in request paths (#9424) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The automation device-name check was narrower than cap-std's own Windows check (COM0/LPT0, COM¹-³/LPT¹-³, spaces before the extension). write() renames through a Win32 path and skips cap-std's check, so a file such as COM¹.png or "NUL .png" could be written but not read back, and export_sequence left an empty folder behind. Co-Authored-By: Claude Opus 5.5 --- crates/automation/src/workspace.rs | 36 +++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index e8566b439ed..f19397e5632 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -221,11 +221,14 @@ fn relative_path(raw: &str) -> Result { Ok(PathBuf::from(raw)) } +/// At least every name cap-std refuses to open on Windows (`COM0`–`COM9`, `COM¹`–`COM³`, the same +/// for `LPT`, spaces before the extension ignored), so nothing written is unreadable (#9424). fn is_windows_device_name(component: &str) -> bool { - let stem = component.split('.').next().unwrap_or(component).to_ascii_uppercase(); + let stem = component.split('.').next().unwrap_or(component).trim_end().to_ascii_uppercase(); + let port = |n: &str| matches!(n, "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"); matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") - || stem.strip_prefix("COM").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) - || stem.strip_prefix("LPT").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) + || stem.strip_prefix("COM").is_some_and(port) + || stem.strip_prefix("LPT").is_some_and(port) } fn path_error(path: &str, reason: &str) -> AutomationError { @@ -675,6 +678,33 @@ mod tests { } } + #[test] + fn every_device_name_cap_std_refuses_to_open_is_refused_before_any_write() { + // cap-std refuses these when it opens a file on Windows, so a file written under one of + // them could not be read back (#9424). + let (inside, _, workspace) = roots("devices"); + std::fs::create_dir(inside.join("sub")).unwrap(); + for path in + ["COM0.txt", "LPT0", "COM¹.png", "com².png", "COM³", "LPT¹.txt", "LPT².txt", "lpt³", "NUL .png", "Aux .txt", "sub/CON .psd", "COM¹/frame.png"] + { + let error = workspace.write(path, b"x").unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "write {path:?}: {error}"); + let error = workspace.read(path).unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "read {path:?}: {error}"); + } + for dir in ["COM¹", "LPT0", "sub/PRN .x"] { + let error = workspace.export_sequence(dir).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(error.contains("reserved device names"), "export {dir:?}: {error}"); + } + assert_eq!(std::fs::read_dir(&inside).unwrap().count(), 1, "only `sub` may exist"); + assert_eq!(std::fs::read_dir(inside.join("sub")).unwrap().count(), 0); + // Look-alikes are ordinary names. + for path in ["COM10.txt", "COM¹0.txt", "LPT00", "console.txt", "NULL.txt", "nul_.txt", " NUL.txt"] { + workspace.write(path, b"x").unwrap_or_else(|e| panic!("write {path:?}: {e}")); + assert_eq!(workspace.read(path).unwrap(), b"x", "{path:?}"); + } + } + #[test] fn read_and_write_authority_are_separate() { let (inside, _, _) = roots("separate");