ssh: accept only /-rooted remote paths on every client

Path::has_root also accepts \x and C:\x on Windows, which name nothing
on the Unix host the helper runs on. Check for a leading / instead.
This commit is contained in:
Harold Hunt
2026-10-23 21:04:22 -04:00
parent 20847a0de8
commit 43d94b07ad
+11 -8
View File
@@ -20,21 +20,20 @@ use crate::tree::{Node, NodeKind, aggregate};
use std::time::Instant;
const VERSION: u32 = 1;
const MAX_LINE: u64 = 1 << 20;
const MAX_DIAGNOSTICS: usize = 16 << 10;
/// Whether `path` names a directory from the remote host's root.
///
/// The SSH helper is Unix-only, so remote paths are always `/`-rooted.
/// `Path::is_absolute` also wants a drive letter on Windows, which would
/// reject every remote path a Windows client receives. On Unix the two
/// checks agree.
/// The SSH helper is Unix-only, so remote paths are `/`-rooted.
/// `Path::is_absolute` also wants a drive letter on Windows, and
/// `Path::has_root` accepts `\x` and `C:\x` there, so neither works for a
/// Windows client. On Unix this agrees with `is_absolute`.
#[must_use]
pub fn is_remote_absolute(path: &Path) -> bool {
path.has_root()
path.as_os_str().as_encoded_bytes().first() == Some(&b'/')
}
const MAX_LINE: u64 = 1 << 20;
const MAX_DIAGNOSTICS: usize = 16 << 10;
// Non-interactive shells often omit ~/.local/bin. Do not require users to
// edit the server's startup files after the documented user-local install.
const REMOTE_COMMAND: &str = r#"if command -v disktree-scan >/dev/null 2>&1; then
@@ -1111,6 +1110,10 @@ mod tests {
assert!(is_remote_absolute(Path::new("/home/someone")));
assert!(!is_remote_absolute(Path::new("home/someone")));
assert!(!is_remote_absolute(Path::new("")));
// Windows-rooted paths name nothing on a Unix host.
assert!(!is_remote_absolute(Path::new(r"\home\someone")));
assert!(!is_remote_absolute(Path::new(r"C:\Users")));
assert!(!is_remote_absolute(Path::new(r"\\server\share")));
}
#[test]