Files
codex/codex-rs/core/src/tools/sandboxing_tests.rs
T
viyatb-oai f7aad26f1d Defer exec-server sandbox preparation to the execution host (#60663)
## Why

Remote commands can use workspace paths from a different operating system. Preparing them on the client should preserve those paths and command arguments for the execution host.

## What changed

- Build exec-server requests directly, carrying the native command and sandbox context without applying the client's sandbox transform.
- Resolve workspace permissions and runtime read access in `SandboxManager` on the execution host, including helper binaries and validated managed-network CA bundles.
- Add explicit backend selection and required platform sandbox selection, rejecting launches when the required sandbox is unavailable.

## Testing

Extend request tests to cover foreign workspace paths, Unicode arguments, and empty arguments. Extend the remote process test on Unix to verify that restricted reads allow the sandbox helper and validated CA bundle while denying an untrusted certificate file.

GitOrigin-RevId: 5fc02ac4208912ecefb80a212742389afc14ac05
2026-11-10 21:26:11 +00:00

479 lines
17 KiB
Rust

use super::*;
use crate::sandboxing::SandboxPermissions;
use crate::tools::hook_names::HookToolName;
use codex_network_proxy::ManagedNetworkSandboxContext;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::permissions::NetworkSandboxPolicy;
use codex_protocol::protocol::GranularApprovalConfig;
use codex_protocol::sandbox::SandboxOverride;
use codex_sandboxing::SandboxCommand;
use codex_sandboxing::SandboxManager;
use codex_sandboxing::SandboxType;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
use pretty_assertions::assert_eq;
use serde_json::json;
use std::collections::HashMap;
#[test]
fn bash_permission_request_payload_omits_missing_description() {
assert_eq!(
PermissionRequestPayload::bash(
"echo hi".to_string(),
/*description*/ None,
PathUri::parse("file:///repo-B").unwrap()
),
PermissionRequestPayload {
tool_name: HookToolName::bash(),
tool_cwd: Some(PathUri::parse("file:///repo-B").unwrap()),
tool_input: json!({ "command": "echo hi" }),
}
);
}
#[test]
fn bash_permission_request_payload_includes_description_when_present() {
assert_eq!(
PermissionRequestPayload::bash(
"echo hi".to_string(),
Some("network-access example.com".to_string()),
PathUri::parse("file:///repo-B").unwrap(),
),
PermissionRequestPayload {
tool_name: HookToolName::bash(),
tool_cwd: Some(PathUri::parse("file:///repo-B").unwrap()),
tool_input: json!({
"command": "echo hi",
"description": "network-access example.com",
}),
}
);
}
#[test]
fn external_sandbox_skips_exec_approval_on_request() {
assert_eq!(
default_exec_approval_requirement(
AskForApproval::OnRequest,
&FileSystemSandboxPolicy::external_sandbox(),
),
ExecApprovalRequirement::Skip {
bypass_sandbox: false,
proposed_execpolicy_amendment: None,
}
);
}
#[test]
fn restricted_sandbox_requires_exec_approval_on_request() {
assert_eq!(
default_exec_approval_requirement(
AskForApproval::OnRequest,
&FileSystemSandboxPolicy::default()
),
ExecApprovalRequirement::NeedsApproval {
reason: None,
proposed_execpolicy_amendment: None,
}
);
}
#[test]
fn windows_sandbox_selection_distinguishes_configured_and_executor_defaults() {
let cwd = PathUri::parse("file:///C:/workspace").expect("Windows path URI");
assert_eq!(
executor_windows_sandbox_level(
SandboxType::WindowsMxc,
codex_protocol::config_types::WindowsSandboxLevel::Disabled,
&cwd,
),
codex_protocol::config_types::WindowsSandboxLevel::Disabled,
);
assert_eq!(
executor_windows_sandbox_selection(
SandboxType::WindowsMxc,
codex_protocol::config_types::WindowsSandboxLevel::Disabled,
&cwd,
),
codex_file_system::WindowsSandboxSelection::Mxc,
);
assert_eq!(
configured_windows_sandbox_selection(
SandboxType::None,
codex_protocol::config_types::WindowsSandboxLevel::Disabled,
&cwd,
),
codex_file_system::WindowsSandboxSelection::Disabled,
);
assert_eq!(
executor_windows_sandbox_selection(
SandboxType::None,
codex_protocol::config_types::WindowsSandboxLevel::Disabled,
&cwd,
),
codex_file_system::WindowsSandboxSelection::RestrictedToken,
);
}
#[test]
fn default_exec_approval_requirement_rejects_sandbox_prompt_when_granular_disables_it() {
let policy = AskForApproval::Granular(GranularApprovalConfig {
sandbox_approval: false,
rules: true,
skill_approval: true,
request_permissions: true,
mcp_elicitations: true,
});
let requirement =
default_exec_approval_requirement(policy, &FileSystemSandboxPolicy::default());
assert_eq!(
requirement,
ExecApprovalRequirement::Forbidden {
reason: "approval policy disallowed sandbox approval prompt".to_string(),
}
);
}
#[test]
fn default_exec_approval_requirement_keeps_prompt_when_granular_allows_sandbox_approval() {
let policy = AskForApproval::Granular(GranularApprovalConfig {
sandbox_approval: true,
rules: false,
skill_approval: true,
request_permissions: true,
mcp_elicitations: false,
});
let requirement =
default_exec_approval_requirement(policy, &FileSystemSandboxPolicy::default());
assert_eq!(
requirement,
ExecApprovalRequirement::NeedsApproval {
reason: None,
proposed_execpolicy_amendment: None,
}
);
}
#[test]
fn additional_permissions_allow_bypass_sandbox_first_attempt_when_execpolicy_skips() {
assert_eq!(
sandbox_override_for_first_attempt(
SandboxPermissions::WithAdditionalPermissions,
&ExecApprovalRequirement::Skip {
bypass_sandbox: true,
proposed_execpolicy_amendment: None,
},
&FileSystemSandboxPolicy::default(),
/*already_approved*/ false,
),
SandboxOverride::BypassSandboxFirstAttempt
);
}
#[test]
fn guardian_bypasses_sandbox_for_explicit_escalation_on_first_attempt() {
assert_eq!(
sandbox_override_for_first_attempt(
SandboxPermissions::RequireEscalated,
&ExecApprovalRequirement::Skip {
bypass_sandbox: false,
proposed_execpolicy_amendment: None,
},
&FileSystemSandboxPolicy::default(),
/*already_approved*/ true,
),
SandboxOverride::BypassSandboxFirstAttempt
);
}
#[test]
fn deny_read_preserves_the_sandbox_for_explicit_escalation_and_blocks_policy_bypass() {
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
path: FileSystemPath::GlobPattern {
pattern: "**/*.env".to_string(),
},
access: FileSystemAccessMode::Deny,
missing_path_behavior: None,
}]);
assert_eq!(
sandbox_override_for_first_attempt(
SandboxPermissions::RequireEscalated,
&ExecApprovalRequirement::Skip {
bypass_sandbox: false,
proposed_execpolicy_amendment: None,
},
&file_system_policy,
/*already_approved*/ true,
),
SandboxOverride::EscalatedSandboxWithRestrictions,
"explicit escalation must widen the first attempt without dropping the sandbox",
);
assert!(!unsandboxed_execution_allowed(&file_system_policy));
assert!(matches!(
ensure_native_sandbox(SandboxOverride::EscalatedSandboxWithRestrictions, SandboxType::None),
Err(ToolError::Rejected(reason)) if reason.contains("requires an available filesystem sandbox"),
));
assert_eq!(
sandbox_permissions_preserving_denied_reads(
SandboxPermissions::RequireEscalated,
&file_system_policy,
),
SandboxPermissions::UseDefault,
);
assert_eq!(
sandbox_permissions_preserving_denied_reads(
SandboxPermissions::WithAdditionalPermissions,
&file_system_policy,
),
SandboxPermissions::WithAdditionalPermissions,
);
assert_eq!(
sandbox_permissions_preserving_denied_reads(
SandboxPermissions::RequireEscalated,
&FileSystemSandboxPolicy::default(),
),
SandboxPermissions::RequireEscalated,
);
assert_eq!(
sandbox_override_for_first_attempt(
SandboxPermissions::WithAdditionalPermissions,
&ExecApprovalRequirement::Skip {
bypass_sandbox: true,
proposed_execpolicy_amendment: None,
},
&file_system_policy,
/*already_approved*/ true,
),
SandboxOverride::NoOverride,
"exec-policy allow rules would drop deny-read filesystem policy, so keep the first attempt sandboxed",
);
}
#[tokio::test]
async fn windows_sandbox_env_preserves_denied_reads_or_rejects_unsupported_backend() {
let temp_dir = tempfile::TempDir::new().expect("create sandbox workspace");
let cwd = AbsolutePathBuf::from_absolute_path(
dunce::canonicalize(temp_dir.path()).expect("canonicalize sandbox workspace"),
)
.expect("absolute sandbox workspace");
let denied_path = cwd.join("blocked");
std::fs::create_dir_all(denied_path.as_path()).expect("create denied directory");
let denied_path = AbsolutePathBuf::from_absolute_path(
dunce::canonicalize(denied_path.as_path()).expect("canonicalize denied directory"),
)
.expect("absolute denied directory");
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![
FileSystemSandboxEntry {
path: FileSystemPath::Special {
value: FileSystemSpecialPath::Root,
},
access: FileSystemAccessMode::Read,
missing_path_behavior: None,
},
FileSystemSandboxEntry {
path: FileSystemPath::Special {
value: FileSystemSpecialPath::project_roots(/*subpath*/ None),
},
access: FileSystemAccessMode::Write,
missing_path_behavior: None,
},
FileSystemSandboxEntry {
path: FileSystemPath::Path {
path: denied_path.clone().into(),
},
access: FileSystemAccessMode::Deny,
missing_path_behavior: None,
},
]);
let permissions = codex_protocol::models::PermissionProfile::from_runtime_permissions(
&file_system_policy,
NetworkSandboxPolicy::Restricted,
);
let cwd_uri = PathUri::from_abs_path(&cwd);
let manager = SandboxManager::new();
let mut attempt = SandboxAttempt {
sandbox_override: SandboxOverride::NoOverride,
sandbox: SandboxType::WindowsRestrictedToken,
sandbox_requested: true,
permissions: &permissions,
exec_server_permissions: &permissions,
enforce_managed_network: false,
manager: &manager,
sandbox_cwd: &cwd_uri,
workspace_roots: std::slice::from_ref(&cwd_uri),
sandbox_exe: None,
use_legacy_landlock: false,
windows_sandbox_type: SandboxType::WindowsRestrictedToken,
windows_sandbox_level: codex_protocol::config_types::WindowsSandboxLevel::Elevated,
network_denial_cancellation_token: None,
network_proxy: None,
};
let command = || SandboxCommand {
program: "cmd.exe".into(),
args: vec!["/C".to_string(), "echo sandboxed".to_string()],
cwd: cwd_uri.clone(),
env: HashMap::new(),
managed_network: None,
additional_permissions: None,
};
let options = || crate::sandboxing::ExecOptions {
expiration: crate::exec::ExecExpiration::DefaultTimeout,
capture_policy: crate::exec::ExecCapturePolicy::ShellTool,
};
let request = attempt
.env_for(
command(),
options(),
/*network*/ None,
/*environment_id*/ None,
)
.await
.expect("prepare elevated Windows sandbox request");
let overrides = request
.windows_sandbox_filesystem_overrides
.expect("elevated Windows sandbox should preserve deny-read overrides");
assert_eq!(overrides.additional_deny_read_paths, vec![denied_path]);
assert_eq!(request.windows_sandbox_workspace_roots, vec![cwd]);
attempt.windows_sandbox_level =
codex_protocol::config_types::WindowsSandboxLevel::RestrictedToken;
let error = attempt
.env_for(
command(),
options(),
/*network*/ None,
/*environment_id*/ None,
)
.await
.expect_err("restricted-token Windows sandbox cannot enforce deny-read restrictions");
assert_eq!(
error.to_string(),
"unsupported operation: windows unelevated restricted-token sandbox cannot enforce deny-read restrictions directly; refusing to run unsandboxed"
);
}
#[tokio::test]
async fn exec_server_env_keeps_command_native_and_carries_sandbox_context() {
let cwd_uri = PathUri::parse(if cfg!(windows) {
"file:///workspace"
} else {
"file:///C:/workspace"
})
.expect("foreign workspace");
let exec_server_permissions = codex_protocol::models::PermissionProfile::workspace_write();
let permissions = exec_server_permissions
.clone()
.materialize_project_roots_with_path_uris(std::slice::from_ref(&cwd_uri));
let manager = SandboxManager::new();
let mut attempt = SandboxAttempt {
sandbox_override: SandboxOverride::NoOverride,
sandbox: SandboxType::None,
sandbox_requested: true,
permissions: &permissions,
exec_server_permissions: &exec_server_permissions,
enforce_managed_network: true,
manager: &manager,
sandbox_cwd: &cwd_uri,
workspace_roots: std::slice::from_ref(&cwd_uri),
sandbox_exe: None,
use_legacy_landlock: false,
windows_sandbox_type: SandboxType::None,
windows_sandbox_level: codex_protocol::config_types::WindowsSandboxLevel::Disabled,
network_denial_cancellation_token: None,
network_proxy: None,
};
let managed_network = ManagedNetworkSandboxContext {
loopback_ports: vec![43123],
allow_local_binding: false,
..Default::default()
};
let argv = vec![
"/bin/bash".to_string(),
"-lc".to_string(),
"printf 'café au lait'".to_string(),
String::new(),
];
let prepare = async |attempt: &SandboxAttempt<'_>| {
attempt
.env_for_exec_server(
argv.clone(),
cwd_uri.clone(),
HashMap::new(),
Some(managed_network.clone()),
/*additional_permissions*/ None,
crate::sandboxing::ExecOptions {
expiration: crate::exec::ExecExpiration::DefaultTimeout,
capture_policy: crate::exec::ExecCapturePolicy::ShellTool,
},
)
.await
};
let request = prepare(&attempt)
.await
.expect("prepare remote exec request");
assert!(!attempt.is_escalated());
assert_eq!(request.command, argv);
assert_eq!(request.arg0, None);
assert_eq!(request.sandbox, SandboxType::None);
assert_eq!(
request.exec_server_sandbox,
Some(codex_exec_server::FileSystemSandboxContext {
sandbox_override: SandboxOverride::NoOverride,
permissions: exec_server_permissions.clone(),
cwd: cwd_uri.clone(),
workspace_roots: vec![cwd_uri.clone()],
user_home_dir: None,
temporary_directories: None,
windows_sandbox_selection: if cfg!(windows) {
codex_file_system::WindowsSandboxSelection::Disabled
} else {
codex_file_system::WindowsSandboxSelection::RestrictedToken
},
windows_sandbox_proxy_settings_mode: None,
use_legacy_landlock: false,
})
);
assert!(request.exec_server_enforce_managed_network);
assert_eq!(
request.exec_server_managed_network,
Some(managed_network.clone())
);
attempt.sandbox_override = SandboxOverride::EscalatedSandboxWithRestrictions;
let escalated_request = prepare(&attempt)
.await
.expect("prepare approved escalation with retained sandbox restrictions");
let mut expected_context = request.exec_server_sandbox.expect("sandbox context");
expected_context.sandbox_override = SandboxOverride::EscalatedSandboxWithRestrictions;
assert_eq!(
escalated_request.exec_server_sandbox,
Some(expected_context)
);
attempt.sandbox_requested = false;
let request = prepare(&attempt)
.await
.expect("prepare unsandboxed remote exec request");
assert!(attempt.is_escalated());
assert_eq!(request.exec_server_sandbox, None);
assert!(!request.exec_server_enforce_managed_network);
assert_eq!(request.exec_server_managed_network, Some(managed_network));
let full_access = codex_protocol::models::PermissionProfile::Disabled;
attempt.permissions = &full_access;
attempt.exec_server_permissions = &full_access;
attempt.enforce_managed_network = false;
assert!(!attempt.is_escalated(), "full access is not an escalation");
}