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
@@ -552,12 +552,7 @@ async fn execute_verified_patch(
|
||||
) -> Result<String, FunctionCallError> {
|
||||
let cwd = action.cwd.clone();
|
||||
let sandbox_context = turn_environment.sandbox_context(/*additional_permissions*/ None);
|
||||
let Some(policy_context) = file_system_sandbox_policy_context_for_cwd(&sandbox_context, &cwd)
|
||||
else {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"apply_patch requires an executor cwd".to_string(),
|
||||
));
|
||||
};
|
||||
let policy_context = file_system_sandbox_policy_context_for_cwd(&sandbox_context, &cwd);
|
||||
let sandbox_route = if turn_environment.environment.is_remote() {
|
||||
PatchSandboxRoute::ExecutorManaged
|
||||
} else {
|
||||
|
||||
@@ -299,7 +299,7 @@ mod tests {
|
||||
|
||||
struct CapturingExtensionExecutor {
|
||||
captured_call: Arc<Mutex<Option<codex_tools::ToolCall<'static>>>>,
|
||||
captured_sandbox_cwds: Arc<Mutex<Vec<Option<PathUri>>>>,
|
||||
captured_sandbox_cwds: Arc<Mutex<Vec<PathUri>>>,
|
||||
}
|
||||
|
||||
impl<'call> codex_extension_api::ToolExecutor<codex_tools::ToolCall<'call>>
|
||||
@@ -432,7 +432,7 @@ mod tests {
|
||||
let expected_sandbox_cwds = turn
|
||||
.environments
|
||||
.turn_environments()
|
||||
.map(|environment| Some(environment.cwd().clone()))
|
||||
.map(|environment| environment.cwd().clone())
|
||||
.collect::<Vec<_>>();
|
||||
let history_item = ResponseItem::Message {
|
||||
id: None,
|
||||
|
||||
@@ -243,10 +243,10 @@ pub(super) struct EffectiveAdditionalPermissions {
|
||||
pub(super) fn file_system_sandbox_policy_context_for_cwd<'a>(
|
||||
sandbox_context: &'a FileSystemSandboxContext,
|
||||
cwd: &'a PathUri,
|
||||
) -> Option<codex_protocol::permissions::FileSystemSandboxPolicyContext<'a>> {
|
||||
let mut context = sandbox_context.policy_context()?;
|
||||
) -> codex_protocol::permissions::FileSystemSandboxPolicyContext<'a> {
|
||||
let mut context = sandbox_context.policy_context();
|
||||
context.cwd = cwd;
|
||||
Some(context)
|
||||
context
|
||||
}
|
||||
|
||||
pub(super) fn implicit_granted_permissions(
|
||||
@@ -298,9 +298,9 @@ pub(super) async fn apply_granted_turn_permissions(
|
||||
if additional_permissions.is_none() {
|
||||
Some(granted.clone())
|
||||
} else {
|
||||
effective_permissions.as_ref().and_then(|effective| {
|
||||
preapproved_permission_profile(effective, granted, context.as_ref()?)
|
||||
})
|
||||
effective_permissions
|
||||
.as_ref()
|
||||
.and_then(|effective| preapproved_permission_profile(effective, granted, &context))
|
||||
}
|
||||
});
|
||||
let permissions_preapproved = preapproved_permissions.is_some();
|
||||
|
||||
@@ -79,11 +79,7 @@ impl RequestPermissionsHandler {
|
||||
};
|
||||
let sandbox_context =
|
||||
turn_environment.sandbox_context(/*additional_permissions*/ None);
|
||||
let context = sandbox_context.policy_context().ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"request_permissions requires an executor cwd".to_string(),
|
||||
)
|
||||
})?;
|
||||
let context = sandbox_context.policy_context();
|
||||
let mut arguments: Value = parse_arguments(&arguments)?;
|
||||
resolve_permission_path_strings(&mut arguments, &context)?;
|
||||
let mut args: RequestPermissionsArgs =
|
||||
|
||||
@@ -322,14 +322,7 @@ impl ExecCommandHandler {
|
||||
let requested_additional_permissions = additional_permissions.clone();
|
||||
let sandbox_context =
|
||||
turn_environment.sandbox_context(/*additional_permissions*/ None);
|
||||
let Some(permission_context) =
|
||||
file_system_sandbox_policy_context_for_cwd(&sandbox_context, &cwd)
|
||||
else {
|
||||
manager.release_process_id(process_id).await;
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"selected environment sandbox context is missing cwd".to_string(),
|
||||
));
|
||||
};
|
||||
let permission_context = file_system_sandbox_policy_context_for_cwd(&sandbox_context, &cwd);
|
||||
let effective_additional_permissions = apply_granted_turn_permissions(
|
||||
context.session.as_ref(),
|
||||
turn_environment,
|
||||
|
||||
@@ -97,8 +97,8 @@ impl ApplyPatchRuntime {
|
||||
req.additional_permissions.as_ref(),
|
||||
);
|
||||
Some(FileSystemSandboxContext {
|
||||
permissions: permissions.into(),
|
||||
cwd: Some(attempt.sandbox_cwd.clone()),
|
||||
permissions,
|
||||
cwd: attempt.sandbox_cwd.clone(),
|
||||
workspace_roots: attempt.workspace_roots.to_vec(),
|
||||
user_home_dir: req.turn_environment.user_home_dir.clone(),
|
||||
temporary_directories: None,
|
||||
|
||||
@@ -265,15 +265,10 @@ async fn file_system_sandbox_context_preserves_executor_workspace_permissions()
|
||||
);
|
||||
let expected_permissions =
|
||||
PermissionProfile::from_runtime_permissions(&file_system_policy, network_policy);
|
||||
let native_permissions: PermissionProfile = sandbox
|
||||
.permissions
|
||||
.clone()
|
||||
.try_into()
|
||||
.expect("native sandbox permissions");
|
||||
assert_eq!(native_permissions, expected_permissions);
|
||||
assert_eq!(sandbox.permissions, expected_permissions);
|
||||
assert_eq!(
|
||||
sandbox.cwd,
|
||||
Some(codex_utils_path_uri::PathUri::from_abs_path(&path))
|
||||
codex_utils_path_uri::PathUri::from_abs_path(&path)
|
||||
);
|
||||
assert_eq!(
|
||||
sandbox.windows_sandbox_selection,
|
||||
@@ -348,8 +343,8 @@ async fn file_system_sandbox_context_respects_sandbox_request() {
|
||||
assert_eq!(
|
||||
ApplyPatchRuntime::file_system_sandbox_context_for_attempt(&req, &attempt),
|
||||
Some(FileSystemSandboxContext {
|
||||
permissions: permissions.into(),
|
||||
cwd: Some(cwd.clone()),
|
||||
permissions,
|
||||
cwd: cwd.clone(),
|
||||
workspace_roots: vec![cwd],
|
||||
user_home_dir: Some(user_home_dir),
|
||||
temporary_directories: None,
|
||||
|
||||
@@ -520,8 +520,8 @@ impl<'a> SandboxAttempt<'a> {
|
||||
exec_request.exec_server_managed_network = managed_network;
|
||||
if self.sandbox_requested {
|
||||
exec_request.exec_server_sandbox = Some(FileSystemSandboxContext {
|
||||
permissions: exec_server_permissions.into(),
|
||||
cwd: Some(exec_request.windows_sandbox_policy_cwd.clone()),
|
||||
permissions: exec_server_permissions,
|
||||
cwd: exec_request.windows_sandbox_policy_cwd.clone(),
|
||||
workspace_roots: self.workspace_roots.to_vec(),
|
||||
user_home_dir: None,
|
||||
temporary_directories: None,
|
||||
|
||||
@@ -368,8 +368,8 @@ fn exec_server_env_keeps_command_native_and_carries_sandbox_context() {
|
||||
assert_eq!(
|
||||
request.exec_server_sandbox,
|
||||
Some(codex_exec_server::FileSystemSandboxContext {
|
||||
permissions: exec_server_permissions.clone().into(),
|
||||
cwd: Some(cwd_uri.clone()),
|
||||
permissions: exec_server_permissions.clone(),
|
||||
cwd: cwd_uri.clone(),
|
||||
workspace_roots: vec![cwd_uri.clone()],
|
||||
user_home_dir: None,
|
||||
temporary_directories: None,
|
||||
|
||||
Reference in new issue
Block a user