mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Use effective permissions when trusting app-server projects (#38390)
## Why Project-local configuration can launch host processes. Requested write access may be reduced to read-only by managed constraints or platform support, so the request alone must not cause the app server to trust a project and load its configuration. ## What changed - Base automatic project trust on the effective permission profile after configuration is loaded. - Trust managed profiles only when their effective filesystem policy allows writes to the working directory. - Continue resolving trusted nested working directories to the repository root. ## Testing - Verify that managed read-only permissions neither persist project trust nor load a project-local MCP server when workspace-write access was requested. - Cover effective read-only fallback for workspace-write requests and nested repository working directories. GitOrigin-RevId: e548fd9c4552c328f779cc0365fe86f37d4a9b71
This commit is contained in:
@@ -436,8 +436,6 @@ use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
#[cfg(test)]
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::protocol::AgentStatus;
|
||||
use codex_protocol::protocol::ConversationAudioParams;
|
||||
use codex_protocol::protocol::ConversationSpeechParams;
|
||||
|
||||
@@ -13,8 +13,6 @@ use codex_extension_api::ThreadIdleCause;
|
||||
use codex_protocol::config_types::MultiAgentMode;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::mcp::ClientMcpExtensions;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_DANGER_FULL_ACCESS;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_WORKSPACE;
|
||||
use codex_protocol::protocol::ThreadHistoryMode;
|
||||
use codex_thread_store::PersistContext;
|
||||
|
||||
@@ -1207,21 +1205,22 @@ impl ThreadRequestProcessor {
|
||||
.load_with_overrides(config_overrides.clone(), typesafe_overrides.clone())
|
||||
.await
|
||||
.map_err(|err| config_load_error(&err))?;
|
||||
// The user may have requested WorkspaceWrite or DangerFullAccess via
|
||||
// the command line, though in the process of deriving the Config, it
|
||||
// could be downgraded to ReadOnly (perhaps there is no sandbox
|
||||
// available on Windows or the enterprise config disallows it). The cwd
|
||||
// should still be considered "trusted" in this case.
|
||||
let requested_permissions_trust_project =
|
||||
requested_permissions_trust_project(&typesafe_overrides, config.cwd.as_path());
|
||||
let effective_permissions_trust_project = permission_profile_trusts_project(
|
||||
&config.permissions.effective_permission_profile(),
|
||||
config.cwd.as_path(),
|
||||
);
|
||||
// Project-local config can launch host processes, so only the effective
|
||||
// permissions after managed constraints can imply project trust.
|
||||
let effective_permission_profile = config.permissions.effective_permission_profile();
|
||||
let effective_permissions_trust_project = match &effective_permission_profile {
|
||||
codex_protocol::models::PermissionProfile::Disabled
|
||||
| codex_protocol::models::PermissionProfile::External { .. } => true,
|
||||
codex_protocol::models::PermissionProfile::Managed { .. } => {
|
||||
effective_permission_profile
|
||||
.file_system_sandbox_policy()
|
||||
.can_write_path_with_cwd(config.cwd.as_path(), config.cwd.as_path())
|
||||
}
|
||||
};
|
||||
|
||||
if requested_cwd.is_some()
|
||||
&& config.active_project.trust_level.is_none()
|
||||
&& (requested_permissions_trust_project || effective_permissions_trust_project)
|
||||
&& effective_permissions_trust_project
|
||||
{
|
||||
let trust_target = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &config.cwd)
|
||||
.await
|
||||
@@ -5410,45 +5409,6 @@ fn preview_from_rollout_items(items: &[RolloutItem]) -> String {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn requested_permissions_trust_project(overrides: &ConfigOverrides, cwd: &Path) -> bool {
|
||||
if matches!(
|
||||
overrides.sandbox_mode,
|
||||
Some(
|
||||
codex_protocol::config_types::SandboxMode::WorkspaceWrite
|
||||
| codex_protocol::config_types::SandboxMode::DangerFullAccess
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if matches!(
|
||||
overrides.default_permissions.as_deref(),
|
||||
Some(
|
||||
BUILT_IN_PERMISSION_PROFILE_WORKSPACE | BUILT_IN_PERMISSION_PROFILE_DANGER_FULL_ACCESS
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
overrides
|
||||
.permission_profile
|
||||
.as_ref()
|
||||
.is_some_and(|profile| permission_profile_trusts_project(profile, cwd))
|
||||
}
|
||||
|
||||
fn permission_profile_trusts_project(
|
||||
profile: &codex_protocol::models::PermissionProfile,
|
||||
cwd: &Path,
|
||||
) -> bool {
|
||||
match profile {
|
||||
codex_protocol::models::PermissionProfile::Disabled
|
||||
| codex_protocol::models::PermissionProfile::External { .. } => true,
|
||||
codex_protocol::models::PermissionProfile::Managed { .. } => profile
|
||||
.file_system_sandbox_policy()
|
||||
.can_write_path_with_cwd(cwd, cwd),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_thread_from_snapshot(
|
||||
thread_id: ThreadId,
|
||||
session_id: String,
|
||||
|
||||
@@ -254,15 +254,8 @@ mod thread_processor_behavior_tests {
|
||||
use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_DANGER_FULL_ACCESS;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_READ_ONLY;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_WORKSPACE;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
@@ -641,84 +634,6 @@ mod thread_processor_behavior_tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_permissions_trust_project_uses_permission_profile_intent() {
|
||||
let cwd = test_path_buf("/tmp/project").abs();
|
||||
let full_access_profile = codex_protocol::models::PermissionProfile::Disabled;
|
||||
let workspace_write_profile = codex_protocol::models::PermissionProfile::workspace_write();
|
||||
let read_only_profile = codex_protocol::models::PermissionProfile::read_only();
|
||||
let split_write_profile =
|
||||
codex_protocol::models::PermissionProfile::from_runtime_permissions(
|
||||
&FileSystemSandboxPolicy::restricted(vec![
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: cwd.clone() },
|
||||
access: FileSystemAccessMode::Write,
|
||||
missing_path_behavior: None,
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "/tmp/project/**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::Deny,
|
||||
missing_path_behavior: None,
|
||||
},
|
||||
]),
|
||||
NetworkSandboxPolicy::Restricted,
|
||||
);
|
||||
|
||||
assert!(requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
permission_profile: Some(full_access_profile),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
permission_profile: Some(workspace_write_profile),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
permission_profile: Some(split_write_profile),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
default_permissions: Some(BUILT_IN_PERMISSION_PROFILE_WORKSPACE.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
default_permissions: Some(
|
||||
BUILT_IN_PERMISSION_PROFILE_DANGER_FULL_ACCESS.to_string()
|
||||
),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(!requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
permission_profile: Some(read_only_profile),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
assert!(!requested_permissions_trust_project(
|
||||
&ConfigOverrides {
|
||||
default_permissions: Some(BUILT_IN_PERMISSION_PROFILE_READ_ONLY.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
cwd.as_path()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_load_error_marks_cloud_config_bundle_failures_for_relogin() {
|
||||
let err = std::io::Error::other(CloudConfigBundleLoadError::new(
|
||||
|
||||
@@ -7,12 +7,16 @@ use app_test_support::create_mock_responses_server_repeating_assistant;
|
||||
use app_test_support::to_response;
|
||||
use app_test_support::write_chatgpt_auth;
|
||||
use codex_app_server_protocol::AskForApproval;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::ConfigWarningNotification;
|
||||
use codex_app_server_protocol::JSONRPCError;
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::JSONRPCNotification;
|
||||
use codex_app_server_protocol::JSONRPCResponse;
|
||||
use codex_app_server_protocol::ListMcpServerStatusParams;
|
||||
use codex_app_server_protocol::ListMcpServerStatusResponse;
|
||||
use codex_app_server_protocol::McpServerStartupState;
|
||||
use codex_app_server_protocol::McpServerStatusDetail;
|
||||
use codex_app_server_protocol::McpServerStatusUpdatedNotification;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::SandboxMode;
|
||||
@@ -39,7 +43,9 @@ use codex_git_utils::resolve_root_git_project_for_trust;
|
||||
use codex_login::REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR;
|
||||
use codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
|
||||
use codex_protocol::config_types::TrustLevel;
|
||||
use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_WORKSPACE;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use core_test_support::stdio_server_bin;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
@@ -1300,7 +1306,7 @@ async fn thread_start_surfaces_cloud_config_bundle_load_errors() -> Result<()> {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_start_with_elevated_sandbox_trusts_project_and_followup_loads_project_config()
|
||||
async fn thread_start_workspace_write_respects_effective_permissions_for_project_trust()
|
||||
-> Result<()> {
|
||||
let server = create_mock_responses_server_repeating_assistant("Done").await;
|
||||
|
||||
@@ -1321,13 +1327,15 @@ model_reasoning_effort = "high"
|
||||
.with_codex_home(codex_home.path())
|
||||
.build_initialized()
|
||||
.await?;
|
||||
let config_before = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
|
||||
|
||||
mcp.start_thread(ThreadStartParams {
|
||||
cwd: Some(workspace.path().display().to_string()),
|
||||
sandbox: Some(SandboxMode::WorkspaceWrite),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let first_response = mcp
|
||||
.start_thread(ThreadStartParams {
|
||||
cwd: Some(workspace.path().display().to_string()),
|
||||
sandbox: Some(SandboxMode::WorkspaceWrite),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
|
||||
let ThreadStartResponse {
|
||||
approval_policy,
|
||||
@@ -1340,23 +1348,111 @@ model_reasoning_effort = "high"
|
||||
})
|
||||
.await?;
|
||||
|
||||
assert_eq!(approval_policy, AskForApproval::OnRequest);
|
||||
assert_eq!(reasoning_effort, Some(ReasoningEffort::High));
|
||||
|
||||
let config_toml = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
|
||||
let workspace_abs = workspace.path().to_path_buf().abs();
|
||||
let trusted_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &workspace_abs)
|
||||
.await
|
||||
.unwrap_or(workspace_abs);
|
||||
let trusted_root_key = project_trust_key(trusted_root.as_path());
|
||||
assert!(config_toml.contains(&trusted_root_key));
|
||||
assert!(config_toml.contains("trust_level = \"trusted\""));
|
||||
if cfg!(windows) {
|
||||
assert_eq!(
|
||||
first_response.sandbox,
|
||||
codex_app_server_protocol::SandboxPolicy::ReadOnly {
|
||||
network_access: false,
|
||||
}
|
||||
);
|
||||
assert_eq!(reasoning_effort, None);
|
||||
assert_eq!(config_toml, config_before);
|
||||
} else {
|
||||
assert_eq!(approval_policy, AskForApproval::OnRequest);
|
||||
assert_eq!(reasoning_effort, Some(ReasoningEffort::High));
|
||||
let workspace_abs = workspace.path().to_path_buf().abs();
|
||||
let trusted_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &workspace_abs)
|
||||
.await
|
||||
.unwrap_or(workspace_abs);
|
||||
let trusted_root_key = project_trust_key(trusted_root.as_path());
|
||||
assert!(config_toml.contains(&trusted_root_key));
|
||||
assert!(config_toml.contains("trust_level = \"trusted\""));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_start_with_nested_git_cwd_trusts_repo_root() -> Result<()> {
|
||||
async fn thread_start_with_managed_read_only_does_not_trust_or_load_project_mcp() -> Result<()> {
|
||||
let server = create_mock_responses_server_repeating_assistant("Done").await;
|
||||
|
||||
let codex_home = TempDir::new()?;
|
||||
create_config_toml_without_approval_policy(codex_home.path(), &server.uri())?;
|
||||
std::fs::write(
|
||||
codex_home.path().join("managed_config.toml"),
|
||||
r#"sandbox_mode = "read-only""#,
|
||||
)?;
|
||||
|
||||
let workspace = TempDir::new()?;
|
||||
std::fs::create_dir(workspace.path().join(".git"))?;
|
||||
let project_config_dir = workspace.path().join(".codex");
|
||||
std::fs::create_dir(&project_config_dir)?;
|
||||
std::fs::write(
|
||||
project_config_dir.join("config.toml"),
|
||||
format!(
|
||||
r#"[mcp_servers.project-server]
|
||||
command = {}
|
||||
required = true
|
||||
"#,
|
||||
toml::Value::String(stdio_server_bin()?),
|
||||
),
|
||||
)?;
|
||||
|
||||
let mut mcp = TestAppServer::builder()
|
||||
.with_codex_home(codex_home.path())
|
||||
.without_auto_env()
|
||||
.build_initialized()
|
||||
.await?;
|
||||
let config_before = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
|
||||
|
||||
let request_id = mcp
|
||||
.send_thread_start_request(ThreadStartParams {
|
||||
cwd: Some(workspace.path().display().to_string()),
|
||||
permissions: Some(BUILT_IN_PERMISSION_PROFILE_WORKSPACE.to_string()),
|
||||
environments: Some(Vec::new()),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let ThreadStartResponse {
|
||||
thread, sandbox, ..
|
||||
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(request_id)).await??;
|
||||
|
||||
assert_eq!(
|
||||
sandbox,
|
||||
codex_app_server_protocol::SandboxPolicy::ReadOnly {
|
||||
network_access: false,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(codex_home.path().join("config.toml"))?,
|
||||
config_before
|
||||
);
|
||||
let mcp_status: ListMcpServerStatusResponse = mcp
|
||||
.request(|request_id| ClientRequest::McpServerStatusList {
|
||||
request_id,
|
||||
params: ListMcpServerStatusParams {
|
||||
cursor: None,
|
||||
limit: None,
|
||||
detail: Some(McpServerStatusDetail::ToolsAndAuthOnly),
|
||||
thread_id: Some(thread.id),
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(
|
||||
mcp_status,
|
||||
ListMcpServerStatusResponse {
|
||||
data: Vec::new(),
|
||||
next_cursor: None,
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_start_with_nested_git_cwd_respects_effective_permissions_for_project_trust()
|
||||
-> Result<()> {
|
||||
let server = create_mock_responses_server_repeating_assistant("Done").await;
|
||||
|
||||
let codex_home = TempDir::new()?;
|
||||
@@ -1371,23 +1467,35 @@ async fn thread_start_with_nested_git_cwd_trusts_repo_root() -> Result<()> {
|
||||
.with_codex_home(codex_home.path())
|
||||
.build_initialized()
|
||||
.await?;
|
||||
let config_before = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
|
||||
|
||||
mcp.start_thread(ThreadStartParams {
|
||||
cwd: Some(nested.display().to_string()),
|
||||
sandbox: Some(SandboxMode::WorkspaceWrite),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let ThreadStartResponse { sandbox, .. } = mcp
|
||||
.start_thread(ThreadStartParams {
|
||||
cwd: Some(nested.display().to_string()),
|
||||
sandbox: Some(SandboxMode::WorkspaceWrite),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
|
||||
let config_toml = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
|
||||
let nested_abs = nested.abs();
|
||||
let trusted_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &nested_abs)
|
||||
.await
|
||||
.expect("git root should resolve");
|
||||
let trusted_root_key = project_trust_key(trusted_root.as_path());
|
||||
let nested_key = project_trust_key(&nested);
|
||||
assert!(config_toml.contains(&trusted_root_key));
|
||||
assert!(!config_toml.contains(&nested_key));
|
||||
if cfg!(windows) {
|
||||
assert_eq!(
|
||||
sandbox,
|
||||
codex_app_server_protocol::SandboxPolicy::ReadOnly {
|
||||
network_access: false,
|
||||
}
|
||||
);
|
||||
assert_eq!(config_toml, config_before);
|
||||
} else {
|
||||
let nested_abs = nested.abs();
|
||||
let trusted_root = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &nested_abs)
|
||||
.await
|
||||
.expect("git root should resolve");
|
||||
let trusted_root_key = project_trust_key(trusted_root.as_path());
|
||||
let nested_key = project_trust_key(&nested);
|
||||
assert!(config_toml.contains(&trusted_root_key));
|
||||
assert!(!config_toml.contains(&nested_key));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user