Handle remote Windows sandbox workspaces early

This commit is contained in:
David Wiesen
2026-04-25 20:40:46 -07:00
parent a9c111da54
commit 395084ec67
3 changed files with 61 additions and 0 deletions
@@ -12,8 +12,54 @@ pub fn canonical_path_key(path: &Path) -> String {
.to_ascii_lowercase()
}
pub fn unsupported_windows_sandbox_workspace_reason(path: &Path) -> Option<String> {
unsupported_windows_sandbox_workspace_kind(path).map(|kind| {
format!(
"windows sandbox does not support {kind} workspace paths ({path}); use a local checkout or disable the sandbox for this session",
path = path.display()
)
})
}
#[cfg(target_os = "windows")]
fn unsupported_windows_sandbox_workspace_kind(path: &Path) -> Option<&'static str> {
use windows_sys::Win32::Storage::FileSystem::DRIVE_REMOTE;
use windows_sys::Win32::Storage::FileSystem::GetDriveTypeW;
let path_text = path.as_os_str().to_string_lossy();
if path_text.starts_with(r"\\") || path_text.starts_with("//") {
return Some("UNC network share");
}
let root = windows_drive_root(path)?;
let root_wide: Vec<u16> = root.encode_utf16().chain(std::iter::once(0)).collect();
let drive_type = unsafe { GetDriveTypeW(root_wide.as_ptr()) };
(drive_type == DRIVE_REMOTE).then_some("mapped network drive")
}
#[cfg(not(target_os = "windows"))]
fn unsupported_windows_sandbox_workspace_kind(_path: &Path) -> Option<&'static str> {
None
}
#[cfg(target_os = "windows")]
fn windows_drive_root(path: &Path) -> Option<String> {
let path_text = path.as_os_str().to_string_lossy();
let bytes = path_text.as_bytes();
if bytes.len() >= 3
&& bytes[0].is_ascii_alphabetic()
&& bytes[1] == b':'
&& matches!(bytes[2], b'\\' | b'/')
{
Some(format!("{}:\\", bytes[0] as char))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::unsupported_windows_sandbox_workspace_reason;
use super::canonical_path_key;
use pretty_assertions::assert_eq;
use std::path::Path;
@@ -25,4 +71,11 @@ mod tests {
assert_eq!(canonical_path_key(windows_style), canonical_path_key(slash_style));
}
#[test]
fn unsupported_workspace_reason_flags_unc_paths() {
let reason = unsupported_windows_sandbox_workspace_reason(Path::new(r"\\server\share\repo"))
.expect("UNC path should be rejected");
assert!(reason.contains("UNC network share"));
}
}
@@ -15,6 +15,7 @@ use crate::allow::compute_allow_paths;
use crate::helper_materialization::helper_bin_dir;
use crate::logging::log_note;
use crate::path_normalization::canonical_path_key;
use crate::path_normalization::unsupported_windows_sandbox_workspace_reason;
use crate::policy::SandboxPolicy;
use crate::setup_error::SetupErrorCode;
use crate::setup_error::SetupFailure;
@@ -165,6 +166,9 @@ fn run_setup_refresh_inner(
) {
return Ok(());
}
if let Some(reason) = unsupported_windows_sandbox_workspace_reason(request.command_cwd) {
anyhow::bail!(reason);
}
let (read_roots, write_roots) = build_payload_roots(&request, &overrides);
let deny_write_paths = build_payload_deny_write_paths(&request, overrides.deny_write_paths);
let network_identity =
@@ -13,6 +13,7 @@ use crate::identity::SandboxCreds;
use crate::identity::require_logon_sandbox_creds;
use crate::logging::log_start;
use crate::path_normalization::canonicalize_path;
use crate::path_normalization::unsupported_windows_sandbox_workspace_reason;
use crate::policy::SandboxPolicy;
use crate::policy::parse_policy;
use crate::sandbox_utils::ensure_codex_home_exists;
@@ -102,6 +103,9 @@ fn prepare_spawn_context_common(
) {
anyhow::bail!("DangerFullAccess and ExternalSandbox are not supported for sandboxing")
}
if let Some(reason) = unsupported_windows_sandbox_workspace_reason(cwd) {
anyhow::bail!(reason);
}
normalize_null_device_env(env_map);
ensure_non_interactive_pager(env_map);