fix(core): generalize deny-read escalation

Co-authored-by: Codex noreply@openai.com
This commit is contained in:
viyatb-oai
2026-06-25 14:45:34 -07:00
co-authored by Codex noreply@openai.com
parent f56b2ae731
commit 3f40c70cc2
3 changed files with 63 additions and 43 deletions
+10 -13
View File
@@ -27,9 +27,9 @@ use crate::tools::sandboxing::ToolCtx;
use crate::tools::sandboxing::ToolError;
use crate::tools::sandboxing::ToolRuntime;
use crate::tools::sandboxing::default_exec_approval_requirement;
use crate::tools::sandboxing::escalation_profile_preserving_deny_read;
use crate::tools::sandboxing::sandbox_override_for_first_attempt;
use crate::tools::sandboxing::unsandboxed_execution_allowed;
use crate::tools::sandboxing::windows_escalation_profile_preserving_deny_read;
use codex_hooks::PermissionRequestDecision;
use codex_otel::ToolDecisionSource;
use codex_protocol::error::CodexErr;
@@ -227,22 +227,19 @@ impl ToolOrchestrator {
&file_system_sandbox_policy,
);
let managed_network_active = turn_ctx.network.is_some();
let windows_escalation_profile = windows_escalation_profile_preserving_deny_read(
cfg!(windows),
sandbox_override,
managed_network_active,
&turn_ctx.permission_profile,
);
let attempt_permissions = windows_escalation_profile
let escalation_profile =
escalation_profile_preserving_deny_read(sandbox_override, &turn_ctx.permission_profile);
let attempt_permissions = escalation_profile
.as_ref()
.unwrap_or(&turn_ctx.permission_profile);
let attempt_file_system_sandbox_policy = attempt_permissions.file_system_sandbox_policy();
let attempt_network_sandbox_policy = attempt_permissions.network_sandbox_policy();
let sandbox_preference = tool.sandbox_preference();
let sandbox_requested = match sandbox_override {
SandboxOverride::BypassSandboxFirstAttempt => false,
SandboxOverride::NoOverride | SandboxOverride::PreserveDenyRead => {
SandboxOverride::NoOverride | SandboxOverride::EscalatedSandboxWithDenyRead => {
self.sandbox.should_sandbox(
&file_system_sandbox_policy,
&attempt_file_system_sandbox_policy,
attempt_network_sandbox_policy,
sandbox_preference,
managed_network_active,
@@ -251,7 +248,7 @@ impl ToolOrchestrator {
};
let initial_sandbox = if sandbox_requested {
self.sandbox.select_initial(
&file_system_sandbox_policy,
&attempt_file_system_sandbox_policy,
attempt_network_sandbox_policy,
sandbox_preference,
turn_ctx.windows_sandbox_level,
@@ -430,14 +427,14 @@ impl ToolOrchestrator {
let retry_sandbox_requested = !unsandboxed_allowed
&& self.sandbox.should_sandbox(
&file_system_sandbox_policy,
&attempt_file_system_sandbox_policy,
attempt_network_sandbox_policy,
sandbox_preference,
managed_network_active,
);
let retry_sandbox = if retry_sandbox_requested {
self.sandbox.select_initial(
&file_system_sandbox_policy,
&attempt_file_system_sandbox_policy,
attempt_network_sandbox_policy,
sandbox_preference,
turn_ctx.windows_sandbox_level,
+12 -10
View File
@@ -245,7 +245,7 @@ pub(crate) fn default_exec_approval_requirement(
pub(crate) enum SandboxOverride {
NoOverride,
BypassSandboxFirstAttempt,
PreserveDenyRead,
EscalatedSandboxWithDenyRead,
}
pub(crate) fn sandbox_override_for_first_attempt(
@@ -266,7 +266,7 @@ pub(crate) fn sandbox_override_for_first_attempt(
// otherwise approved by rules or explicit escalation.
if !unsandboxed_execution_allowed(file_system_sandbox_policy) {
return if bypass_sandbox {
SandboxOverride::PreserveDenyRead
SandboxOverride::EscalatedSandboxWithDenyRead
} else {
SandboxOverride::NoOverride
};
@@ -281,20 +281,22 @@ pub(crate) fn sandbox_override_for_first_attempt(
SandboxOverride::NoOverride
}
pub(crate) fn windows_escalation_profile_preserving_deny_read(
is_windows: bool,
/// Builds an otherwise-unrestricted profile for an escalation that must retain
/// administrator-defined deny-read restrictions.
pub(crate) fn escalation_profile_preserving_deny_read(
sandbox_override: SandboxOverride,
managed_network_active: bool,
permission_profile: &PermissionProfile,
) -> Option<PermissionProfile> {
if !is_windows
|| !matches!(sandbox_override, SandboxOverride::PreserveDenyRead)
|| managed_network_active
{
if !matches!(
sandbox_override,
SandboxOverride::EscalatedSandboxWithDenyRead
) {
return None;
}
let file_system_policy = permission_profile.file_system_sandbox_policy();
let mut file_system_policy = FileSystemSandboxPolicy::unrestricted();
file_system_policy
.preserve_deny_read_restrictions_from(&permission_profile.file_system_sandbox_policy());
Some(
PermissionProfile::from_runtime_permissions_with_enforcement(
permission_profile.enforcement(),
+41 -20
View File
@@ -145,10 +145,12 @@ fn guardian_bypasses_sandbox_for_explicit_escalation_on_first_attempt() {
}
#[test]
fn deny_read_blocks_explicit_escalation_and_policy_bypass() {
fn deny_read_is_preserved_during_explicit_escalation_and_policy_bypass() {
let cwd = std::env::current_dir().expect("current directory");
let denied_path = cwd.join("secret.env");
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
path: FileSystemPath::GlobPattern {
pattern: "**/*.env".to_string(),
path: FileSystemPath::Path {
path: AbsolutePathBuf::try_from(denied_path.clone()).expect("absolute denied path"),
},
access: FileSystemAccessMode::Deny,
}]);
@@ -162,8 +164,8 @@ fn deny_read_blocks_explicit_escalation_and_policy_bypass() {
},
&file_system_policy,
),
SandboxOverride::PreserveDenyRead,
"explicit escalation would drop deny-read filesystem policy, so keep the first attempt sandboxed",
SandboxOverride::EscalatedSandboxWithDenyRead,
"explicit escalation must keep the first attempt sandboxed to preserve deny-read restrictions",
);
assert!(!unsandboxed_execution_allowed(&file_system_policy));
assert_eq!(
@@ -196,35 +198,54 @@ fn deny_read_blocks_explicit_escalation_and_policy_bypass() {
},
&file_system_policy,
),
SandboxOverride::PreserveDenyRead,
"exec-policy allow rules would drop deny-read filesystem policy, so keep the first attempt sandboxed",
SandboxOverride::EscalatedSandboxWithDenyRead,
"exec-policy allow rules must keep the first attempt sandboxed to preserve deny-read restrictions",
);
let permission_profile = PermissionProfile::from_runtime_permissions(
&file_system_policy,
NetworkSandboxPolicy::Restricted,
);
let escalated = windows_escalation_profile_preserving_deny_read(
/*is_windows*/ true,
SandboxOverride::PreserveDenyRead,
/*managed_network_active*/ false,
let escalated = escalation_profile_preserving_deny_read(
SandboxOverride::EscalatedSandboxWithDenyRead,
&permission_profile,
)
.expect("Windows escalation profile");
assert_eq!(escalated.file_system_sandbox_policy(), file_system_policy);
.expect("escalation profile");
assert_eq!(escalated.enforcement(), permission_profile.enforcement());
let mut expected_file_system_policy = FileSystemSandboxPolicy::unrestricted();
expected_file_system_policy.preserve_deny_read_restrictions_from(&file_system_policy);
assert_eq!(
escalated.file_system_sandbox_policy(),
expected_file_system_policy
);
let allowed_path = cwd.join("allowed.txt");
assert!(!file_system_policy.can_write_path_with_cwd(&allowed_path, &cwd));
assert!(
escalated
.file_system_sandbox_policy()
.can_write_path_with_cwd(&allowed_path, &cwd),
"escalation should grant ordinary filesystem writes",
);
assert!(
!escalated
.file_system_sandbox_policy()
.can_read_path_with_cwd(&denied_path, &cwd),
"escalation must retain deny-read restrictions",
);
assert_eq!(
escalated.network_sandbox_policy(),
NetworkSandboxPolicy::Enabled
);
assert!(SandboxManager::new().should_sandbox(
&escalated.file_system_sandbox_policy(),
escalated.network_sandbox_policy(),
SandboxablePreference::Auto,
/*has_managed_network_requirements*/ false,
));
assert_eq!(
windows_escalation_profile_preserving_deny_read(
/*is_windows*/ true,
SandboxOverride::PreserveDenyRead,
/*managed_network_active*/ true,
&permission_profile,
),
escalation_profile_preserving_deny_read(SandboxOverride::NoOverride, &permission_profile,),
None,
"managed network requirements remain authoritative",
"ordinary sandboxed attempts keep their original profile",
);
}