Resolve permission requests in the executor context (#42146)

## Why

`request_permissions` paths and grants need to be evaluated against the selected executor environment, including its path convention, home directory, workspace roots, and temporary directories.

## What changed

- Resolve relative and home-relative permission paths using the executor context, reject mismatched path conventions and lossy paths, and support legacy `read` and `write` path lists.
- Move grant intersection into core so requested and granted permissions use the originating environment's sandbox context. Preserve deny entries conservatively when a special path cannot be resolved.
- Keep the full originating environment with pending permission requests so delayed responses are normalized against the same context.

## Testing

- Cover POSIX, Windows, UNC, relative, and home-relative path resolution and invalid path contexts.
- Verify end-to-end app-server grants are limited to the requested workspace scope and unresolved temporary-directory denies are preserved.

GitOrigin-RevId: 730a2aacd391262e92a6314f3a5b6c262e3dca10
This commit is contained in:
iceweasel-oai authored and copyberry committed 2026-09-01 22:08:08 +00:00
1 parent 86b7d12742
commit cd8bd62c6e
10 files changed
+492 -307

No files matched your search

@@ -1,5 +1,8 @@
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
use codex_protocol::request_permissions::RequestPermissionsArgs;
use codex_sandboxing::policy_transforms::normalize_additional_permissions;
use codex_sandboxing::policy_transforms::normalize_additional_permissions_with_context;
use codex_utils_path_uri::LegacyAppPathString;
use serde_json::Value;
use crate::function_tool::FunctionCallError;
use crate::tools::context::FunctionToolOutput;
@@ -7,7 +10,6 @@ use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
use crate::tools::context::boxed_tool_output;
use crate::tools::handlers::parse_arguments;
use crate::tools::handlers::parse_arguments_with_base_path;
use crate::tools::handlers::resolve_tool_environment;
use crate::tools::handlers::shell_spec::create_request_permissions_tool;
use crate::tools::handlers::shell_spec::request_permissions_tool_description;
@@ -75,19 +77,25 @@ impl RequestPermissionsHandler {
"request_permissions requires a primary environment".to_string(),
));
};
// TODO(anp): Migrate request_permissions parsing and permission profiles to PathUri so
// environment-native foreign paths do not require host conversion.
let native_cwd = turn_environment.cwd().to_abs_path().map_err(|err| {
FunctionCallError::RespondToModel(format!(
"request_permissions cwd `{}` is not native to the Codex host: {err}",
turn_environment.cwd()
))
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 mut arguments: Value = parse_arguments(&arguments)?;
resolve_permission_path_strings(&mut arguments, &context)?;
let mut args: RequestPermissionsArgs =
parse_arguments_with_base_path(&arguments, &native_cwd)?;
args.permissions = normalize_additional_permissions(args.permissions.into())
.map(codex_protocol::request_permissions::RequestPermissionProfile::from)
.map_err(FunctionCallError::RespondToModel)?;
serde_json::from_value(arguments).map_err(|err| {
FunctionCallError::RespondToModel(format!(
"failed to parse function arguments: {err}"
))
})?;
args.permissions =
normalize_additional_permissions_with_context(args.permissions.into(), &context)
.map(codex_protocol::request_permissions::RequestPermissionProfile::from)
.map_err(FunctionCallError::RespondToModel)?;
if args.permissions.is_empty() {
return Err(FunctionCallError::RespondToModel(
"request_permissions requires at least one permission".to_string(),
@@ -123,3 +131,83 @@ impl RequestPermissionsHandler {
}
impl CoreToolRuntime for RequestPermissionsHandler {}
fn resolve_permission_path_strings(
arguments: &mut Value,
context: &FileSystemSandboxPolicyContext<'_>,
) -> Result<(), FunctionCallError> {
let Some(file_system) = arguments
.pointer_mut("/permissions/file_system")
.and_then(Value::as_object_mut)
else {
return Ok(());
};
let mut legacy_entries = Vec::new();
for (field, access) in [("read", "read"), ("write", "write")] {
if let Some(paths) = file_system.remove(field) {
if paths.is_null() {
continue;
}
let Value::Array(paths) = paths else {
file_system.insert(field.to_string(), paths);
continue;
};
for mut path in paths {
resolve_permission_path_string(&mut path, context)?;
legacy_entries.push(serde_json::json!({
"path": {"type": "path", "path": path},
"access": access,
}));
}
}
}
if !legacy_entries.is_empty() {
file_system
.entry("entries".to_string())
.or_insert_with(|| Value::Array(Vec::new()))
.as_array_mut()
.ok_or_else(|| {
FunctionCallError::RespondToModel(
"request_permissions file_system.entries must be an array".to_string(),
)
})?
.extend(legacy_entries);
}
if let Some(entries) = file_system.get_mut("entries").and_then(Value::as_array_mut) {
for entry in entries {
if entry.pointer("/path/type").and_then(Value::as_str) == Some("path")
&& let Some(path) = entry.pointer_mut("/path/path")
{
resolve_permission_path_string(path, context)?;
}
}
}
Ok(())
}
fn resolve_permission_path_string(
value: &mut Value,
context: &FileSystemSandboxPolicyContext<'_>,
) -> Result<(), FunctionCallError> {
let Some(path) = value.as_str() else {
return Ok(());
};
let cwd = context.cwd;
let convention = cwd.infer_path_convention().ok_or_else(|| {
FunctionCallError::RespondToModel(format!(
"request_permissions cwd `{cwd}` has no path convention"
))
})?;
let path = LegacyAppPathString::from_string(path)
.resolve_against(cwd, context.user_home_dir)
.map_err(|err| FunctionCallError::RespondToModel(err.to_string()))?;
if path.is_opaque() || std::str::from_utf8(&path.decoded_path_bytes()).is_err() {
return Err(FunctionCallError::RespondToModel(
"permission path cannot be represented losslessly".to_string(),
));
}
let path = LegacyAppPathString::from_path_uri(&path, convention)
.map_err(|err| FunctionCallError::RespondToModel(err.to_string()))?;
*value = Value::String(path.into_string());
Ok(())
}