mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve filesystem sandbox policy context when the cwd disappears (#46112)
## Why Removing the selected working directory can prevent filesystem sandbox helpers from launching, even when the requested absolute paths remain accessible. Permission rules must stay anchored to the selected directory while those operations continue. ## What changed - Require a policy `cwd` in `FileSystemSandboxContext` and launch filesystem helpers from the filesystem root while preserving the policy directory and workspace roots. - Carry explicit `policyContext` in filesystem RPCs, preserving legacy wire fields and resolving omitted directories from older clients at executor ingress. - Keep permission paths as executor file URIs and validate host compatibility where they are enforced. - Bind Windows relative denial globs to the policy directory before changing the helper's launch directory, preserving home-relative patterns. ## Testing Add regression coverage for `apply_patch` after working-directory removal, legacy RPC directory fallbacks, cross-platform permission URI transport, and Windows relative read denials. The patch regression verifies that an allowed file is updated while an explicitly denied file remains unreadable and unchanged. GitOrigin-RevId: b0f4db722b27cb72ec129fc297c85732afac11f7
This commit is contained in:
1 parent
108e6a6dbe
commit
841b5490b2
48 files changed
+2419
-326
No files matched your search
@@ -14,6 +14,7 @@ use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context as _;
|
||||
use anyhow::Result;
|
||||
use codex_exec_server::CreateDirectoryOptions;
|
||||
use codex_exec_server::FileSystemSandboxContext;
|
||||
@@ -22,6 +23,13 @@ use codex_exec_server::ReadFileOptions;
|
||||
use codex_exec_server::RemoveOptions;
|
||||
use codex_exec_server::WindowsSandboxSelection;
|
||||
use codex_exec_server::WriteFileOptions;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::SandboxPolicy;
|
||||
use codex_sandboxing::SandboxType;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
@@ -430,6 +438,118 @@ async fn file_system_remote_fs_helper_respects_windows_sandbox_write_policy(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An elevated filesystem helper must enforce relative deny globs from the policy cwd.
|
||||
#[test_case(FileSystemImplementation::Local ; "local")]
|
||||
#[test_case(FileSystemImplementation::Remote ; "remote")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[serial_test::serial(remote_exec_server)]
|
||||
async fn file_system_elevated_relative_read_denial_uses_policy_cwd(
|
||||
implementation: FileSystemImplementation,
|
||||
) -> Result<()> {
|
||||
// Both implementations re-enter this test binary; the elevated backend finds its helpers
|
||||
// next to that binary, while Cargo and Bazel provide them separately.
|
||||
let test_exe = std::env::current_exe()?;
|
||||
let resources = test_exe
|
||||
.parent()
|
||||
.context("Windows test executable should have a parent directory")?
|
||||
.join("codex-resources");
|
||||
if let Err(error) = std::fs::create_dir_all(&resources)
|
||||
&& !(error.kind() == std::io::ErrorKind::PermissionDenied && resources.is_dir())
|
||||
{
|
||||
return Err(error).context("create Windows sandbox test resources");
|
||||
}
|
||||
for name in ["codex-windows-sandbox-setup", "codex-command-runner"] {
|
||||
let source = codex_utils_cargo_bin::cargo_bin(name)?;
|
||||
let destination = resources.join(Path::new(name).with_extension("exe"));
|
||||
if let Err(error) = std::fs::copy(&source, &destination)
|
||||
&& !(error.kind() == std::io::ErrorKind::PermissionDenied && destination.is_file())
|
||||
{
|
||||
return Err(error).with_context(|| format!("stage Windows sandbox helper {name}"));
|
||||
}
|
||||
}
|
||||
let context = create_file_system_context(implementation).await?;
|
||||
let tmp = tempfile::TempDir::new()?;
|
||||
let policy_cwd = tmp.path().join("checkout");
|
||||
let selected_files = policy_cwd.join("files");
|
||||
let other_files = tmp.path().join("files");
|
||||
std::fs::create_dir_all(&selected_files)?;
|
||||
std::fs::create_dir(&other_files)?;
|
||||
let allowed_neighbor = selected_files.join("allowed.txt");
|
||||
let denied = selected_files.join("blocked.env");
|
||||
let same_name_outside = other_files.join("blocked.env");
|
||||
std::fs::write(&allowed_neighbor, b"allowed neighbor")?;
|
||||
std::fs::write(&denied, b"denied")?;
|
||||
std::fs::write(&same_name_outside, b"allowed outside")?;
|
||||
|
||||
let cwd = PathUri::from_host_native_path(&policy_cwd)?;
|
||||
let policy = FileSystemSandboxPolicy::restricted(vec![
|
||||
FileSystemSandboxEntry::new(
|
||||
FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Root,
|
||||
},
|
||||
FileSystemAccessMode::Read,
|
||||
),
|
||||
FileSystemSandboxEntry::new(
|
||||
FileSystemPath::Path {
|
||||
path: PathUri::from_host_native_path(tmp.path())?,
|
||||
},
|
||||
FileSystemAccessMode::Write,
|
||||
),
|
||||
FileSystemSandboxEntry::new(
|
||||
FileSystemPath::GlobPattern {
|
||||
pattern: "files/*.env".to_string(),
|
||||
},
|
||||
FileSystemAccessMode::Deny,
|
||||
),
|
||||
]);
|
||||
let mut sandbox = FileSystemSandboxContext::from_permission_profile(
|
||||
PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted),
|
||||
cwd,
|
||||
);
|
||||
sandbox.windows_sandbox_selection = WindowsSandboxSelection::Elevated;
|
||||
|
||||
let file_system = &context.file_system;
|
||||
let allowed_neighbor = file_system
|
||||
.read_file(
|
||||
&PathUri::from_host_native_path(&allowed_neighbor)?,
|
||||
ReadFileOptions::default(),
|
||||
Some(&sandbox),
|
||||
)
|
||||
.await?;
|
||||
let same_name_outside = file_system
|
||||
.read_file(
|
||||
&PathUri::from_host_native_path(&same_name_outside)?,
|
||||
ReadFileOptions::default(),
|
||||
Some(&sandbox),
|
||||
)
|
||||
.await?;
|
||||
let denied = file_system
|
||||
.read_file(
|
||||
&PathUri::from_host_native_path(&denied)?,
|
||||
ReadFileOptions::default(),
|
||||
Some(&sandbox),
|
||||
)
|
||||
.await
|
||||
.expect_err("read matching the policy-cwd denial must be rejected");
|
||||
assert_eq!(
|
||||
(
|
||||
allowed_neighbor.as_slice(),
|
||||
same_name_outside.as_slice(),
|
||||
denied.kind(),
|
||||
),
|
||||
(
|
||||
b"allowed neighbor".as_slice(),
|
||||
b"allowed outside".as_slice(),
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
)
|
||||
);
|
||||
assert!(
|
||||
denied.to_string().contains("Access is denied"),
|
||||
"expected Windows access denial, got: {denied}"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn file_system_private_desktop_survives_helper_exits_and_separates_permissions() -> Result<()>
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user