fix(permissions): block deny_read bypass via exec escalation

This commit is contained in:
viyatb-oai
2026-02-20 16:54:40 -08:00
parent 86c0fc8bc5
commit 15fcc43cc6
4 changed files with 78 additions and 0 deletions
+57
View File
@@ -19,7 +19,9 @@ pub use plan::PLAN_TOOL;
use serde::Deserialize;
use crate::function_tool::FunctionCallError;
use crate::protocol::SandboxPolicy;
pub use apply_patch::ApplyPatchHandler;
use codex_protocol::models::SandboxPermissions;
pub use dynamic::DynamicToolHandler;
pub use grep_files::GrepFilesHandler;
pub use js_repl::JsReplHandler;
@@ -49,3 +51,58 @@ where
FunctionCallError::RespondToModel(format!("failed to parse function arguments: {err}"))
})
}
fn reject_explicit_escalation_if_deny_read_present(
sandbox_permissions: SandboxPermissions,
sandbox_policy: &SandboxPolicy,
) -> Result<(), FunctionCallError> {
if sandbox_permissions.requires_escalated_permissions()
&& sandbox_policy.has_denied_read_paths()
{
return Err(FunctionCallError::RespondToModel(
"filesystem deny_read policy is enforced; reject command — you cannot ask for escalated permissions because managed read restrictions must remain sandboxed".to_string(),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::reject_explicit_escalation_if_deny_read_present;
use crate::function_tool::FunctionCallError;
use crate::protocol::SandboxPolicy;
use codex_protocol::models::SandboxPermissions;
use codex_utils_absolute_path::AbsolutePathBuf;
#[test]
fn explicit_escalation_is_rejected_when_deny_read_paths_exist() {
let mut policy = SandboxPolicy::new_workspace_write_policy();
let path = AbsolutePathBuf::try_from("/tmp/deny-read-test").expect("absolute path");
policy.append_deny_read_paths(&[path]);
let result = reject_explicit_escalation_if_deny_read_present(
SandboxPermissions::RequireEscalated,
&policy,
);
let Err(FunctionCallError::RespondToModel(message)) = result else {
panic!("expected managed deny_read explicit escalation rejection");
};
assert!(message.contains("filesystem deny_read policy is enforced"));
}
#[test]
fn non_escalated_command_is_allowed_when_deny_read_paths_exist() {
let mut policy = SandboxPolicy::new_workspace_write_policy();
let path = AbsolutePathBuf::try_from("/tmp/deny-read-test").expect("absolute path");
policy.append_deny_read_paths(&[path]);
let result = reject_explicit_escalation_if_deny_read_present(
SandboxPermissions::UseDefault,
&policy,
);
assert!(result.is_ok());
}
}
@@ -20,6 +20,7 @@ use crate::tools::events::ToolEmitter;
use crate::tools::events::ToolEventCtx;
use crate::tools::handlers::apply_patch::intercept_apply_patch;
use crate::tools::handlers::parse_arguments;
use crate::tools::handlers::reject_explicit_escalation_if_deny_read_present;
use crate::tools::orchestrator::ToolOrchestrator;
use crate::tools::registry::ToolHandler;
use crate::tools::registry::ToolKind;
@@ -268,6 +269,10 @@ impl ShellHandler {
"approval policy is {approval_policy:?}; reject command — you should not ask for escalated permissions if the approval policy is {approval_policy:?}"
)));
}
reject_explicit_escalation_if_deny_read_present(
exec_params.sandbox_permissions,
&turn.sandbox_policy,
)?;
// Intercept apply_patch if present.
if let Some(output) = intercept_apply_patch(
@@ -10,6 +10,7 @@ use crate::tools::context::ToolOutput;
use crate::tools::context::ToolPayload;
use crate::tools::handlers::apply_patch::intercept_apply_patch;
use crate::tools::handlers::parse_arguments;
use crate::tools::handlers::reject_explicit_escalation_if_deny_read_present;
use crate::tools::registry::ToolHandler;
use crate::tools::registry::ToolKind;
use crate::unified_exec::ExecCommandRequest;
@@ -154,6 +155,10 @@ impl ToolHandler for UnifiedExecHandler {
"approval policy is {approval_policy:?}; reject command — you cannot ask for escalated permissions if the approval policy is {approval_policy:?}"
)));
}
reject_explicit_escalation_if_deny_read_present(
sandbox_permissions,
&context.turn.sandbox_policy,
)?;
let workdir = workdir.filter(|value| !value.is_empty());
+11
View File
@@ -214,6 +214,17 @@ impl ToolOrchestrator {
network_policy_decision,
})));
}
// Managed filesystem read denies are non-overridable. If a command
// was denied by the sandbox and this was not a managed network
// policy denial, do not offer a no-sandbox retry path.
if turn_ctx.sandbox_policy.has_denied_read_paths()
&& network_approval_context.is_none()
{
return Err(ToolError::Codex(CodexErr::Sandbox(SandboxErr::Denied {
output,
network_policy_decision,
})));
}
if !tool.escalate_on_failure() {
return Err(ToolError::Codex(CodexErr::Sandbox(SandboxErr::Denied {
output,