mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Preserve sandbox writable roots across client and server platforms (#62376)
## Why A TUI connected to an app server on a different operating system could fail to decode a workspace-write policy. For example, a Unix client could reject a Windows writable root such as `C:\extra` because it is relative under Unix path rules. ## What changed Keep `writableRoots` as path strings on the wire. Use path URIs to preserve server paths when displaying thread permissions, and forward the original sandbox policy to the first turn of a child thread that inherits a legacy policy. The app server validates that each writable root is absolute on its host before enforcing a policy supplied to `command/exec` or `turn/start`. ## Testing Cover writable roots from another platform in permission profile templates, their round trip through thread lifecycle responses, and rejection of home-relative roots during enforcement conversion. GitOrigin-RevId: 57365b977bfced6c6ee864a0e54f25e8f9d09981
This commit is contained in:
@@ -556,7 +556,9 @@ pub enum SandboxPolicy {
|
||||
#[ts(rename_all = "camelCase")]
|
||||
WorkspaceWrite {
|
||||
#[serde(default)]
|
||||
writable_roots: Vec<AbsolutePathBuf>,
|
||||
#[schemars(with = "Vec<AbsolutePathBuf>")]
|
||||
#[ts(as = "Vec<AbsolutePathBuf>")]
|
||||
writable_roots: Vec<LegacyAppPathString>,
|
||||
#[serde(default)]
|
||||
network_access: bool,
|
||||
#[serde(default)]
|
||||
@@ -585,7 +587,7 @@ enum SandboxPolicyDeserialize {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
WorkspaceWrite {
|
||||
#[serde(default)]
|
||||
writable_roots: Vec<AbsolutePathBuf>,
|
||||
writable_roots: Vec<LegacyAppPathString>,
|
||||
#[serde(default)]
|
||||
read_only_access: Option<LegacyReadOnlyAccess>,
|
||||
#[serde(default)]
|
||||
@@ -649,8 +651,67 @@ impl<'de> Deserialize<'de> for SandboxPolicy {
|
||||
}
|
||||
|
||||
impl SandboxPolicy {
|
||||
pub fn to_core(&self) -> codex_protocol::protocol::SandboxPolicy {
|
||||
match self {
|
||||
/// Projects a server policy without resolving cwd-dependent permissions on the client.
|
||||
pub fn permission_profile_template(
|
||||
&self,
|
||||
) -> io::Result<codex_protocol::models::PermissionProfile> {
|
||||
self.permission_profile(/*cwd*/ None)
|
||||
}
|
||||
|
||||
/// Projects the server policy into a permission profile for display in a client.
|
||||
pub fn permission_profile_for_cwd(
|
||||
&self,
|
||||
cwd: &PathUri,
|
||||
) -> io::Result<codex_protocol::models::PermissionProfile> {
|
||||
self.permission_profile(Some(cwd))
|
||||
}
|
||||
|
||||
fn permission_profile(
|
||||
&self,
|
||||
cwd: Option<&PathUri>,
|
||||
) -> io::Result<codex_protocol::models::PermissionProfile> {
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
if let Self::WorkspaceWrite {
|
||||
writable_roots,
|
||||
network_access,
|
||||
exclude_tmpdir_env_var,
|
||||
exclude_slash_tmp,
|
||||
} = self
|
||||
{
|
||||
let roots = writable_roots
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(PathUri::try_from)
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(io::Error::other)?;
|
||||
if cwd.is_none_or(|cwd| cwd.to_abs_path().is_err())
|
||||
|| roots.iter().any(|root| root.to_abs_path().is_err())
|
||||
{
|
||||
return Ok(PermissionProfile::workspace_write_with_path_uris(
|
||||
&roots,
|
||||
if *network_access {
|
||||
NetworkSandboxPolicy::Enabled
|
||||
} else {
|
||||
NetworkSandboxPolicy::Restricted
|
||||
},
|
||||
*exclude_tmpdir_env_var,
|
||||
*exclude_slash_tmp,
|
||||
));
|
||||
}
|
||||
}
|
||||
let policy = self.try_to_core()?;
|
||||
Ok(match cwd {
|
||||
Some(cwd) => {
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(&policy, &cwd.to_path_buf())
|
||||
}
|
||||
None => PermissionProfile::from_legacy_sandbox_policy_template(&policy),
|
||||
})
|
||||
}
|
||||
|
||||
/// Converts the policy for enforcement on this host, validating every writable root.
|
||||
pub fn try_to_core(&self) -> io::Result<codex_protocol::protocol::SandboxPolicy> {
|
||||
Ok(match self {
|
||||
SandboxPolicy::DangerFullAccess => {
|
||||
codex_protocol::protocol::SandboxPolicy::DangerFullAccess
|
||||
}
|
||||
@@ -673,12 +734,24 @@ impl SandboxPolicy {
|
||||
exclude_tmpdir_env_var,
|
||||
exclude_slash_tmp,
|
||||
} => codex_protocol::protocol::SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: writable_roots.clone(),
|
||||
writable_roots: writable_roots
|
||||
.iter()
|
||||
.map(|root| {
|
||||
let path = root.as_path();
|
||||
if !path.is_absolute() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
format!("writable root is not absolute: {}", path.display()),
|
||||
));
|
||||
}
|
||||
AbsolutePathBuf::from_absolute_path_checked(path)
|
||||
})
|
||||
.collect::<io::Result<_>>()?,
|
||||
network_access: *network_access,
|
||||
exclude_tmpdir_env_var: *exclude_tmpdir_env_var,
|
||||
exclude_slash_tmp: *exclude_slash_tmp,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -705,7 +778,7 @@ impl From<codex_protocol::protocol::SandboxPolicy> for SandboxPolicy {
|
||||
exclude_tmpdir_env_var,
|
||||
exclude_slash_tmp,
|
||||
} => SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots,
|
||||
writable_roots: writable_roots.into_iter().map(Into::into).collect(),
|
||||
network_access,
|
||||
exclude_tmpdir_env_var,
|
||||
exclude_slash_tmp,
|
||||
|
||||
@@ -2015,7 +2015,7 @@ fn sandbox_policy_round_trips_external_sandbox_network_access() {
|
||||
network_access: NetworkAccess::Enabled,
|
||||
};
|
||||
|
||||
let core_policy = v2_policy.to_core();
|
||||
let core_policy = v2_policy.try_to_core().unwrap();
|
||||
assert_eq!(
|
||||
core_policy,
|
||||
codex_protocol::protocol::SandboxPolicy::ExternalSandbox {
|
||||
@@ -2033,7 +2033,7 @@ fn sandbox_policy_round_trips_read_only_network_access() {
|
||||
network_access: true,
|
||||
};
|
||||
|
||||
let core_policy = v2_policy.to_core();
|
||||
let core_policy = v2_policy.try_to_core().unwrap();
|
||||
assert_eq!(
|
||||
core_policy,
|
||||
codex_protocol::protocol::SandboxPolicy::ReadOnly {
|
||||
@@ -2889,13 +2889,13 @@ fn sandbox_policy_round_trips_workspace_write_access() {
|
||||
let root = AbsolutePathBuf::from_absolute_path(directory.path()).expect("absolute root");
|
||||
std::fs::create_dir(root.join(".git")).expect("git metadata");
|
||||
let v2_policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![root.clone(), root.clone()],
|
||||
writable_roots: vec![root.clone().into(), root.clone().into()],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
};
|
||||
|
||||
let core_policy = v2_policy.to_core();
|
||||
let core_policy = v2_policy.try_to_core().unwrap();
|
||||
assert_eq!(
|
||||
core_policy,
|
||||
codex_protocol::protocol::SandboxPolicy::WorkspaceWrite {
|
||||
@@ -2907,7 +2907,7 @@ fn sandbox_policy_round_trips_workspace_write_access() {
|
||||
);
|
||||
|
||||
let profile = codex_protocol::models::PermissionProfile::from_legacy_sandbox_policy_template(
|
||||
&v2_policy.to_core(),
|
||||
&core_policy,
|
||||
);
|
||||
assert!(
|
||||
!profile
|
||||
@@ -2936,6 +2936,43 @@ fn sandbox_policy_round_trips_workspace_write_access() {
|
||||
assert_eq!(back_to_v2, v2_policy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_policy_template_preserves_foreign_writable_roots() {
|
||||
let root = if cfg!(windows) { "/extra" } else { r"C:\extra" };
|
||||
let policy: SandboxPolicy = serde_json::from_value(json!({
|
||||
"type": "workspaceWrite",
|
||||
"writableRoots": [root],
|
||||
}))
|
||||
.expect("workspace-write policy");
|
||||
let root = PathUri::try_from(LegacyAppPathString::from_string(root)).expect("foreign root");
|
||||
let file_system = policy
|
||||
.permission_profile_template()
|
||||
.expect("permission profile template")
|
||||
.file_system_sandbox_policy();
|
||||
assert!(file_system.entries.iter().any(|entry| {
|
||||
entry.access.can_write()
|
||||
&& matches!(&entry.path, codex_protocol::permissions::FileSystemPath::Path { path } if path == &root)
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_policy_rejects_home_relative_writable_roots_for_enforcement() {
|
||||
let policy: SandboxPolicy = serde_json::from_value(json!({
|
||||
"type": "workspaceWrite",
|
||||
"writableRoots": ["~/extra"],
|
||||
}))
|
||||
.expect("workspace-write policy");
|
||||
let home = std::env::current_dir().expect("current directory");
|
||||
let result =
|
||||
codex_utils_absolute_path::AbsolutePathBufGuard::with_home_directory(&home, || {
|
||||
policy.try_to_core()
|
||||
});
|
||||
assert_eq!(
|
||||
result.expect_err("writable roots must be absolute").kind(),
|
||||
std::io::ErrorKind::InvalidInput
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_policy_deserializes_legacy_read_only_full_access_field() {
|
||||
let policy = serde_json::from_value::<SandboxPolicy>(json!({
|
||||
@@ -2971,7 +3008,7 @@ fn sandbox_policy_deserializes_legacy_workspace_write_full_access_field() {
|
||||
assert_eq!(
|
||||
policy,
|
||||
SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![absolute_path("/workspace")],
|
||||
writable_roots: vec![absolute_path("/workspace").into()],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
@@ -5194,7 +5231,7 @@ fn thread_lifecycle_responses_default_missing_optional_fields() {
|
||||
"cwd": cwd,
|
||||
"approvalPolicy": "on-request",
|
||||
"approvalsReviewer": "user",
|
||||
"sandbox": { "type": "dangerFullAccess" },
|
||||
"sandbox": { "type": "workspaceWrite", "writableRoots": [cwd] },
|
||||
"reasoningEffort": null
|
||||
});
|
||||
|
||||
@@ -5208,6 +5245,7 @@ fn thread_lifecycle_responses_default_missing_optional_fields() {
|
||||
for encoded in [json!(start), json!(resume), json!(fork)] {
|
||||
assert_eq!(encoded["thread"]["cwd"], cwd);
|
||||
assert_eq!(encoded["cwd"], cwd);
|
||||
assert_eq!(encoded["sandbox"]["writableRoots"], json!([cwd]));
|
||||
}
|
||||
assert_eq!(start.instruction_sources, Vec::<LegacyAppPathString>::new());
|
||||
assert_eq!(start.thread.parent_thread_id, None);
|
||||
|
||||
Reference in New Issue
Block a user