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);
|
||||
|
||||
@@ -239,7 +239,11 @@ impl CommandExecRequestProcessor {
|
||||
config.managed_network_requirements_enabled(),
|
||||
config.effective_workspace_roots(),
|
||||
)
|
||||
} else if let Some(policy) = sandbox_policy.map(|policy| policy.to_core()) {
|
||||
} else if let Some(policy) = sandbox_policy
|
||||
.map(|policy| policy.try_to_core())
|
||||
.transpose()
|
||||
.map_err(|err| invalid_request(format!("invalid sandbox policy: {err}")))?
|
||||
{
|
||||
let mut permissions = self.config.permissions.clone();
|
||||
permissions
|
||||
.set_legacy_sandbox_policy(policy, &sandbox_cwd)
|
||||
|
||||
@@ -919,7 +919,10 @@ impl TurnRequestProcessor {
|
||||
approval_policy.map(codex_app_server_protocol::AskForApproval::to_core);
|
||||
let approvals_reviewer =
|
||||
approvals_reviewer.map(codex_app_server_protocol::ApprovalsReviewer::to_core);
|
||||
let sandbox_policy = sandbox_policy.map(|policy| policy.to_core());
|
||||
let sandbox_policy = sandbox_policy
|
||||
.map(|policy| policy.try_to_core())
|
||||
.transpose()
|
||||
.map_err(|err| invalid_request(format!("invalid sandboxPolicy: {err}")))?;
|
||||
let (permission_profile, active_permission_profile, profile_workspace_roots) =
|
||||
if let Some(permissions) = permissions {
|
||||
let Some(snapshot) = snapshot.as_ref() else {
|
||||
|
||||
@@ -20,6 +20,7 @@ use codex_core::exec_env::CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR;
|
||||
use codex_exec_server::CODEX_EXEC_SERVER_URL_ENV_VAR;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_READ_ONLY;
|
||||
use codex_protocol::shell_environment::OPENAI_FEDERATION_RULE_ID_ENV_VAR;
|
||||
use codex_utils_path_uri::LegacyAppPathString;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
@@ -380,7 +381,7 @@ async fn command_exec_enforces_managed_deny_read_requirements() -> Result<()> {
|
||||
for sandbox_policy in [
|
||||
SandboxPolicy::DangerFullAccess,
|
||||
SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![nested_root.try_into()?],
|
||||
writable_roots: vec![LegacyAppPathString::from_path(&nested_root)],
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
|
||||
@@ -585,7 +585,7 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
|
||||
panic!("expected workspace-write sandbox");
|
||||
};
|
||||
assert!(
|
||||
writable_roots.contains(&extra_root.abs().canonicalize()?),
|
||||
writable_roots.contains(&extra_root.abs().canonicalize()?.into()),
|
||||
"legacy sandbox projection should include the runtime workspace root"
|
||||
);
|
||||
}
|
||||
@@ -616,7 +616,7 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
|
||||
panic!("expected workspace-write sandbox");
|
||||
};
|
||||
assert!(
|
||||
writable_roots.contains(&environment_root.abs().canonicalize()?),
|
||||
writable_roots.contains(&environment_root.abs().canonicalize()?.into()),
|
||||
"legacy sandbox projection should include the environment workspace root"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3534,7 +3534,7 @@ async fn turn_start_explicit_local_environment_updates_legacy_cwd_between_turns(
|
||||
approval_policy: Some(codex_app_server_protocol::AskForApproval::Never),
|
||||
approvals_reviewer: None,
|
||||
sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![first_writable_root],
|
||||
writable_roots: vec![first_writable_root.into()],
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
|
||||
@@ -294,10 +294,10 @@ async fn custom_permission_selection_scenario() -> Result<()> {
|
||||
before
|
||||
);
|
||||
let settings = next_thread_settings_updated(&mut server, thread_id).await;
|
||||
let expected = PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&settings.thread_settings.sandbox_policy.to_core(),
|
||||
settings.thread_settings.cwd.as_path(),
|
||||
);
|
||||
let expected = settings
|
||||
.thread_settings
|
||||
.sandbox_policy
|
||||
.permission_profile_for_cwd(&settings.thread_settings.cwd.clone().try_into()?)?;
|
||||
assert!(matches!(
|
||||
settings.thread_settings.sandbox_policy,
|
||||
codex_app_server_protocol::SandboxPolicy::WorkspaceWrite { .. }
|
||||
|
||||
@@ -284,6 +284,13 @@ pub(super) fn apply_thread_settings_to_session(
|
||||
settings: &ThreadSettings,
|
||||
cwd: &codex_utils_path_uri::PathUri,
|
||||
) {
|
||||
let permission_profile = match settings.sandbox_policy.permission_profile_for_cwd(cwd) {
|
||||
Ok(profile) => profile,
|
||||
Err(err) => {
|
||||
tracing::warn!(%err, "Invalid sandbox policy in thread settings");
|
||||
return;
|
||||
}
|
||||
};
|
||||
if settings.collaboration_mode.mode == ModeKind::Default {
|
||||
session.model = settings.model.clone();
|
||||
session.reasoning_effort = settings.effort.clone();
|
||||
@@ -292,10 +299,7 @@ pub(super) fn apply_thread_settings_to_session(
|
||||
session.service_tier = settings.service_tier.clone();
|
||||
session.approval_policy = settings.approval_policy;
|
||||
session.approvals_reviewer = settings.approvals_reviewer.to_core();
|
||||
session.permission_profile = PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&settings.sandbox_policy.to_core(),
|
||||
settings.cwd.as_path(),
|
||||
);
|
||||
session.permission_profile = permission_profile;
|
||||
session.active_permission_profile = settings.active_permission_profile.clone().map(Into::into);
|
||||
if let Some(environments) = &settings.environments {
|
||||
session.primary_environment_cwd =
|
||||
|
||||
@@ -1245,18 +1245,16 @@ impl AppServerSession {
|
||||
self.history_support = ThreadHistorySupport::LegacyOnly;
|
||||
}
|
||||
// An explicit server selection must not reuse the client's local display policy.
|
||||
let selected_permissions = selected_profile.map(|_| {
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&response.sandbox.to_core(),
|
||||
response.cwd.as_path(),
|
||||
)
|
||||
});
|
||||
let cwd = decode_thread_cwd(&response.cwd, self.path_convention())?;
|
||||
let selected_permissions = selected_profile
|
||||
.map(|_| response.sandbox.permission_profile_for_cwd(&cwd))
|
||||
.transpose()?;
|
||||
let mut started = started_thread_from_start_response(
|
||||
response,
|
||||
local_settings,
|
||||
config,
|
||||
self.thread_params_mode(),
|
||||
self.path_convention(),
|
||||
cwd,
|
||||
)
|
||||
.await?;
|
||||
if let Some(permissions) = selected_permissions {
|
||||
@@ -1623,18 +1621,16 @@ impl AppServerSession {
|
||||
);
|
||||
}
|
||||
// Explicit selections use the server's effective policy, including on local daemons.
|
||||
let selected_permissions = selected_profile.map(|_| {
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&response.sandbox.to_core(),
|
||||
response.cwd.as_path(),
|
||||
)
|
||||
});
|
||||
let cwd = decode_thread_cwd(&response.cwd, self.path_convention())?;
|
||||
let selected_permissions = selected_profile
|
||||
.map(|_| response.sandbox.permission_profile_for_cwd(&cwd))
|
||||
.transpose()?;
|
||||
let mut started = started_thread_from_fork_response(
|
||||
response,
|
||||
local_settings,
|
||||
&config,
|
||||
self.thread_params_mode(),
|
||||
self.path_convention(),
|
||||
cwd,
|
||||
)
|
||||
.await?;
|
||||
if let Some(permissions) = selected_permissions {
|
||||
@@ -2415,12 +2411,14 @@ pub(crate) async fn start_thread_with_request_handle(
|
||||
.map_err(|err| {
|
||||
bootstrap_request_error("thread/start failed during TUI bootstrap", err)
|
||||
})?;
|
||||
let convention = Platform::from_platform_os(request_handle.platform_os()).path_convention();
|
||||
let cwd = decode_thread_cwd(&response.cwd, convention)?;
|
||||
let mut started = started_thread_from_start_response(
|
||||
response,
|
||||
local_settings,
|
||||
&config,
|
||||
thread_params_mode,
|
||||
Platform::from_platform_os(request_handle.platform_os()).path_convention(),
|
||||
cwd,
|
||||
)
|
||||
.await?;
|
||||
started.task_tools_available = task_tools_available;
|
||||
@@ -2698,6 +2696,61 @@ fn permission_profile_id_from_active_profile(active: ActivePermissionProfile) ->
|
||||
active.id
|
||||
}
|
||||
|
||||
/// Inherits settings for a child thread and prepares its first turn's permission override.
|
||||
pub(crate) fn inherit_thread_start_settings(
|
||||
thread_start: &mut ThreadStartParams,
|
||||
source: ThreadResumeResponse,
|
||||
) -> Result<TurnStartParams, String> {
|
||||
use codex_app_server_protocol::SandboxMode;
|
||||
|
||||
thread_start.model = Some(source.model);
|
||||
thread_start.service_tier = Some(source.service_tier);
|
||||
thread_start.runtime_workspace_roots = Some(source.runtime_workspace_roots);
|
||||
thread_start.approval_policy = Some(source.approval_policy);
|
||||
thread_start.approvals_reviewer = Some(source.approvals_reviewer);
|
||||
thread_start.permissions = source.active_permission_profile.map(|profile| profile.id);
|
||||
if thread_start.permissions.is_some() {
|
||||
thread_start.sandbox = None;
|
||||
return Ok(TurnStartParams::default());
|
||||
}
|
||||
let source_permissions = source
|
||||
.sandbox
|
||||
.permission_profile_template()
|
||||
.map_err(|err| err.to_string())?;
|
||||
thread_start.sandbox = Some(match &source_permissions {
|
||||
PermissionProfile::Disabled => SandboxMode::DangerFullAccess,
|
||||
PermissionProfile::Managed { .. } => {
|
||||
if source_permissions
|
||||
.file_system_sandbox_policy()
|
||||
.entries
|
||||
.iter()
|
||||
.any(|entry| entry.access.can_write())
|
||||
{
|
||||
SandboxMode::WorkspaceWrite
|
||||
} else {
|
||||
SandboxMode::ReadOnly
|
||||
}
|
||||
}
|
||||
PermissionProfile::External { .. } => {
|
||||
return Err(
|
||||
"Cannot inherit an external sandbox without a permission profile".to_string(),
|
||||
);
|
||||
}
|
||||
});
|
||||
Ok(TurnStartParams {
|
||||
// Forward the server's writable roots in their original path convention.
|
||||
sandbox_policy: Some(source.sandbox),
|
||||
..TurnStartParams::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn thread_started_with_read_only_sandbox(response: &ThreadStartResponse) -> bool {
|
||||
matches!(
|
||||
response.sandbox,
|
||||
codex_app_server_protocol::SandboxPolicy::ReadOnly { .. }
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn turn_permissions_overrides(
|
||||
permissions_override: TurnPermissionsOverride,
|
||||
cwd: &std::path::Path,
|
||||
@@ -2968,7 +3021,7 @@ async fn started_thread_from_start_response(
|
||||
local_settings: &LocalSettings,
|
||||
config: &Config,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
cwd_convention: Option<PathConvention>,
|
||||
cwd: PathUri,
|
||||
) -> Result<AppServerStartedThread> {
|
||||
let blocks_direct_input = thread_blocks_direct_input(&response.thread);
|
||||
let session = thread_session_state_from_thread_start_response(
|
||||
@@ -2976,7 +3029,7 @@ async fn started_thread_from_start_response(
|
||||
local_settings,
|
||||
config,
|
||||
thread_params_mode,
|
||||
cwd_convention,
|
||||
cwd,
|
||||
)
|
||||
.await
|
||||
.map_err(color_eyre::eyre::Report::msg)?;
|
||||
@@ -3011,7 +3064,7 @@ async fn started_thread_from_fork_response(
|
||||
local_settings: &LocalSettings,
|
||||
config: &Config,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
cwd_convention: Option<PathConvention>,
|
||||
cwd: PathUri,
|
||||
) -> Result<AppServerStartedThread> {
|
||||
let blocks_direct_input = thread_blocks_direct_input(&response.thread);
|
||||
let session = thread_session_state_from_thread_fork_response(
|
||||
@@ -3019,7 +3072,7 @@ async fn started_thread_from_fork_response(
|
||||
local_settings,
|
||||
config,
|
||||
thread_params_mode,
|
||||
cwd_convention,
|
||||
cwd,
|
||||
)
|
||||
.await
|
||||
.map_err(color_eyre::eyre::Report::msg)?;
|
||||
@@ -3036,14 +3089,14 @@ async fn thread_session_state_from_thread_start_response(
|
||||
local_settings: &LocalSettings,
|
||||
config: &Config,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
cwd_convention: Option<PathConvention>,
|
||||
cwd: PathUri,
|
||||
) -> Result<ThreadSessionState, String> {
|
||||
let permission_profile = display_permission_profile_from_thread_response(
|
||||
&response.sandbox,
|
||||
response.cwd.as_path(),
|
||||
&cwd,
|
||||
config,
|
||||
thread_params_mode,
|
||||
);
|
||||
)?;
|
||||
let mut session = thread_session_state_from_thread_response(
|
||||
&response.thread.id,
|
||||
response.thread.environments.as_deref(),
|
||||
@@ -3057,7 +3110,7 @@ async fn thread_session_state_from_thread_start_response(
|
||||
response.approvals_reviewer.to_core(),
|
||||
permission_profile,
|
||||
response.active_permission_profile.clone().map(Into::into),
|
||||
decode_thread_cwd(&response.cwd, cwd_convention).map_err(|err| err.to_string())?,
|
||||
cwd,
|
||||
response.runtime_workspace_roots.clone(),
|
||||
response.instruction_source_path_uris(),
|
||||
response.reasoning_effort.clone(),
|
||||
@@ -3078,10 +3131,11 @@ async fn thread_session_state_from_thread_resume_response(
|
||||
) -> Result<ThreadSessionState, String> {
|
||||
// The server owns restored rules. This legacy projection is for display only;
|
||||
// normal turns do not send it back as an execution override.
|
||||
let permission_profile = PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&response.sandbox.to_core(),
|
||||
response.cwd.as_path(),
|
||||
);
|
||||
let cwd = decode_thread_cwd(&response.cwd, cwd_convention).map_err(|err| err.to_string())?;
|
||||
let permission_profile = response
|
||||
.sandbox
|
||||
.permission_profile_for_cwd(&cwd)
|
||||
.map_err(|err| err.to_string())?;
|
||||
let mut session = thread_session_state_from_thread_response(
|
||||
&response.thread.id,
|
||||
response.thread.environments.as_deref(),
|
||||
@@ -3095,7 +3149,7 @@ async fn thread_session_state_from_thread_resume_response(
|
||||
response.approvals_reviewer.to_core(),
|
||||
permission_profile,
|
||||
response.active_permission_profile.clone().map(Into::into),
|
||||
decode_thread_cwd(&response.cwd, cwd_convention).map_err(|err| err.to_string())?,
|
||||
cwd,
|
||||
response.runtime_workspace_roots.clone(),
|
||||
response.instruction_source_path_uris(),
|
||||
response.reasoning_effort.clone(),
|
||||
@@ -3112,14 +3166,14 @@ async fn thread_session_state_from_thread_fork_response(
|
||||
local_settings: &LocalSettings,
|
||||
config: &Config,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
cwd_convention: Option<PathConvention>,
|
||||
cwd: PathUri,
|
||||
) -> Result<ThreadSessionState, String> {
|
||||
let permission_profile = display_permission_profile_from_thread_response(
|
||||
&response.sandbox,
|
||||
response.cwd.as_path(),
|
||||
&cwd,
|
||||
config,
|
||||
thread_params_mode,
|
||||
);
|
||||
)?;
|
||||
let mut session = thread_session_state_from_thread_response(
|
||||
&response.thread.id,
|
||||
response.thread.environments.as_deref(),
|
||||
@@ -3133,7 +3187,7 @@ async fn thread_session_state_from_thread_fork_response(
|
||||
response.approvals_reviewer.to_core(),
|
||||
permission_profile,
|
||||
response.active_permission_profile.clone().map(Into::into),
|
||||
decode_thread_cwd(&response.cwd, cwd_convention).map_err(|err| err.to_string())?,
|
||||
cwd,
|
||||
response.runtime_workspace_roots.clone(),
|
||||
response.instruction_source_path_uris(),
|
||||
response.reasoning_effort.clone(),
|
||||
@@ -3146,10 +3200,10 @@ async fn thread_session_state_from_thread_fork_response(
|
||||
|
||||
fn display_permission_profile_from_thread_response(
|
||||
sandbox: &codex_app_server_protocol::SandboxPolicy,
|
||||
cwd: &std::path::Path,
|
||||
cwd: &PathUri,
|
||||
config: &Config,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
) -> PermissionProfile {
|
||||
) -> Result<PermissionProfile, String> {
|
||||
match thread_params_mode {
|
||||
ThreadParamsMode::Embedded
|
||||
if matches!(
|
||||
@@ -3160,12 +3214,14 @@ fn display_permission_profile_from_thread_response(
|
||||
codex_app_server_protocol::SandboxPolicy::DangerFullAccess
|
||||
) =>
|
||||
{
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(&sandbox.to_core(), cwd)
|
||||
}
|
||||
ThreadParamsMode::Embedded => config.permissions.effective_permission_profile(),
|
||||
ThreadParamsMode::Remote | ThreadParamsMode::Cloud => {
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(&sandbox.to_core(), cwd)
|
||||
sandbox
|
||||
.permission_profile_for_cwd(cwd)
|
||||
.map_err(|err| err.to_string())
|
||||
}
|
||||
ThreadParamsMode::Embedded => Ok(config.permissions.effective_permission_profile()),
|
||||
ThreadParamsMode::Remote | ThreadParamsMode::Cloud => sandbox
|
||||
.permission_profile_for_cwd(cwd)
|
||||
.map_err(|err| err.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5395,10 +5451,11 @@ mod tests {
|
||||
assert_eq!(
|
||||
display_permission_profile_from_thread_response(
|
||||
&sandbox,
|
||||
cwd.as_path(),
|
||||
&PathUri::from_abs_path(&cwd),
|
||||
&config,
|
||||
ThreadParamsMode::Remote,
|
||||
),
|
||||
)
|
||||
.unwrap(),
|
||||
PermissionProfile::read_only()
|
||||
);
|
||||
}
|
||||
@@ -5420,10 +5477,11 @@ mod tests {
|
||||
assert_eq!(
|
||||
display_permission_profile_from_thread_response(
|
||||
&codex_app_server_protocol::SandboxPolicy::DangerFullAccess,
|
||||
cwd.as_path(),
|
||||
&PathUri::from_abs_path(&cwd),
|
||||
&config,
|
||||
ThreadParamsMode::Embedded,
|
||||
),
|
||||
)
|
||||
.unwrap(),
|
||||
PermissionProfile::read_only()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -545,6 +545,13 @@ impl ChatWidget {
|
||||
tracing::warn!("Invalid cwd in thread settings");
|
||||
return;
|
||||
};
|
||||
let permission_profile = match settings.sandbox_policy.permission_profile_for_cwd(&cwd) {
|
||||
Ok(profile) => profile,
|
||||
Err(err) => {
|
||||
tracing::warn!(%err, "Invalid sandbox policy in thread settings");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let cwd_changed = self.current_cwd != cwd;
|
||||
self.status_line_project_root_name_cache = None;
|
||||
if let Ok(local) = cwd.to_abs_path() {
|
||||
@@ -566,10 +573,6 @@ impl ChatWidget {
|
||||
}
|
||||
self.set_approvals_reviewer(settings.approvals_reviewer.to_core());
|
||||
|
||||
let permission_profile = PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&settings.sandbox_policy.to_core(),
|
||||
settings.cwd.as_path(),
|
||||
);
|
||||
let permission_snapshot = PermissionProfileSnapshot::from_session_snapshot(
|
||||
permission_profile,
|
||||
settings.active_permission_profile.take().map(Into::into),
|
||||
|
||||
@@ -21,7 +21,6 @@ use codex_app_server_protocol::ImageReference;
|
||||
use codex_app_server_protocol::ModelListParams;
|
||||
use codex_app_server_protocol::ModelListResponse;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::SandboxMode;
|
||||
use codex_app_server_protocol::SortDirection;
|
||||
use codex_app_server_protocol::Thread;
|
||||
use codex_app_server_protocol::ThreadArchiveParams;
|
||||
@@ -59,7 +58,6 @@ use codex_features::Feature;
|
||||
use codex_features::Features;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::FunctionCallOutputBody;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use serde::Deserialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::Value;
|
||||
@@ -574,43 +572,10 @@ async fn execute_inner(
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
thread_start_params.model = Some(source.model);
|
||||
thread_start_params.service_tier = Some(source.service_tier);
|
||||
thread_start_params.runtime_workspace_roots = Some(source.runtime_workspace_roots);
|
||||
thread_start_params.approval_policy = Some(source.approval_policy);
|
||||
thread_start_params.approvals_reviewer = Some(source.approvals_reviewer);
|
||||
let source_permissions =
|
||||
PermissionProfile::from_legacy_sandbox_policy_template(&source.sandbox.to_core());
|
||||
let permission_profile_override =
|
||||
if let Some(profile) = source.active_permission_profile {
|
||||
thread_start_params.permissions = Some(profile.id);
|
||||
thread_start_params.sandbox = None;
|
||||
None
|
||||
} else {
|
||||
thread_start_params.permissions = None;
|
||||
thread_start_params.sandbox = Some(match &source_permissions {
|
||||
PermissionProfile::Disabled => SandboxMode::DangerFullAccess,
|
||||
PermissionProfile::Managed { .. } => {
|
||||
if source_permissions
|
||||
.file_system_sandbox_policy()
|
||||
.entries
|
||||
.iter()
|
||||
.any(|entry| entry.access.can_write())
|
||||
{
|
||||
SandboxMode::WorkspaceWrite
|
||||
} else {
|
||||
SandboxMode::ReadOnly
|
||||
}
|
||||
}
|
||||
PermissionProfile::External { .. } => {
|
||||
return Err(
|
||||
"Cannot inherit an external sandbox without a permission profile"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
});
|
||||
Some(source_permissions)
|
||||
};
|
||||
let turn_start_params = crate::app_server_session::inherit_thread_start_settings(
|
||||
&mut thread_start_params,
|
||||
source,
|
||||
)?;
|
||||
if let Some(model) = arguments.model {
|
||||
thread_start_params.model = Some(model);
|
||||
}
|
||||
@@ -654,7 +619,7 @@ async fn execute_inner(
|
||||
"create_thread",
|
||||
prompt,
|
||||
/*model*/ None,
|
||||
permission_profile_override,
|
||||
turn_start_params,
|
||||
cyber_access_program,
|
||||
)
|
||||
.await?;
|
||||
@@ -792,7 +757,7 @@ async fn execute_inner(
|
||||
"send_message_to_thread",
|
||||
prompt,
|
||||
arguments.model,
|
||||
/*permission_profile_override*/ None,
|
||||
TurnStartParams::default(),
|
||||
cyber_access_program,
|
||||
)
|
||||
.await?;
|
||||
@@ -1300,13 +1265,9 @@ async fn start_turn(
|
||||
tool: &str,
|
||||
prompt: String,
|
||||
model: Option<String>,
|
||||
permission_profile_override: Option<PermissionProfile>,
|
||||
turn_start_params: TurnStartParams,
|
||||
cyber_access_program: Option<codex_app_server_protocol::CyberAccessProgram>,
|
||||
) -> Result<TurnStartResponse, String> {
|
||||
let sandbox_policy = permission_profile_override
|
||||
.map(|profile| profile.to_legacy_sandbox_policy_template().map(Into::into))
|
||||
.transpose()
|
||||
.map_err(|error| error.to_string())?;
|
||||
request(handle, |request_id| ClientRequest::TurnStart {
|
||||
request_id,
|
||||
params: TurnStartParams {
|
||||
@@ -1321,9 +1282,8 @@ async fn start_turn(
|
||||
output: FunctionCallOutputBody::Text(prompt),
|
||||
})),
|
||||
model,
|
||||
sandbox_policy,
|
||||
cyber_access_program,
|
||||
..TurnStartParams::default()
|
||||
..turn_start_params
|
||||
},
|
||||
})
|
||||
.await
|
||||
|
||||
@@ -815,7 +815,8 @@ async fn task_creation_and_followup_start_background_turns() -> color_eyre::Resu
|
||||
|
||||
let inherited_sandbox = codex_app_server_protocol::SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![
|
||||
codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(codex_home.path())?,
|
||||
codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(codex_home.path())?
|
||||
.into(),
|
||||
],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: true,
|
||||
|
||||
@@ -19,7 +19,6 @@ use codex_app_server_protocol::TurnStartResponse;
|
||||
use codex_app_server_protocol::TurnStatus;
|
||||
use codex_app_server_protocol::UserInput;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_READ_ONLY;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use color_eyre::eyre::eyre;
|
||||
use serde_json::Value;
|
||||
@@ -163,21 +162,11 @@ pub(crate) async fn start_temporary_thread(
|
||||
"temporary structured thread did not preserve permission profile {expected_profile}"
|
||||
));
|
||||
}
|
||||
} else {
|
||||
let profile =
|
||||
PermissionProfile::from_legacy_sandbox_policy_template(&response.sandbox.to_core());
|
||||
if !matches!(profile, PermissionProfile::Managed { .. })
|
||||
|| profile
|
||||
.file_system_sandbox_policy()
|
||||
.entries
|
||||
.iter()
|
||||
.any(|entry| entry.access.can_write())
|
||||
{
|
||||
unsubscribe_temporary_thread(request_handle, response.thread.id).await;
|
||||
return Err(eyre!(
|
||||
"temporary structured thread did not start with read-only permissions"
|
||||
));
|
||||
}
|
||||
} else if !crate::app_server_session::thread_started_with_read_only_sandbox(&response) {
|
||||
unsubscribe_temporary_thread(request_handle, response.thread.id).await;
|
||||
return Err(eyre!(
|
||||
"temporary structured thread did not start with read-only permissions"
|
||||
));
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
|
||||
Reference in New Issue
Block a user