From 8d479f741c501fc21e65f58918548b669a9b492d Mon Sep 17 00:00:00 2001 From: nicholasclark-openai Date: Thu, 26 Mar 2026 10:08:02 -0700 Subject: [PATCH 001/191] Add MCP connector metrics (#15805) ## Summary - enrich `codex.mcp.call` with `tool`, `connector_id`, and sanitized `connector_name` for actual MCP executions - record `codex.mcp.call.duration_ms` for actual MCP executions so connector-level latency is visible in metrics - keep skipped, blocked, declined, and cancelled paths on the plain status-only `codex.mcp.call` counter ## Included Changes - `codex-rs/core/src/mcp_tool_call.rs`: add connector-sliced MCP count and duration metrics only for executed tool calls, while leaving non-executed outcomes as status-only counts - `codex-rs/core/src/mcp_tool_call_tests.rs`: cover metric tag shaping, connector-name sanitization, and the new duration metric tags ## Testing - `cargo test -p codex-core` - `just fix -p codex-core` - `just fmt` ## Notes - `cargo test -p codex-core` still hits existing unrelated failures in approvals-reviewer config tests and the sandboxed JS REPL `mktemp` test - full workspace `cargo test` was not run --------- Co-authored-by: Codex --- codex-rs/core/src/mcp_tool_call.rs | 140 +++++++++++++++++++++-------- 1 file changed, 104 insertions(+), 36 deletions(-) diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index 9855481b01..128e7a30ba 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -38,6 +38,7 @@ use codex_analytics::AppInvocation; use codex_analytics::InvocationType; use codex_analytics::build_track_events_context; use codex_features::Feature; +use codex_otel::sanitize_metric_tag_value; use codex_protocol::mcp::CallToolResult; use codex_protocol::openai_models::InputModality; use codex_protocol::protocol::AskForApproval; @@ -61,6 +62,9 @@ use tracing::Span; use tracing::field::Empty; use url::Url; +const MCP_CALL_COUNT_METRIC: &str = "codex.mcp.call"; +const MCP_CALL_DURATION_METRIC: &str = "codex.mcp.call.duration_ms"; + /// Handles the specified tool call dispatches the appropriate /// `McpToolCallBegin` and `McpToolCallEnd` events to the `Session`. pub(crate) async fn handle_mcp_tool_call( @@ -128,7 +132,7 @@ pub(crate) async fn handle_mcp_tool_call( .await; let status = if result.is_ok() { "ok" } else { "error" }; turn_context.session_telemetry.counter( - "codex.mcp.call", + MCP_CALL_COUNT_METRIC, /*inc*/ 1, &[("status", status)], ); @@ -166,7 +170,7 @@ pub(crate) async fn handle_mcp_tool_call( ) .await { - let result = match decision { + let (result, call_duration) = match decision { McpToolApprovalDecision::Accept | McpToolApprovalDecision::AcceptForSession | McpToolApprovalDecision::AcceptAndRemember => { @@ -206,10 +210,11 @@ pub(crate) async fn handle_mcp_tool_call( if let Err(error) = &result { tracing::warn!("MCP tool call error: {error:?}"); } + let duration = start.elapsed(); let tool_call_end_event = EventMsg::McpToolCallEnd(McpToolCallEndEvent { call_id: call_id.clone(), invocation, - duration: start.elapsed(), + duration, result: result.clone(), }); notify_mcp_tool_call_event( @@ -225,50 +230,62 @@ pub(crate) async fn handle_mcp_tool_call( &tool_name, ) .await; - result + (result, Some(duration)) } McpToolApprovalDecision::Decline => { let message = "user rejected MCP tool call".to_string(); - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - message, - /*already_started*/ true, + ( + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + message, + /*already_started*/ true, + ) + .await, + None, ) - .await } McpToolApprovalDecision::Cancel => { let message = "user cancelled MCP tool call".to_string(); - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - message, - /*already_started*/ true, + ( + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + message, + /*already_started*/ true, + ) + .await, + None, ) - .await } McpToolApprovalDecision::BlockedBySafetyMonitor(message) => { - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - message, - /*already_started*/ true, + ( + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + message, + /*already_started*/ true, + ) + .await, + None, ) - .await } }; let status = if result.is_ok() { "ok" } else { "error" }; - turn_context.session_telemetry.counter( - "codex.mcp.call", - /*inc*/ 1, - &[("status", status)], + emit_mcp_call_metrics( + turn_context.as_ref(), + status, + &tool_name, + connector_id.as_deref(), + connector_name.as_deref(), + call_duration, ); return CallToolResult::from_result(result); @@ -306,10 +323,11 @@ pub(crate) async fn handle_mcp_tool_call( if let Err(error) = &result { tracing::warn!("MCP tool call error: {error:?}"); } + let duration = start.elapsed(); let tool_call_end_event = EventMsg::McpToolCallEnd(McpToolCallEndEvent { call_id: call_id.clone(), invocation, - duration: start.elapsed(), + duration, result: result.clone(), }); @@ -322,13 +340,63 @@ pub(crate) async fn handle_mcp_tool_call( maybe_track_codex_app_used(sess.as_ref(), turn_context.as_ref(), &server, &tool_name).await; let status = if result.is_ok() { "ok" } else { "error" }; - turn_context - .session_telemetry - .counter("codex.mcp.call", /*inc*/ 1, &[("status", status)]); + emit_mcp_call_metrics( + turn_context.as_ref(), + status, + &tool_name, + connector_id.as_deref(), + connector_name.as_deref(), + Some(duration), + ); CallToolResult::from_result(result) } +fn emit_mcp_call_metrics( + turn_context: &TurnContext, + status: &str, + tool_name: &str, + connector_id: Option<&str>, + connector_name: Option<&str>, + duration: Option, +) { + let tags = mcp_call_metric_tags(status, tool_name, connector_id, connector_name); + let tag_refs: Vec<(&str, &str)> = tags + .iter() + .map(|(key, value)| (*key, value.as_str())) + .collect(); + turn_context + .session_telemetry + .counter(MCP_CALL_COUNT_METRIC, /*inc*/ 1, &tag_refs); + if let Some(duration) = duration { + turn_context.session_telemetry.record_duration( + MCP_CALL_DURATION_METRIC, + duration, + &tag_refs, + ); + } +} + +fn mcp_call_metric_tags( + status: &str, + tool_name: &str, + connector_id: Option<&str>, + connector_name: Option<&str>, +) -> Vec<(&'static str, String)> { + let mut tags = vec![ + ("status", sanitize_metric_tag_value(status)), + ("tool", sanitize_metric_tag_value(tool_name)), + ]; + if let Some(connector_id) = connector_id.filter(|connector_id| !connector_id.is_empty()) { + tags.push(("connector_id", sanitize_metric_tag_value(connector_id))); + } + if let Some(connector_name) = connector_name.filter(|connector_name| !connector_name.is_empty()) + { + tags.push(("connector_name", sanitize_metric_tag_value(connector_name))); + } + tags +} + fn mcp_tool_call_span( session: &Session, turn_context: &TurnContext, From e7139e14a29de0411a61658a0e5765e2502a0cd2 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 26 Mar 2026 11:28:25 -0600 Subject: [PATCH 002/191] Enable `tui_app_server` feature by default (#15661) --- codex-rs/features/src/lib.rs | 8 ++------ codex-rs/tui/tests/suite/model_availability_nux.rs | 2 ++ codex-rs/tui_app_server/src/app_server_session.rs | 10 ++++------ .../tests/suite/model_availability_nux.rs | 2 ++ 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index ccde6a5e54..81cc95a47b 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -822,12 +822,8 @@ pub const FEATURES: &[FeatureSpec] = &[ FeatureSpec { id: Feature::TuiAppServer, key: "tui_app_server", - stage: Stage::Experimental { - name: "App-server TUI", - menu_description: "Use the app-server-backed TUI implementation.", - announcement: "", - }, - default_enabled: false, + stage: Stage::Stable, + default_enabled: true, }, FeatureSpec { id: Feature::PreventIdleSleep, diff --git a/codex-rs/tui/tests/suite/model_availability_nux.rs b/codex-rs/tui/tests/suite/model_availability_nux.rs index bcbb3f0e2c..ef2c87c7ab 100644 --- a/codex-rs/tui/tests/suite/model_availability_nux.rs +++ b/codex-rs/tui/tests/suite/model_availability_nux.rs @@ -112,6 +112,8 @@ trust_level = "trusted" "resume".to_string(), "--last".to_string(), "--no-alt-screen".to_string(), + "--disable".to_string(), + "tui_app_server".to_string(), "-C".to_string(), repo_root.display().to_string(), "-c".to_string(), diff --git a/codex-rs/tui_app_server/src/app_server_session.rs b/codex-rs/tui_app_server/src/app_server_session.rs index cf325786be..3e7eddd62a 100644 --- a/codex-rs/tui_app_server/src/app_server_session.rs +++ b/codex-rs/tui_app_server/src/app_server_session.rs @@ -1,3 +1,5 @@ +use crate::bottom_pane::FeedbackAudience; +use crate::status::StatusAccountDisplay; use codex_app_server_client::AppServerClient; use codex_app_server_client::AppServerEvent; use codex_app_server_client::AppServerRequestHandle; @@ -84,10 +86,6 @@ use color_eyre::eyre::Result; use color_eyre::eyre::WrapErr; use std::collections::HashMap; use std::path::PathBuf; -use tracing::debug; - -use crate::bottom_pane::FeedbackAudience; -use crate::status::StatusAccountDisplay; pub(crate) struct AppServerBootstrap { pub(crate) account_auth_mode: Option, @@ -258,8 +256,8 @@ impl AppServerSession { .await { Ok(rate_limits) => app_server_rate_limit_snapshots_to_core(rate_limits), - Err(error) => { - debug!(error = ?error, "failed to fetch rate limits during TUI bootstrap"); + Err(err) => { + tracing::warn!("account/rateLimits/read failed during TUI bootstrap: {err}"); Vec::new() } } diff --git a/codex-rs/tui_app_server/tests/suite/model_availability_nux.rs b/codex-rs/tui_app_server/tests/suite/model_availability_nux.rs index bcbb3f0e2c..ac61cba3ea 100644 --- a/codex-rs/tui_app_server/tests/suite/model_availability_nux.rs +++ b/codex-rs/tui_app_server/tests/suite/model_availability_nux.rs @@ -112,6 +112,8 @@ trust_level = "trusted" "resume".to_string(), "--last".to_string(), "--no-alt-screen".to_string(), + "--enable".to_string(), + "tui_app_server".to_string(), "-C".to_string(), repo_root.display().to_string(), "-c".to_string(), From e36ebaa3daca0e5e73dc2d2af82b04c9eca2526b Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 10:32:04 -0700 Subject: [PATCH 003/191] fix: box apply_patch test harness futures (#15835) ## Why `#[large_stack_test]` made the `apply_patch_cli` tests pass by giving them more stack, but it did not address why those tests needed the extra stack in the first place. The real problem is the async state built by the `apply_patch_cli` harness path. Those tests await three helper boundaries directly: harness construction, turn submission, and apply-patch output collection. If those helpers inline their full child futures, the test future grows to include the whole harness startup and request/response path. This change replaces the workaround from #12768 with the same basic approach used in #13429, but keeps the fix narrower: only the helper boundaries awaited directly by `apply_patch_cli` stay boxed. ## What Changed - removed `#[large_stack_test]` from `core/tests/suite/apply_patch_cli.rs` - restored ordinary `#[tokio::test(flavor = "multi_thread", worker_threads = 2)]` annotations in that suite - deleted the now-unused `codex-test-macros` crate and removed its workspace wiring - boxed only the three helper boundaries that the suite awaits directly: - `apply_patch_harness_with(...)` - `TestCodexHarness::submit(...)` - `TestCodexHarness::apply_patch_output(...)` - added comments at those boxed boundaries explaining why they remain boxed ## Testing - `cargo test -p codex-core --test all suite::apply_patch_cli -- --nocapture` ## References - #12768 - #13429 --- codex-rs/Cargo.lock | 10 -- codex-rs/Cargo.toml | 2 - codex-rs/core/Cargo.toml | 1 - codex-rs/core/tests/common/test_codex.rs | 12 +- codex-rs/core/tests/suite/apply_patch_cli.rs | 65 ++++---- codex-rs/test-macros/BUILD.bazel | 7 - codex-rs/test-macros/Cargo.toml | 16 -- codex-rs/test-macros/src/lib.rs | 155 ------------------- 8 files changed, 42 insertions(+), 226 deletions(-) delete mode 100644 codex-rs/test-macros/BUILD.bazel delete mode 100644 codex-rs/test-macros/Cargo.toml delete mode 100644 codex-rs/test-macros/src/lib.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index a2b1450e8a..9ae4e7c463 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1846,7 +1846,6 @@ dependencies = [ "codex-shell-escalation", "codex-state", "codex-terminal-detection", - "codex-test-macros", "codex-utils-absolute-path", "codex-utils-cache", "codex-utils-cargo-bin", @@ -2625,15 +2624,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "codex-test-macros" -version = "0.0.0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.114", -] - [[package]] name = "codex-tui" version = "0.0.0" diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 8d047d9232..ba287de5c5 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -80,7 +80,6 @@ members = [ "state", "terminal-detection", "codex-experimental-api-macros", - "test-macros", "package-manager", "plugin", ] @@ -150,7 +149,6 @@ codex-shell-escalation = { path = "shell-escalation" } codex-skills = { path = "skills" } codex-state = { path = "state" } codex-stdio-to-uds = { path = "stdio-to-uds" } -codex-test-macros = { path = "test-macros" } codex-terminal-detection = { path = "terminal-detection" } codex-tui = { path = "tui" } codex-tui-app-server = { path = "tui_app_server" } diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index f99487a6b3..f64a0a5280 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -148,7 +148,6 @@ codex-arg0 = { workspace = true } codex-otel = { workspace = true, features = [ "disable-default-metrics-exporter", ] } -codex-test-macros = { workspace = true } codex-utils-cargo-bin = { workspace = true } core_test_support = { workspace = true } ctor = { workspace = true } diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index efbcf8d8ce..90ea36776e 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -792,7 +792,9 @@ impl TestCodexHarness { } pub async fn submit(&self, prompt: &str) -> Result<()> { - self.test.submit_turn(prompt).await + // Box the submit-and-wait path so callers do not inline the full turn + // future into their own async state. + Box::pin(self.test.submit_turn(prompt)).await } pub async fn submit_with_policy( @@ -844,13 +846,17 @@ impl TestCodexHarness { call_id: &str, output_type: ApplyPatchModelOutput, ) -> String { + // Box the awaited output helpers so callers do not inline request + // capture and response parsing into their own async state. match output_type { - ApplyPatchModelOutput::Freeform => self.custom_tool_call_output(call_id).await, + ApplyPatchModelOutput::Freeform => { + Box::pin(self.custom_tool_call_output(call_id)).await + } ApplyPatchModelOutput::Function | ApplyPatchModelOutput::Shell | ApplyPatchModelOutput::ShellViaHeredoc | ApplyPatchModelOutput::ShellCommandViaHeredoc => { - self.function_call_stdout(call_id).await + Box::pin(self.function_call_stdout(call_id)).await } } } diff --git a/codex-rs/core/tests/suite/apply_patch_cli.rs b/codex-rs/core/tests/suite/apply_patch_cli.rs index 15fd3b7d36..a8b429c0dd 100644 --- a/codex-rs/core/tests/suite/apply_patch_cli.rs +++ b/codex-rs/core/tests/suite/apply_patch_cli.rs @@ -3,7 +3,6 @@ use anyhow::Result; use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; -use codex_test_macros::large_stack_test; use core_test_support::responses::ev_apply_patch_call; use core_test_support::responses::ev_apply_patch_custom_tool_call; use core_test_support::responses::ev_shell_command_call; @@ -53,7 +52,9 @@ async fn apply_patch_harness_with( let builder = configure(test_codex()).with_config(|config| { config.include_apply_patch_tool = true; }); - TestCodexHarness::with_builder(builder).await + // Box harness construction so apply_patch_cli tests do not inline the + // full test-thread startup path into each test future. + Box::pin(TestCodexHarness::with_builder(builder)).await } pub async fn mount_apply_patch( @@ -90,7 +91,7 @@ fn apply_patch_responses( } #[cfg(target_os = "linux")] -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_cli_uses_codex_self_exe_with_linux_sandbox_helper_alias() -> Result<()> { skip_if_no_network!(Ok(())); @@ -136,7 +137,7 @@ async fn apply_patch_cli_uses_codex_self_exe_with_linux_sandbox_helper_alias() - Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -183,7 +184,7 @@ D delete.txt Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -210,7 +211,7 @@ async fn apply_patch_cli_multiple_chunks(model_output: ApplyPatchModelOutput) -> Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -239,7 +240,7 @@ async fn apply_patch_cli_moves_file_to_new_directory( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -267,7 +268,7 @@ async fn apply_patch_cli_updates_file_appends_trailing_newline( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -293,7 +294,7 @@ async fn apply_patch_cli_insert_only_hunk_modifies_file( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -324,7 +325,7 @@ async fn apply_patch_cli_move_overwrites_existing_destination( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -387,7 +388,7 @@ async fn apply_patch_cli_move_without_content_change_has_no_turn_diff( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -413,7 +414,7 @@ async fn apply_patch_cli_add_overwrites_existing_file( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -445,7 +446,7 @@ async fn apply_patch_cli_rejects_invalid_hunk_header( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -479,7 +480,7 @@ async fn apply_patch_cli_reports_missing_context( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -515,7 +516,7 @@ async fn apply_patch_cli_reports_missing_target_file( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -552,7 +553,7 @@ async fn apply_patch_cli_delete_missing_file_reports_error( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -577,7 +578,7 @@ async fn apply_patch_cli_rejects_empty_patch(model_output: ApplyPatchModelOutput Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -604,7 +605,7 @@ async fn apply_patch_cli_delete_directory_reports_verification_error( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -658,7 +659,7 @@ async fn apply_patch_cli_rejects_path_traversal_outside_workspace( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -713,7 +714,7 @@ async fn apply_patch_cli_rejects_move_path_traversal_outside_workspace( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -750,7 +751,7 @@ async fn apply_patch_cli_verification_failure_has_no_side_effects( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_shell_command_heredoc_with_cd_updates_relative_workdir() -> Result<()> { skip_if_no_network!(Ok(())); @@ -788,7 +789,7 @@ async fn apply_patch_shell_command_heredoc_with_cd_updates_relative_workdir() -> Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_cli_can_use_shell_command_output_as_patch_input() -> Result<()> { skip_if_no_network!(Ok(())); @@ -928,7 +929,7 @@ async fn apply_patch_cli_can_use_shell_command_output_as_patch_input() -> Result Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_shell_command_heredoc_with_cd_emits_turn_diff() -> Result<()> { skip_if_no_network!(Ok(())); @@ -1013,7 +1014,7 @@ async fn apply_patch_shell_command_heredoc_with_cd_emits_turn_diff() -> Result<( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_shell_command_failure_propagates_error_and_skips_diff() -> Result<()> { skip_if_no_network!(Ok(())); @@ -1091,7 +1092,7 @@ async fn apply_patch_shell_command_failure_propagates_error_and_skips_diff() -> Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] #[test_case(ApplyPatchModelOutput::ShellCommandViaHeredoc)] async fn apply_patch_function_accepts_lenient_heredoc_wrapped_patch( @@ -1114,7 +1115,7 @@ async fn apply_patch_function_accepts_lenient_heredoc_wrapped_patch( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -1137,7 +1138,7 @@ async fn apply_patch_cli_end_of_file_anchor(model_output: ApplyPatchModelOutput) Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -1172,7 +1173,7 @@ async fn apply_patch_cli_missing_second_chunk_context_rejected( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -1233,7 +1234,7 @@ async fn apply_patch_emits_turn_diff_event_with_unified_diff( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] @@ -1301,7 +1302,7 @@ async fn apply_patch_turn_diff_for_rename_with_content_change( Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_aggregates_diff_across_multiple_tool_calls() -> Result<()> { skip_if_no_network!(Ok(())); @@ -1371,7 +1372,7 @@ async fn apply_patch_aggregates_diff_across_multiple_tool_calls() -> Result<()> Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn apply_patch_aggregates_diff_preserves_success_after_failure() -> Result<()> { skip_if_no_network!(Ok(())); @@ -1463,7 +1464,7 @@ async fn apply_patch_aggregates_diff_preserves_success_after_failure() -> Result Ok(()) } -#[large_stack_test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[test_case(ApplyPatchModelOutput::Freeform)] #[test_case(ApplyPatchModelOutput::Function)] #[test_case(ApplyPatchModelOutput::Shell)] diff --git a/codex-rs/test-macros/BUILD.bazel b/codex-rs/test-macros/BUILD.bazel deleted file mode 100644 index 0ff074f5e5..0000000000 --- a/codex-rs/test-macros/BUILD.bazel +++ /dev/null @@ -1,7 +0,0 @@ -load("//:defs.bzl", "codex_rust_crate") - -codex_rust_crate( - name = "test-macros", - crate_name = "codex_test_macros", - proc_macro = True, -) diff --git a/codex-rs/test-macros/Cargo.toml b/codex-rs/test-macros/Cargo.toml deleted file mode 100644 index 275580322d..0000000000 --- a/codex-rs/test-macros/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "codex-test-macros" -version.workspace = true -edition.workspace = true -license.workspace = true - -[lib] -proc-macro = true - -[dependencies] -proc-macro2 = "1" -quote = "1" -syn = { version = "2", features = ["full"] } - -[lints] -workspace = true diff --git a/codex-rs/test-macros/src/lib.rs b/codex-rs/test-macros/src/lib.rs deleted file mode 100644 index 16c87f8582..0000000000 --- a/codex-rs/test-macros/src/lib.rs +++ /dev/null @@ -1,155 +0,0 @@ -use proc_macro::TokenStream; -use proc_macro2::TokenStream as TokenStream2; -use quote::quote; -use syn::Attribute; -use syn::ItemFn; -use syn::parse::Nothing; -use syn::parse_macro_input; -use syn::parse_quote; - -const LARGE_STACK_TEST_STACK_SIZE_BYTES: usize = 16 * 1024 * 1024; - -/// Run a test body on a dedicated thread with a larger stack. -/// -/// For async tests, this macro creates a Tokio multi-thread runtime with two -/// worker threads and blocks on the original async body inside the large-stack -/// thread. -#[proc_macro_attribute] -pub fn large_stack_test(attr: TokenStream, item: TokenStream) -> TokenStream { - parse_macro_input!(attr as Nothing); - - let item = parse_macro_input!(item as ItemFn); - expand_large_stack_test(item).into() -} - -fn expand_large_stack_test(mut item: ItemFn) -> TokenStream2 { - let attrs = filtered_attributes(&item.attrs); - item.attrs = attrs; - - let is_async = item.sig.asyncness.take().is_some(); - let name = &item.sig.ident; - let body = &item.block; - - let thread_body = if is_async { - quote! { - { - let runtime = ::tokio::runtime::Builder::new_multi_thread() - .worker_threads(2) - .enable_all() - .build() - .unwrap_or_else(|error| { - panic!("failed to build tokio runtime for large-stack test: {error}") - }); - runtime.block_on(async move #body) - } - } - } else { - quote! { #body } - }; - - *item.block = parse_quote!({ - let handle = ::std::thread::Builder::new() - .name(::std::string::String::from(::std::stringify!(#name))) - .stack_size(#LARGE_STACK_TEST_STACK_SIZE_BYTES) - .spawn(move || #thread_body) - .unwrap_or_else(|error| { - panic!("failed to spawn large-stack test thread: {error}") - }); - - match handle.join() { - Ok(result) => result, - Err(payload) => ::std::panic::resume_unwind(payload), - } - }); - - quote! { #item } -} - -fn filtered_attributes(attrs: &[Attribute]) -> Vec { - let mut filtered = Vec::with_capacity(attrs.len() + 1); - let mut has_test_attr = false; - - for attr in attrs { - if is_tokio_test_attr(attr) { - continue; - } - if is_test_attr(attr) || is_test_case_attr(attr) { - has_test_attr = true; - } - filtered.push(attr.clone()); - } - - if !has_test_attr { - filtered.push(parse_quote!(#[test])); - } - - filtered -} - -fn is_test_attr(attr: &Attribute) -> bool { - attr.path().is_ident("test") -} - -fn is_test_case_attr(attr: &Attribute) -> bool { - attr.path().is_ident("test_case") -} - -fn is_tokio_test_attr(attr: &Attribute) -> bool { - let mut segments = attr.path().segments.iter(); - matches!( - (segments.next(), segments.next(), segments.next()), - (Some(first), Some(second), None) if first.ident == "tokio" && second.ident == "test" - ) -} - -#[cfg(test)] -mod tests { - use super::expand_large_stack_test; - use syn::ItemFn; - use syn::parse_quote; - - fn has_attr(item: &ItemFn, name: &str) -> bool { - item.attrs.iter().any(|attr| attr.path().is_ident(name)) - } - - #[test] - fn adds_test_attribute_when_missing() { - let item: ItemFn = parse_quote! { - fn sample() {} - }; - - let expanded_tokens = expand_large_stack_test(item); - let expanded: ItemFn = match syn::parse2(expanded_tokens) { - Ok(expanded) => expanded, - Err(error) => panic!("failed to parse expanded function: {error}"), - }; - - assert!(has_attr(&expanded, "test")); - let body = quote::quote!(#expanded).to_string(); - assert!(body.contains("stack_size")); - } - - #[test] - fn removes_tokio_test_and_keeps_test_case() { - let item: ItemFn = parse_quote! { - #[test_case(1)] - #[tokio::test(flavor = "multi_thread", worker_threads = 2)] - async fn sample(value: usize) -> anyhow::Result<()> { - let _ = value; - Ok(()) - } - }; - - let expanded_tokens = expand_large_stack_test(item); - let expanded: ItemFn = match syn::parse2(expanded_tokens) { - Ok(expanded) => expanded, - Err(error) => panic!("failed to parse expanded function: {error}"), - }; - - assert!(has_attr(&expanded, "test_case")); - assert!(!has_attr(&expanded, "test")); - let body = quote::quote!(#expanded).to_string(); - assert!(body.contains("tokio :: runtime :: Builder")); - assert!(!body.contains("tokio :: test")); - } -} From af0427377898290356e47b9b3a3311083d9ed3fb Mon Sep 17 00:00:00 2001 From: Adrian <145513011+adrian-openai@users.noreply.github.com> Date: Thu, 26 Mar 2026 10:46:04 -0700 Subject: [PATCH 004/191] [codex] Block unsafe git global options from safe allowlist (#15796) ## Summary - block git global options that can redirect config, repository, or helper lookup from being auto-approved as safe - share the unsafe global-option predicate across the Unix and Windows git safety checks - add regression coverage for inline and split forms, including `bash -lc` and PowerShell wrappers ## Root cause The Unix safe-command gate only rejected `-c` and `--config-env`, even though the shared git parser already knew how to skip additional pre-subcommand globals such as `--git-dir`, `--work-tree`, `--exec-path`, `--namespace`, and `--super-prefix`. That let those arguments slip through safe-command classification on otherwise read-only git invocations and bypass approval. The Windows-specific safe-command path had the same trust-boundary gap for git global options. --- .../command_safety/is_dangerous_command.rs | 23 ++++++ .../src/command_safety/is_safe_command.rs | 53 +++++++++++--- .../command_safety/windows_safe_commands.rs | 72 +++++++++++-------- 3 files changed, 107 insertions(+), 41 deletions(-) diff --git a/codex-rs/shell-command/src/command_safety/is_dangerous_command.rs b/codex-rs/shell-command/src/command_safety/is_dangerous_command.rs index 9e4bfe16a8..3aa31c80f9 100644 --- a/codex-rs/shell-command/src/command_safety/is_dangerous_command.rs +++ b/codex-rs/shell-command/src/command_safety/is_dangerous_command.rs @@ -53,6 +53,29 @@ fn is_git_global_option_with_inline_value(arg: &str) -> bool { ) || ((arg.starts_with("-C") || arg.starts_with("-c")) && arg.len() > 2) } +/// Git global options that can redirect config, repository, or helper lookup +/// and therefore must never be auto-approved as "safe". +pub(crate) fn git_global_option_requires_prompt(arg: &str) -> bool { + matches!( + arg, + "-c" | "--config-env" + | "--exec-path" + | "--git-dir" + | "--namespace" + | "--super-prefix" + | "--work-tree" + ) || matches!( + arg, + s if (s.starts_with("-c") && s.len() > 2) + || s.starts_with("--config-env=") + || s.starts_with("--exec-path=") + || s.starts_with("--git-dir=") + || s.starts_with("--namespace=") + || s.starts_with("--super-prefix=") + || s.starts_with("--work-tree=") + ) +} + pub(crate) fn executable_name_lookup_key(raw: &str) -> Option { #[cfg(windows)] { diff --git a/codex-rs/shell-command/src/command_safety/is_safe_command.rs b/codex-rs/shell-command/src/command_safety/is_safe_command.rs index b7349f220a..2906fef276 100644 --- a/codex-rs/shell-command/src/command_safety/is_safe_command.rs +++ b/codex-rs/shell-command/src/command_safety/is_safe_command.rs @@ -4,6 +4,7 @@ use crate::command_safety::is_dangerous_command::executable_name_lookup_key; // may appear before it (e.g., `-C`, `-c`, `--git-dir`). // Implemented in `is_dangerous_command` and shared here. use crate::command_safety::is_dangerous_command::find_git_subcommand; +use crate::command_safety::is_dangerous_command::git_global_option_requires_prompt; use crate::command_safety::windows_safe_commands::is_safe_command_windows; pub fn is_known_safe_command(command: &[String]) -> bool { @@ -134,10 +135,10 @@ fn is_safe_to_call_with_exec(command: &[String]) -> bool { // Git Some("git") => { - // Global config overrides like `-c core.pager=...` can force git - // to execute arbitrary external commands. With no sandboxing, we - // should always prompt in those cases. - if git_has_config_override_global_option(command) { + // Global options that redirect config, repository, or helper + // lookup can make otherwise read-only git commands execute + // attacker-controlled code, so they must never be auto-approved. + if git_has_unsafe_global_option(command) { return false; } @@ -208,12 +209,12 @@ fn git_branch_is_read_only(branch_args: &[String]) -> bool { saw_read_only_flag } -fn git_has_config_override_global_option(command: &[String]) -> bool { - command.iter().map(String::as_str).any(|arg| { - matches!(arg, "-c" | "--config-env") - || (arg.starts_with("-c") && arg.len() > 2) - || arg.starts_with("--config-env=") - }) +fn git_has_unsafe_global_option(command: &[String]) -> bool { + command + .iter() + .skip(1) + .map(String::as_str) + .any(git_global_option_requires_prompt) } fn git_subcommand_args_are_read_only(args: &[String]) -> bool { @@ -356,7 +357,7 @@ mod tests { } #[test] - fn git_output_and_config_override_flags_are_not_safe() { + fn git_output_flags_are_not_safe() { assert!(!is_known_safe_command(&vec_str(&[ "git", "log", @@ -376,6 +377,10 @@ mod tests { "--output=/tmp/git-show-out-test", "HEAD", ]))); + } + + #[test] + fn git_global_override_flags_are_not_safe() { assert!(!is_known_safe_command(&vec_str(&[ "git", "-c", @@ -389,6 +394,32 @@ mod tests { "-ccore.pager=cat", "status", ]))); + + for args in [ + vec_str(&["git", "--config-env", "core.pager=PAGER", "show", "HEAD"]), + vec_str(&["git", "--config-env=core.pager=PAGER", "show", "HEAD"]), + vec_str(&["git", "--git-dir", ".evil-git", "diff", "HEAD~1..HEAD"]), + vec_str(&["git", "--git-dir=.evil-git", "diff", "HEAD~1..HEAD"]), + vec_str(&["git", "--work-tree", ".", "status"]), + vec_str(&["git", "--work-tree=.", "status"]), + vec_str(&["git", "--exec-path", ".git/helpers", "show", "HEAD"]), + vec_str(&["git", "--exec-path=.git/helpers", "show", "HEAD"]), + vec_str(&["git", "--namespace", "attacker", "show", "HEAD"]), + vec_str(&["git", "--namespace=attacker", "show", "HEAD"]), + vec_str(&["git", "--super-prefix", "attacker/", "show", "HEAD"]), + vec_str(&["git", "--super-prefix=attacker/", "show", "HEAD"]), + ] { + assert!( + !is_known_safe_command(&args), + "expected {args:?} to require approval due to unsafe git global option", + ); + } + + assert!(!is_known_safe_command(&vec_str(&[ + "bash", + "-lc", + "git --git-dir=.evil-git diff HEAD~1..HEAD", + ]))); } #[test] diff --git a/codex-rs/shell-command/src/command_safety/windows_safe_commands.rs b/codex-rs/shell-command/src/command_safety/windows_safe_commands.rs index ac479a4d2c..aa7ff96818 100644 --- a/codex-rs/shell-command/src/command_safety/windows_safe_commands.rs +++ b/codex-rs/shell-command/src/command_safety/windows_safe_commands.rs @@ -1,3 +1,4 @@ +use crate::command_safety::is_dangerous_command::git_global_option_requires_prompt; use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use serde::Deserialize; @@ -305,33 +306,17 @@ fn is_safe_ripgrep(words: &[String]) -> bool { fn is_safe_git_command(words: &[String]) -> bool { const SAFE_SUBCOMMANDS: &[&str] = &["status", "log", "show", "diff", "cat-file"]; - let mut iter = words.iter().skip(1); - while let Some(arg) = iter.next() { + for arg in words.iter().skip(1) { let arg_lc = arg.to_ascii_lowercase(); if arg.starts_with('-') { - if arg.eq_ignore_ascii_case("-c") || arg.eq_ignore_ascii_case("--config") { - if iter.next().is_none() { - // Examples rejected here: "pwsh -Command 'git -c'" and "pwsh -Command 'git --config'". - return false; - } - continue; - } - - if arg_lc.starts_with("-c=") + if git_global_option_requires_prompt(&arg_lc) + || arg.eq_ignore_ascii_case("--config") || arg_lc.starts_with("--config=") - || arg_lc.starts_with("--git-dir=") - || arg_lc.starts_with("--work-tree=") { - continue; - } - - if arg.eq_ignore_ascii_case("--git-dir") || arg.eq_ignore_ascii_case("--work-tree") { - if iter.next().is_none() { - // Examples rejected here: "pwsh -Command 'git --git-dir'" and "pwsh -Command 'git --work-tree'". - return false; - } - continue; + // Examples rejected here: "pwsh -Command 'git --git-dir=.evil-git diff'" and + // "pwsh -Command 'git -c core.pager=cat show HEAD:foo.rs'". + return false; } continue; @@ -435,14 +420,6 @@ mod tests { "Get-Content foo.rs | Select-Object -Skip 200".to_string() ])); - assert!(is_safe_command_windows(&[ - pwsh.clone(), - "-NoLogo".to_string(), - "-NoProfile".to_string(), - "-Command".to_string(), - "git -c core.pager=cat show HEAD:foo.rs".to_string() - ])); - assert!(is_safe_command_windows(&[ pwsh.clone(), "-Command".to_string(), @@ -462,6 +439,41 @@ mod tests { ])); } + #[test] + fn rejects_git_global_override_options() { + let Some(pwsh) = try_find_pwsh_executable_blocking() else { + return; + }; + + let pwsh: String = pwsh.as_path().to_str().unwrap().into(); + for script in [ + "git -c core.pager=cat show HEAD:foo.rs", + "git --config-env core.pager=PAGER show HEAD:foo.rs", + "git --config-env=core.pager=PAGER show HEAD:foo.rs", + "git --git-dir .evil-git diff HEAD~1..HEAD", + "git --git-dir=.evil-git diff HEAD~1..HEAD", + "git --work-tree . status", + "git --work-tree=. status", + "git --exec-path .git/helpers show HEAD:foo.rs", + "git --exec-path=.git/helpers show HEAD:foo.rs", + "git --namespace attacker show HEAD:foo.rs", + "git --namespace=attacker show HEAD:foo.rs", + "git --super-prefix attacker/ show HEAD:foo.rs", + "git --super-prefix=attacker/ show HEAD:foo.rs", + ] { + assert!( + !is_safe_command_windows(&[ + pwsh.clone(), + "-NoLogo".to_string(), + "-NoProfile".to_string(), + "-Command".to_string(), + script.to_string(), + ]), + "expected {script:?} to require approval due to unsafe git global option", + ); + } + } + #[test] fn rejects_powershell_commands_with_side_effects() { assert!(!is_safe_command_windows(&vec_str(&[ From 0bd34c28c768fce784d0648fe45e651b9a9b37d4 Mon Sep 17 00:00:00 2001 From: evawong-oai Date: Thu, 26 Mar 2026 10:53:31 -0700 Subject: [PATCH 005/191] Add wildcard in the middle test coverage (#15813) ## Summary Add a focused codex network proxy unit test for the denylist pattern with wildcard in the middle `region*.some.malicious.tunnel.com`. This does not change how existing code works, just ensure that behavior stays the same and we got CI guards to guard existin behavior. ## Why The managed Codex denylist update relies on this mid label glob form, and the existing tests only covered exact hosts, `*.` subdomains, and `**.` apex plus subdomains. ## Validation `cargo test -p codex-network-proxy compile_globset_supports_mid_label_wildcards` `cargo test -p codex-network-proxy` `./tools/argument-comment-lint/run-prebuilt-linter.sh -p codex-network-proxy` --- codex-rs/network-proxy/src/policy.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/codex-rs/network-proxy/src/policy.rs b/codex-rs/network-proxy/src/policy.rs index c586ee9c11..40e71caf36 100644 --- a/codex-rs/network-proxy/src/policy.rs +++ b/codex-rs/network-proxy/src/policy.rs @@ -367,6 +367,16 @@ mod tests { assert_eq!(false, set.is_match("example.com")); } + #[test] + fn compile_globset_supports_mid_label_wildcards() { + let set = compile_denylist_globset(&["region*.v2.argotunnel.com".to_string()]).unwrap(); + + assert_eq!(true, set.is_match("region1.v2.argotunnel.com")); + assert_eq!(true, set.is_match("region.v2.argotunnel.com")); + assert_eq!(false, set.is_match("xregion1.v2.argotunnel.com")); + assert_eq!(false, set.is_match("foo.region1.v2.argotunnel.com")); + } + #[test] fn compile_globset_normalizes_apex_and_subdomains() { let set = compile_denylist_globset(&["**.Example.COM.".to_string()]).unwrap(); From 970386e8b2a776d47ef6ac6c2bd67a3c0ed86744 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Thu, 26 Mar 2026 17:57:34 +0000 Subject: [PATCH 006/191] fix: root as std agent (#15881) --- codex-rs/core/src/agent/control.rs | 23 ++- .../src/tools/handlers/multi_agents_tests.rs | 153 +++++++++++++++++- .../handlers/multi_agents_v2/close_agent.rs | 9 ++ 3 files changed, 173 insertions(+), 12 deletions(-) diff --git a/codex-rs/core/src/agent/control.rs b/codex-rs/core/src/agent/control.rs index a3b9897688..1efd514f62 100644 --- a/codex-rs/core/src/agent/control.rs +++ b/codex-rs/core/src/agent/control.rs @@ -44,6 +44,7 @@ use tracing::warn; const AGENT_NAMES: &str = include_str!("agent_names.txt"); const FORKED_SPAWN_AGENT_OUTPUT_MESSAGE: &str = "You are the newly spawned agent. The prior conversation history was forked from your parent agent. Treat the next user message as your new task, and use the forked history only as background context."; +const ROOT_LAST_TASK_MESSAGE: &str = "Main thread"; #[derive(Clone, Debug, Default)] pub(crate) struct SpawnAgentOptions { @@ -650,12 +651,6 @@ impl AgentControl { let agent_path = current_agent_path .resolve(agent_reference) .map_err(CodexErr::UnsupportedOperation)?; - if agent_path.is_root() { - return Err(CodexErr::UnsupportedOperation( - "root is not a spawned agent".to_string(), - )); - } - if let Some(thread_id) = self.state.agent_id_for_path(&agent_path) { return Ok(thread_id); } @@ -737,7 +732,21 @@ impl AgentControl { }) }); - let mut agents = Vec::with_capacity(live_agents.len()); + let root_path = AgentPath::root(); + let mut agents = Vec::with_capacity(live_agents.len().saturating_add(1)); + if resolved_prefix + .as_ref() + .is_none_or(|prefix| agent_matches_prefix(Some(&root_path), prefix)) + && let Some(root_thread_id) = self.state.agent_id_for_path(&root_path) + && let Ok(root_thread) = state.get_thread(root_thread_id).await + { + agents.push(ListedAgent { + agent_name: root_path.to_string(), + agent_status: root_thread.agent_status().await, + last_task_message: Some(ROOT_LAST_TASK_MESSAGE.to_string()), + }); + } + for metadata in live_agents { let Some(thread_id) = metadata.agent_id else { continue; diff --git a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs index ab40b20587..e57491762c 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs @@ -430,6 +430,81 @@ async fn multi_agent_v2_spawn_returns_path_and_send_message_accepts_relative_pat })); } +#[tokio::test] +async fn multi_agent_v2_send_message_accepts_root_target_from_child() { + let (mut session, mut turn) = make_session_and_context().await; + let manager = thread_manager(); + let root = manager + .start_thread((*turn.config).clone()) + .await + .expect("root thread should start"); + session.services.agent_control = manager.agent_control(); + session.conversation_id = root.thread_id; + let mut config = (*turn.config).clone(); + config + .features + .enable(Feature::MultiAgentV2) + .expect("test config should allow feature update"); + turn.config = Arc::new(config); + + let child_path = AgentPath::try_from("/root/worker").expect("agent path"); + let child_thread_id = session + .services + .agent_control + .spawn_agent_with_metadata( + (*turn.config).clone(), + vec![UserInput::Text { + text: "inspect this repo".to_string(), + text_elements: Vec::new(), + }], + Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn { + parent_thread_id: root.thread_id, + depth: 1, + agent_path: Some(child_path.clone()), + agent_nickname: None, + agent_role: None, + })), + crate::agent::control::SpawnAgentOptions::default(), + ) + .await + .expect("worker spawn should succeed") + .thread_id; + session.conversation_id = child_thread_id; + turn.session_source = SessionSource::SubAgent(SubAgentSource::ThreadSpawn { + parent_thread_id: root.thread_id, + depth: 1, + agent_path: Some(child_path.clone()), + agent_nickname: None, + agent_role: None, + }); + + SendMessageHandlerV2 + .handle(invocation( + Arc::new(session), + Arc::new(turn), + "send_message", + function_payload(json!({ + "target": "/root", + "items": [{"type": "text", "text": "done"}] + })), + )) + .await + .expect("send_message should accept the root agent path"); + + assert!(manager.captured_ops().iter().any(|(id, op)| { + *id == root.thread_id + && matches!( + op, + Op::InterAgentCommunication { communication } + if communication.author == child_path + && communication.recipient == AgentPath::root() + && communication.other_recipients.is_empty() + && communication.content == "done" + && !communication.trigger_turn + ) + })); +} + #[tokio::test] async fn multi_agent_v2_list_agents_returns_completed_status_and_last_task_message() { let (mut session, mut turn) = make_session_and_context().await; @@ -496,11 +571,26 @@ async fn multi_agent_v2_list_agents_returns_completed_status_and_last_task_messa let result: ListAgentsResult = serde_json::from_str(&content).expect("list_agents result should be json"); - assert_eq!(result.agents.len(), 1); - assert_eq!(result.agents[0].agent_name, "/root/worker"); - assert_eq!(result.agents[0].agent_status, json!({"completed": "done"})); + let agent_names = result + .agents + .iter() + .map(|agent| agent.agent_name.as_str()) + .collect::>(); + assert_eq!(agent_names, vec!["/root", "/root/worker"]); + let root_agent = result + .agents + .iter() + .find(|agent| agent.agent_name == "/root") + .expect("root agent should be listed"); + assert_eq!(root_agent.last_task_message.as_deref(), Some("Main thread")); + let worker = result + .agents + .iter() + .find(|agent| agent.agent_name == "/root/worker") + .expect("worker agent should be listed"); + assert_eq!(worker.agent_status, json!({"completed": "done"})); assert_eq!( - result.agents[0].last_task_message.as_deref(), + worker.last_task_message.as_deref(), Some("inspect this repo") ); assert_eq!(success, Some(true)); @@ -647,7 +737,12 @@ async fn multi_agent_v2_list_agents_omits_closed_agents() { let result: ListAgentsResult = serde_json::from_str(&content).expect("list_agents result should be json"); - assert!(result.agents.is_empty()); + assert_eq!(result.agents.len(), 1); + assert_eq!(result.agents[0].agent_name, "/root"); + assert_eq!( + result.agents[0].last_task_message.as_deref(), + Some("Main thread") + ); } #[tokio::test] @@ -2036,6 +2131,54 @@ async fn multi_agent_v2_close_agent_accepts_task_name_target() { ); } +#[tokio::test] +async fn multi_agent_v2_close_agent_rejects_root_target_and_id() { + let (mut session, mut turn) = make_session_and_context().await; + let manager = thread_manager(); + let root = manager + .start_thread((*turn.config).clone()) + .await + .expect("root thread should start"); + session.services.agent_control = manager.agent_control(); + session.conversation_id = root.thread_id; + let mut config = (*turn.config).clone(); + config + .features + .enable(Feature::MultiAgentV2) + .expect("test config should allow feature update"); + turn.config = Arc::new(config); + + let session = Arc::new(session); + let turn = Arc::new(turn); + let root_path_error = CloseAgentHandlerV2 + .handle(invocation( + session.clone(), + turn.clone(), + "close_agent", + function_payload(json!({"target": "/root"})), + )) + .await + .expect_err("close_agent should reject the root path"); + assert_eq!( + root_path_error, + FunctionCallError::RespondToModel("root is not a spawned agent".to_string()) + ); + + let root_id_error = CloseAgentHandlerV2 + .handle(invocation( + session, + turn, + "close_agent", + function_payload(json!({"target": root.thread_id.to_string()})), + )) + .await + .expect_err("close_agent should reject the root thread id"); + assert_eq!( + root_id_error, + FunctionCallError::RespondToModel("root is not a spawned agent".to_string()) + ); +} + #[tokio::test] async fn close_agent_submits_shutdown_and_returns_previous_status() { let (mut session, turn) = make_session_and_context().await; diff --git a/codex-rs/core/src/tools/handlers/multi_agents_v2/close_agent.rs b/codex-rs/core/src/tools/handlers/multi_agents_v2/close_agent.rs index e2b43e6e12..2fe7048233 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_v2/close_agent.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_v2/close_agent.rs @@ -30,6 +30,15 @@ impl ToolHandler for Handler { .agent_control .get_agent_metadata(agent_id) .unwrap_or_default(); + if receiver_agent + .agent_path + .as_ref() + .is_some_and(AgentPath::is_root) + { + return Err(FunctionCallError::RespondToModel( + "root is not a spawned agent".to_string(), + )); + } session .send_event( &turn, From 2c54d4b1608d6305362dd2c776ee6ac83a37852a Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Thu, 26 Mar 2026 14:59:12 -0300 Subject: [PATCH 007/191] feat(tui): add terminal title support to tui app server (#15860) ## TR;DR Replicates the `/title` command from `tui` to `tui_app_server`. ## Problem The classic `tui` crate supports customizing the terminal window/tab title via `/title`, but the `tui_app_server` crate does not. Users on the app-server path have no way to configure what their terminal title shows (project name, status, spinner, thread, etc.), making it harder to identify Codex sessions across tabs or windows. ## Mental model The terminal title is a *status surface* -- conceptually parallel to the footer status line. Both surfaces are configurable lists of items, both share expensive inputs (git branch lookup, project root discovery), and both must be refreshed at the same lifecycle points. This change ports the classic `tui`'s design verbatim: 1. **`terminal_title.rs`** owns the low-level OSC write path and input sanitization. It strips control characters and bidi/invisible codepoints before placing untrusted text (model output, thread names, project paths) inside an escape sequence. 2. **`title_setup.rs`** defines `TerminalTitleItem` (the 8 configurable items) and `TerminalTitleSetupView` (the interactive picker that wraps `MultiSelectPicker`). 3. **`status_surfaces.rs`** is the shared refresh pipeline. It parses both surface configs once per refresh, warns about invalid items once per session, synchronizes the git-branch cache, then renders each surface from the same `StatusSurfaceSelections` snapshot. 4. **`chatwidget.rs`** sets `TerminalTitleStatusKind` at each state transition (Working, Thinking, Undoing, WaitingForBackgroundTerminal) and calls `refresh_terminal_title()` whenever relevant state changes. 5. **`app.rs`** handles the three setup events (confirm/preview/cancel), persists config via `ConfigEditsBuilder`, and clears the managed title on `Drop`. ## Non-goals - **Restoring the previous terminal title on exit.** There is no portable way to read the terminal's current title, so `Drop` clears the managed title rather than restoring it. - **Sharing code between `tui` and `tui_app_server`.** The implementation is a parallel copy, matching the existing pattern for the status-line feature. Extracting a shared crate is future work. ## Tradeoffs - **Duplicate code across crates.** The three core files (`terminal_title.rs`, `title_setup.rs`, `status_surfaces.rs`) are byte-for-byte copies from the classic `tui`. This was chosen for consistency with the existing status-line port and to avoid coupling the two crates at the dependency level. Future changes must be applied in both places. - **`status_surfaces.rs` is large (~660 lines).** It absorbs logic that previously lived inline in `chatwidget.rs` (status-line refresh, git branch management, project root discovery) plus all new terminal-title logic. This consolidation trades file size for a single place where both surfaces are coordinated. - **Spinner scheduling on every refresh.** The terminal title spinner (when active) schedules a frame every 100ms. This is the same pattern the status-indicator spinner already uses; the overhead is a timer registration, not a redraw. ## Architecture ``` /title command -> SlashCommand::Title -> open_terminal_title_setup() -> TerminalTitleSetupView (MultiSelectPicker) -> on_change: AppEvent::TerminalTitleSetupPreview -> preview_terminal_title() -> on_confirm: AppEvent::TerminalTitleSetup -> ConfigEditsBuilder + setup_terminal_title() -> on_cancel: AppEvent::TerminalTitleSetupCancelled -> cancel_terminal_title_setup() Runtime title refresh: state change (turn start, reasoning, undo, plan update, thread rename, ...) -> set terminal_title_status_kind -> refresh_terminal_title() -> status_surface_selections() (parse configs, collect invalids) -> refresh_terminal_title_from_selections() -> terminal_title_value_for_item() for each configured item -> assemble title string with separators -> skip if identical to last_terminal_title (dedup OSC writes) -> set_terminal_title() (sanitize + OSC 0 write) -> schedule spinner frame if animating Widget replacement: replace_chat_widget_with_app_server_thread() -> transfer last_terminal_title from old widget to new -> avoids redundant OSC clear+rewrite on session switch ``` ## Observability - Invalid terminal-title item IDs in config emit a one-per-session warning via `on_warning()` (gated by `terminal_title_invalid_items_warned` `AtomicBool`). - OSC write failures are logged at `tracing::debug` level. - Config persistence failures are logged at `tracing::error` and surfaced to the user via `add_error_message()`. ## Tests - `terminal_title.rs`: 4 unit tests covering sanitization (control chars, bidi codepoints, truncation) and OSC output format. - `title_setup.rs`: 3 tests covering setup view snapshot rendering, parse order preservation, and invalid-ID rejection. - `chatwidget/tests.rs`: Updated test helpers with new fields; existing tests continue to pass. --------- Co-authored-by: Eric Traut --- codex-rs/tui_app_server/src/app.rs | 58 ++ codex-rs/tui_app_server/src/app_event.rs | 12 + .../tui_app_server/src/bottom_pane/mod.rs | 3 + ...up__tests__terminal_title_setup_basic.snap | 21 + .../src/bottom_pane/title_setup.rs | 319 +++++++++ codex-rs/tui_app_server/src/chatwidget.rs | 390 ++++------- .../src/chatwidget/status_surfaces.rs | 662 ++++++++++++++++++ .../tui_app_server/src/chatwidget/tests.rs | 10 + codex-rs/tui_app_server/src/lib.rs | 1 + codex-rs/tui_app_server/src/slash_command.rs | 3 + codex-rs/tui_app_server/src/terminal_title.rs | 224 ++++++ 11 files changed, 1455 insertions(+), 248 deletions(-) create mode 100644 codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__title_setup__tests__terminal_title_setup_basic.snap create mode 100644 codex-rs/tui_app_server/src/bottom_pane/title_setup.rs create mode 100644 codex-rs/tui_app_server/src/chatwidget/status_surfaces.rs create mode 100644 codex-rs/tui_app_server/src/terminal_title.rs diff --git a/codex-rs/tui_app_server/src/app.rs b/codex-rs/tui_app_server/src/app.rs index dbef11ebc6..95e231b970 100644 --- a/codex-rs/tui_app_server/src/app.rs +++ b/codex-rs/tui_app_server/src/app.rs @@ -948,6 +948,8 @@ pub(crate) struct App { pub(crate) commit_anim_running: Arc, // Shared across ChatWidget instances so invalid status-line config warnings only emit once. status_line_invalid_items_warned: Arc, + // Shared across ChatWidget instances so invalid terminal-title config warnings only emit once. + terminal_title_invalid_items_warned: Arc, // Esc-backtracking state grouped pub(crate) backtrack: crate::app_backtrack::BacktrackState, @@ -1053,6 +1055,7 @@ impl App { model: Some(self.chat_widget.current_model().to_string()), startup_tooltip_override: None, status_line_invalid_items_warned: self.status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: self.terminal_title_invalid_items_warned.clone(), session_telemetry: self.session_telemetry.clone(), } } @@ -2728,6 +2731,14 @@ impl App { /// This helper copies every known nickname/role from `AgentNavigationState` into the /// replacement widget so that replayed collab items render agent names immediately. fn replace_chat_widget(&mut self, mut chat_widget: ChatWidget) { + // Transfer the last-written terminal title to the replacement widget + // so it knows what OSC title is currently displayed. Without this, the + // new widget would redundantly clear and rewrite the same title, causing + // a visible flicker in some terminals. + let previous_terminal_title = self.chat_widget.last_terminal_title.take(); + if chat_widget.last_terminal_title.is_none() { + chat_widget.last_terminal_title = previous_terminal_title; + } for (thread_id, entry) in self.agent_navigation.ordered_threads() { chat_widget.set_collab_agent_metadata( thread_id, @@ -3195,6 +3206,7 @@ impl App { } let status_line_invalid_items_warned = Arc::new(AtomicBool::new(false)); + let terminal_title_invalid_items_warned = Arc::new(AtomicBool::new(false)); let enhanced_keys_supported = tui.enhanced_keys_supported(); let wait_for_initial_session_configured = @@ -3226,6 +3238,8 @@ impl App { model: Some(model.clone()), startup_tooltip_override, status_line_invalid_items_warned: status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: terminal_title_invalid_items_warned + .clone(), session_telemetry: session_telemetry.clone(), }; (ChatWidget::new_with_app_event(init), Some(started)) @@ -3259,6 +3273,8 @@ impl App { model: config.model.clone(), startup_tooltip_override: None, status_line_invalid_items_warned: status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: terminal_title_invalid_items_warned + .clone(), session_telemetry: session_telemetry.clone(), }; (ChatWidget::new_with_app_event(init), Some(resumed)) @@ -3297,6 +3313,8 @@ impl App { model: config.model.clone(), startup_tooltip_override: None, status_line_invalid_items_warned: status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: terminal_title_invalid_items_warned + .clone(), session_telemetry: session_telemetry.clone(), }; (ChatWidget::new_with_app_event(init), Some(forked)) @@ -3332,6 +3350,7 @@ impl App { has_emitted_history_lines: false, commit_anim_running: Arc::new(AtomicBool::new(false)), status_line_invalid_items_warned: status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: terminal_title_invalid_items_warned.clone(), backtrack: BacktrackState::default(), backtrack_render_pending: false, feedback: feedback.clone(), @@ -5007,6 +5026,33 @@ impl App { AppEvent::StatusLineSetupCancelled => { self.chat_widget.cancel_status_line_setup(); } + AppEvent::TerminalTitleSetup { items } => { + let ids = items.iter().map(ToString::to_string).collect::>(); + let edit = codex_core::config::edit::terminal_title_items_edit(&ids); + let apply_result = ConfigEditsBuilder::new(&self.config.codex_home) + .with_edits([edit]) + .apply() + .await; + match apply_result { + Ok(()) => { + self.config.tui_terminal_title = Some(ids.clone()); + self.chat_widget.setup_terminal_title(items); + } + Err(err) => { + tracing::error!(error = %err, "failed to persist terminal title items; keeping previous selection"); + self.chat_widget.revert_terminal_title_setup_preview(); + self.chat_widget.add_error_message(format!( + "Failed to save terminal title items: {err}" + )); + } + } + } + AppEvent::TerminalTitleSetupPreview { items } => { + self.chat_widget.preview_terminal_title(items); + } + AppEvent::TerminalTitleSetupCancelled => { + self.chat_widget.cancel_terminal_title_setup(); + } AppEvent::SyntaxThemeSelected { name } => { let edit = codex_core::config::edit::syntax_theme_edit(&name); let apply_result = ConfigEditsBuilder::new(&self.config.codex_home) @@ -5649,6 +5695,14 @@ fn mcp_inventory_maps_from_statuses(statuses: Vec) -> McpInvent (tools, resources, resource_templates, auth_statuses) } +impl Drop for App { + fn drop(&mut self) { + if let Err(err) = self.chat_widget.clear_managed_terminal_title() { + tracing::debug!(error = %err, "failed to clear terminal title on app drop"); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -5978,6 +6032,7 @@ mod tests { model: Some(model), startup_tooltip_override: None, status_line_invalid_items_warned: app.status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: app.terminal_title_invalid_items_warned.clone(), session_telemetry: app.session_telemetry.clone(), }); @@ -8122,6 +8177,7 @@ guardian_approval = true enhanced_keys_supported: false, commit_anim_running: Arc::new(AtomicBool::new(false)), status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), backtrack: BacktrackState::default(), backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), @@ -8175,6 +8231,7 @@ guardian_approval = true enhanced_keys_supported: false, commit_anim_running: Arc::new(AtomicBool::new(false)), status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), backtrack: BacktrackState::default(), backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), @@ -9306,6 +9363,7 @@ guardian_approval = true model: Some(app.chat_widget.current_model().to_string()), startup_tooltip_override: None, status_line_invalid_items_warned: app.status_line_invalid_items_warned.clone(), + terminal_title_invalid_items_warned: app.terminal_title_invalid_items_warned.clone(), session_telemetry: app.session_telemetry.clone(), }); app.replace_chat_widget(replacement); diff --git a/codex-rs/tui_app_server/src/app_event.rs b/codex-rs/tui_app_server/src/app_event.rs index f5284fd610..e6d8bdecda 100644 --- a/codex-rs/tui_app_server/src/app_event.rs +++ b/codex-rs/tui_app_server/src/app_event.rs @@ -28,6 +28,7 @@ use codex_utils_approval_presets::ApprovalPreset; use crate::bottom_pane::ApprovalRequest; use crate::bottom_pane::StatusLineItem; +use crate::bottom_pane::TerminalTitleItem; use crate::history_cell::HistoryCell; use codex_core::config::types::ApprovalsReviewer; @@ -547,6 +548,17 @@ pub(crate) enum AppEvent { /// Dismiss the status-line setup UI without changing config. StatusLineSetupCancelled, + /// Apply a user-confirmed terminal-title item ordering/selection. + TerminalTitleSetup { + items: Vec, + }, + /// Apply a temporary terminal-title preview while the setup UI is open. + TerminalTitleSetupPreview { + items: Vec, + }, + /// Dismiss the terminal-title setup UI without changing config. + TerminalTitleSetupCancelled, + /// Apply a user-confirmed syntax theme selection. SyntaxThemeSelected { name: String, diff --git a/codex-rs/tui_app_server/src/bottom_pane/mod.rs b/codex-rs/tui_app_server/src/bottom_pane/mod.rs index dd90bc11bd..78cf6a2359 100644 --- a/codex-rs/tui_app_server/src/bottom_pane/mod.rs +++ b/codex-rs/tui_app_server/src/bottom_pane/mod.rs @@ -47,6 +47,7 @@ mod mcp_server_elicitation; mod multi_select_picker; mod request_user_input; mod status_line_setup; +mod title_setup; pub(crate) use app_link_view::AppLinkElicitationTarget; pub(crate) use app_link_view::AppLinkSuggestionType; pub(crate) use app_link_view::AppLinkView; @@ -100,6 +101,8 @@ pub(crate) use skills_toggle_view::SkillsToggleView; pub(crate) use status_line_setup::StatusLineItem; pub(crate) use status_line_setup::StatusLinePreviewData; pub(crate) use status_line_setup::StatusLineSetupView; +pub(crate) use title_setup::TerminalTitleItem; +pub(crate) use title_setup::TerminalTitleSetupView; mod paste_burst; mod pending_input_preview; mod pending_thread_approvals; diff --git a/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__title_setup__tests__terminal_title_setup_basic.snap b/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__title_setup__tests__terminal_title_setup_basic.snap new file mode 100644 index 0000000000..0316b957b4 --- /dev/null +++ b/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__title_setup__tests__terminal_title_setup_basic.snap @@ -0,0 +1,21 @@ +--- +source: tui_app_server/src/bottom_pane/title_setup.rs +expression: "render_lines(&view, 84)" +--- + + Configure Terminal Title + Select which items to display in the terminal title. + + Type to search + > +› [x] project Project name (falls back to current directory name) + [x] spinner Animated task spinner (omitted while idle or when animations… + [x] status Compact session status text (Ready, Working, Thinking) + [x] thread Current thread title (omitted until available) + [ ] app-name Codex app name + [ ] git-branch Current Git branch (omitted when unavailable) + [ ] model Current model name + [ ] task-progress Latest task progress from update_plan (omitted until availab… + + my-project ⠋ Working | Investigate flaky test + Use ↑↓ to navigate, ←→ to move, space to select, enter to confirm, esc to cancel. diff --git a/codex-rs/tui_app_server/src/bottom_pane/title_setup.rs b/codex-rs/tui_app_server/src/bottom_pane/title_setup.rs new file mode 100644 index 0000000000..6714d8b3ad --- /dev/null +++ b/codex-rs/tui_app_server/src/bottom_pane/title_setup.rs @@ -0,0 +1,319 @@ +//! Terminal title configuration view for customizing the terminal window/tab title. +//! +//! This module provides an interactive picker for selecting which items appear +//! in the terminal title. Users can: +//! +//! - Select items +//! - Reorder items +//! - Preview the rendered title + +use itertools::Itertools; +use ratatui::buffer::Buffer; +use ratatui::layout::Rect; +use ratatui::text::Line; +use strum::IntoEnumIterator; +use strum_macros::Display; +use strum_macros::EnumIter; +use strum_macros::EnumString; + +use crate::app_event::AppEvent; +use crate::app_event_sender::AppEventSender; +use crate::bottom_pane::CancellationEvent; +use crate::bottom_pane::bottom_pane_view::BottomPaneView; +use crate::bottom_pane::multi_select_picker::MultiSelectItem; +use crate::bottom_pane::multi_select_picker::MultiSelectPicker; +use crate::render::renderable::Renderable; + +/// Available items that can be displayed in the terminal title. +/// +/// Variants serialize to kebab-case identifiers (e.g. `AppName` -> `"app-name"`) +/// via strum. These identifiers are persisted in user config files, so renaming +/// or removing a variant is a breaking config change. +#[derive(EnumIter, EnumString, Display, Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[strum(serialize_all = "kebab-case")] +pub(crate) enum TerminalTitleItem { + /// Codex app name. + AppName, + /// Project root name, or a compact cwd fallback. + Project, + /// Animated task spinner while active. + Spinner, + /// Compact runtime status text. + Status, + /// Current thread title (if available). + Thread, + /// Current git branch (if available). + GitBranch, + /// Current model name. + Model, + /// Latest checklist task progress from `update_plan` (if available). + TaskProgress, +} + +impl TerminalTitleItem { + pub(crate) fn description(self) -> &'static str { + match self { + TerminalTitleItem::AppName => "Codex app name", + TerminalTitleItem::Project => "Project name (falls back to current directory name)", + TerminalTitleItem::Spinner => { + "Animated task spinner (omitted while idle or when animations are off)" + } + TerminalTitleItem::Status => "Compact session status text (Ready, Working, Thinking)", + TerminalTitleItem::Thread => "Current thread title (omitted until available)", + TerminalTitleItem::GitBranch => "Current Git branch (omitted when unavailable)", + TerminalTitleItem::Model => "Current model name", + TerminalTitleItem::TaskProgress => { + "Latest task progress from update_plan (omitted until available)" + } + } + } + + /// Example text used when previewing the title picker. + /// + /// These are illustrative sample values, not live data from the current + /// session. + pub(crate) fn preview_example(self) -> &'static str { + match self { + TerminalTitleItem::AppName => "codex", + TerminalTitleItem::Project => "my-project", + TerminalTitleItem::Spinner => "⠋", + TerminalTitleItem::Status => "Working", + TerminalTitleItem::Thread => "Investigate flaky test", + TerminalTitleItem::GitBranch => "feat/awesome-feature", + TerminalTitleItem::Model => "gpt-5.2-codex", + TerminalTitleItem::TaskProgress => "Tasks 2/5", + } + } + + /// Returns the separator to place before this item in a rendered title. + /// + /// The spinner gets a plain space on either side so it reads as + /// `my-project Working` rather than `my-project | | Working`. + /// All other adjacent items are joined with ` | `. + pub(crate) fn separator_from_previous(self, previous: Option) -> &'static str { + match previous { + None => "", + Some(previous) + if previous == TerminalTitleItem::Spinner || self == TerminalTitleItem::Spinner => + { + " " + } + Some(_) => " | ", + } + } +} + +fn parse_terminal_title_items(ids: impl Iterator) -> Option> +where + T: AsRef, +{ + // Treat parsing as all-or-nothing so preview/confirm callbacks never emit + // a partially interpreted ordering. Invalid ids are ignored when building + // the picker, but once the user is interacting with the picker we only want + // to persist or preview a fully valid selection. + ids.map(|id| id.as_ref().parse::()) + .collect::, _>>() + .ok() +} + +/// Interactive view for configuring terminal-title items. +pub(crate) struct TerminalTitleSetupView { + picker: MultiSelectPicker, +} + +impl TerminalTitleSetupView { + /// Creates the terminal-title picker, preserving the configured item order first. + /// + /// Unknown configured ids are skipped here instead of surfaced inline. The + /// main TUI still warns about them when rendering the actual title, but the + /// picker itself only exposes the selectable items it can meaningfully + /// preview and persist. + pub(crate) fn new(title_items: Option<&[String]>, app_event_tx: AppEventSender) -> Self { + let selected_items = title_items + .into_iter() + .flatten() + .filter_map(|id| id.parse::().ok()) + .unique() + .collect_vec(); + let selected_set = selected_items + .iter() + .copied() + .collect::>(); + let items = selected_items + .into_iter() + .map(|item| Self::title_select_item(item, /*enabled*/ true)) + .chain( + TerminalTitleItem::iter() + .filter(|item| !selected_set.contains(item)) + .map(|item| Self::title_select_item(item, /*enabled*/ false)), + ) + .collect(); + + Self { + picker: MultiSelectPicker::builder( + "Configure Terminal Title".to_string(), + Some("Select which items to display in the terminal title.".to_string()), + app_event_tx, + ) + .instructions(vec![ + "Use ↑↓ to navigate, ←→ to move, space to select, enter to confirm, esc to cancel." + .into(), + ]) + .items(items) + .enable_ordering() + .on_preview(|items| { + let items = parse_terminal_title_items( + items + .iter() + .filter(|item| item.enabled) + .map(|item| item.id.as_str()), + )?; + let mut preview = String::new(); + let mut previous = None; + for item in items.iter().copied() { + preview.push_str(item.separator_from_previous(previous)); + preview.push_str(item.preview_example()); + previous = Some(item); + } + if preview.is_empty() { + None + } else { + Some(Line::from(preview)) + } + }) + .on_change(|items, app_event| { + let Some(items) = parse_terminal_title_items( + items + .iter() + .filter(|item| item.enabled) + .map(|item| item.id.as_str()), + ) else { + return; + }; + app_event.send(AppEvent::TerminalTitleSetupPreview { items }); + }) + .on_confirm(|ids, app_event| { + let Some(items) = parse_terminal_title_items(ids.iter().map(String::as_str)) else { + return; + }; + app_event.send(AppEvent::TerminalTitleSetup { items }); + }) + .on_cancel(|app_event| { + app_event.send(AppEvent::TerminalTitleSetupCancelled); + }) + .build(), + } + } + + fn title_select_item(item: TerminalTitleItem, enabled: bool) -> MultiSelectItem { + MultiSelectItem { + id: item.to_string(), + name: item.to_string(), + description: Some(item.description().to_string()), + enabled, + } + } +} + +impl BottomPaneView for TerminalTitleSetupView { + fn handle_key_event(&mut self, key_event: crossterm::event::KeyEvent) { + self.picker.handle_key_event(key_event); + } + + fn is_complete(&self) -> bool { + self.picker.complete + } + + fn on_ctrl_c(&mut self) -> CancellationEvent { + self.picker.close(); + CancellationEvent::Handled + } +} + +impl Renderable for TerminalTitleSetupView { + fn render(&self, area: Rect, buf: &mut Buffer) { + self.picker.render(area, buf); + } + + fn desired_height(&self, width: u16) -> u16 { + self.picker.desired_height(width) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use insta::assert_snapshot; + use pretty_assertions::assert_eq; + use tokio::sync::mpsc::unbounded_channel; + + fn render_lines(view: &TerminalTitleSetupView, width: u16) -> String { + let height = view.desired_height(width); + let area = Rect::new(0, 0, width, height); + let mut buf = Buffer::empty(area); + view.render(area, &mut buf); + + let lines: Vec = (0..area.height) + .map(|row| { + let mut line = String::new(); + for col in 0..area.width { + let symbol = buf[(area.x + col, area.y + row)].symbol(); + if symbol.is_empty() { + line.push(' '); + } else { + line.push_str(symbol); + } + } + line + }) + .collect(); + lines.join("\n") + } + + #[test] + fn renders_title_setup_popup() { + let (tx_raw, _rx) = unbounded_channel::(); + let tx = AppEventSender::new(tx_raw); + let selected = [ + "project".to_string(), + "spinner".to_string(), + "status".to_string(), + "thread".to_string(), + ]; + let view = TerminalTitleSetupView::new(Some(&selected), tx); + assert_snapshot!("terminal_title_setup_basic", render_lines(&view, 84)); + } + + #[test] + fn parse_terminal_title_items_preserves_order() { + let items = + parse_terminal_title_items(["project", "spinner", "status", "thread"].into_iter()); + assert_eq!( + items, + Some(vec![ + TerminalTitleItem::Project, + TerminalTitleItem::Spinner, + TerminalTitleItem::Status, + TerminalTitleItem::Thread, + ]) + ); + } + + #[test] + fn parse_terminal_title_items_rejects_invalid_ids() { + let items = parse_terminal_title_items(["project", "not-a-title-item"].into_iter()); + assert_eq!(items, None); + } + + #[test] + fn parse_terminal_title_items_accepts_kebab_case_variants() { + let items = parse_terminal_title_items(["app-name", "git-branch"].into_iter()); + assert_eq!( + items, + Some(vec![ + TerminalTitleItem::AppName, + TerminalTitleItem::GitBranch, + ]) + ); + } +} diff --git a/codex-rs/tui_app_server/src/chatwidget.rs b/codex-rs/tui_app_server/src/chatwidget.rs index 7f6a9b4c68..636349aae5 100644 --- a/codex-rs/tui_app_server/src/chatwidget.rs +++ b/codex-rs/tui_app_server/src/chatwidget.rs @@ -48,6 +48,8 @@ use crate::audio_device::list_realtime_audio_device_names; use crate::bottom_pane::StatusLineItem; use crate::bottom_pane::StatusLinePreviewData; use crate::bottom_pane::StatusLineSetupView; +use crate::bottom_pane::TerminalTitleItem; +use crate::bottom_pane::TerminalTitleSetupView; use crate::mention_codec::LinkedMention; use crate::mention_codec::encode_history_mentions; use crate::model_catalog::ModelCatalog; @@ -57,6 +59,9 @@ use crate::status::StatusAccountDisplay; use crate::status::format_directory_display; use crate::status::format_tokens_compact; use crate::status::rate_limit_snapshot_display_for_limit; +use crate::terminal_title::SetTerminalTitleResult; +use crate::terminal_title::clear_terminal_title; +use crate::terminal_title::set_terminal_title; use crate::text_formatting::proper_join; use crate::version::CODEX_CLI_VERSION; use codex_app_server_protocol::AppSummary; @@ -350,6 +355,9 @@ use self::plugins::PluginsCacheState; mod realtime; use self::realtime::RealtimeConversationUiState; use self::realtime::RenderedUserMessageEvent; +mod status_surfaces; +use self::status_surfaces::CachedProjectRootName; +use self::status_surfaces::TerminalTitleStatusKind; use crate::streaming::chunking::AdaptiveChunkingPolicy; use crate::streaming::commit_tick::CommitTickScope; use crate::streaming::commit_tick::run_commit_tick; @@ -361,12 +369,14 @@ use codex_file_search::FileMatch; use codex_protocol::openai_models::InputModality; use codex_protocol::openai_models::ModelPreset; use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; +use codex_protocol::plan_tool::StepStatus; use codex_protocol::plan_tool::UpdatePlanArgs; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::SandboxPolicy; use codex_utils_approval_presets::ApprovalPreset; use codex_utils_approval_presets::builtin_approval_presets; use strum::IntoEnumIterator; +use unicode_segmentation::UnicodeSegmentation; const USER_SHELL_COMMAND_HELP_TITLE: &str = "Prefix a command with ! to run it locally"; const USER_SHELL_COMMAND_HELP_HINT: &str = "Example: !ls"; @@ -547,6 +557,8 @@ pub(crate) struct ChatWidgetInit { pub(crate) startup_tooltip_override: Option, // Shared latch so we only warn once about invalid status-line item IDs. pub(crate) status_line_invalid_items_warned: Arc, + // Shared latch so we only warn once about invalid terminal-title item IDs. + pub(crate) terminal_title_invalid_items_warned: Arc, pub(crate) session_telemetry: SessionTelemetry, } @@ -801,6 +813,8 @@ pub(crate) struct ChatWidget { // Guardian review keeps its own pending set so it can derive a single // footer summary from one or more in-flight review events. pending_guardian_review_status: PendingGuardianReviewStatus, + // Semantic status used for terminal-title status rendering. + terminal_title_status_kind: TerminalTitleStatusKind, // Previous status header to restore after a transient stream retry. retry_status_header: Option, // Set when commentary output completes; once stream queues go idle we restore the status row. @@ -869,6 +883,8 @@ pub(crate) struct ChatWidget { // later steer. This is cleared when the user submits a steer so the plan popup only appears // if a newer proposed plan arrives afterward. saw_plan_item_this_turn: bool, + // Latest `update_plan` checklist task counts for terminal-title rendering. + last_plan_progress: Option<(usize, usize)>, // Incremental buffer for streamed plan content. plan_delta_buffer: String, // True while a plan item is streaming. @@ -892,6 +908,22 @@ pub(crate) struct ChatWidget { session_network_proxy: Option, // Shared latch so we only warn once about invalid status-line item IDs. status_line_invalid_items_warned: Arc, + // Shared latch so we only warn once about invalid terminal-title item IDs. + terminal_title_invalid_items_warned: Arc, + // Last terminal title emitted, to avoid writing duplicate OSC updates. + pub(crate) last_terminal_title: Option, + // Original terminal-title config captured when the setup UI opens. + // + // The outer `Option` tracks whether a setup session is active (`Some`) + // or not (`None`). The inner `Option>` mirrors the shape + // of `config.tui_terminal_title` (which is `None` when using defaults). + // On cancel or persist-failure the inner value is restored to config; + // on confirm the outer is set to `None` to end the session. + terminal_title_setup_original_items: Option>>, + // Baseline instant used to animate spinner-prefixed title statuses. + terminal_title_animation_origin: Instant, + // Cached project-root display name keyed by cwd for status/title rendering. + status_line_project_root_name_cache: Option, // Cached git branch name for the status line (None if unknown). status_line_branch: Option, // CWD used to resolve the cached branch; change resets branch state. @@ -1584,12 +1616,15 @@ impl ChatWidget { fn update_task_running_state(&mut self) { self.bottom_pane .set_task_running(self.agent_turn_running || self.mcp_startup_status.is_some()); + self.refresh_terminal_title(); } fn restore_reasoning_status_header(&mut self) { if let Some(header) = extract_first_bold(&self.reasoning_buffer) { + self.terminal_title_status_kind = TerminalTitleStatusKind::Thinking; self.set_status_header(header); } else if self.bottom_pane.is_task_running() { + self.terminal_title_status_kind = TerminalTitleStatusKind::Working; self.set_status_header(String::from("Working")); } } @@ -1682,6 +1717,22 @@ impl ChatWidget { StatusDetailsCapitalization::Preserve, details_max_lines, ); + let title_uses_status = self + .config + .tui_terminal_title + .as_ref() + .is_some_and(|items| items.iter().any(|item| item == "status")); + let title_uses_spinner = self + .config + .tui_terminal_title + .as_ref() + .is_none_or(|items| items.iter().any(|item| item == "spinner")); + if title_uses_status + || (title_uses_spinner + && self.terminal_title_status_kind == TerminalTitleStatusKind::Undoing) + { + self.refresh_terminal_title(); + } } /// Convenience wrapper around [`Self::set_status`]; @@ -1719,57 +1770,7 @@ impl ChatWidget { /// a session id exists or before branch lookup completes), those items are skipped without /// placeholders so the line remains compact and stable. pub(crate) fn refresh_status_line(&mut self) { - let (items, invalid_items) = self.status_line_items_with_invalids(); - if self.thread_id.is_some() - && !invalid_items.is_empty() - && self - .status_line_invalid_items_warned - .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) - .is_ok() - { - let label = if invalid_items.len() == 1 { - "item" - } else { - "items" - }; - let message = format!( - "Ignored invalid status line {label}: {}.", - proper_join(invalid_items.as_slice()) - ); - self.on_warning(message); - } - if !items.contains(&StatusLineItem::GitBranch) { - self.status_line_branch = None; - self.status_line_branch_pending = false; - self.status_line_branch_lookup_complete = false; - } - let enabled = !items.is_empty(); - self.bottom_pane.set_status_line_enabled(enabled); - if !enabled { - self.set_status_line(/*status_line*/ None); - return; - } - - let cwd = self.status_line_cwd().to_path_buf(); - self.sync_status_line_branch_state(&cwd); - - if items.contains(&StatusLineItem::GitBranch) && !self.status_line_branch_lookup_complete { - self.request_status_line_branch(cwd); - } - - let mut parts = Vec::new(); - for item in items { - if let Some(value) = self.status_line_value_for_item(&item) { - parts.push(value); - } - } - - let line = if parts.is_empty() { - None - } else { - Some(Line::from(parts.join(" · "))) - }; - self.set_status_line(line); + self.refresh_status_surfaces(); } /// Records that status-line setup was canceled. @@ -1790,6 +1791,46 @@ impl ChatWidget { self.refresh_status_line(); } + /// Applies a temporary terminal-title selection while the setup UI is open. + pub(crate) fn preview_terminal_title(&mut self, items: Vec) { + if self.terminal_title_setup_original_items.is_none() { + self.terminal_title_setup_original_items = Some(self.config.tui_terminal_title.clone()); + } + + let ids = items.iter().map(ToString::to_string).collect::>(); + self.config.tui_terminal_title = Some(ids); + self.refresh_terminal_title(); + } + + /// Restores the terminal-title config that was active before the setup UI + /// opened, undoing any preview changes. No-op if no setup session is active. + pub(crate) fn revert_terminal_title_setup_preview(&mut self) { + let Some(original_items) = self.terminal_title_setup_original_items.take() else { + return; + }; + + self.config.tui_terminal_title = original_items; + self.refresh_terminal_title(); + } + + /// Dismisses the terminal-title setup UI and reverts to the pre-setup config. + pub(crate) fn cancel_terminal_title_setup(&mut self) { + tracing::info!("Terminal title setup canceled by user"); + self.revert_terminal_title_setup_preview(); + } + + /// Commits a confirmed terminal-title selection, ending the setup session. + /// + /// After this call, `revert_terminal_title_setup_preview` becomes a no-op + /// because the original config snapshot is discarded. + pub(crate) fn setup_terminal_title(&mut self, items: Vec) { + tracing::info!("terminal title setup confirmed with items: {items:#?}"); + let ids = items.iter().map(ToString::to_string).collect::>(); + self.terminal_title_setup_original_items = None; + self.config.tui_terminal_title = Some(ids); + self.refresh_terminal_title(); + } + /// Stores async git-branch lookup results for the current status-line cwd. /// /// Results are dropped when they target an out-of-date cwd to avoid rendering stale branch @@ -1802,17 +1843,7 @@ impl ChatWidget { self.status_line_branch = branch; self.status_line_branch_pending = false; self.status_line_branch_lookup_complete = true; - } - - /// Forces a new git-branch lookup when `GitBranch` is part of the configured status line. - fn request_status_line_branch_refresh(&mut self) { - let (items, _) = self.status_line_items_with_invalids(); - if items.is_empty() || !items.contains(&StatusLineItem::GitBranch) { - return; - } - let cwd = self.status_line_cwd().to_path_buf(); - self.sync_status_line_branch_state(&cwd); - self.request_status_line_branch(cwd); + self.refresh_status_surfaces(); } fn collect_runtime_metrics_delta(&mut self) { @@ -1885,6 +1916,7 @@ impl ChatWidget { Constrained::allow_only(event.sandbox_policy.clone()); } self.config.approvals_reviewer = event.approvals_reviewer; + self.status_line_project_root_name_cache = None; self.last_copyable_output = None; let forked_from_id = event.forked_from_id; let model_for_header = event.model.clone(); @@ -1899,6 +1931,7 @@ impl ChatWidget { mask.reasoning_effort = Some(event.reasoning_effort); } self.refresh_model_display(); + self.refresh_status_surfaces(); self.sync_fast_command_enabled(); self.sync_personality_command_enabled(); self.sync_plugins_command_enabled(); @@ -2005,6 +2038,7 @@ impl ChatWidget { fn on_thread_name_updated(&mut self, event: codex_protocol::protocol::ThreadNameUpdatedEvent) { if self.thread_id == Some(event.thread_id) { self.thread_name = event.thread_name; + self.refresh_terminal_title(); self.request_redraw(); } } @@ -2162,6 +2196,7 @@ impl ChatWidget { if let Some(header) = extract_first_bold(&self.reasoning_buffer) { // Update the shimmer header to the extracted reasoning chunk header. + self.terminal_title_status_kind = TerminalTitleStatusKind::Thinking; self.set_status_header(header); } else { // Fallback while we don't yet have a bold header: leave existing header as-is. @@ -2199,6 +2234,7 @@ impl ChatWidget { .set_turn_running(/*turn_running*/ true); self.saw_plan_update_this_turn = false; self.saw_plan_item_this_turn = false; + self.last_plan_progress = None; self.plan_delta_buffer.clear(); self.plan_item_active = false; self.adaptive_chunking.reset(); @@ -2213,6 +2249,7 @@ impl ChatWidget { self.pending_status_indicator_restore = false; self.bottom_pane .set_interrupt_hint_visible(/*visible*/ true); + self.terminal_title_status_kind = TerminalTitleStatusKind::Working; self.set_status_header(String::from("Working")); self.full_reasoning_buffer.clear(); self.reasoning_buffer.clear(); @@ -2948,6 +2985,7 @@ impl ChatWidget { self.update_task_running_state(); if restored_task_running && !self.bottom_pane.is_task_running() { self.bottom_pane.set_task_running(/*running*/ true); + self.refresh_terminal_title(); } self.refresh_pending_input_preview(); self.request_redraw(); @@ -2959,6 +2997,17 @@ impl ChatWidget { fn on_plan_update(&mut self, update: UpdatePlanArgs) { self.saw_plan_update_this_turn = true; + let total = update.plan.len(); + let completed = update + .plan + .iter() + .filter(|item| match &item.status { + StepStatus::Completed => true, + StepStatus::Pending | StepStatus::InProgress => false, + }) + .count(); + self.last_plan_progress = (total > 0).then_some((completed, total)); + self.refresh_terminal_title(); self.add_to_history(history_cell::new_plan_update(update)); } @@ -3277,6 +3326,7 @@ impl ChatWidget { self.bottom_pane.ensure_status_indicator(); self.bottom_pane .set_interrupt_hint_visible(/*visible*/ true); + self.terminal_title_status_kind = TerminalTitleStatusKind::WaitingForBackgroundTerminal; self.set_status( "Waiting for background terminal".to_string(), command_display.clone(), @@ -3738,6 +3788,7 @@ impl ChatWidget { self.bottom_pane.ensure_status_indicator(); self.bottom_pane .set_interrupt_hint_visible(/*visible*/ true); + self.terminal_title_status_kind = TerminalTitleStatusKind::Thinking; self.set_status_header(message); } @@ -3780,6 +3831,7 @@ impl ChatWidget { let message = event .message .unwrap_or_else(|| "Undo in progress...".to_string()); + self.terminal_title_status_kind = TerminalTitleStatusKind::Undoing; self.set_status_header(message); } @@ -3787,6 +3839,8 @@ impl ChatWidget { fn on_undo_completed(&mut self, event: UndoCompletedEvent) { let UndoCompletedEvent { success, message } = event; self.bottom_pane.hide_status_indicator(); + self.terminal_title_status_kind = TerminalTitleStatusKind::Working; + self.refresh_terminal_title(); let message = message.unwrap_or_else(|| { if success { "Undo completed successfully.".to_string() @@ -3806,6 +3860,7 @@ impl ChatWidget { self.retry_status_header = Some(self.current_status.header.clone()); } self.bottom_pane.ensure_status_indicator(); + self.terminal_title_status_kind = TerminalTitleStatusKind::Thinking; self.set_status( message, additional_details, @@ -3816,6 +3871,9 @@ impl ChatWidget { pub(crate) fn pre_draw_tick(&mut self) { self.bottom_pane.pre_draw_tick(); + if self.should_animate_terminal_title_spinner() { + self.refresh_terminal_title(); + } } /// Handle completion of an `AgentMessage` turn item. @@ -4352,6 +4410,7 @@ impl ChatWidget { model, startup_tooltip_override, status_line_invalid_items_warned, + terminal_title_invalid_items_warned, session_telemetry, } = common; let model = model.filter(|m| !m.trim().is_empty()); @@ -4446,6 +4505,7 @@ impl ChatWidget { full_reasoning_buffer: String::new(), current_status: StatusIndicatorState::working(), pending_guardian_review_status: PendingGuardianReviewStatus::default(), + terminal_title_status_kind: TerminalTitleStatusKind::Working, retry_status_header: None, pending_status_indicator_restore: false, suppress_queue_autosend: false, @@ -4470,6 +4530,7 @@ impl ChatWidget { had_work_activity: false, saw_plan_update_this_turn: false, saw_plan_item_this_turn: false, + last_plan_progress: None, plan_delta_buffer: String::new(), plan_item_active: false, last_separator_elapsed_secs: None, @@ -4481,6 +4542,11 @@ impl ChatWidget { current_cwd, session_network_proxy: None, status_line_invalid_items_warned, + terminal_title_invalid_items_warned, + last_terminal_title: None, + terminal_title_setup_original_items: None, + terminal_title_animation_origin: Instant::now(), + status_line_project_root_name_cache: None, status_line_branch: None, status_line_branch_cwd: None, status_line_branch_pending: false, @@ -4525,6 +4591,7 @@ impl ChatWidget { widget .bottom_pane .set_connectors_enabled(widget.connectors_enabled()); + widget.refresh_status_surfaces(); widget } @@ -5033,6 +5100,9 @@ impl ChatWidget { SlashCommand::DebugConfig => { self.add_debug_config_output(); } + SlashCommand::Title => { + self.open_terminal_title_setup(); + } SlashCommand::Statusline => { self.open_status_line_setup(); } @@ -7102,6 +7172,16 @@ impl ChatWidget { self.bottom_pane.show_view(Box::new(view)); } + fn open_terminal_title_setup(&mut self) { + let configured_terminal_title_items = self.configured_terminal_title_items(); + self.terminal_title_setup_original_items = Some(self.config.tui_terminal_title.clone()); + let view = TerminalTitleSetupView::new( + Some(configured_terminal_title_items.as_slice()), + self.app_event_tx.clone(), + ); + self.bottom_pane.show_view(Box::new(view)); + } + fn open_theme_picker(&mut self) { let codex_home = codex_core::config::find_codex_home().ok(); let terminal_width = self @@ -7116,192 +7196,6 @@ impl ChatWidget { self.bottom_pane.show_selection_view(params); } - /// Parses configured status-line ids into known items and collects unknown ids. - /// - /// Unknown ids are deduplicated in insertion order for warning messages. - fn status_line_items_with_invalids(&self) -> (Vec, Vec) { - let mut invalid = Vec::new(); - let mut invalid_seen = HashSet::new(); - let mut items = Vec::new(); - for id in self.configured_status_line_items() { - match id.parse::() { - Ok(item) => items.push(item), - Err(_) => { - if invalid_seen.insert(id.clone()) { - invalid.push(format!(r#""{id}""#)); - } - } - } - } - (items, invalid) - } - - fn configured_status_line_items(&self) -> Vec { - self.config.tui_status_line.clone().unwrap_or_else(|| { - DEFAULT_STATUS_LINE_ITEMS - .iter() - .map(ToString::to_string) - .collect() - }) - } - - fn status_line_cwd(&self) -> &Path { - self.current_cwd - .as_deref() - .unwrap_or(self.config.cwd.as_path()) - } - - fn status_line_project_root(&self) -> Option { - let cwd = self.status_line_cwd(); - if let Some(repo_root) = get_git_repo_root(cwd) { - return Some(repo_root); - } - - self.config - .config_layer_stack - .get_layers( - ConfigLayerStackOrdering::LowestPrecedenceFirst, - /*include_disabled*/ true, - ) - .iter() - .find_map(|layer| match &layer.name { - ConfigLayerSource::Project { dot_codex_folder } => { - dot_codex_folder.as_path().parent().map(Path::to_path_buf) - } - _ => None, - }) - } - - fn status_line_project_root_name(&self) -> Option { - self.status_line_project_root().map(|root| { - root.file_name() - .map(|name| name.to_string_lossy().to_string()) - .unwrap_or_else(|| format_directory_display(&root, /*max_width*/ None)) - }) - } - - /// Resets git-branch cache state when the status-line cwd changes. - /// - /// The branch cache is keyed by cwd because branch lookup is performed relative to that path. - /// Keeping stale branch values across cwd changes would surface incorrect repository context. - fn sync_status_line_branch_state(&mut self, cwd: &Path) { - if self - .status_line_branch_cwd - .as_ref() - .is_some_and(|path| path == cwd) - { - return; - } - self.status_line_branch_cwd = Some(cwd.to_path_buf()); - self.status_line_branch = None; - self.status_line_branch_pending = false; - self.status_line_branch_lookup_complete = false; - } - - /// Starts an async git-branch lookup unless one is already running. - /// - /// The resulting `StatusLineBranchUpdated` event carries the lookup cwd so callers can reject - /// stale completions after directory changes. - fn request_status_line_branch(&mut self, cwd: PathBuf) { - if self.status_line_branch_pending { - return; - } - self.status_line_branch_pending = true; - let tx = self.app_event_tx.clone(); - tokio::spawn(async move { - let branch = current_branch_name(&cwd).await; - tx.send(AppEvent::StatusLineBranchUpdated { cwd, branch }); - }); - } - - /// Resolves a display string for one configured status-line item. - /// - /// Returning `None` means "omit this item for now", not "configuration error". Callers rely on - /// this to keep partially available status lines readable while waiting for session, token, or - /// git metadata. - fn status_line_value_for_item(&self, item: &StatusLineItem) -> Option { - match item { - StatusLineItem::ModelName => Some(self.model_display_name().to_string()), - StatusLineItem::ModelWithReasoning => { - let label = - Self::status_line_reasoning_effort_label(self.effective_reasoning_effort()); - let fast_label = if self - .should_show_fast_status(self.current_model(), self.config.service_tier) - { - " fast" - } else { - "" - }; - Some(format!("{} {label}{fast_label}", self.model_display_name())) - } - StatusLineItem::CurrentDir => { - Some(format_directory_display( - self.status_line_cwd(), - /*max_width*/ None, - )) - } - StatusLineItem::ProjectRoot => self.status_line_project_root_name(), - StatusLineItem::GitBranch => self.status_line_branch.clone(), - StatusLineItem::UsedTokens => { - let usage = self.status_line_total_usage(); - let total = usage.tokens_in_context_window(); - if total <= 0 { - None - } else { - Some(format!("{} used", format_tokens_compact(total))) - } - } - StatusLineItem::ContextRemaining => self - .status_line_context_remaining_percent() - .map(|remaining| format!("{remaining}% left")), - StatusLineItem::ContextUsed => self - .status_line_context_used_percent() - .map(|used| format!("{used}% used")), - StatusLineItem::FiveHourLimit => { - let window = self - .rate_limit_snapshots_by_limit_id - .get("codex") - .and_then(|s| s.primary.as_ref()); - let label = window - .and_then(|window| window.window_minutes) - .map(get_limits_duration) - .unwrap_or_else(|| "5h".to_string()); - self.status_line_limit_display(window, &label) - } - StatusLineItem::WeeklyLimit => { - let window = self - .rate_limit_snapshots_by_limit_id - .get("codex") - .and_then(|s| s.secondary.as_ref()); - let label = window - .and_then(|window| window.window_minutes) - .map(get_limits_duration) - .unwrap_or_else(|| "weekly".to_string()); - self.status_line_limit_display(window, &label) - } - StatusLineItem::CodexVersion => Some(CODEX_CLI_VERSION.to_string()), - StatusLineItem::ContextWindowSize => self - .status_line_context_window_size() - .map(|cws| format!("{} window", format_tokens_compact(cws))), - StatusLineItem::TotalInputTokens => Some(format!( - "{} in", - format_tokens_compact(self.status_line_total_usage().input_tokens) - )), - StatusLineItem::TotalOutputTokens => Some(format!( - "{} out", - format_tokens_compact(self.status_line_total_usage().output_tokens) - )), - StatusLineItem::SessionId => self.thread_id.map(|id| id.to_string()), - StatusLineItem::FastMode => Some( - if matches!(self.config.service_tier, Some(ServiceTier::Fast)) { - "Fast on".to_string() - } else { - "Fast off".to_string() - }, - ), - } - } - fn status_line_context_window_size(&self) -> Option { self.token_info .as_ref() diff --git a/codex-rs/tui_app_server/src/chatwidget/status_surfaces.rs b/codex-rs/tui_app_server/src/chatwidget/status_surfaces.rs new file mode 100644 index 0000000000..d80003cc51 --- /dev/null +++ b/codex-rs/tui_app_server/src/chatwidget/status_surfaces.rs @@ -0,0 +1,662 @@ +//! Status-line and terminal-title rendering helpers for `ChatWidget`. +//! +//! Keeping this logic in a focused submodule makes the additive title/status +//! behavior easier to review without paging through the rest of `chatwidget.rs`. + +use super::*; + +/// Items shown in the terminal title when the user has not configured a +/// custom selection. Intentionally minimal: spinner + project name. +pub(super) const DEFAULT_TERMINAL_TITLE_ITEMS: [&str; 2] = ["spinner", "project"]; + +/// Braille-pattern dot-spinner frames for the terminal title animation. +pub(super) const TERMINAL_TITLE_SPINNER_FRAMES: [&str; 10] = + ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; + +/// Time between spinner frame advances in the terminal title. +pub(super) const TERMINAL_TITLE_SPINNER_INTERVAL: Duration = Duration::from_millis(100); + +/// Compact runtime states that can be rendered into the terminal title. +/// +/// This is intentionally smaller than the full status-header vocabulary. The +/// title needs short, stable labels, so callers map richer lifecycle events +/// onto one of these buckets before rendering. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub(super) enum TerminalTitleStatusKind { + Working, + WaitingForBackgroundTerminal, + Undoing, + #[default] + Thinking, +} + +#[derive(Debug)] +/// Parsed status-surface configuration for one refresh pass. +/// +/// The status line and terminal title share some expensive or stateful inputs +/// (notably git branch lookup and invalid-item warnings). This snapshot lets one +/// refresh pass compute those shared concerns once, then render both surfaces +/// from the same selection set. +struct StatusSurfaceSelections { + status_line_items: Vec, + invalid_status_line_items: Vec, + terminal_title_items: Vec, + invalid_terminal_title_items: Vec, +} + +impl StatusSurfaceSelections { + fn uses_git_branch(&self) -> bool { + self.status_line_items.contains(&StatusLineItem::GitBranch) + || self + .terminal_title_items + .contains(&TerminalTitleItem::GitBranch) + } +} + +/// Cached project-root display name keyed by the cwd used for the last lookup. +/// +/// Terminal-title refreshes can happen very frequently, so the title path avoids +/// repeatedly walking up the filesystem to rediscover the same project root name +/// while the working directory is unchanged. +#[derive(Clone, Debug)] +pub(super) struct CachedProjectRootName { + pub(super) cwd: PathBuf, + pub(super) root_name: Option, +} + +impl ChatWidget { + fn status_surface_selections(&self) -> StatusSurfaceSelections { + let (status_line_items, invalid_status_line_items) = self.status_line_items_with_invalids(); + let (terminal_title_items, invalid_terminal_title_items) = + self.terminal_title_items_with_invalids(); + StatusSurfaceSelections { + status_line_items, + invalid_status_line_items, + terminal_title_items, + invalid_terminal_title_items, + } + } + + fn warn_invalid_status_line_items_once(&mut self, invalid_items: &[String]) { + if self.thread_id.is_some() + && !invalid_items.is_empty() + && self + .status_line_invalid_items_warned + .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) + .is_ok() + { + let label = if invalid_items.len() == 1 { + "item" + } else { + "items" + }; + let message = format!( + "Ignored invalid status line {label}: {}.", + proper_join(invalid_items) + ); + self.on_warning(message); + } + } + + fn warn_invalid_terminal_title_items_once(&mut self, invalid_items: &[String]) { + if self.thread_id.is_some() + && !invalid_items.is_empty() + && self + .terminal_title_invalid_items_warned + .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) + .is_ok() + { + let label = if invalid_items.len() == 1 { + "item" + } else { + "items" + }; + let message = format!( + "Ignored invalid terminal title {label}: {}.", + proper_join(invalid_items) + ); + self.on_warning(message); + } + } + + fn sync_status_surface_shared_state(&mut self, selections: &StatusSurfaceSelections) { + if !selections.uses_git_branch() { + self.status_line_branch = None; + self.status_line_branch_pending = false; + self.status_line_branch_lookup_complete = false; + return; + } + + let cwd = self.status_line_cwd().to_path_buf(); + self.sync_status_line_branch_state(&cwd); + if !self.status_line_branch_lookup_complete { + self.request_status_line_branch(cwd); + } + } + + fn refresh_status_line_from_selections(&mut self, selections: &StatusSurfaceSelections) { + let enabled = !selections.status_line_items.is_empty(); + self.bottom_pane.set_status_line_enabled(enabled); + if !enabled { + self.set_status_line(/*status_line*/ None); + return; + } + + let mut parts = Vec::new(); + for item in &selections.status_line_items { + if let Some(value) = self.status_line_value_for_item(item) { + parts.push(value); + } + } + + let line = if parts.is_empty() { + None + } else { + Some(Line::from(parts.join(" · "))) + }; + self.set_status_line(line); + } + + /// Clears the terminal title Codex most recently wrote, if any. + /// + /// This does not attempt to restore the shell or terminal's previous title; + /// it only clears the managed title and updates the cache after a successful + /// OSC write. + pub(crate) fn clear_managed_terminal_title(&mut self) -> std::io::Result<()> { + if self.last_terminal_title.is_some() { + clear_terminal_title()?; + self.last_terminal_title = None; + } + + Ok(()) + } + + /// Renders and applies the terminal title for one parsed selection snapshot. + /// + /// Empty selections clear the managed title. Non-empty selections render the + /// current values in configured order, skip unavailable segments, and cache + /// the last successfully written title so redundant OSC writes are avoided. + /// When the `spinner` item is present in an animated running state, this also + /// schedules the next frame so the spinner keeps advancing. + fn refresh_terminal_title_from_selections(&mut self, selections: &StatusSurfaceSelections) { + if selections.terminal_title_items.is_empty() { + if let Err(err) = self.clear_managed_terminal_title() { + tracing::debug!(error = %err, "failed to clear terminal title"); + } + return; + } + + let now = Instant::now(); + let mut previous = None; + let title = selections + .terminal_title_items + .iter() + .copied() + .filter_map(|item| { + self.terminal_title_value_for_item(item, now) + .map(|value| (item, value)) + }) + .fold(String::new(), |mut title, (item, value)| { + title.push_str(item.separator_from_previous(previous)); + title.push_str(&value); + previous = Some(item); + title + }); + let title = (!title.is_empty()).then_some(title); + let should_animate_spinner = + self.should_animate_terminal_title_spinner_with_selections(selections); + if self.last_terminal_title == title { + if should_animate_spinner { + self.frame_requester + .schedule_frame_in(TERMINAL_TITLE_SPINNER_INTERVAL); + } + return; + } + match title { + Some(title) => match set_terminal_title(&title) { + Ok(SetTerminalTitleResult::Applied) => { + self.last_terminal_title = Some(title); + } + Ok(SetTerminalTitleResult::NoVisibleContent) => { + if let Err(err) = self.clear_managed_terminal_title() { + tracing::debug!(error = %err, "failed to clear terminal title"); + } + } + Err(err) => { + tracing::debug!(error = %err, "failed to set terminal title"); + } + }, + None => { + if let Err(err) = self.clear_managed_terminal_title() { + tracing::debug!(error = %err, "failed to clear terminal title"); + } + } + } + + if should_animate_spinner { + self.frame_requester + .schedule_frame_in(TERMINAL_TITLE_SPINNER_INTERVAL); + } + } + + /// Recomputes both status surfaces from one shared config snapshot. + /// + /// This is the common refresh entrypoint for the footer status line and the + /// terminal title. It parses both configurations once, emits invalid-item + /// warnings once, synchronizes shared cached state (such as git-branch + /// lookup), then renders each surface from that shared snapshot. + pub(crate) fn refresh_status_surfaces(&mut self) { + let selections = self.status_surface_selections(); + self.warn_invalid_status_line_items_once(&selections.invalid_status_line_items); + self.warn_invalid_terminal_title_items_once(&selections.invalid_terminal_title_items); + self.sync_status_surface_shared_state(&selections); + self.refresh_status_line_from_selections(&selections); + self.refresh_terminal_title_from_selections(&selections); + } + + /// Recomputes and emits the terminal title from config and runtime state. + pub(crate) fn refresh_terminal_title(&mut self) { + let selections = self.status_surface_selections(); + self.warn_invalid_terminal_title_items_once(&selections.invalid_terminal_title_items); + self.sync_status_surface_shared_state(&selections); + self.refresh_terminal_title_from_selections(&selections); + } + + pub(super) fn request_status_line_branch_refresh(&mut self) { + let selections = self.status_surface_selections(); + if !selections.uses_git_branch() { + return; + } + let cwd = self.status_line_cwd().to_path_buf(); + self.sync_status_line_branch_state(&cwd); + self.request_status_line_branch(cwd); + } + + /// Parses configured status-line ids into known items and collects unknown ids. + /// + /// Unknown ids are deduplicated in insertion order for warning messages. + fn status_line_items_with_invalids(&self) -> (Vec, Vec) { + parse_items_with_invalids(self.configured_status_line_items()) + } + + pub(super) fn configured_status_line_items(&self) -> Vec { + self.config.tui_status_line.clone().unwrap_or_else(|| { + DEFAULT_STATUS_LINE_ITEMS + .iter() + .map(ToString::to_string) + .collect() + }) + } + + /// Parses configured terminal-title ids into known items and collects unknown ids. + /// + /// Unknown ids are deduplicated in insertion order for warning messages. + fn terminal_title_items_with_invalids(&self) -> (Vec, Vec) { + parse_items_with_invalids(self.configured_terminal_title_items()) + } + + /// Returns the configured terminal-title ids, or the default ordering when unset. + pub(super) fn configured_terminal_title_items(&self) -> Vec { + self.config.tui_terminal_title.clone().unwrap_or_else(|| { + DEFAULT_TERMINAL_TITLE_ITEMS + .iter() + .map(ToString::to_string) + .collect() + }) + } + + fn status_line_cwd(&self) -> &Path { + self.current_cwd + .as_deref() + .unwrap_or(self.config.cwd.as_path()) + } + + /// Resolves the project root associated with `cwd`. + /// + /// Git repository root wins when available. Otherwise we fall back to the + /// nearest project config layer so non-git projects can still surface a + /// stable project label. + fn status_line_project_root_for_cwd(&self, cwd: &Path) -> Option { + if let Some(repo_root) = get_git_repo_root(cwd) { + return Some(repo_root); + } + + self.config + .config_layer_stack + .get_layers( + ConfigLayerStackOrdering::LowestPrecedenceFirst, + /*include_disabled*/ true, + ) + .iter() + .find_map(|layer| match &layer.name { + ConfigLayerSource::Project { dot_codex_folder } => { + dot_codex_folder.as_path().parent().map(Path::to_path_buf) + } + _ => None, + }) + } + + fn status_line_project_root_name_for_cwd(&self, cwd: &Path) -> Option { + self.status_line_project_root_for_cwd(cwd).map(|root| { + root.file_name() + .map(|name| name.to_string_lossy().to_string()) + .unwrap_or_else(|| format_directory_display(&root, /*max_width*/ None)) + }) + } + + /// Returns a cached project-root display name for the active cwd. + fn status_line_project_root_name(&mut self) -> Option { + let cwd = self.status_line_cwd().to_path_buf(); + if let Some(cache) = &self.status_line_project_root_name_cache + && cache.cwd == cwd + { + return cache.root_name.clone(); + } + + let root_name = self.status_line_project_root_name_for_cwd(&cwd); + self.status_line_project_root_name_cache = Some(CachedProjectRootName { + cwd, + root_name: root_name.clone(), + }); + root_name + } + + /// Produces the terminal-title `project` value. + /// + /// This prefers the cached project-root name and falls back to the current + /// directory name when no project root can be inferred. + fn terminal_title_project_name(&mut self) -> Option { + let project = self.status_line_project_root_name().or_else(|| { + let cwd = self.status_line_cwd(); + Some( + cwd.file_name() + .map(|name| name.to_string_lossy().to_string()) + .unwrap_or_else(|| format_directory_display(cwd, /*max_width*/ None)), + ) + })?; + Some(Self::truncate_terminal_title_part( + project, /*max_chars*/ 24, + )) + } + + /// Resets git-branch cache state when the status-line cwd changes. + /// + /// The branch cache is keyed by cwd because branch lookup is performed relative to that path. + /// Keeping stale branch values across cwd changes would surface incorrect repository context. + fn sync_status_line_branch_state(&mut self, cwd: &Path) { + if self + .status_line_branch_cwd + .as_ref() + .is_some_and(|path| path == cwd) + { + return; + } + self.status_line_branch_cwd = Some(cwd.to_path_buf()); + self.status_line_branch = None; + self.status_line_branch_pending = false; + self.status_line_branch_lookup_complete = false; + } + + /// Starts an async git-branch lookup unless one is already running. + /// + /// The resulting `StatusLineBranchUpdated` event carries the lookup cwd so callers can reject + /// stale completions after directory changes. + fn request_status_line_branch(&mut self, cwd: PathBuf) { + if self.status_line_branch_pending { + return; + } + self.status_line_branch_pending = true; + let tx = self.app_event_tx.clone(); + tokio::spawn(async move { + let branch = current_branch_name(&cwd).await; + tx.send(AppEvent::StatusLineBranchUpdated { cwd, branch }); + }); + } + + /// Resolves a display string for one configured status-line item. + /// + /// Returning `None` means "omit this item for now", not "configuration error". Callers rely on + /// this to keep partially available status lines readable while waiting for session, token, or + /// git metadata. + pub(super) fn status_line_value_for_item(&mut self, item: &StatusLineItem) -> Option { + match item { + StatusLineItem::ModelName => Some(self.model_display_name().to_string()), + StatusLineItem::ModelWithReasoning => { + let label = + Self::status_line_reasoning_effort_label(self.effective_reasoning_effort()); + let fast_label = if self + .should_show_fast_status(self.current_model(), self.config.service_tier) + { + " fast" + } else { + "" + }; + Some(format!("{} {label}{fast_label}", self.model_display_name())) + } + StatusLineItem::CurrentDir => { + Some(format_directory_display( + self.status_line_cwd(), + /*max_width*/ None, + )) + } + StatusLineItem::ProjectRoot => self.status_line_project_root_name(), + StatusLineItem::GitBranch => self.status_line_branch.clone(), + StatusLineItem::UsedTokens => { + let usage = self.status_line_total_usage(); + let total = usage.tokens_in_context_window(); + if total <= 0 { + None + } else { + Some(format!("{} used", format_tokens_compact(total))) + } + } + StatusLineItem::ContextRemaining => self + .status_line_context_remaining_percent() + .map(|remaining| format!("{remaining}% left")), + StatusLineItem::ContextUsed => self + .status_line_context_used_percent() + .map(|used| format!("{used}% used")), + StatusLineItem::FiveHourLimit => { + let window = self + .rate_limit_snapshots_by_limit_id + .get("codex") + .and_then(|s| s.primary.as_ref()); + let label = window + .and_then(|window| window.window_minutes) + .map(get_limits_duration) + .unwrap_or_else(|| "5h".to_string()); + self.status_line_limit_display(window, &label) + } + StatusLineItem::WeeklyLimit => { + let window = self + .rate_limit_snapshots_by_limit_id + .get("codex") + .and_then(|s| s.secondary.as_ref()); + let label = window + .and_then(|window| window.window_minutes) + .map(get_limits_duration) + .unwrap_or_else(|| "weekly".to_string()); + self.status_line_limit_display(window, &label) + } + StatusLineItem::CodexVersion => Some(CODEX_CLI_VERSION.to_string()), + StatusLineItem::ContextWindowSize => self + .status_line_context_window_size() + .map(|cws| format!("{} window", format_tokens_compact(cws))), + StatusLineItem::TotalInputTokens => Some(format!( + "{} in", + format_tokens_compact(self.status_line_total_usage().input_tokens) + )), + StatusLineItem::TotalOutputTokens => Some(format!( + "{} out", + format_tokens_compact(self.status_line_total_usage().output_tokens) + )), + StatusLineItem::SessionId => self.thread_id.map(|id| id.to_string()), + StatusLineItem::FastMode => Some( + if matches!(self.config.service_tier, Some(ServiceTier::Fast)) { + "Fast on".to_string() + } else { + "Fast off".to_string() + }, + ), + } + } + + /// Resolves one configured terminal-title item into a displayable segment. + /// + /// Returning `None` means "omit this segment for now" so callers can keep + /// the configured order while hiding values that are not yet available. + fn terminal_title_value_for_item( + &mut self, + item: TerminalTitleItem, + now: Instant, + ) -> Option { + match item { + TerminalTitleItem::AppName => Some("codex".to_string()), + TerminalTitleItem::Project => self.terminal_title_project_name(), + TerminalTitleItem::Spinner => self.terminal_title_spinner_text_at(now), + TerminalTitleItem::Status => Some(self.terminal_title_status_text()), + TerminalTitleItem::Thread => self.thread_name.as_ref().and_then(|name| { + let trimmed = name.trim(); + if trimmed.is_empty() { + None + } else { + Some(Self::truncate_terminal_title_part( + trimmed.to_string(), + /*max_chars*/ 48, + )) + } + }), + TerminalTitleItem::GitBranch => self.status_line_branch.as_ref().map(|branch| { + Self::truncate_terminal_title_part(branch.clone(), /*max_chars*/ 32) + }), + TerminalTitleItem::Model => Some(Self::truncate_terminal_title_part( + self.model_display_name().to_string(), + /*max_chars*/ 32, + )), + TerminalTitleItem::TaskProgress => self.terminal_title_task_progress(), + } + } + + /// Computes the compact runtime status label used by the terminal title. + /// + /// Startup takes precedence over normal task states, and idle state renders + /// as `Ready` regardless of the last active status bucket. + pub(super) fn terminal_title_status_text(&self) -> String { + if self.mcp_startup_status.is_some() { + return "Starting".to_string(); + } + + match self.terminal_title_status_kind { + TerminalTitleStatusKind::Working if !self.bottom_pane.is_task_running() => { + "Ready".to_string() + } + TerminalTitleStatusKind::WaitingForBackgroundTerminal + if !self.bottom_pane.is_task_running() => + { + "Ready".to_string() + } + TerminalTitleStatusKind::Thinking if !self.bottom_pane.is_task_running() => { + "Ready".to_string() + } + TerminalTitleStatusKind::Working => "Working".to_string(), + TerminalTitleStatusKind::WaitingForBackgroundTerminal => "Waiting".to_string(), + TerminalTitleStatusKind::Undoing => "Undoing".to_string(), + TerminalTitleStatusKind::Thinking => "Thinking".to_string(), + } + } + + pub(super) fn terminal_title_spinner_text_at(&self, now: Instant) -> Option { + if !self.config.animations { + return None; + } + + if !self.terminal_title_has_active_progress() { + return None; + } + + Some(self.terminal_title_spinner_frame_at(now).to_string()) + } + + fn terminal_title_spinner_frame_at(&self, now: Instant) -> &'static str { + let elapsed = now.saturating_duration_since(self.terminal_title_animation_origin); + let frame_index = + (elapsed.as_millis() / TERMINAL_TITLE_SPINNER_INTERVAL.as_millis()) as usize; + TERMINAL_TITLE_SPINNER_FRAMES[frame_index % TERMINAL_TITLE_SPINNER_FRAMES.len()] + } + + fn terminal_title_uses_spinner(&self) -> bool { + self.config + .tui_terminal_title + .as_ref() + .is_none_or(|items| items.iter().any(|item| item == "spinner")) + } + + fn terminal_title_has_active_progress(&self) -> bool { + self.mcp_startup_status.is_some() + || self.bottom_pane.is_task_running() + || self.terminal_title_status_kind == TerminalTitleStatusKind::Undoing + } + + pub(super) fn should_animate_terminal_title_spinner(&self) -> bool { + self.config.animations + && self.terminal_title_uses_spinner() + && self.terminal_title_has_active_progress() + } + + fn should_animate_terminal_title_spinner_with_selections( + &self, + selections: &StatusSurfaceSelections, + ) -> bool { + self.config.animations + && selections + .terminal_title_items + .contains(&TerminalTitleItem::Spinner) + && self.terminal_title_has_active_progress() + } + + /// Formats the last `update_plan` progress snapshot for terminal-title display. + pub(super) fn terminal_title_task_progress(&self) -> Option { + let (completed, total) = self.last_plan_progress?; + if total == 0 { + return None; + } + Some(format!("Tasks {completed}/{total}")) + } + + /// Truncates a title segment by grapheme cluster and appends `...` when needed. + pub(super) fn truncate_terminal_title_part(value: String, max_chars: usize) -> String { + if max_chars == 0 { + return String::new(); + } + + let mut graphemes = value.graphemes(true); + let head: String = graphemes.by_ref().take(max_chars).collect(); + if graphemes.next().is_none() || max_chars <= 3 { + return head; + } + + let mut truncated = head.graphemes(true).take(max_chars - 3).collect::(); + truncated.push_str("..."); + truncated + } +} + +fn parse_items_with_invalids(ids: impl IntoIterator) -> (Vec, Vec) +where + T: std::str::FromStr, +{ + let mut invalid = Vec::new(); + let mut invalid_seen = HashSet::new(); + let mut items = Vec::new(); + for id in ids { + match id.parse::() { + Ok(item) => items.push(item), + Err(_) => { + if invalid_seen.insert(id.clone()) { + invalid.push(format!(r#""{id}""#)); + } + } + } + } + (items, invalid) +} diff --git a/codex-rs/tui_app_server/src/chatwidget/tests.rs b/codex-rs/tui_app_server/src/chatwidget/tests.rs index a74b43fa81..8191acc3db 100644 --- a/codex-rs/tui_app_server/src/chatwidget/tests.rs +++ b/codex-rs/tui_app_server/src/chatwidget/tests.rs @@ -1946,6 +1946,7 @@ async fn helpers_are_available_and_do_not_panic() { model: Some(resolved_model), startup_tooltip_override: None, status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), session_telemetry, }; let mut w = ChatWidget::new_with_app_event(init); @@ -2048,6 +2049,7 @@ async fn make_chatwidget_manual( stream_controller: None, plan_stream_controller: None, pending_guardian_review_status: PendingGuardianReviewStatus::default(), + terminal_title_status_kind: TerminalTitleStatusKind::Working, last_copyable_output: None, running_commands: HashMap::new(), collab_agent_metadata: HashMap::new(), @@ -2099,6 +2101,7 @@ async fn make_chatwidget_manual( had_work_activity: false, saw_plan_update_this_turn: false, saw_plan_item_this_turn: false, + last_plan_progress: None, plan_delta_buffer: String::new(), plan_item_active: false, last_separator_elapsed_secs: None, @@ -2110,6 +2113,11 @@ async fn make_chatwidget_manual( current_cwd: None, session_network_proxy: None, status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), + last_terminal_title: None, + terminal_title_setup_original_items: None, + terminal_title_animation_origin: Instant::now(), + status_line_project_root_name_cache: None, status_line_branch: None, status_line_branch_cwd: None, status_line_branch_pending: false, @@ -6848,6 +6856,7 @@ async fn collaboration_modes_defaults_to_code_on_startup() { model: Some(resolved_model.clone()), startup_tooltip_override: None, status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), session_telemetry, }; @@ -6892,6 +6901,7 @@ async fn experimental_mode_plan_is_ignored_on_startup() { model: Some(resolved_model.clone()), startup_tooltip_override: None, status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), session_telemetry, }; diff --git a/codex-rs/tui_app_server/src/lib.rs b/codex-rs/tui_app_server/src/lib.rs index 1e40866855..22de8b38c9 100644 --- a/codex-rs/tui_app_server/src/lib.rs +++ b/codex-rs/tui_app_server/src/lib.rs @@ -141,6 +141,7 @@ mod status_indicator_widget; mod streaming; mod style; mod terminal_palette; +mod terminal_title; mod text_formatting; mod theme_picker; mod tooltips; diff --git a/codex-rs/tui_app_server/src/slash_command.rs b/codex-rs/tui_app_server/src/slash_command.rs index 2281204002..ec624d3fb9 100644 --- a/codex-rs/tui_app_server/src/slash_command.rs +++ b/codex-rs/tui_app_server/src/slash_command.rs @@ -38,6 +38,7 @@ pub enum SlashCommand { Mention, Status, DebugConfig, + Title, Statusline, Theme, Mcp, @@ -86,6 +87,7 @@ impl SlashCommand { SlashCommand::Skills => "use skills to improve how Codex performs specific tasks", SlashCommand::Status => "show current session configuration and token usage", SlashCommand::DebugConfig => "show config layers and requirement sources for debugging", + SlashCommand::Title => "configure which items appear in the terminal title", SlashCommand::Statusline => "configure which items appear in the status line", SlashCommand::Theme => "choose a syntax highlighting theme", SlashCommand::Ps => "list background terminals", @@ -180,6 +182,7 @@ impl SlashCommand { SlashCommand::Agent | SlashCommand::MultiAgents => true, SlashCommand::Statusline => false, SlashCommand::Theme => false, + SlashCommand::Title => false, } } diff --git a/codex-rs/tui_app_server/src/terminal_title.rs b/codex-rs/tui_app_server/src/terminal_title.rs new file mode 100644 index 0000000000..86c51e4b6e --- /dev/null +++ b/codex-rs/tui_app_server/src/terminal_title.rs @@ -0,0 +1,224 @@ +//! Terminal-title output helpers for the TUI. +//! +//! This module owns the low-level OSC title write path and the sanitization +//! that happens immediately before we emit it. It is intentionally narrow: +//! callers decide when the title should change and whether an empty title means +//! "leave the old title alone" or "clear the title Codex last wrote". +//! This module does not attempt to read or restore the terminal's previous +//! title because that is not portable across terminals. +//! +//! Sanitization is necessary because title content is assembled from untrusted +//! text sources such as model output, thread names, project paths, and config. +//! Before we place that text inside an OSC sequence, we strip: +//! - control characters that could terminate or reshape the escape sequence +//! - bidi/invisible formatting codepoints that can visually reorder or hide +//! text (the same family of issues discussed in Trojan Source writeups) +//! - redundant whitespace that would make titles noisy or hard to scan + +use std::fmt; +use std::io; +use std::io::IsTerminal; +use std::io::stdout; + +use crossterm::Command; +use ratatui::crossterm::execute; + +/// Practical upper bound on title length, measured in Rust `char`s. +/// +/// Most terminals silently truncate titles beyond a few hundred characters. +/// 240 leaves headroom for the OSC framing bytes while keeping titles +/// readable in tab bars and window managers. +const MAX_TERMINAL_TITLE_CHARS: usize = 240; + +/// Outcome of a [`set_terminal_title`] call. +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub(crate) enum SetTerminalTitleResult { + /// A sanitized title was written, or stdout is not a terminal so no write was needed. + Applied, + /// Sanitization removed every visible character, so no title was emitted. + /// + /// This is distinct from clearing the title. Callers decide whether an + /// empty post-sanitization value should result in no-op behavior, clearing + /// the title Codex manages, or some other fallback. + NoVisibleContent, +} + +/// Writes a sanitized OSC window-title sequence to stdout. +/// +/// The input is treated as untrusted display text: control characters, +/// invisible formatting characters, and redundant whitespace are removed before +/// the title is emitted. If sanitization removes all visible content, the +/// function returns [`SetTerminalTitleResult::NoVisibleContent`] instead of +/// clearing the title because clearing and restoring are policy decisions for +/// higher-level callers. Mechanically, sanitization collapses whitespace runs +/// to single spaces, drops disallowed codepoints, and bounds the result to +/// [`MAX_TERMINAL_TITLE_CHARS`] visible characters before writing OSC 0. +pub(crate) fn set_terminal_title(title: &str) -> io::Result { + if !stdout().is_terminal() { + return Ok(SetTerminalTitleResult::Applied); + } + + let title = sanitize_terminal_title(title); + if title.is_empty() { + return Ok(SetTerminalTitleResult::NoVisibleContent); + } + + execute!(stdout(), SetWindowTitle(title))?; + Ok(SetTerminalTitleResult::Applied) +} + +/// Clears the current terminal title by writing an empty OSC title payload. +/// +/// This clears the visible title; it does not restore whatever title the shell +/// or a previous program may have set before Codex started managing the title. +pub(crate) fn clear_terminal_title() -> io::Result<()> { + if !stdout().is_terminal() { + return Ok(()); + } + + execute!(stdout(), SetWindowTitle(String::new())) +} + +#[derive(Debug, Clone)] +struct SetWindowTitle(String); + +impl Command for SetWindowTitle { + fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { + // xterm/ctlseqs documents OSC 0/2 title sequences with ST (ESC \) termination. + // Most terminals also accept BEL for compatibility, but ST is the canonical form. + write!(f, "\x1b]0;{}\x1b\\", self.0) + } + + #[cfg(windows)] + fn execute_winapi(&self) -> io::Result<()> { + Err(std::io::Error::other( + "tried to execute SetWindowTitle using WinAPI; use ANSI instead", + )) + } + + #[cfg(windows)] + fn is_ansi_code_supported(&self) -> bool { + true + } +} + +/// Normalizes untrusted title text into a single bounded display line. +/// +/// This removes terminal control characters, strips invisible/bidi formatting +/// characters, collapses any whitespace run into a single ASCII space, and +/// truncates after [`MAX_TERMINAL_TITLE_CHARS`] emitted characters. +fn sanitize_terminal_title(title: &str) -> String { + let mut sanitized = String::new(); + let mut chars_written = 0; + let mut pending_space = false; + + for ch in title.chars() { + if ch.is_whitespace() { + // Only set pending if we've already written content; this + // strips leading whitespace without an extra trim pass. + pending_space = !sanitized.is_empty(); + continue; + } + + if is_disallowed_terminal_title_char(ch) { + continue; + } + + if pending_space { + let remaining = MAX_TERMINAL_TITLE_CHARS.saturating_sub(chars_written); + if remaining > 1 { + sanitized.push(' '); + chars_written += 1; + pending_space = false; + } + } + + if chars_written >= MAX_TERMINAL_TITLE_CHARS { + break; + } + + sanitized.push(ch); + chars_written += 1; + } + + sanitized +} + +/// Returns whether `ch` should be dropped from terminal-title output. +/// +/// This includes both plain control characters and a curated set of invisible +/// formatting codepoints. The bidi entries here cover the Trojan-Source-style +/// text-reordering controls that can make a title render misleadingly relative +/// to its underlying byte sequence. +fn is_disallowed_terminal_title_char(ch: char) -> bool { + if ch.is_control() { + return true; + } + + // Strip Trojan-Source-related bidi controls plus common non-rendering + // formatting characters so title text cannot smuggle terminal control + // semantics or visually misleading content. + matches!( + ch, + '\u{00AD}' + | '\u{034F}' + | '\u{061C}' + | '\u{180E}' + | '\u{200B}'..='\u{200F}' + | '\u{202A}'..='\u{202E}' + | '\u{2060}'..='\u{206F}' + | '\u{FE00}'..='\u{FE0F}' + | '\u{FEFF}' + | '\u{FFF9}'..='\u{FFFB}' + | '\u{1BCA0}'..='\u{1BCA3}' + | '\u{E0100}'..='\u{E01EF}' + ) +} + +#[cfg(test)] +mod tests { + use super::MAX_TERMINAL_TITLE_CHARS; + use super::SetWindowTitle; + use super::sanitize_terminal_title; + use crossterm::Command; + use pretty_assertions::assert_eq; + + #[test] + fn sanitizes_terminal_title() { + let sanitized = + sanitize_terminal_title(" Project\t|\nWorking\x1b\x07\u{009D}\u{009C} | Thread "); + assert_eq!(sanitized, "Project | Working | Thread"); + } + + #[test] + fn strips_invisible_format_chars_from_terminal_title() { + let sanitized = sanitize_terminal_title( + "Pro\u{202E}j\u{2066}e\u{200F}c\u{061C}t\u{200B} \u{FEFF}T\u{2060}itle", + ); + assert_eq!(sanitized, "Project Title"); + } + + #[test] + fn truncates_terminal_title() { + let input = "a".repeat(MAX_TERMINAL_TITLE_CHARS + 10); + let sanitized = sanitize_terminal_title(&input); + assert_eq!(sanitized.len(), MAX_TERMINAL_TITLE_CHARS); + } + + #[test] + fn truncation_prefers_visible_char_over_pending_space() { + let input = format!("{} b", "a".repeat(MAX_TERMINAL_TITLE_CHARS - 1)); + let sanitized = sanitize_terminal_title(&input); + assert_eq!(sanitized.len(), MAX_TERMINAL_TITLE_CHARS); + assert_eq!(sanitized.chars().last(), Some('b')); + } + + #[test] + fn writes_osc_title_with_string_terminator() { + let mut out = String::new(); + SetWindowTitle("hello".to_string()) + .write_ansi(&mut out) + .expect("encode terminal title"); + assert_eq!(out, "\x1b]0;hello\x1b\\"); + } +} From 25134b592c13328f76b2a56631dcec1abdb1b5b6 Mon Sep 17 00:00:00 2001 From: Matthew Zeng Date: Thu, 26 Mar 2026 11:08:49 -0700 Subject: [PATCH 008/191] [mcp] Fix legacy_tools (#15885) - [x] Fix legacy_tools --- codex-rs/core/config.schema.json | 4 +-- codex-rs/core/src/config/config_tests.rs | 19 ++++--------- codex-rs/core/src/config/types.rs | 9 +------ codex-rs/core/src/config/types_tests.rs | 34 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 300716dc1f..3d091006c9 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1258,9 +1258,7 @@ "type": "object" }, "RawMcpServerConfig": { - "additionalProperties": { - "$ref": "#/definitions/McpServerToolConfig" - }, + "additionalProperties": false, "properties": { "args": { "default": null, diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 7d2f6c573a..949906ac41 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -1991,28 +1991,19 @@ approval_mode = "approve" } #[test] -fn mcp_servers_toml_parses_legacy_flattened_per_tool_approval_overrides() { +fn mcp_servers_toml_ignores_unknown_server_fields() { let config = toml::from_str::( r#" [mcp_servers.docs] command = "docs-server" - -[mcp_servers.docs.search] -approval_mode = "approve" +trust_level = "trusted" "#, ) - .expect("legacy TOML deserialization should succeed"); - let tool = config - .mcp_servers - .get("docs") - .and_then(|server| server.tools.get("search")) - .expect("docs/search tool config exists"); + .expect("unknown MCP server fields should be ignored"); assert_eq!( - tool, - &McpServerToolConfig { - approval_mode: Some(AppToolApproval::Approve), - } + config.mcp_servers.get("docs"), + Some(&stdio_mcp("docs-server")) ); } diff --git a/codex-rs/core/src/config/types.rs b/codex-rs/core/src/config/types.rs index a69fec3431..05fbaaaa4d 100644 --- a/codex-rs/core/src/config/types.rs +++ b/codex-rs/core/src/config/types.rs @@ -163,10 +163,6 @@ pub(crate) struct RawMcpServerConfig { pub _name: Option, #[serde(default)] pub tools: Option>, - /// Legacy flattened per-tool approval settings accepted for backward compatibility. - #[serde(default)] - #[serde(flatten)] - pub legacy_tools: HashMap, } impl<'de> Deserialize<'de> for McpServerConfig { @@ -191,10 +187,7 @@ impl<'de> Deserialize<'de> for McpServerConfig { let disabled_tools = raw.disabled_tools.clone(); let scopes = raw.scopes.clone(); let oauth_resource = raw.oauth_resource.clone(); - let mut tools = raw.legacy_tools.clone(); - if let Some(nested_tools) = raw.tools.clone() { - tools.extend(nested_tools); - } + let tools = raw.tools.clone().unwrap_or_default(); fn throw_if_set(transport: &str, field: &str, value: Option<&T>) -> Result<(), E> where diff --git a/codex-rs/core/src/config/types_tests.rs b/codex-rs/core/src/config/types_tests.rs index dde0cd18fc..cba01e7876 100644 --- a/codex-rs/core/src/config/types_tests.rs +++ b/codex-rs/core/src/config/types_tests.rs @@ -243,6 +243,40 @@ fn deserialize_server_config_with_tool_filters() { assert_eq!(cfg.disabled_tools, Some(vec!["blocked".to_string()])); } +#[test] +fn deserialize_ignores_unknown_server_fields() { + let cfg: McpServerConfig = toml::from_str( + r#" + command = "echo" + trust_level = "trusted" + "#, + ) + .expect("should ignore unknown server fields"); + + assert_eq!( + cfg, + McpServerConfig { + transport: McpServerTransportConfig::Stdio { + command: "echo".to_string(), + args: vec![], + env: None, + env_vars: Vec::new(), + cwd: None, + }, + enabled: true, + required: false, + disabled_reason: None, + startup_timeout_sec: None, + tool_timeout_sec: None, + enabled_tools: None, + disabled_tools: None, + scopes: None, + oauth_resource: None, + tools: HashMap::new(), + } + ); +} + #[test] fn deserialize_skill_config_with_name_selector() { let cfg: SkillConfig = toml::from_str( From 3360f128f46f2aed392dea0d9557d0bb9536d2c2 Mon Sep 17 00:00:00 2001 From: Matthew Zeng Date: Thu, 26 Mar 2026 11:52:59 -0700 Subject: [PATCH 009/191] [plugins] Polish tool suggest prompts. (#15891) - [x] Polish tool suggest prompts to distinguish between missing connectors and discoverable plugins, and be very precise about the triggering conditions. --- codex-rs/core/src/tools/spec_tests.rs | 30 ++++++++++++++----- .../search_tool/tool_suggest_description.md | 7 +++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/codex-rs/core/src/tools/spec_tests.rs b/codex-rs/core/src/tools/spec_tests.rs index bab44764f0..6f0e7496b8 100644 --- a/codex-rs/core/src/tools/spec_tests.rs +++ b/codex-rs/core/src/tools/spec_tests.rs @@ -2090,12 +2090,12 @@ fn tool_suggest_can_be_registered_without_search_tool() { let ToolSpec::Function(ResponsesApiTool { description, .. }) = &tool_suggest.spec else { panic!("expected function tool"); }; - assert!( - description.contains( - "You've already tried to find a matching available tool for the user's request" - ) - ); - assert!(description.contains("This includes `tool_search` (if available) and other means.")); + assert!(description.contains( + "Suggests a missing connector in an installed plugin, or in narrower cases a not installed but discoverable plugin" + )); + assert!(description.contains( + "You've already tried to find a matching available tool for the user's request but couldn't find a good match. This includes `tool_search` (if available) and other means." + )); } #[test] @@ -2343,6 +2343,9 @@ fn tool_suggest_description_lists_discoverable_tools() { else { panic!("expected function tool"); }; + assert!(description.contains( + "Suggests a missing connector in an installed plugin, or in narrower cases a not installed but discoverable plugin" + )); assert!(description.contains("Google Calendar")); assert!(description.contains("Gmail")); assert!(description.contains("Sample Plugin")); @@ -2355,10 +2358,21 @@ fn tool_suggest_description_lists_discoverable_tools() { ); assert!( description.contains( - "You've already tried to find a matching available tool for the user's request" + "You've already tried to find a matching available tool for the user's request but couldn't find a good match. This includes `tool_search` (if available) and other means." ) ); - assert!(description.contains("This includes `tool_search` (if available) and other means.")); + assert!(description.contains( + "For connectors/apps that are not installed but needed for an installed plugin, suggest to install them if the task requirements match precisely." + )); + assert!(description.contains( + "For plugins that are not installed but discoverable, only suggest discoverable and installable plugins when the user's intent very explicitly and unambiguously matches that plugin itself." + )); + assert!(description.contains( + "Do not suggest a plugin just because one of its connectors or capabilities seems relevant." + )); + assert!(description.contains( + "Apply the stricter explicit-and-unambiguous rule for *discoverable tools* like plugin install suggestions; *missing tools* like connector install suggestions continue to use the normal clear-fit standard." + )); assert!(description.contains("DO NOT explore or recommend tools that are not on this list.")); assert!(!description.contains("tool_search fails to find a good match")); let JsonSchema::Object { required, .. } = parameters else { diff --git a/codex-rs/core/templates/search_tool/tool_suggest_description.md b/codex-rs/core/templates/search_tool/tool_suggest_description.md index 75e58958b5..ad0a50fcc1 100644 --- a/codex-rs/core/templates/search_tool/tool_suggest_description.md +++ b/codex-rs/core/templates/search_tool/tool_suggest_description.md @@ -1,10 +1,11 @@ # Tool suggestion discovery -Suggests a discoverable connector or plugin when the user clearly wants a capability that is not currently available in the active `tools` list. +Suggests a missing connector in an installed plugin, or in narrower cases a not installed but discoverable plugin, when the user clearly wants a capability that is not currently available in the active `tools` list. Use this ONLY when: - You've already tried to find a matching available tool for the user's request but couldn't find a good match. This includes `tool_search` (if available) and other means. -- AND the user's request strongly matches one of the discoverable tools listed below. +- For connectors/apps that are not installed but needed for an installed plugin, suggest to install them if the task requirements match precisely. +- For plugins that are not installed but discoverable, only suggest discoverable and installable plugins when the user's intent very explicitly and unambiguously matches that plugin itself. Do not suggest a plugin just because one of its connectors or capabilities seems relevant. Tool suggestions should only use the discoverable tools listed here. DO NOT explore or recommend tools that are not on this list. @@ -14,7 +15,7 @@ Discoverable tools: Workflow: 1. Ensure all possible means have been exhausted to find an existing available tool but none of them matches the request intent. -2. Match the user's request against the discoverable tools list above. +2. Match the user's request against the discoverable tools list above. Apply the stricter explicit-and-unambiguous rule for *discoverable tools* like plugin install suggestions; *missing tools* like connector install suggestions continue to use the normal clear-fit standard. 3. If one tool clearly fits, call `tool_suggest` with: - `tool_type`: `connector` or `plugin` - `action_type`: `install` or `enable` From b6050b42ae7e13b1eb26e9732a1582bd3c055505 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Thu, 26 Mar 2026 12:13:51 -0700 Subject: [PATCH 010/191] fix: resolve bwrap from trusted PATH entry (#15791) ## Summary - resolve system bwrap from PATH instead of hardcoding /usr/bin/bwrap - skip PATH entries that resolve inside the current workspace before launching the sandbox helper - keep the vendored bubblewrap fallback when no trusted system bwrap is found ## Validation - cargo test -p codex-core bwrap --lib - cargo test -p codex-linux-sandbox - just fix -p codex-core - just fix -p codex-linux-sandbox - just fmt - just argument-comment-lint - cargo clean --- codex-rs/core/README.md | 14 ++--- codex-rs/core/src/config/config_tests.rs | 65 ++++++++++++++++++++++-- codex-rs/core/src/config/mod.rs | 43 ++++++++++++---- codex-rs/linux-sandbox/README.md | 30 ++++++----- codex-rs/linux-sandbox/src/launcher.rs | 7 +-- 5 files changed, 121 insertions(+), 38 deletions(-) diff --git a/codex-rs/core/README.md b/codex-rs/core/README.md index 4b8cb793b7..1605d28a07 100644 --- a/codex-rs/core/README.md +++ b/codex-rs/core/README.md @@ -59,13 +59,13 @@ only when the split filesystem policy round-trips through the legacy cases like `/repo = write`, `/repo/a = none`, `/repo/a/b = write`, where the more specific writable child must reopen under a denied parent. -The Linux sandbox helper prefers `/usr/bin/bwrap` whenever it is available. If -`/usr/bin/bwrap` is present but too old to support `--argv0`, the helper keeps -using system bubblewrap and switches to a no-`--argv0` compatibility path for -the inner re-exec. If `/usr/bin/bwrap` is missing, it falls back to the -vendored bubblewrap path compiled into the binary and Codex surfaces a startup -warning through its normal notification path instead of printing directly from -the sandbox helper. +The Linux sandbox helper prefers the first `bwrap` found on `PATH` outside the +current working directory whenever it is available. If `bwrap` is present but +too old to support `--argv0`, the helper keeps using system bubblewrap and +switches to a no-`--argv0` compatibility path for the inner re-exec. If +`bwrap` is missing, it falls back to the vendored bubblewrap path compiled into +the binary and Codex surfaces a startup warning through its normal notification +path instead of printing directly from the sandbox helper. ### Windows diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 949906ac41..421bfa6bd6 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -5695,8 +5695,8 @@ shell_tool = true #[cfg(target_os = "linux")] #[test] fn system_bwrap_warning_reports_missing_system_bwrap() { - let warning = system_bwrap_warning_for_path(Path::new("/definitely/not/a/bwrap")) - .expect("missing system bwrap should emit a warning"); + let warning = + system_bwrap_warning_for_lookup(None).expect("missing system bwrap should emit a warning"); assert!(warning.contains("could not find system bubblewrap")); } @@ -5716,12 +5716,48 @@ exit 1 let fake_bwrap_path: &Path = fake_bwrap.as_ref(); assert_eq!( - system_bwrap_warning_for_path(fake_bwrap_path), + system_bwrap_warning_for_lookup(Some(fake_bwrap_path.to_path_buf())), None, "Do not warn even if bwrap does not support `--argv0`", ); } +#[cfg(target_os = "linux")] +#[test] +fn finds_first_executable_bwrap_in_search_paths() { + let temp_dir = tempdir().expect("temp dir"); + let cwd = temp_dir.path().join("cwd"); + let first_dir = temp_dir.path().join("first"); + let second_dir = temp_dir.path().join("second"); + std::fs::create_dir_all(&cwd).expect("create cwd"); + std::fs::create_dir_all(&first_dir).expect("create first dir"); + std::fs::create_dir_all(&second_dir).expect("create second dir"); + std::fs::write(first_dir.join("bwrap"), "not executable").expect("write non-executable bwrap"); + let expected_bwrap = write_named_fake_bwrap_in(&second_dir); + + assert_eq!( + find_system_bwrap_in_search_paths(vec![first_dir, second_dir], &cwd), + Some(expected_bwrap) + ); +} + +#[cfg(target_os = "linux")] +#[test] +fn skips_workspace_local_bwrap_in_search_paths() { + let temp_dir = tempdir().expect("temp dir"); + let cwd = temp_dir.path().join("cwd"); + let trusted_dir = temp_dir.path().join("trusted"); + std::fs::create_dir_all(&cwd).expect("create cwd"); + std::fs::create_dir_all(&trusted_dir).expect("create trusted dir"); + let _workspace_bwrap = write_named_fake_bwrap_in(&cwd); + let expected_bwrap = write_named_fake_bwrap_in(&trusted_dir); + + assert_eq!( + find_system_bwrap_in_search_paths(vec![cwd.clone(), trusted_dir], &cwd), + Some(expected_bwrap) + ); +} + #[cfg(not(target_os = "linux"))] #[test] fn system_bwrap_warning_is_disabled_off_linux() { @@ -5730,6 +5766,14 @@ fn system_bwrap_warning_is_disabled_off_linux() { #[cfg(target_os = "linux")] fn write_fake_bwrap(contents: &str) -> tempfile::TempPath { + write_fake_bwrap_in( + &std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), + contents, + ) +} + +#[cfg(target_os = "linux")] +fn write_fake_bwrap_in(dir: &Path, contents: &str) -> tempfile::TempPath { use std::fs; use std::os::unix::fs::PermissionsExt; use tempfile::NamedTempFile; @@ -5737,9 +5781,8 @@ fn write_fake_bwrap(contents: &str) -> tempfile::TempPath { // Bazel can mount the OS temp directory `noexec`, so prefer the current // working directory for fake executables and fall back to the default temp // dir outside that environment. - let temp_file = std::env::current_dir() + let temp_file = NamedTempFile::new_in(dir) .ok() - .and_then(|dir| NamedTempFile::new_in(dir).ok()) .unwrap_or_else(|| NamedTempFile::new().expect("temp file")); // Linux rejects exec-ing a file that is still open for writing. let path = temp_file.into_temp_path(); @@ -5749,6 +5792,18 @@ fn write_fake_bwrap(contents: &str) -> tempfile::TempPath { path } +#[cfg(target_os = "linux")] +fn write_named_fake_bwrap_in(dir: &Path) -> PathBuf { + use std::fs; + use std::os::unix::fs::PermissionsExt; + + let path = dir.join("bwrap"); + fs::write(&path, "#!/bin/sh\n").expect("write fake bwrap"); + let permissions = fs::Permissions::from_mode(0o755); + fs::set_permissions(&path, permissions).expect("chmod fake bwrap"); + path +} + #[tokio::test] async fn approvals_reviewer_defaults_to_manual_only_without_guardian_feature() -> std::io::Result<()> { diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index b2b55feeca..6db6f0e613 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -145,7 +145,7 @@ pub(crate) const DEFAULT_AGENT_JOB_MAX_RUNTIME_SECONDS: Option = None; pub const CONFIG_TOML_FILE: &str = "config.toml"; const OPENAI_BASE_URL_ENV_VAR: &str = "OPENAI_BASE_URL"; #[cfg(target_os = "linux")] -const SYSTEM_BWRAP_PATH: &str = "/usr/bin/bwrap"; +const SYSTEM_BWRAP_PROGRAM: &str = "bwrap"; const RESERVED_MODEL_PROVIDER_IDS: [&str; 3] = [ OPENAI_PROVIDER_ID, OLLAMA_OSS_PROVIDER_ID, @@ -154,7 +154,7 @@ const RESERVED_MODEL_PROVIDER_IDS: [&str; 3] = [ #[cfg(target_os = "linux")] pub fn system_bwrap_warning() -> Option { - system_bwrap_warning_for_path(Path::new(SYSTEM_BWRAP_PATH)) + system_bwrap_warning_for_lookup(find_system_bwrap_in_path()) } #[cfg(not(target_os = "linux"))] @@ -163,15 +163,40 @@ pub fn system_bwrap_warning() -> Option { } #[cfg(target_os = "linux")] -fn system_bwrap_warning_for_path(system_bwrap_path: &Path) -> Option { - if !system_bwrap_path.is_file() { - return Some(format!( - "Codex could not find system bubblewrap at {}. Please install bubblewrap with your package manager. Codex will use the vendored bubblewrap in the meantime.", - system_bwrap_path.display() - )); +fn system_bwrap_warning_for_lookup(system_bwrap_path: Option) -> Option { + match system_bwrap_path { + Some(_) => None, + None => Some( + "Codex could not find system bubblewrap on PATH. Please install bubblewrap with your package manager. Codex will use the vendored bubblewrap in the meantime." + .to_string(), + ), } +} - None +#[cfg(target_os = "linux")] +pub fn find_system_bwrap_in_path() -> Option { + let search_path = std::env::var_os("PATH")?; + let cwd = std::env::current_dir().ok()?; + find_system_bwrap_in_search_paths(std::iter::once(PathBuf::from(search_path)), &cwd) +} + +#[cfg(target_os = "linux")] +fn find_system_bwrap_in_search_paths( + search_paths: impl IntoIterator, + cwd: &Path, +) -> Option { + let search_path = std::env::join_paths(search_paths).ok()?; + let cwd = std::fs::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf()); + which::which_in_all(SYSTEM_BWRAP_PROGRAM, Some(search_path), &cwd) + .ok()? + .find_map(|path| { + let path = std::fs::canonicalize(path).ok()?; + if path.starts_with(&cwd) { + None + } else { + Some(path) + } + }) } fn resolve_sqlite_home_env(resolved_cwd: &Path) -> Option { diff --git a/codex-rs/linux-sandbox/README.md b/codex-rs/linux-sandbox/README.md index 5898a9c688..d8af1959a4 100644 --- a/codex-rs/linux-sandbox/README.md +++ b/codex-rs/linux-sandbox/README.md @@ -7,25 +7,27 @@ This crate is responsible for producing: - the `codex-exec` CLI can check if its arg0 is `codex-linux-sandbox` and, if so, execute as if it were `codex-linux-sandbox` - this should also be true of the `codex` multitool CLI -On Linux, the bubblewrap pipeline prefers the system `/usr/bin/bwrap` whenever -it is available. If `/usr/bin/bwrap` is present but too old to support +On Linux, the bubblewrap pipeline prefers the first `bwrap` found on `PATH` +outside the current working directory whenever it is available. If `bwrap` is +present but too old to support `--argv0`, the helper keeps using system bubblewrap and switches to a -no-`--argv0` compatibility path for the inner re-exec. If `/usr/bin/bwrap` is -missing, the helper falls back to the vendored bubblewrap path compiled into -this binary. -Codex also surfaces a startup warning when `/usr/bin/bwrap` is missing so users -know it is falling back to the vendored helper. +no-`--argv0` compatibility path for the inner re-exec. If `bwrap` is missing, +the helper falls back to the vendored bubblewrap path compiled into this +binary. +Codex also surfaces a startup warning when `bwrap` is missing so users know it +is falling back to the vendored helper. **Current Behavior** - Legacy `SandboxPolicy` / `sandbox_mode` configs remain supported. - Bubblewrap is the default filesystem sandbox pipeline. -- If `/usr/bin/bwrap` is present, the helper uses it. -- If `/usr/bin/bwrap` is present but too old to support `--argv0`, the helper - uses a no-`--argv0` compatibility path for the inner re-exec. -- If `/usr/bin/bwrap` is missing, the helper falls back to the vendored - bubblewrap path. -- If `/usr/bin/bwrap` is missing, Codex also surfaces a startup warning instead - of printing directly from the sandbox helper. +- If `bwrap` is present on `PATH` outside the current working directory, the + helper uses it. +- If `bwrap` is present but too old to support `--argv0`, the helper uses a + no-`--argv0` compatibility path for the inner re-exec. +- If `bwrap` is missing, the helper falls back to the vendored bubblewrap + path. +- If `bwrap` is missing, Codex also surfaces a startup warning instead of + printing directly from the sandbox helper. - Legacy Landlock + mount protections remain available as an explicit legacy fallback path. - Set `features.use_legacy_landlock = true` (or CLI `-c use_legacy_landlock=true`) diff --git a/codex-rs/linux-sandbox/src/launcher.rs b/codex-rs/linux-sandbox/src/launcher.rs index e68eea2725..c36f1c5ff1 100644 --- a/codex-rs/linux-sandbox/src/launcher.rs +++ b/codex-rs/linux-sandbox/src/launcher.rs @@ -10,8 +10,6 @@ use std::sync::OnceLock; use crate::vendored_bwrap::exec_vendored_bwrap; use codex_utils_absolute_path::AbsolutePathBuf; -const SYSTEM_BWRAP_PATH: &str = "/usr/bin/bwrap"; - #[derive(Debug, Clone, PartialEq, Eq)] enum BubblewrapLauncher { System(SystemBwrapLauncher), @@ -36,7 +34,10 @@ pub(crate) fn exec_bwrap(argv: Vec, preserved_files: Vec) -> ! { fn preferred_bwrap_launcher() -> BubblewrapLauncher { static LAUNCHER: OnceLock = OnceLock::new(); LAUNCHER - .get_or_init(|| preferred_bwrap_launcher_for_path(Path::new(SYSTEM_BWRAP_PATH))) + .get_or_init(|| match codex_core::config::find_system_bwrap_in_path() { + Some(path) => preferred_bwrap_launcher_for_path(&path), + None => BubblewrapLauncher::Vendored, + }) .clone() } From b3e069e8cbed9ef86a47335041632ddfb458b79f Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 12:33:23 -0700 Subject: [PATCH 011/191] skills: remove unused skill permission metadata (#15900) ## Why Skill metadata accepted a `permissions` block and stored the result on `SkillMetadata`, but that data was never consumed by runtime behavior. Leaving the dead parsing path in place makes it look like skills can widen or otherwise influence execution permissions when, in practice, declared skill permissions are ignored. This change removes that misleading surface area so the skill metadata model matches what the system actually uses. ## What changed - removed `permission_profile` and `managed_network_override` from `core-skills::SkillMetadata` - stopped parsing `permissions` from skill metadata in `core-skills/src/loader.rs` - deleted the loader tests that only exercised the removed permissions parsing path - cleaned up dependent `SkillMetadata` constructors in tests and TUI code that were only carrying `None` for those fields ## Testing - `cargo test -p codex-core-skills` - `cargo test -p codex-tui submission_prefers_selected_duplicate_skill_path` - `just argument-comment-lint` --- codex-rs/core-skills/src/injection_tests.rs | 2 - .../core-skills/src/invocation_utils_tests.rs | 2 - codex-rs/core-skills/src/loader.rs | 73 ---- codex-rs/core-skills/src/loader_tests.rs | 396 ------------------ codex-rs/core-skills/src/manager_tests.rs | 2 - codex-rs/core-skills/src/model.rs | 16 - .../core/src/mcp/skill_dependencies_tests.rs | 2 - codex-rs/tui/src/bottom_pane/chat_composer.rs | 4 - codex-rs/tui/src/bottom_pane/mod.rs | 2 - codex-rs/tui/src/chatwidget/skills.rs | 2 - codex-rs/tui/src/chatwidget/tests.rs | 4 - .../src/bottom_pane/chat_composer.rs | 4 - .../tui_app_server/src/bottom_pane/mod.rs | 2 - .../tui_app_server/src/chatwidget/skills.rs | 2 - .../tui_app_server/src/chatwidget/tests.rs | 4 - 15 files changed, 517 deletions(-) diff --git a/codex-rs/core-skills/src/injection_tests.rs b/codex-rs/core-skills/src/injection_tests.rs index 8d66a0af57..b8611de4ef 100644 --- a/codex-rs/core-skills/src/injection_tests.rs +++ b/codex-rs/core-skills/src/injection_tests.rs @@ -11,8 +11,6 @@ fn make_skill(name: &str, path: &str) -> SkillMetadata { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from(path), scope: codex_protocol::protocol::SkillScope::User, } diff --git a/codex-rs/core-skills/src/invocation_utils_tests.rs b/codex-rs/core-skills/src/invocation_utils_tests.rs index 657582b742..6d74dbe9a7 100644 --- a/codex-rs/core-skills/src/invocation_utils_tests.rs +++ b/codex-rs/core-skills/src/invocation_utils_tests.rs @@ -18,8 +18,6 @@ fn test_skill_metadata(skill_doc_path: PathBuf) -> SkillMetadata { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: skill_doc_path, scope: codex_protocol::protocol::SkillScope::User, } diff --git a/codex-rs/core-skills/src/loader.rs b/codex-rs/core-skills/src/loader.rs index b6ef000845..42de9fb288 100644 --- a/codex-rs/core-skills/src/loader.rs +++ b/codex-rs/core-skills/src/loader.rs @@ -2,7 +2,6 @@ use crate::model::SkillDependencies; use crate::model::SkillError; use crate::model::SkillInterface; use crate::model::SkillLoadOutcome; -use crate::model::SkillManagedNetworkOverride; use crate::model::SkillMetadata; use crate::model::SkillPolicy; use crate::model::SkillToolDependency; @@ -13,10 +12,6 @@ use codex_config::ConfigLayerStackOrdering; use codex_config::default_project_root_markers; use codex_config::merge_toml_values; use codex_config::project_root_markers_from_config; -use codex_protocol::models::FileSystemPermissions; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; -use codex_protocol::models::NetworkPermissions; -use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::Product; use codex_protocol::protocol::SkillScope; use codex_utils_absolute_path::AbsolutePathBufGuard; @@ -59,8 +54,6 @@ struct SkillMetadataFile { dependencies: Option, #[serde(default)] policy: Option, - #[serde(default)] - permissions: Option, } #[derive(Default)] @@ -68,28 +61,6 @@ struct LoadedSkillMetadata { interface: Option, dependencies: Option, policy: Option, - permission_profile: Option, - managed_network_override: Option, -} - -#[derive(Debug, Default, Deserialize, PartialEq, Eq)] -struct SkillPermissionProfile { - #[serde(default)] - network: Option, - #[serde(default)] - file_system: Option, - #[serde(default)] - macos: Option, -} - -#[derive(Debug, Default, Deserialize, PartialEq, Eq)] -struct SkillNetworkPermissions { - #[serde(default)] - enabled: Option, - #[serde(default)] - allowed_domains: Option>, - #[serde(default)] - denied_domains: Option>, } #[derive(Debug, Default, Deserialize)] @@ -551,8 +522,6 @@ fn parse_skill_file(path: &Path, scope: SkillScope) -> Result Result LoadedSkillMetadata { interface, dependencies, policy, - permissions, } = parsed; - let (permission_profile, managed_network_override) = normalize_permissions(permissions); LoadedSkillMetadata { interface: resolve_interface(interface, skill_dir), dependencies: resolve_dependencies(dependencies), policy: resolve_policy(policy), - permission_profile, - managed_network_override, } } -fn normalize_permissions( - permissions: Option, -) -> ( - Option, - Option, -) { - let Some(permissions) = permissions else { - return (None, None); - }; - let managed_network_override = permissions - .network - .as_ref() - .map(|network| SkillManagedNetworkOverride { - allowed_domains: network.allowed_domains.clone(), - denied_domains: network.denied_domains.clone(), - }) - .filter(SkillManagedNetworkOverride::has_domain_overrides); - let permission_profile = PermissionProfile { - network: permissions.network.and_then(|network| { - let network = NetworkPermissions { - enabled: network.enabled, - }; - (!network.is_empty()).then_some(network) - }), - file_system: permissions - .file_system - .filter(|file_system| !file_system.is_empty()), - macos: permissions.macos, - }; - - ( - (!permission_profile.is_empty()).then_some(permission_profile), - managed_network_override, - ) -} - fn resolve_interface(interface: Option, skill_dir: &Path) -> Option { let interface = interface?; let interface = SkillInterface { diff --git a/codex-rs/core-skills/src/loader_tests.rs b/codex-rs/core-skills/src/loader_tests.rs index 5875b1a043..ad196df670 100644 --- a/codex-rs/core-skills/src/loader_tests.rs +++ b/codex-rs/core-skills/src/loader_tests.rs @@ -4,12 +4,6 @@ use codex_config::ConfigLayerEntry; use codex_config::ConfigLayerStack; use codex_config::ConfigRequirements; use codex_config::ConfigRequirementsToml; -use codex_protocol::models::FileSystemPermissions; -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; -use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::Product; use codex_protocol::protocol::SkillScope; use codex_utils_absolute_path::AbsolutePathBuf; @@ -290,8 +284,6 @@ fn loads_skills_from_home_agents_dir_for_user_scope() -> anyhow::Result<()> { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -442,8 +434,6 @@ async fn loads_skill_dependencies_metadata_from_yaml() { ], }), policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -499,8 +489,6 @@ interface: }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(skill_path.as_path()), scope: SkillScope::User, }] @@ -610,340 +598,6 @@ policy: ); } -#[tokio::test] -async fn loads_skill_permissions_from_yaml() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let skill_path = write_skill(&codex_home, "demo", "permissions-skill", "from yaml"); - let skill_dir = skill_path.parent().expect("skill dir"); - fs::create_dir_all(skill_dir.join("data")).expect("create read path"); - fs::create_dir_all(skill_dir.join("output")).expect("create write path"); - - write_skill_metadata_at( - skill_dir, - r#" -permissions: - network: - enabled: true - file_system: - read: - - "./data" - write: - - "./output" -"#, - ); - - let cfg = make_config(&codex_home).await; - let outcome = load_skills_for_test(&cfg); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!(outcome.skills.len(), 1); - assert_eq!( - outcome.skills[0].permission_profile, - Some(PermissionProfile { - network: Some(NetworkPermissions { - enabled: Some(true), - }), - file_system: Some(FileSystemPermissions { - read: Some(vec![ - AbsolutePathBuf::try_from(normalized(skill_dir.join("data").as_path())) - .expect("absolute data path"), - ]), - write: Some(vec![ - AbsolutePathBuf::try_from(normalized(skill_dir.join("output").as_path())) - .expect("absolute output path"), - ]), - }), - macos: None, - }) - ); - assert_eq!(outcome.skills[0].managed_network_override, None); -} - -#[tokio::test] -async fn empty_skill_permissions_do_not_create_profile() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let skill_path = write_skill(&codex_home, "demo", "permissions-empty", "from yaml"); - let skill_dir = skill_path.parent().expect("skill dir"); - - write_skill_metadata_at( - skill_dir, - r#" -permissions: {} -"#, - ); - - let cfg = make_config(&codex_home).await; - let outcome = load_skills_for_test(&cfg); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!(outcome.skills.len(), 1); - assert_eq!(outcome.skills[0].permission_profile, None); -} - -#[test] -fn normalize_permissions_splits_managed_network_overrides() { - let (permission_profile, managed_network_override) = - normalize_permissions(Some(SkillPermissionProfile { - network: Some(SkillNetworkPermissions { - enabled: Some(true), - allowed_domains: Some(vec!["skill.example.com".to_string()]), - denied_domains: Some(vec!["blocked.skill.example.com".to_string()]), - }), - file_system: None, - macos: None, - })); - - assert_eq!( - permission_profile, - Some(PermissionProfile { - network: Some(NetworkPermissions { - enabled: Some(true), - }), - file_system: None, - macos: None, - }) - ); - assert_eq!( - managed_network_override, - Some(SkillManagedNetworkOverride { - allowed_domains: Some(vec!["skill.example.com".to_string()]), - denied_domains: Some(vec!["blocked.skill.example.com".to_string()]), - }) - ); -} - -#[test] -fn normalize_permissions_preserves_network_gate_separately_from_overrides() { - let (permission_profile, managed_network_override) = - normalize_permissions(Some(SkillPermissionProfile { - network: Some(SkillNetworkPermissions { - enabled: Some(false), - allowed_domains: Some(vec!["skill.example.com".to_string()]), - denied_domains: None, - }), - file_system: None, - macos: None, - })); - - assert_eq!( - permission_profile, - Some(PermissionProfile { - network: Some(NetworkPermissions { - enabled: Some(false), - }), - file_system: None, - macos: None, - }) - ); - assert_eq!( - managed_network_override, - Some(SkillManagedNetworkOverride { - allowed_domains: Some(vec!["skill.example.com".to_string()]), - denied_domains: None, - }) - ); -} - -#[test] -fn skill_metadata_parses_macos_permissions_yaml() { - let parsed = serde_yaml::from_str::( - r#" -permissions: - macos: - macos_preferences: "read_write" - macos_automation: - - "com.apple.Notes" - macos_launch_services: true - macos_accessibility: true - macos_calendar: true -"#, - ) - .expect("parse skill metadata"); - - assert_eq!( - parsed.permissions, - Some(SkillPermissionProfile { - network: None, - file_system: None, - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - }) - ); -} - -#[test] -fn skill_metadata_parses_macos_reminders_permission_yaml() { - let parsed = serde_yaml::from_str::( - r#" -permissions: - macos: - macos_reminders: true -"#, - ) - .expect("parse reminders skill metadata"); - - assert_eq!( - parsed.permissions, - Some(SkillPermissionProfile { - network: None, - file_system: None, - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - macos_automation: MacOsAutomationPermission::None, - macos_launch_services: false, - macos_accessibility: false, - macos_calendar: false, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::None, - }), - }) - ); -} - -#[test] -fn skill_metadata_parses_network_domain_overrides_under_permissions() { - let parsed = serde_yaml::from_str::( - r#" -permissions: - network: - enabled: true - allowed_domains: - - "skill.example.com" - denied_domains: - - "blocked.skill.example.com" -"#, - ) - .expect("parse network skill metadata"); - - assert_eq!( - parsed.permissions, - Some(SkillPermissionProfile { - network: Some(SkillNetworkPermissions { - enabled: Some(true), - allowed_domains: Some(vec!["skill.example.com".to_string()]), - denied_domains: Some(vec!["blocked.skill.example.com".to_string()]), - }), - file_system: None, - macos: None, - }) - ); -} - -#[cfg(target_os = "macos")] -#[tokio::test] -async fn loads_skill_macos_permissions_from_yaml() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let skill_path = write_skill(&codex_home, "demo", "permissions-macos", "from yaml"); - let skill_dir = skill_path.parent().expect("skill dir"); - - write_skill_metadata_at( - skill_dir, - r#" -permissions: - macos: - macos_preferences: "read_write" - macos_automation: - - "com.apple.Notes" - macos_launch_services: true - macos_accessibility: true - macos_calendar: true -"#, - ); - - let cfg = make_config(&codex_home).await; - let outcome = load_skills_for_test(&cfg); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!(outcome.skills.len(), 1); - assert_eq!( - outcome.skills[0].permission_profile, - Some(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string() - ],), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - ..Default::default() - }) - ); -} - -#[cfg(not(target_os = "macos"))] -#[tokio::test] -async fn loads_skill_macos_permissions_from_yaml_non_macos_does_not_create_profile() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let skill_path = write_skill(&codex_home, "demo", "permissions-macos", "from yaml"); - let skill_dir = skill_path.parent().expect("skill dir"); - - write_skill_metadata_at( - skill_dir, - r#" -permissions: - macos: - macos_preferences: "read_write" - macos_automation: - - "com.apple.Notes" - macos_launch_services: true - macos_accessibility: true - macos_calendar: true -"#, - ); - - let cfg = make_config(&codex_home).await; - let outcome = load_skills_for_test(&cfg); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!(outcome.skills.len(), 1); - assert_eq!( - outcome.skills[0].permission_profile, - Some(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string() - ],), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - ..Default::default() - }) - ); -} - #[tokio::test] async fn accepts_icon_paths_under_assets_dir() { let codex_home = tempfile::tempdir().expect("tempdir"); @@ -988,8 +642,6 @@ async fn accepts_icon_paths_under_assets_dir() { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1030,8 +682,6 @@ async fn ignores_invalid_brand_color() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1085,8 +735,6 @@ async fn ignores_default_prompt_over_max_length() { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1128,8 +776,6 @@ async fn drops_interface_when_icons_are_invalid() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1174,8 +820,6 @@ async fn loads_skills_via_symlinked_subdir_for_user_scope() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&shared_skill_path), scope: SkillScope::User, }] @@ -1235,8 +879,6 @@ async fn does_not_loop_on_symlink_cycle_for_user_scope() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1273,8 +915,6 @@ fn loads_skills_via_symlinked_subdir_for_admin_scope() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&shared_skill_path), scope: SkillScope::Admin, }] @@ -1314,8 +954,6 @@ async fn loads_skills_via_symlinked_subdir_for_repo_scope() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&linked_skill_path), scope: SkillScope::Repo, }] @@ -1383,8 +1021,6 @@ async fn respects_max_scan_depth_for_user_scope() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&within_depth_path), scope: SkillScope::User, }] @@ -1412,8 +1048,6 @@ async fn loads_valid_skill() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1446,8 +1080,6 @@ async fn falls_back_to_directory_name_when_skill_name_is_missing() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1489,8 +1121,6 @@ async fn namespaces_plugin_skills_using_plugin_name() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1522,8 +1152,6 @@ async fn loads_short_description_from_metadata() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::User, }] @@ -1636,8 +1264,6 @@ async fn loads_skills_from_repo_root() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::Repo, }] @@ -1673,8 +1299,6 @@ async fn loads_skills_from_agents_dir_without_codex_dir() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::Repo, }] @@ -1728,8 +1352,6 @@ async fn loads_skills_from_all_codex_dirs_under_project_root() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&nested_skill_path), scope: SkillScope::Repo, }, @@ -1740,8 +1362,6 @@ async fn loads_skills_from_all_codex_dirs_under_project_root() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&root_skill_path), scope: SkillScope::Repo, }, @@ -1781,8 +1401,6 @@ async fn loads_skills_from_codex_dir_when_not_git_repo() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::Repo, }] @@ -1820,8 +1438,6 @@ async fn deduplicates_by_path_preferring_first_root() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::Repo, }] @@ -1863,8 +1479,6 @@ async fn keeps_duplicate_names_from_repo_and_user() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&repo_skill_path), scope: SkillScope::Repo, }, @@ -1875,8 +1489,6 @@ async fn keeps_duplicate_names_from_repo_and_user() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&user_skill_path), scope: SkillScope::User, }, @@ -1940,8 +1552,6 @@ async fn keeps_duplicate_names_from_nested_codex_dirs() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: first_path, scope: SkillScope::Repo, }, @@ -1952,8 +1562,6 @@ async fn keeps_duplicate_names_from_nested_codex_dirs() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: second_path, scope: SkillScope::Repo, }, @@ -2025,8 +1633,6 @@ async fn loads_skills_when_cwd_is_file_in_repo() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::Repo, }] @@ -2085,8 +1691,6 @@ async fn loads_skills_from_system_cache_when_present() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: normalized(&skill_path), scope: SkillScope::System, }] diff --git a/codex-rs/core-skills/src/manager_tests.rs b/codex-rs/core-skills/src/manager_tests.rs index 2507fc9823..9e6c60c0e6 100644 --- a/codex-rs/core-skills/src/manager_tests.rs +++ b/codex-rs/core-skills/src/manager_tests.rs @@ -57,8 +57,6 @@ fn test_skill(name: &str, path: PathBuf) -> SkillMetadata { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: path, scope: SkillScope::User, } diff --git a/codex-rs/core-skills/src/model.rs b/codex-rs/core-skills/src/model.rs index d47904b9c7..319ca4e64e 100644 --- a/codex-rs/core-skills/src/model.rs +++ b/codex-rs/core-skills/src/model.rs @@ -3,22 +3,8 @@ use std::collections::HashSet; use std::path::PathBuf; use std::sync::Arc; -use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::Product; use codex_protocol::protocol::SkillScope; -use serde::Deserialize; - -#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq)] -pub struct SkillManagedNetworkOverride { - pub allowed_domains: Option>, - pub denied_domains: Option>, -} - -impl SkillManagedNetworkOverride { - pub fn has_domain_overrides(&self) -> bool { - self.allowed_domains.is_some() || self.denied_domains.is_some() - } -} #[derive(Debug, Clone, PartialEq)] pub struct SkillMetadata { @@ -28,8 +14,6 @@ pub struct SkillMetadata { pub interface: Option, pub dependencies: Option, pub policy: Option, - pub permission_profile: Option, - pub managed_network_override: Option, /// Path to the SKILLS.md file that declares this skill. pub path_to_skills_md: PathBuf, pub scope: SkillScope, diff --git a/codex-rs/core/src/mcp/skill_dependencies_tests.rs b/codex-rs/core/src/mcp/skill_dependencies_tests.rs index 34c6a767f5..7387ef4b31 100644 --- a/codex-rs/core/src/mcp/skill_dependencies_tests.rs +++ b/codex-rs/core/src/mcp/skill_dependencies_tests.rs @@ -12,8 +12,6 @@ fn skill_with_tools(tools: Vec) -> SkillMetadata { interface: None, dependencies: Some(SkillDependencies { tools }), policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("skill"), scope: SkillScope::User, } diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 6d5c92d490..3e85d03f53 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -5409,8 +5409,6 @@ mod tests { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("/tmp/repo/google-calendar/SKILL.md"), scope: codex_protocol::protocol::SkillScope::Repo, }])); @@ -5501,8 +5499,6 @@ mod tests { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("/tmp/repo/google-calendar/SKILL.md"), scope: codex_protocol::protocol::SkillScope::Repo, }])); diff --git a/codex-rs/tui/src/bottom_pane/mod.rs b/codex-rs/tui/src/bottom_pane/mod.rs index eda37fe9c4..a405a5837a 100644 --- a/codex-rs/tui/src/bottom_pane/mod.rs +++ b/codex-rs/tui/src/bottom_pane/mod.rs @@ -1708,8 +1708,6 @@ mod tests { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("test-skill"), scope: SkillScope::User, }]), diff --git a/codex-rs/tui/src/chatwidget/skills.rs b/codex-rs/tui/src/chatwidget/skills.rs index 24273b6976..6e161087c3 100644 --- a/codex-rs/tui/src/chatwidget/skills.rs +++ b/codex-rs/tui/src/chatwidget/skills.rs @@ -191,8 +191,6 @@ fn protocol_skill_to_core(skill: &ProtocolSkillMetadata) -> SkillMetadata { .collect(), }), policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: skill.path.clone(), scope: skill.scope, } diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index cba1b285e0..90fea87e7f 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -1049,8 +1049,6 @@ async fn submission_prefers_selected_duplicate_skill_path() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: repo_skill_path, scope: SkillScope::Repo, }, @@ -1061,8 +1059,6 @@ async fn submission_prefers_selected_duplicate_skill_path() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: user_skill_path.clone(), scope: SkillScope::User, }, diff --git a/codex-rs/tui_app_server/src/bottom_pane/chat_composer.rs b/codex-rs/tui_app_server/src/bottom_pane/chat_composer.rs index fea3b8c2ad..e972750f53 100644 --- a/codex-rs/tui_app_server/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui_app_server/src/bottom_pane/chat_composer.rs @@ -5424,8 +5424,6 @@ mod tests { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("/tmp/repo/google-calendar/SKILL.md"), scope: codex_protocol::protocol::SkillScope::Repo, }])); @@ -5516,8 +5514,6 @@ mod tests { }), dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("/tmp/repo/google-calendar/SKILL.md"), scope: codex_protocol::protocol::SkillScope::Repo, }])); diff --git a/codex-rs/tui_app_server/src/bottom_pane/mod.rs b/codex-rs/tui_app_server/src/bottom_pane/mod.rs index 78cf6a2359..d74f05b5d4 100644 --- a/codex-rs/tui_app_server/src/bottom_pane/mod.rs +++ b/codex-rs/tui_app_server/src/bottom_pane/mod.rs @@ -1701,8 +1701,6 @@ mod tests { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: PathBuf::from("test-skill"), scope: SkillScope::User, }]), diff --git a/codex-rs/tui_app_server/src/chatwidget/skills.rs b/codex-rs/tui_app_server/src/chatwidget/skills.rs index 24273b6976..6e161087c3 100644 --- a/codex-rs/tui_app_server/src/chatwidget/skills.rs +++ b/codex-rs/tui_app_server/src/chatwidget/skills.rs @@ -191,8 +191,6 @@ fn protocol_skill_to_core(skill: &ProtocolSkillMetadata) -> SkillMetadata { .collect(), }), policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: skill.path.clone(), scope: skill.scope, } diff --git a/codex-rs/tui_app_server/src/chatwidget/tests.rs b/codex-rs/tui_app_server/src/chatwidget/tests.rs index 8191acc3db..7dadb963d8 100644 --- a/codex-rs/tui_app_server/src/chatwidget/tests.rs +++ b/codex-rs/tui_app_server/src/chatwidget/tests.rs @@ -1078,8 +1078,6 @@ async fn submission_prefers_selected_duplicate_skill_path() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: repo_skill_path, scope: SkillScope::Repo, }, @@ -1090,8 +1088,6 @@ async fn submission_prefers_selected_duplicate_skill_path() { interface: None, dependencies: None, policy: None, - permission_profile: None, - managed_network_override: None, path_to_skills_md: user_skill_path.clone(), scope: SkillScope::User, }, From 9736fa5e3d5da08e20c533f07c30c2960475e19b Mon Sep 17 00:00:00 2001 From: Ruslan Nigmatullin Date: Thu, 26 Mar 2026 13:01:35 -0700 Subject: [PATCH 012/191] app-server: Split transport module (#15811) `transport.rs` is getting pretty big, split individual transport implementations into separate files. --- .../src/{transport.rs => transport/mod.rs} | 374 +----------------- codex-rs/app-server/src/transport/stdio.rs | 88 +++++ .../app-server/src/transport/websocket.rs | 308 +++++++++++++++ 3 files changed, 400 insertions(+), 370 deletions(-) rename codex-rs/app-server/src/{transport.rs => transport/mod.rs} (74%) create mode 100644 codex-rs/app-server/src/transport/stdio.rs create mode 100644 codex-rs/app-server/src/transport/websocket.rs diff --git a/codex-rs/app-server/src/transport.rs b/codex-rs/app-server/src/transport/mod.rs similarity index 74% rename from codex-rs/app-server/src/transport.rs rename to codex-rs/app-server/src/transport/mod.rs index 020e441da5..2f80a009d3 100644 --- a/codex-rs/app-server/src/transport.rs +++ b/codex-rs/app-server/src/transport/mod.rs @@ -1,8 +1,5 @@ pub(crate) mod auth; -use self::auth::WebsocketAuthPolicy; -use self::auth::authorize_upgrade; -use self::auth::should_warn_about_unauthenticated_non_loopback_listener; use crate::error_code::OVERLOADED_ERROR_CODE; use crate::message_processor::ConnectionSessionState; use crate::outgoing_message::ConnectionId; @@ -10,53 +7,20 @@ use crate::outgoing_message::OutgoingEnvelope; use crate::outgoing_message::OutgoingError; use crate::outgoing_message::OutgoingMessage; use crate::outgoing_message::QueuedOutgoingMessage; -use axum::Router; -use axum::body::Body; -use axum::extract::ConnectInfo; -use axum::extract::State; -use axum::extract::ws::Message as WebSocketMessage; -use axum::extract::ws::WebSocket; -use axum::extract::ws::WebSocketUpgrade; -use axum::http::HeaderMap; -use axum::http::Request; -use axum::http::StatusCode; -use axum::http::header::ORIGIN; -use axum::middleware; -use axum::middleware::Next; -use axum::response::IntoResponse; -use axum::response::Response; -use axum::routing::any; -use axum::routing::get; use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::JSONRPCMessage; use codex_app_server_protocol::ServerRequest; -use futures::SinkExt; -use futures::StreamExt; -use owo_colors::OwoColorize; -use owo_colors::Stream; -use owo_colors::Style; use std::collections::HashMap; use std::collections::HashSet; -use std::io::ErrorKind; -use std::io::Result as IoResult; use std::net::SocketAddr; use std::str::FromStr; use std::sync::Arc; use std::sync::RwLock; use std::sync::atomic::AtomicBool; -use std::sync::atomic::AtomicU64; use std::sync::atomic::Ordering; -use tokio::io::AsyncBufReadExt; -use tokio::io::AsyncWriteExt; -use tokio::io::BufReader; -use tokio::io::{self}; -use tokio::net::TcpListener; use tokio::sync::mpsc; -use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; -use tracing::debug; use tracing::error; -use tracing::info; use tracing::warn; /// Size of the bounded channels used to communicate between tasks. The value @@ -64,85 +28,11 @@ use tracing::warn; /// plenty for an interactive CLI. pub(crate) const CHANNEL_CAPACITY: usize = 128; -fn colorize(text: &str, style: Style) -> String { - text.if_supports_color(Stream::Stderr, |value| value.style(style)) - .to_string() -} +mod stdio; +mod websocket; -#[allow(clippy::print_stderr)] -fn print_websocket_startup_banner(addr: SocketAddr) { - let title = colorize("codex app-server (WebSockets)", Style::new().bold().cyan()); - let listening_label = colorize("listening on:", Style::new().dimmed()); - let listen_url = colorize(&format!("ws://{addr}"), Style::new().green()); - let ready_label = colorize("readyz:", Style::new().dimmed()); - let ready_url = colorize(&format!("http://{addr}/readyz"), Style::new().green()); - let health_label = colorize("healthz:", Style::new().dimmed()); - let health_url = colorize(&format!("http://{addr}/healthz"), Style::new().green()); - let note_label = colorize("note:", Style::new().dimmed()); - eprintln!("{title}"); - eprintln!(" {listening_label} {listen_url}"); - eprintln!(" {ready_label} {ready_url}"); - eprintln!(" {health_label} {health_url}"); - if addr.ip().is_loopback() { - eprintln!( - " {note_label} binds localhost only (use SSH port-forwarding for remote access)" - ); - } else { - eprintln!( - " {note_label} websocket auth is opt-in in this build; configure `--ws-auth ...` before real remote use" - ); - } -} - -#[derive(Clone)] -struct WebSocketListenerState { - transport_event_tx: mpsc::Sender, - connection_counter: Arc, - auth_policy: Arc, -} - -async fn health_check_handler() -> StatusCode { - StatusCode::OK -} - -async fn reject_requests_with_origin_header( - request: Request, - next: Next, -) -> Result { - if request.headers().contains_key(ORIGIN) { - warn!( - method = %request.method(), - uri = %request.uri(), - "rejecting websocket listener request with Origin header" - ); - Err(StatusCode::FORBIDDEN) - } else { - Ok(next.run(request).await) - } -} - -async fn websocket_upgrade_handler( - websocket: WebSocketUpgrade, - ConnectInfo(peer_addr): ConnectInfo, - State(state): State, - headers: HeaderMap, -) -> impl IntoResponse { - if let Err(err) = authorize_upgrade(&headers, state.auth_policy.as_ref()) { - warn!( - %peer_addr, - message = err.message(), - "rejecting websocket client during upgrade" - ); - return (err.status_code(), err.message()).into_response(); - } - let connection_id = ConnectionId(state.connection_counter.fetch_add(1, Ordering::Relaxed)); - info!(%peer_addr, "websocket client connected"); - websocket - .on_upgrade(move |stream| async move { - run_websocket_connection(connection_id, stream, state.transport_event_tx).await; - }) - .into_response() -} +pub(crate) use stdio::start_stdio_connection; +pub(crate) use websocket::start_websocket_acceptor; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum AppServerTransport { @@ -276,262 +166,6 @@ impl OutboundConnectionState { } } -pub(crate) async fn start_stdio_connection( - transport_event_tx: mpsc::Sender, - stdio_handles: &mut Vec>, -) -> IoResult<()> { - let connection_id = ConnectionId(0); - let (writer_tx, mut writer_rx) = mpsc::channel::(CHANNEL_CAPACITY); - let writer_tx_for_reader = writer_tx.clone(); - transport_event_tx - .send(TransportEvent::ConnectionOpened { - connection_id, - writer: writer_tx, - disconnect_sender: None, - }) - .await - .map_err(|_| std::io::Error::new(ErrorKind::BrokenPipe, "processor unavailable"))?; - - let transport_event_tx_for_reader = transport_event_tx.clone(); - stdio_handles.push(tokio::spawn(async move { - let stdin = io::stdin(); - let reader = BufReader::new(stdin); - let mut lines = reader.lines(); - - loop { - match lines.next_line().await { - Ok(Some(line)) => { - if !forward_incoming_message( - &transport_event_tx_for_reader, - &writer_tx_for_reader, - connection_id, - &line, - ) - .await - { - break; - } - } - Ok(None) => break, - Err(err) => { - error!("Failed reading stdin: {err}"); - break; - } - } - } - - let _ = transport_event_tx_for_reader - .send(TransportEvent::ConnectionClosed { connection_id }) - .await; - debug!("stdin reader finished (EOF)"); - })); - - stdio_handles.push(tokio::spawn(async move { - let mut stdout = io::stdout(); - while let Some(queued_message) = writer_rx.recv().await { - let Some(mut json) = serialize_outgoing_message(queued_message.message) else { - continue; - }; - json.push('\n'); - if let Err(err) = stdout.write_all(json.as_bytes()).await { - error!("Failed to write to stdout: {err}"); - break; - } - if let Some(write_complete_tx) = queued_message.write_complete_tx { - let _ = write_complete_tx.send(()); - } - } - info!("stdout writer exited (channel closed)"); - })); - - Ok(()) -} - -pub(crate) async fn start_websocket_acceptor( - bind_address: SocketAddr, - transport_event_tx: mpsc::Sender, - shutdown_token: CancellationToken, - auth_policy: WebsocketAuthPolicy, -) -> IoResult> { - if should_warn_about_unauthenticated_non_loopback_listener(bind_address, &auth_policy) { - warn!( - %bind_address, - "starting non-loopback websocket listener without auth; websocket auth is opt-in for now and will become the default in a future release" - ); - } - let listener = TcpListener::bind(bind_address).await?; - let local_addr = listener.local_addr()?; - print_websocket_startup_banner(local_addr); - info!("app-server websocket listening on ws://{local_addr}"); - - let router = Router::new() - .route("/readyz", get(health_check_handler)) - .route("/healthz", get(health_check_handler)) - .fallback(any(websocket_upgrade_handler)) - .layer(middleware::from_fn(reject_requests_with_origin_header)) - .with_state(WebSocketListenerState { - transport_event_tx, - connection_counter: Arc::new(AtomicU64::new(1)), - auth_policy: Arc::new(auth_policy), - }); - let server = axum::serve( - listener, - router.into_make_service_with_connect_info::(), - ) - .with_graceful_shutdown(async move { - shutdown_token.cancelled().await; - }); - Ok(tokio::spawn(async move { - if let Err(err) = server.await { - error!("websocket acceptor failed: {err}"); - } - info!("websocket acceptor shutting down"); - })) -} - -async fn run_websocket_connection( - connection_id: ConnectionId, - websocket_stream: WebSocket, - transport_event_tx: mpsc::Sender, -) { - let (writer_tx, writer_rx) = mpsc::channel::(CHANNEL_CAPACITY); - let writer_tx_for_reader = writer_tx.clone(); - let disconnect_token = CancellationToken::new(); - if transport_event_tx - .send(TransportEvent::ConnectionOpened { - connection_id, - writer: writer_tx, - disconnect_sender: Some(disconnect_token.clone()), - }) - .await - .is_err() - { - return; - } - - let (websocket_writer, websocket_reader) = websocket_stream.split(); - let (writer_control_tx, writer_control_rx) = - mpsc::channel::(CHANNEL_CAPACITY); - let mut outbound_task = tokio::spawn(run_websocket_outbound_loop( - websocket_writer, - writer_rx, - writer_control_rx, - disconnect_token.clone(), - )); - let mut inbound_task = tokio::spawn(run_websocket_inbound_loop( - websocket_reader, - transport_event_tx.clone(), - writer_tx_for_reader, - writer_control_tx, - connection_id, - disconnect_token.clone(), - )); - - tokio::select! { - _ = &mut outbound_task => { - disconnect_token.cancel(); - inbound_task.abort(); - } - _ = &mut inbound_task => { - disconnect_token.cancel(); - outbound_task.abort(); - } - } - - let _ = transport_event_tx - .send(TransportEvent::ConnectionClosed { connection_id }) - .await; -} - -async fn run_websocket_outbound_loop( - mut websocket_writer: futures::stream::SplitSink, - mut writer_rx: mpsc::Receiver, - mut writer_control_rx: mpsc::Receiver, - disconnect_token: CancellationToken, -) { - loop { - tokio::select! { - _ = disconnect_token.cancelled() => { - break; - } - message = writer_control_rx.recv() => { - let Some(message) = message else { - break; - }; - if websocket_writer.send(message).await.is_err() { - break; - } - } - queued_message = writer_rx.recv() => { - let Some(queued_message) = queued_message else { - break; - }; - let Some(json) = serialize_outgoing_message(queued_message.message) else { - continue; - }; - if websocket_writer.send(WebSocketMessage::Text(json.into())).await.is_err() { - break; - } - if let Some(write_complete_tx) = queued_message.write_complete_tx { - let _ = write_complete_tx.send(()); - } - } - } - } -} - -async fn run_websocket_inbound_loop( - mut websocket_reader: futures::stream::SplitStream, - transport_event_tx: mpsc::Sender, - writer_tx_for_reader: mpsc::Sender, - writer_control_tx: mpsc::Sender, - connection_id: ConnectionId, - disconnect_token: CancellationToken, -) { - loop { - tokio::select! { - _ = disconnect_token.cancelled() => { - break; - } - incoming_message = websocket_reader.next() => { - match incoming_message { - Some(Ok(WebSocketMessage::Text(text))) => { - if !forward_incoming_message( - &transport_event_tx, - &writer_tx_for_reader, - connection_id, - text.as_ref(), - ) - .await - { - break; - } - } - Some(Ok(WebSocketMessage::Ping(payload))) => { - match writer_control_tx.try_send(WebSocketMessage::Pong(payload)) { - Ok(()) => {} - Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => break, - Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => { - warn!("websocket control queue full while replying to ping; closing connection"); - break; - } - } - } - Some(Ok(WebSocketMessage::Pong(_))) => {} - Some(Ok(WebSocketMessage::Close(_))) | None => break, - Some(Ok(WebSocketMessage::Binary(_))) => { - warn!("dropping unsupported binary websocket message"); - } - Some(Err(err)) => { - warn!("websocket receive error: {err}"); - break; - } - } - } - } - } -} - async fn forward_incoming_message( transport_event_tx: &mpsc::Sender, writer: &mpsc::Sender, diff --git a/codex-rs/app-server/src/transport/stdio.rs b/codex-rs/app-server/src/transport/stdio.rs new file mode 100644 index 0000000000..4f2bf26745 --- /dev/null +++ b/codex-rs/app-server/src/transport/stdio.rs @@ -0,0 +1,88 @@ +use super::CHANNEL_CAPACITY; +use super::TransportEvent; +use super::forward_incoming_message; +use super::serialize_outgoing_message; +use crate::outgoing_message::ConnectionId; +use crate::outgoing_message::QueuedOutgoingMessage; +use std::io::ErrorKind; +use std::io::Result as IoResult; +use tokio::io; +use tokio::io::AsyncBufReadExt; +use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; +use tokio::sync::mpsc; +use tokio::task::JoinHandle; +use tracing::debug; +use tracing::error; +use tracing::info; + +pub(crate) async fn start_stdio_connection( + transport_event_tx: mpsc::Sender, + stdio_handles: &mut Vec>, +) -> IoResult<()> { + let connection_id = ConnectionId(0); + let (writer_tx, mut writer_rx) = mpsc::channel::(CHANNEL_CAPACITY); + let writer_tx_for_reader = writer_tx.clone(); + transport_event_tx + .send(TransportEvent::ConnectionOpened { + connection_id, + writer: writer_tx, + disconnect_sender: None, + }) + .await + .map_err(|_| std::io::Error::new(ErrorKind::BrokenPipe, "processor unavailable"))?; + + let transport_event_tx_for_reader = transport_event_tx.clone(); + stdio_handles.push(tokio::spawn(async move { + let stdin = io::stdin(); + let reader = BufReader::new(stdin); + let mut lines = reader.lines(); + + loop { + match lines.next_line().await { + Ok(Some(line)) => { + if !forward_incoming_message( + &transport_event_tx_for_reader, + &writer_tx_for_reader, + connection_id, + &line, + ) + .await + { + break; + } + } + Ok(None) => break, + Err(err) => { + error!("Failed reading stdin: {err}"); + break; + } + } + } + + let _ = transport_event_tx_for_reader + .send(TransportEvent::ConnectionClosed { connection_id }) + .await; + debug!("stdin reader finished (EOF)"); + })); + + stdio_handles.push(tokio::spawn(async move { + let mut stdout = io::stdout(); + while let Some(queued_message) = writer_rx.recv().await { + let Some(mut json) = serialize_outgoing_message(queued_message.message) else { + continue; + }; + json.push('\n'); + if let Err(err) = stdout.write_all(json.as_bytes()).await { + error!("Failed to write to stdout: {err}"); + break; + } + if let Some(write_complete_tx) = queued_message.write_complete_tx { + let _ = write_complete_tx.send(()); + } + } + info!("stdout writer exited (channel closed)"); + })); + + Ok(()) +} diff --git a/codex-rs/app-server/src/transport/websocket.rs b/codex-rs/app-server/src/transport/websocket.rs new file mode 100644 index 0000000000..05dfe24b05 --- /dev/null +++ b/codex-rs/app-server/src/transport/websocket.rs @@ -0,0 +1,308 @@ +use super::CHANNEL_CAPACITY; +use super::TransportEvent; +use super::auth::WebsocketAuthPolicy; +use super::auth::authorize_upgrade; +use super::auth::should_warn_about_unauthenticated_non_loopback_listener; +use super::forward_incoming_message; +use super::serialize_outgoing_message; +use crate::outgoing_message::ConnectionId; +use crate::outgoing_message::QueuedOutgoingMessage; +use axum::Router; +use axum::body::Body; +use axum::extract::ConnectInfo; +use axum::extract::State; +use axum::extract::ws::Message as WebSocketMessage; +use axum::extract::ws::WebSocket; +use axum::extract::ws::WebSocketUpgrade; +use axum::http::HeaderMap; +use axum::http::Request; +use axum::http::StatusCode; +use axum::http::header::ORIGIN; +use axum::middleware; +use axum::middleware::Next; +use axum::response::IntoResponse; +use axum::response::Response; +use axum::routing::any; +use axum::routing::get; +use futures::SinkExt; +use futures::StreamExt; +use owo_colors::OwoColorize; +use owo_colors::Stream; +use owo_colors::Style; +use std::io::Result as IoResult; +use std::net::SocketAddr; +use std::sync::Arc; +use std::sync::atomic::AtomicU64; +use std::sync::atomic::Ordering; +use tokio::net::TcpListener; +use tokio::sync::mpsc; +use tokio::task::JoinHandle; +use tokio_util::sync::CancellationToken; +use tracing::error; +use tracing::info; +use tracing::warn; + +fn colorize(text: &str, style: Style) -> String { + text.if_supports_color(Stream::Stderr, |value| value.style(style)) + .to_string() +} + +#[allow(clippy::print_stderr)] +fn print_websocket_startup_banner(addr: SocketAddr) { + let title = colorize("codex app-server (WebSockets)", Style::new().bold().cyan()); + let listening_label = colorize("listening on:", Style::new().dimmed()); + let listen_url = colorize(&format!("ws://{addr}"), Style::new().green()); + let ready_label = colorize("readyz:", Style::new().dimmed()); + let ready_url = colorize(&format!("http://{addr}/readyz"), Style::new().green()); + let health_label = colorize("healthz:", Style::new().dimmed()); + let health_url = colorize(&format!("http://{addr}/healthz"), Style::new().green()); + let note_label = colorize("note:", Style::new().dimmed()); + eprintln!("{title}"); + eprintln!(" {listening_label} {listen_url}"); + eprintln!(" {ready_label} {ready_url}"); + eprintln!(" {health_label} {health_url}"); + if addr.ip().is_loopback() { + eprintln!( + " {note_label} binds localhost only (use SSH port-forwarding for remote access)" + ); + } else { + eprintln!( + " {note_label} websocket auth is opt-in in this build; configure `--ws-auth ...` before real remote use" + ); + } +} + +#[derive(Clone)] +struct WebSocketListenerState { + transport_event_tx: mpsc::Sender, + connection_counter: Arc, + auth_policy: Arc, +} + +async fn health_check_handler() -> StatusCode { + StatusCode::OK +} + +async fn reject_requests_with_origin_header( + request: Request, + next: Next, +) -> Result { + if request.headers().contains_key(ORIGIN) { + warn!( + method = %request.method(), + uri = %request.uri(), + "rejecting websocket listener request with Origin header" + ); + Err(StatusCode::FORBIDDEN) + } else { + Ok(next.run(request).await) + } +} + +async fn websocket_upgrade_handler( + websocket: WebSocketUpgrade, + ConnectInfo(peer_addr): ConnectInfo, + State(state): State, + headers: HeaderMap, +) -> impl IntoResponse { + if let Err(err) = authorize_upgrade(&headers, state.auth_policy.as_ref()) { + warn!( + %peer_addr, + message = err.message(), + "rejecting websocket client during upgrade" + ); + return (err.status_code(), err.message()).into_response(); + } + let connection_id = ConnectionId(state.connection_counter.fetch_add(1, Ordering::Relaxed)); + info!(%peer_addr, "websocket client connected"); + websocket + .on_upgrade(move |stream| async move { + run_websocket_connection(connection_id, stream, state.transport_event_tx).await; + }) + .into_response() +} + +pub(crate) async fn start_websocket_acceptor( + bind_address: SocketAddr, + transport_event_tx: mpsc::Sender, + shutdown_token: CancellationToken, + auth_policy: WebsocketAuthPolicy, +) -> IoResult> { + if should_warn_about_unauthenticated_non_loopback_listener(bind_address, &auth_policy) { + warn!( + %bind_address, + "starting non-loopback websocket listener without auth; websocket auth is opt-in for now and will become the default in a future release" + ); + } + let listener = TcpListener::bind(bind_address).await?; + let local_addr = listener.local_addr()?; + print_websocket_startup_banner(local_addr); + info!("app-server websocket listening on ws://{local_addr}"); + + let router = Router::new() + .route("/readyz", get(health_check_handler)) + .route("/healthz", get(health_check_handler)) + .fallback(any(websocket_upgrade_handler)) + .layer(middleware::from_fn(reject_requests_with_origin_header)) + .with_state(WebSocketListenerState { + transport_event_tx, + connection_counter: Arc::new(AtomicU64::new(1)), + auth_policy: Arc::new(auth_policy), + }); + let server = axum::serve( + listener, + router.into_make_service_with_connect_info::(), + ) + .with_graceful_shutdown(async move { + shutdown_token.cancelled().await; + }); + Ok(tokio::spawn(async move { + if let Err(err) = server.await { + error!("websocket acceptor failed: {err}"); + } + info!("websocket acceptor shutting down"); + })) +} + +async fn run_websocket_connection( + connection_id: ConnectionId, + websocket_stream: WebSocket, + transport_event_tx: mpsc::Sender, +) { + let (writer_tx, writer_rx) = mpsc::channel::(CHANNEL_CAPACITY); + let writer_tx_for_reader = writer_tx.clone(); + let disconnect_token = CancellationToken::new(); + if transport_event_tx + .send(TransportEvent::ConnectionOpened { + connection_id, + writer: writer_tx, + disconnect_sender: Some(disconnect_token.clone()), + }) + .await + .is_err() + { + return; + } + + let (websocket_writer, websocket_reader) = websocket_stream.split(); + let (writer_control_tx, writer_control_rx) = + mpsc::channel::(CHANNEL_CAPACITY); + let mut outbound_task = tokio::spawn(run_websocket_outbound_loop( + websocket_writer, + writer_rx, + writer_control_rx, + disconnect_token.clone(), + )); + let mut inbound_task = tokio::spawn(run_websocket_inbound_loop( + websocket_reader, + transport_event_tx.clone(), + writer_tx_for_reader, + writer_control_tx, + connection_id, + disconnect_token.clone(), + )); + + tokio::select! { + _ = &mut outbound_task => { + disconnect_token.cancel(); + inbound_task.abort(); + } + _ = &mut inbound_task => { + disconnect_token.cancel(); + outbound_task.abort(); + } + } + + let _ = transport_event_tx + .send(TransportEvent::ConnectionClosed { connection_id }) + .await; +} + +async fn run_websocket_outbound_loop( + mut websocket_writer: futures::stream::SplitSink, + mut writer_rx: mpsc::Receiver, + mut writer_control_rx: mpsc::Receiver, + disconnect_token: CancellationToken, +) { + loop { + tokio::select! { + _ = disconnect_token.cancelled() => { + break; + } + message = writer_control_rx.recv() => { + let Some(message) = message else { + break; + }; + if websocket_writer.send(message).await.is_err() { + break; + } + } + queued_message = writer_rx.recv() => { + let Some(queued_message) = queued_message else { + break; + }; + let Some(json) = serialize_outgoing_message(queued_message.message) else { + continue; + }; + if websocket_writer.send(WebSocketMessage::Text(json.into())).await.is_err() { + break; + } + if let Some(write_complete_tx) = queued_message.write_complete_tx { + let _ = write_complete_tx.send(()); + } + } + } + } +} + +async fn run_websocket_inbound_loop( + mut websocket_reader: futures::stream::SplitStream, + transport_event_tx: mpsc::Sender, + writer_tx_for_reader: mpsc::Sender, + writer_control_tx: mpsc::Sender, + connection_id: ConnectionId, + disconnect_token: CancellationToken, +) { + loop { + tokio::select! { + _ = disconnect_token.cancelled() => { + break; + } + incoming_message = websocket_reader.next() => { + match incoming_message { + Some(Ok(WebSocketMessage::Text(text))) => { + if !forward_incoming_message( + &transport_event_tx, + &writer_tx_for_reader, + connection_id, + text.as_ref(), + ) + .await + { + break; + } + } + Some(Ok(WebSocketMessage::Ping(payload))) => { + match writer_control_tx.try_send(WebSocketMessage::Pong(payload)) { + Ok(()) => {} + Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => break, + Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => { + warn!("websocket control queue full while replying to ping; closing connection"); + break; + } + } + } + Some(Ok(WebSocketMessage::Pong(_))) => {} + Some(Ok(WebSocketMessage::Close(_))) | None => break, + Some(Ok(WebSocketMessage::Binary(_))) => { + warn!("dropping unsupported binary websocket message"); + } + Some(Err(err)) => { + warn!("websocket receive error: {err}"); + break; + } + } + } + } + } +} From 86764af684e9091963085f361daa3c521649bb76 Mon Sep 17 00:00:00 2001 From: rreichel3-oai Date: Thu, 26 Mar 2026 16:06:53 -0400 Subject: [PATCH 013/191] Protect first-time project .codex creation across Linux and macOS sandboxes (#15067) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem Codex already treated an existing top-level project `./.codex` directory as protected, but there was a gap on first creation. If `./.codex` did not exist yet, a turn could create files under it, such as `./.codex/config.toml`, without going through the same approval path as later modifications. That meant the initial write could bypass the intended protection for project-local Codex state. ## What this changes This PR closes that first-creation gap in the Unix enforcement layers: - `codex-protocol` - treat the top-level project `./.codex` path as a protected carveout even when it does not exist yet - avoid injecting the default carveout when the user already has an explicit rule for that exact path - macOS Seatbelt - deny writes to both the exact protected path and anything beneath it, so creating `./.codex` itself is blocked in addition to writes inside it - Linux bubblewrap - preserve the same protected-path behavior for first-time creation under `./.codex` - tests - add protocol regressions for missing `./.codex` and explicit-rule collisions - add Unix sandbox coverage for blocking first-time `./.codex` creation - tighten Seatbelt policy assertions around excluded subpaths ## Scope This change is intentionally scoped to protecting the top-level project `.codex` subtree from agent writes. It does not make `.codex` unreadable, and it does not change the product behavior around loading project skills from `.codex` when project config is untrusted. ## Why this shape The fix is pointed rather than broad: - it preserves the current model of “project `.codex` is protected from writes” - it closes the security-relevant first-write hole - it avoids folding a larger permissions-model redesign into this PR ## Validation - `cargo test -p codex-protocol` - `cargo test -p codex-sandboxing seatbelt` - `cargo test -p codex-exec --test all sandbox_blocks_first_time_dot_codex_creation -- --nocapture` --------- Co-authored-by: Michael Bolin --- codex-rs/core/src/codex_tests.rs | 33 +-- codex-rs/core/src/codex_thread.rs | 10 + codex-rs/core/src/exec_tests.rs | 18 +- codex-rs/core/src/safety_tests.rs | 34 +++ codex-rs/exec/tests/suite/sandbox.rs | 73 +++++++ codex-rs/linux-sandbox/src/bwrap.rs | 11 + codex-rs/protocol/src/permissions.rs | 248 ++++++++++++++++++++-- codex-rs/protocol/src/protocol.rs | 54 +++-- codex-rs/sandboxing/src/seatbelt.rs | 8 +- codex-rs/sandboxing/src/seatbelt_tests.rs | 194 ++++++++++++----- 10 files changed, 571 insertions(+), 112 deletions(-) diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index 8bd6759202..5a556f2071 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -1162,35 +1162,10 @@ async fn fork_startup_context_then_first_turn_diff_snapshot() -> anyhow::Result< }) .await?; wait_for_event(&initial.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; - // The parent rollout writer drains asynchronously after turn completion. - // Wait until the persisted JSONL includes the source user turn before forking from it. - let mut source_history_persisted = false; - for _ in 0..100 { - let history = RolloutRecorder::get_rollout_history(&rollout_path).await; - source_history_persisted = history.ok().is_some_and(|history| { - history.get_rollout_items().into_iter().any(|item| { - matches!( - item, - RolloutItem::ResponseItem(ResponseItem::Message { role, content, .. }) - if role == "user" - && content.iter().any(|content_item| { - matches!( - content_item, - ContentItem::InputText { text } if text == "fork seed" - ) - }) - ) - }) - }); - if source_history_persisted { - break; - } - sleep(StdDuration::from_millis(10)).await; - } - assert!( - source_history_persisted, - "source rollout should contain the completed pre-fork user turn before forking" - ); + // Forking reads the persisted rollout JSONL, so force the completed source turn to disk + // before snapshotting from it. + initial.codex.ensure_rollout_materialized().await; + initial.codex.flush_rollout().await; let mut fork_config = initial.config.clone(); fork_config.permissions.approval_policy = diff --git a/codex-rs/core/src/codex_thread.rs b/codex-rs/core/src/codex_thread.rs index 9fa7069118..0635718c9c 100644 --- a/codex-rs/core/src/codex_thread.rs +++ b/codex-rs/core/src/codex_thread.rs @@ -74,6 +74,16 @@ impl CodexThread { self.codex.shutdown_and_wait().await } + #[doc(hidden)] + pub async fn ensure_rollout_materialized(&self) { + self.codex.session.ensure_rollout_materialized().await; + } + + #[doc(hidden)] + pub async fn flush_rollout(&self) { + self.codex.session.flush_rollout().await; + } + pub async fn submit_with_trace( &self, op: Op, diff --git a/codex-rs/core/src/exec_tests.rs b/codex-rs/core/src/exec_tests.rs index 074db95e1c..07532e5a18 100644 --- a/codex-rs/core/src/exec_tests.rs +++ b/codex-rs/core/src/exec_tests.rs @@ -595,6 +595,19 @@ fn windows_restricted_token_supports_full_read_split_write_read_carveouts() { }, ]); + #[cfg(windows)] + let expected_deny_write_paths = vec![ + codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(cwd.join(".codex")) + .expect("absolute .codex"), + codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(&docs) + .expect("absolute docs"), + ]; + #[cfg(not(windows))] + let expected_deny_write_paths = vec![ + codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(&docs) + .expect("absolute docs"), + ]; + assert_eq!( resolve_windows_restricted_token_filesystem_overlay( SandboxType::WindowsRestrictedToken, @@ -605,10 +618,7 @@ fn windows_restricted_token_supports_full_read_split_write_read_carveouts() { WindowsSandboxLevel::RestrictedToken, ), Ok(Some(WindowsRestrictedTokenFilesystemOverlay { - additional_deny_write_paths: vec![ - codex_utils_absolute_path::AbsolutePathBuf::from_absolute_path(&docs) - .expect("absolute docs"), - ], + additional_deny_write_paths: expected_deny_write_paths, })) ); } diff --git a/codex-rs/core/src/safety_tests.rs b/codex-rs/core/src/safety_tests.rs index 3d05664ba3..e7f55693d1 100644 --- a/codex-rs/core/src/safety_tests.rs +++ b/codex-rs/core/src/safety_tests.rs @@ -252,3 +252,37 @@ fn explicit_read_only_subpaths_prevent_auto_approval_for_external_sandbox() { SafetyCheck::AskUser, ); } + +#[test] +fn missing_project_dot_codex_config_requires_approval() { + let tmp = TempDir::new().unwrap(); + let cwd = tmp.path().to_path_buf(); + let action = + ApplyPatchAction::new_add_for_test(&cwd.join(".codex").join("config.toml"), "".to_string()); + let sandbox_policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![], + read_only_access: Default::default(), + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + let file_system_sandbox_policy = + FileSystemSandboxPolicy::from_legacy_sandbox_policy(&sandbox_policy, &cwd); + + assert!(!is_write_patch_constrained_to_writable_paths( + &action, + &file_system_sandbox_policy, + &cwd, + )); + assert_eq!( + assess_patch_safety( + &action, + AskForApproval::OnRequest, + &sandbox_policy, + &file_system_sandbox_policy, + &cwd, + WindowsSandboxLevel::Disabled, + ), + SafetyCheck::AskUser, + ); +} diff --git a/codex-rs/exec/tests/suite/sandbox.rs b/codex-rs/exec/tests/suite/sandbox.rs index 744a9998ff..196fc68e84 100644 --- a/codex-rs/exec/tests/suite/sandbox.rs +++ b/codex-rs/exec/tests/suite/sandbox.rs @@ -118,6 +118,7 @@ async fn python_multiprocessing_lock_works_under_sandbox() { #[cfg(target_os = "linux")] let sandbox_env = match linux_sandbox_test_env().await { Some(env) => env, + // Skip on Linux hosts where Landlock cannot actually be enforced. None => return, }; #[cfg(not(target_os = "linux"))] @@ -312,6 +313,78 @@ async fn sandbox_distinguishes_command_and_policy_cwds() { assert!(allowed_exists, "allowed path should exist"); } +#[tokio::test] +async fn sandbox_blocks_first_time_dot_codex_creation() { + core_test_support::skip_if_sandbox!(); + #[cfg(target_os = "linux")] + let sandbox_env = match linux_sandbox_test_env().await { + Some(env) => env, + None => return, + }; + #[cfg(not(target_os = "linux"))] + let sandbox_env = HashMap::new(); + + let temp = tempfile::tempdir().expect("should be able to create temp dir"); + let repo_root = temp.path().join("repo"); + create_dir_all(&repo_root).await.expect("mkdir repo"); + let dot_codex = repo_root.join(".codex"); + let config_toml = dot_codex.join("config.toml"); + let policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![], + read_only_access: Default::default(), + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + + let mut child = spawn_command_under_sandbox( + vec![ + "bash".to_string(), + "-lc".to_string(), + "mkdir -p .codex && echo 'sandbox_mode = \"danger-full-access\"' > .codex/config.toml" + .to_string(), + ], + repo_root.clone(), + &policy, + repo_root.as_path(), + StdioPolicy::RedirectForShellTool, + sandbox_env, + ) + .await + .expect("should spawn command creating .codex"); + + let status = child.wait().await.expect("should wait for .codex command"); + assert!( + !status.success(), + "sandbox unexpectedly allowed first-time .codex creation: {status:?}" + ); + let dot_codex_metadata = tokio::fs::symlink_metadata(&dot_codex).await; + if let Ok(metadata) = dot_codex_metadata { + assert!( + !metadata.is_dir(), + "{} should not be creatable as a directory", + dot_codex.display() + ); + } else if let Err(err) = &dot_codex_metadata { + assert_eq!( + err.kind(), + io::ErrorKind::NotFound, + "unexpected metadata error for {}: {err}", + dot_codex.display() + ); + } + let config_toml_exists = match tokio::fs::try_exists(&config_toml).await { + Ok(exists) => exists, + Err(err) if err.kind() == io::ErrorKind::NotADirectory => false, + Err(err) => panic!("try_exists {} failed: {err}", config_toml.display()), + }; + assert!( + !config_toml_exists, + "{} should not have been created", + config_toml.display() + ); +} + fn unix_sock_body() { unsafe { let mut fds = [0i32; 2]; diff --git a/codex-rs/linux-sandbox/src/bwrap.rs b/codex-rs/linux-sandbox/src/bwrap.rs index 05e7b58e2c..dfc5b1282a 100644 --- a/codex-rs/linux-sandbox/src/bwrap.rs +++ b/codex-rs/linux-sandbox/src/bwrap.rs @@ -770,14 +770,25 @@ mod tests { assert_eq!( args.args, vec![ + // Start from a read-only view of the full filesystem. "--ro-bind".to_string(), "/".to_string(), "/".to_string(), + // Recreate a writable /dev inside the sandbox. "--dev".to_string(), "/dev".to_string(), + // Make the writable root itself writable again. "--bind".to_string(), "/".to_string(), "/".to_string(), + // Mask the default protected .codex subpath under that writable + // root. Because the root is `/` in this test, the carveout path + // appears as `/.codex`. + "--ro-bind".to_string(), + "/dev/null".to_string(), + "/.codex".to_string(), + // Rebind /dev after the root bind so device nodes remain + // writable/usable inside the writable root. "--bind".to_string(), "/dev".to_string(), "/dev".to_string(), diff --git a/codex-rs/protocol/src/permissions.rs b/codex-rs/protocol/src/permissions.rs index 0a5b337029..978334f696 100644 --- a/codex-rs/protocol/src/permissions.rs +++ b/codex-rs/protocol/src/permissions.rs @@ -264,7 +264,7 @@ impl FileSystemSandboxPolicy { /// into split filesystem policy. pub fn from_legacy_sandbox_policy(sandbox_policy: &SandboxPolicy, cwd: &Path) -> Self { let mut file_system_policy = Self::from(sandbox_policy); - if matches!(sandbox_policy, SandboxPolicy::WorkspaceWrite { .. }) { + if let SandboxPolicy::WorkspaceWrite { writable_roots, .. } = sandbox_policy { let legacy_writable_roots = sandbox_policy.get_writable_roots_with_cwd(cwd); file_system_policy.entries.retain(|entry| { if entry.access != FileSystemAccessMode::Read { @@ -278,6 +278,28 @@ impl FileSystemSandboxPolicy { FileSystemPath::Special { .. } => true, } }); + + if let Ok(cwd_root) = AbsolutePathBuf::from_absolute_path(cwd) { + for protected_path in default_read_only_subpaths_for_writable_root( + &cwd_root, /*protect_missing_dot_codex*/ true, + ) { + append_default_read_only_path_if_no_explicit_rule( + &mut file_system_policy.entries, + protected_path, + ); + } + } + for writable_root in writable_roots { + for protected_path in default_read_only_subpaths_for_writable_root( + writable_root, + /*protect_missing_dot_codex*/ false, + ) { + append_default_read_only_path_if_no_explicit_rule( + &mut file_system_policy.entries, + protected_path, + ); + } + } } file_system_policy @@ -454,7 +476,14 @@ impl FileSystemSandboxPolicy { .iter() .filter(|path| normalize_effective_absolute_path((*path).clone()) == root) .collect(); - let mut read_only_subpaths = default_read_only_subpaths_for_writable_root(&root); + let protect_missing_dot_codex = AbsolutePathBuf::from_absolute_path(cwd) + .ok() + .is_some_and(|cwd| normalize_effective_absolute_path(cwd) == root); + let mut read_only_subpaths: Vec = + default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex) + .into_iter() + .filter(|path| !has_explicit_resolved_path_entry(&resolved_entries, path)) + .collect(); // Narrower explicit non-write entries carve out broader writable roots. // More specific write entries still remain writable because they appear // as separate WritableRoot values and are checked independently. @@ -1068,6 +1097,7 @@ fn normalize_effective_absolute_path(path: AbsolutePathBuf) -> AbsolutePathBuf { fn default_read_only_subpaths_for_writable_root( writable_root: &AbsolutePathBuf, + protect_missing_dot_codex: bool, ) -> Vec { let mut subpaths: Vec = Vec::new(); #[allow(clippy::expect_used)] @@ -1089,19 +1119,69 @@ fn default_read_only_subpaths_for_writable_root( subpaths.push(top_level_git); } - // Make .agents/skills and .codex/config.toml and related files read-only - // to the agent, by default. - for subdir in &[".agents", ".codex"] { - #[allow(clippy::expect_used)] - let top_level_codex = writable_root.join(subdir).expect("valid relative path"); - if top_level_codex.as_path().is_dir() { - subpaths.push(top_level_codex); - } + #[allow(clippy::expect_used)] + let top_level_agents = writable_root.join(".agents").expect("valid relative path"); + if top_level_agents.as_path().is_dir() { + subpaths.push(top_level_agents); + } + + // Keep top-level project metadata under .codex read-only to the agent by + // default. For the workspace root itself, protect it even before the + // directory exists so first-time creation still goes through the + // protected-path approval flow. + #[allow(clippy::expect_used)] + let top_level_codex = writable_root.join(".codex").expect("valid relative path"); + if protect_missing_dot_codex || top_level_codex.as_path().is_dir() { + subpaths.push(top_level_codex); } dedup_absolute_paths(subpaths, /*normalize_effective_paths*/ false) } +fn append_path_entry_if_missing( + entries: &mut Vec, + path: AbsolutePathBuf, + access: FileSystemAccessMode, +) { + if entries.iter().any(|entry| { + entry.access == access + && matches!( + &entry.path, + FileSystemPath::Path { path: existing } if existing == &path + ) + }) { + return; + } + + entries.push(FileSystemSandboxEntry { + path: FileSystemPath::Path { path }, + access, + }); +} + +fn append_default_read_only_path_if_no_explicit_rule( + entries: &mut Vec, + path: AbsolutePathBuf, +) { + if entries.iter().any(|entry| { + matches!( + &entry.path, + FileSystemPath::Path { path: existing } if existing == &path + ) + }) { + return; + } + + append_path_entry_if_missing(entries, path, FileSystemAccessMode::Read); +} + +fn has_explicit_resolved_path_entry( + entries: &[ResolvedFileSystemEntry], + path: &AbsolutePathBuf, +) -> bool { + entries.iter().any(|entry| &entry.path == path) +} + fn is_git_pointer_file(path: &AbsolutePathBuf) -> bool { path.as_path().is_file() && path.as_path().file_name() == Some(OsStr::new(".git")) } @@ -1211,6 +1291,148 @@ mod tests { Ok(()) } + #[cfg(unix)] + #[test] + fn writable_roots_proactively_protect_missing_dot_codex() { + let cwd = TempDir::new().expect("tempdir"); + let expected_root = AbsolutePathBuf::from_absolute_path( + cwd.path().canonicalize().expect("canonicalize cwd"), + ) + .expect("absolute canonical root"); + let expected_dot_codex = expected_root.join(".codex").expect("expected .codex path"); + + let policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::CurrentWorkingDirectory, + }, + access: FileSystemAccessMode::Write, + }]); + + let writable_roots = policy.get_writable_roots_with_cwd(cwd.path()); + assert_eq!(writable_roots.len(), 1); + assert_eq!(writable_roots[0].root, expected_root); + assert!( + writable_roots[0] + .read_only_subpaths + .contains(&expected_dot_codex) + ); + } + + #[cfg(unix)] + #[test] + fn writable_roots_skip_default_dot_codex_when_explicit_user_rule_exists() { + let cwd = TempDir::new().expect("tempdir"); + let expected_root = AbsolutePathBuf::from_absolute_path( + cwd.path().canonicalize().expect("canonicalize cwd"), + ) + .expect("absolute canonical root"); + let explicit_dot_codex = expected_root.join(".codex").expect("expected .codex path"); + + let policy = FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::CurrentWorkingDirectory, + }, + access: FileSystemAccessMode::Write, + }, + FileSystemSandboxEntry { + path: FileSystemPath::Path { + path: explicit_dot_codex.clone(), + }, + access: FileSystemAccessMode::Write, + }, + ]); + + let writable_roots = policy.get_writable_roots_with_cwd(cwd.path()); + let workspace_root = writable_roots + .iter() + .find(|root| root.root == expected_root) + .expect("workspace writable root"); + assert!( + !workspace_root + .read_only_subpaths + .contains(&explicit_dot_codex), + "explicit .codex rule should win over the default protected carveout" + ); + assert!( + policy.can_write_path_with_cwd( + explicit_dot_codex + .join("config.toml") + .expect("config.toml") + .as_path(), + cwd.path() + ) + ); + } + + #[test] + fn legacy_workspace_write_projection_blocks_missing_dot_codex_writes() { + let cwd = TempDir::new().expect("tempdir"); + let dot_codex_config = cwd.path().join(".codex").join("config.toml"); + let policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![], + read_only_access: ReadOnlyAccess::Restricted { + include_platform_defaults: false, + readable_roots: vec![], + }, + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + + let file_system_policy = + FileSystemSandboxPolicy::from_legacy_sandbox_policy(&policy, cwd.path()); + + assert!(!file_system_policy.can_write_path_with_cwd(&dot_codex_config, cwd.path())); + } + + #[test] + fn legacy_workspace_write_projection_accepts_relative_cwd() { + let relative_cwd = Path::new("workspace"); + let expected_dot_codex = AbsolutePathBuf::from_absolute_path( + std::env::current_dir() + .expect("current dir") + .join(relative_cwd) + .join(".codex"), + ) + .expect("absolute dot codex"); + let policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![], + read_only_access: ReadOnlyAccess::Restricted { + include_platform_defaults: false, + readable_roots: vec![], + }, + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + + let file_system_policy = + FileSystemSandboxPolicy::from_legacy_sandbox_policy(&policy, relative_cwd); + + assert_eq!( + file_system_policy, + FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::CurrentWorkingDirectory, + }, + access: FileSystemAccessMode::Write, + }, + FileSystemSandboxEntry { + path: FileSystemPath::Path { + path: expected_dot_codex, + }, + access: FileSystemAccessMode::Read, + }, + ]) + ); + assert!( + !file_system_policy + .can_write_path_with_cwd(Path::new(".codex/config.toml"), relative_cwd,) + ); + } + #[cfg(unix)] #[test] fn effective_runtime_roots_canonicalize_symlinked_paths() { @@ -1695,8 +1917,10 @@ mod tests { policy.needs_direct_runtime_enforcement(NetworkSandboxPolicy::Restricted, cwd.path(),) ); - let legacy_workspace_write = - FileSystemSandboxPolicy::from(&SandboxPolicy::new_workspace_write_policy()); + let legacy_workspace_write = FileSystemSandboxPolicy::from_legacy_sandbox_policy( + &SandboxPolicy::new_workspace_write_policy(), + cwd.path(), + ); assert!( !legacy_workspace_write .needs_direct_runtime_enforcement(NetworkSandboxPolicy::Restricted, cwd.path(),) diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 2e67fa5d73..71c9cbda64 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1064,13 +1064,20 @@ impl SandboxPolicy { } // For each root, compute subpaths that should remain read-only. + let cwd_root = AbsolutePathBuf::from_absolute_path(cwd).ok(); roots .into_iter() - .map(|writable_root| WritableRoot { - read_only_subpaths: default_read_only_subpaths_for_writable_root( - &writable_root, - ), - root: writable_root, + .map(|writable_root| { + let protect_missing_dot_codex = cwd_root + .as_ref() + .is_some_and(|cwd_root| cwd_root == &writable_root); + WritableRoot { + read_only_subpaths: default_read_only_subpaths_for_writable_root( + &writable_root, + protect_missing_dot_codex, + ), + root: writable_root, + } }) .collect() } @@ -1080,6 +1087,7 @@ impl SandboxPolicy { fn default_read_only_subpaths_for_writable_root( writable_root: &AbsolutePathBuf, + protect_missing_dot_codex: bool, ) -> Vec { let mut subpaths: Vec = Vec::new(); #[allow(clippy::expect_used)] @@ -1101,14 +1109,20 @@ fn default_read_only_subpaths_for_writable_root( subpaths.push(top_level_git); } - // Make .agents/skills and .codex/config.toml and related files read-only - // to the agent, by default. - for subdir in &[".agents", ".codex"] { - #[allow(clippy::expect_used)] - let top_level_codex = writable_root.join(subdir).expect("valid relative path"); - if top_level_codex.as_path().is_dir() { - subpaths.push(top_level_codex); - } + #[allow(clippy::expect_used)] + let top_level_agents = writable_root.join(".agents").expect("valid relative path"); + if top_level_agents.as_path().is_dir() { + subpaths.push(top_level_agents); + } + + // Keep top-level project metadata under .codex read-only to the agent by + // default. For the workspace root itself, protect it even before the + // directory exists so first-time creation still goes through the + // protected-path approval flow. + #[allow(clippy::expect_used)] + let top_level_codex = writable_root.join(".codex").expect("valid relative path"); + if protect_missing_dot_codex || top_level_codex.as_path().is_dir() { + subpaths.push(top_level_codex); } let mut deduped = Vec::with_capacity(subpaths.len()); @@ -4095,6 +4109,8 @@ mod tests { let expected_docs_public = AbsolutePathBuf::from_absolute_path(canonical_cwd.join("docs/public")) .expect("canonical docs/public"); + let expected_dot_codex = AbsolutePathBuf::from_absolute_path(canonical_cwd.join(".codex")) + .expect("canonical .codex"); let policy = FileSystemSandboxPolicy::restricted(vec![ FileSystemSandboxEntry { path: FileSystemPath::Special { @@ -4116,7 +4132,13 @@ mod tests { assert_eq!( sorted_writable_roots(policy.get_writable_roots_with_cwd(cwd.path())), vec![ - (canonical_cwd, vec![expected_docs.to_path_buf()]), + ( + canonical_cwd, + vec![ + expected_dot_codex.to_path_buf(), + expected_docs.to_path_buf() + ], + ), (expected_docs_public.to_path_buf(), Vec::new()), ] ); @@ -4128,6 +4150,8 @@ mod tests { let docs = AbsolutePathBuf::resolve_path_against_base("docs", cwd.path()).expect("resolve docs"); let canonical_cwd = cwd.path().canonicalize().expect("canonicalize cwd"); + let expected_dot_codex = AbsolutePathBuf::from_absolute_path(canonical_cwd.join(".codex")) + .expect("canonical .codex"); let policy = SandboxPolicy::WorkspaceWrite { writable_roots: vec![], read_only_access: ReadOnlyAccess::Restricted { @@ -4144,7 +4168,7 @@ mod tests { FileSystemSandboxPolicy::from_legacy_sandbox_policy(&policy, cwd.path()) .get_writable_roots_with_cwd(cwd.path()) ), - vec![(canonical_cwd, Vec::new())] + vec![(canonical_cwd, vec![expected_dot_codex.to_path_buf()])] ); } diff --git a/codex-rs/sandboxing/src/seatbelt.rs b/codex-rs/sandboxing/src/seatbelt.rs index e7cca3d2ac..e6ff71d0d3 100644 --- a/codex-rs/sandboxing/src/seatbelt.rs +++ b/codex-rs/sandboxing/src/seatbelt.rs @@ -330,8 +330,14 @@ fn build_seatbelt_access_policy( { let excluded_subpath = normalize_path_for_sandbox(excluded_subpath.as_path()).unwrap_or(excluded_subpath); - let excluded_param = format!("{param_prefix}_{index}_RO_{excluded_index}"); + let excluded_param = format!("{param_prefix}_{index}_EXCLUDED_{excluded_index}"); params.push((excluded_param.clone(), excluded_subpath.into_path_buf())); + // Exclude both the exact protected path and anything beneath it. + // `subpath` alone leaves a gap for first-time creation of the + // protected directory itself, such as `mkdir .codex`. + require_parts.push(format!( + "(require-not (literal (param \"{excluded_param}\")))" + )); require_parts.push(format!( "(require-not (subpath (param \"{excluded_param}\")))" )); diff --git a/codex-rs/sandboxing/src/seatbelt_tests.rs b/codex-rs/sandboxing/src/seatbelt_tests.rs index 925285763e..e508a028c2 100644 --- a/codex-rs/sandboxing/src/seatbelt_tests.rs +++ b/codex-rs/sandboxing/src/seatbelt_tests.rs @@ -129,22 +129,40 @@ fn explicit_unreadable_paths_are_excluded_from_full_disk_read_and_write_access() let unreadable_roots = file_system_policy.get_unreadable_roots_with_cwd(Path::new("/")); let unreadable_root = unreadable_roots.first().expect("expected unreadable root"); assert!( - policy.contains("(require-not (subpath (param \"READABLE_ROOT_0_RO_0\")))"), + policy.contains("(require-not (literal (param \"READABLE_ROOT_0_EXCLUDED_0\")))"), + "expected exact read carveout in policy:\n{policy}" + ); + assert!( + policy.contains("(require-not (subpath (param \"READABLE_ROOT_0_EXCLUDED_0\")))"), "expected read carveout in policy:\n{policy}" ); assert!( - policy.contains("(require-not (subpath (param \"WRITABLE_ROOT_0_RO_0\")))"), + policy.contains("(require-not (literal (param \"WRITABLE_ROOT_0_EXCLUDED_0\")))"), + "expected exact write carveout in policy:\n{policy}" + ); + assert!( + policy.contains("(require-not (subpath (param \"WRITABLE_ROOT_0_EXCLUDED_0\")))"), "expected write carveout in policy:\n{policy}" ); assert!( - args.iter() - .any(|arg| arg == &format!("-DREADABLE_ROOT_0_RO_0={}", unreadable_root.display())), + args.iter().any( + |arg| arg == &format!("-DREADABLE_ROOT_0_EXCLUDED_0={}", unreadable_root.display()) + ), "expected read carveout parameter in args: {args:#?}" ); - assert!( - args.iter() - .any(|arg| arg == &format!("-DWRITABLE_ROOT_0_RO_0={}", unreadable_root.display())), - "expected write carveout parameter in args: {args:#?}" + let writable_definitions: Vec = args + .iter() + .filter(|arg| arg.starts_with("-DWRITABLE_ROOT_")) + .cloned() + .collect(); + assert_eq!( + writable_definitions, + vec![ + "-DWRITABLE_ROOT_0=/".to_string(), + "-DWRITABLE_ROOT_0_EXCLUDED_0=/.codex".to_string(), + format!("-DWRITABLE_ROOT_0_EXCLUDED_1={}", unreadable_root.display()), + ], + "unexpected write carveout parameters in args: {args:#?}" ); } @@ -179,7 +197,11 @@ fn explicit_unreadable_paths_are_excluded_from_readable_roots() { let unreadable_roots = file_system_policy.get_unreadable_roots_with_cwd(Path::new("/")); let unreadable_root = unreadable_roots.first().expect("expected unreadable root"); assert!( - policy.contains("(require-not (subpath (param \"READABLE_ROOT_0_RO_0\")))"), + policy.contains("(require-not (literal (param \"READABLE_ROOT_0_EXCLUDED_0\")))"), + "expected exact read carveout in policy:\n{policy}" + ); + assert!( + policy.contains("(require-not (subpath (param \"READABLE_ROOT_0_EXCLUDED_0\")))"), "expected read carveout in policy:\n{policy}" ); assert!( @@ -188,8 +210,9 @@ fn explicit_unreadable_paths_are_excluded_from_readable_roots() { "expected readable root parameter in args: {args:#?}" ); assert!( - args.iter() - .any(|arg| arg == &format!("-DREADABLE_ROOT_0_RO_0={}", unreadable_root.display())), + args.iter().any( + |arg| arg == &format!("-DREADABLE_ROOT_0_EXCLUDED_0={}", unreadable_root.display()) + ), "expected read carveout parameter in args: {args:#?}" ); } @@ -314,14 +337,17 @@ fn seatbelt_legacy_workspace_write_nested_readable_root_stays_writable() { /*extensions*/ None, ); - let docs_param = format!("-DWRITABLE_ROOT_0_RO_0={}", docs.as_path().display()); assert!( - !seatbelt_policy_arg(&args).contains("WRITABLE_ROOT_0_RO_0"), - "legacy workspace-write readable roots under cwd should not become seatbelt carveouts:\n{args:#?}" + !args + .iter() + .any(|arg| arg.ends_with(&format!("={}", docs.as_path().display()))), + "legacy workspace-write readable roots under cwd should not become seatbelt carveouts:\n{args:#?}", ); assert!( - !args.iter().any(|arg| arg == &docs_param), - "unexpected seatbelt carveout parameter for redundant legacy readable root: {args:#?}" + args.iter() + .any(|arg| arg.starts_with("-DWRITABLE_ROOT_0_EXCLUDED_") + && arg.ends_with("/workspace/.codex")), + "expected proactive .codex carveout for cwd root: {args:#?}", ); } @@ -647,30 +673,24 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { /*extensions*/ None, ); - // Build the expected policy text using a raw string for readability. - // Note that the policy includes: - // - the base policy, - // - read-only access to the filesystem, - // - write access to WRITABLE_ROOT_0 (but not its .git or .codex), WRITABLE_ROOT_1, and cwd as WRITABLE_ROOT_2. - let expected_policy = format!( - r#"{MACOS_SEATBELT_BASE_POLICY} -; allow read-only file operations -(allow file-read*) -(allow file-write* -(subpath (param "WRITABLE_ROOT_0")) (require-all (subpath (param "WRITABLE_ROOT_1")) (require-not (subpath (param "WRITABLE_ROOT_1_RO_0"))) (require-not (subpath (param "WRITABLE_ROOT_1_RO_1"))) ) (subpath (param "WRITABLE_ROOT_2")) -) - -; macOS permission profile extensions -(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) -(allow mach-lookup - (global-name "com.apple.cfprefsd.daemon") - (global-name "com.apple.cfprefsd.agent") - (local-name "com.apple.cfprefsd.agent")) -(allow user-preference-read) -"#, + let policy_text = seatbelt_policy_arg(&args); + assert!( + policy_text.contains("(require-all (subpath (param \"WRITABLE_ROOT_0\"))"), + "expected cwd writable root to carry protected carveouts:\n{policy_text}", + ); + assert!( + policy_text.contains("WRITABLE_ROOT_0_EXCLUDED_0"), + "expected cwd .codex carveout in policy:\n{policy_text}", + ); + assert!( + policy_text.contains("WRITABLE_ROOT_1_EXCLUDED_0") + && policy_text.contains("WRITABLE_ROOT_1_EXCLUDED_1"), + "expected explicit writable root .git/.codex carveouts in policy:\n{policy_text}", + ); + assert!( + policy_text.contains("(subpath (param \"WRITABLE_ROOT_2\"))"), + "expected second explicit writable root grant in policy:\n{policy_text}", ); - - assert_eq!(seatbelt_policy_arg(&args), expected_policy); let expected_definitions = [ format!( @@ -679,16 +699,23 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { .expect("canonicalize cwd") .to_string_lossy() ), + format!( + "-DWRITABLE_ROOT_0_EXCLUDED_0={}", + cwd.canonicalize() + .expect("canonicalize cwd") + .join(".codex") + .display() + ), format!( "-DWRITABLE_ROOT_1={}", vulnerable_root_canonical.to_string_lossy() ), format!( - "-DWRITABLE_ROOT_1_RO_0={}", + "-DWRITABLE_ROOT_1_EXCLUDED_0={}", dot_git_canonical.to_string_lossy() ), format!( - "-DWRITABLE_ROOT_1_RO_1={}", + "-DWRITABLE_ROOT_1_EXCLUDED_1={}", dot_codex_canonical.to_string_lossy() ), format!( @@ -696,12 +723,15 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { empty_root_canonical.to_string_lossy() ), ]; - for expected_definition in expected_definitions { - assert!( - args.contains(&expected_definition), - "expected definition arg `{expected_definition}` in {args:#?}" - ); - } + let writable_definitions: Vec = args + .iter() + .filter(|arg| arg.starts_with("-DWRITABLE_ROOT_")) + .cloned() + .collect(); + assert_eq!( + writable_definitions, expected_definitions, + "unexpected writable-root parameter definitions in {args:#?}" + ); for (key, value) in macos_dir_params() { let expected_definition = format!("-D{key}={}", value.to_string_lossy()); assert!( @@ -818,6 +848,60 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { ); } +#[test] +fn create_seatbelt_args_block_first_time_dot_codex_creation_with_exact_and_descendant_carveouts() { + let tmp = TempDir::new().expect("tempdir"); + let repo_root = tmp.path().join("repo"); + fs::create_dir_all(&repo_root).expect("create repo root"); + + Command::new("git") + .arg("init") + .arg(".") + .current_dir(&repo_root) + .output() + .expect("git init ."); + + let dot_codex = repo_root.join(".codex"); + let config_toml = dot_codex.join("config.toml"); + let policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![repo_root.as_path().try_into().expect("absolute repo root")], + read_only_access: Default::default(), + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + + let shell_command: Vec = [ + "bash", + "-c", + "mkdir -p \"$1\" && echo 'sandbox_mode = \"danger-full-access\"' > \"$2\"", + "bash", + dot_codex.to_string_lossy().as_ref(), + config_toml.to_string_lossy().as_ref(), + ] + .iter() + .map(std::string::ToString::to_string) + .collect(); + let args = create_seatbelt_command_args_with_extensions( + shell_command, + &policy, + repo_root.as_path(), + /*enforce_managed_network*/ false, + /*network*/ None, + /*extensions*/ None, + ); + + let policy_text = seatbelt_policy_arg(&args); + assert!( + policy_text.contains("(require-not (literal (param \"WRITABLE_ROOT_0_EXCLUDED_1\")))"), + "expected exact .codex carveout in policy:\n{policy_text}" + ); + assert!( + policy_text.contains("(require-not (subpath (param \"WRITABLE_ROOT_0_EXCLUDED_1\")))"), + "expected descendant .codex carveout in policy:\n{policy_text}" + ); +} + #[test] fn create_seatbelt_args_with_read_only_git_pointer_file() { let tmp = TempDir::new().expect("tempdir"); @@ -971,7 +1055,7 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { .map(|p| p.to_string_lossy().to_string()); let tempdir_policy_entry = if tmpdir_env_var.is_some() { - r#" (subpath (param "WRITABLE_ROOT_2"))"# + r#" (require-all (subpath (param "WRITABLE_ROOT_2")) (require-not (literal (param "WRITABLE_ROOT_2_EXCLUDED_0"))) (require-not (subpath (param "WRITABLE_ROOT_2_EXCLUDED_0"))) (require-not (literal (param "WRITABLE_ROOT_2_EXCLUDED_1"))) (require-not (subpath (param "WRITABLE_ROOT_2_EXCLUDED_1"))) )"# } else { "" }; @@ -986,7 +1070,7 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { ; allow read-only file operations (allow file-read*) (allow file-write* -(require-all (subpath (param "WRITABLE_ROOT_0")) (require-not (subpath (param "WRITABLE_ROOT_0_RO_0"))) (require-not (subpath (param "WRITABLE_ROOT_0_RO_1"))) ) (subpath (param "WRITABLE_ROOT_1")){tempdir_policy_entry} +(require-all (subpath (param "WRITABLE_ROOT_0")) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_1"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_1"))) ) (subpath (param "WRITABLE_ROOT_1")){tempdir_policy_entry} ) ; macOS permission profile extensions @@ -1007,11 +1091,11 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { vulnerable_root_canonical.to_string_lossy() ), format!( - "-DWRITABLE_ROOT_0_RO_0={}", + "-DWRITABLE_ROOT_0_EXCLUDED_0={}", dot_git_canonical.to_string_lossy() ), format!( - "-DWRITABLE_ROOT_0_RO_1={}", + "-DWRITABLE_ROOT_0_EXCLUDED_1={}", dot_codex_canonical.to_string_lossy() ), format!( @@ -1025,6 +1109,14 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { if let Some(p) = tmpdir_env_var { expected_args.push(format!("-DWRITABLE_ROOT_2={p}")); + expected_args.push(format!( + "-DWRITABLE_ROOT_2_EXCLUDED_0={}", + dot_git_canonical.to_string_lossy() + )); + expected_args.push(format!( + "-DWRITABLE_ROOT_2_EXCLUDED_1={}", + dot_codex_canonical.to_string_lossy() + )); } expected_args.extend( From b23789b770039bca614ac109f3600919c381fbcb Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 13:34:02 -0700 Subject: [PATCH 014/191] [codex] import token_data from codex-login directly (#15903) ## Why `token_data` is owned by `codex-login`, but `codex-core` was still re-exporting it. That let callers pull auth token types through `codex-core`, which keeps otherwise unrelated crates coupled to `codex-core` and makes `codex-core` more of a build-graph bottleneck. ## What changed - remove the `codex-core` re-export of `codex_login::token_data` - update the remaining `codex-core` internals that used `crate::token_data` to import `codex_login::token_data` directly - update downstream callers in `codex-rs/chatgpt`, `codex-rs/tui_app_server`, `codex-rs/app-server/tests/common`, and `codex-rs/core/tests` to import `codex_login::token_data` directly - add explicit `codex-login` workspace dependencies and refresh lock metadata for crates that now depend on it directly ## Validation - `cargo test -p codex-chatgpt --locked` - `just argument-comment-lint` - `just bazel-lock-update` - `just bazel-lock-check` ## Notes - attempted `cargo test -p codex-core --locked` and `cargo test -p codex-core auth_refresh --locked`, but both ran out of disk while linking `codex-core` test binaries in the local environment --- codex-rs/Cargo.lock | 2 ++ codex-rs/app-server/tests/common/Cargo.toml | 1 + codex-rs/app-server/tests/common/auth_fixtures.rs | 4 ++-- codex-rs/chatgpt/Cargo.toml | 1 + codex-rs/chatgpt/src/chatgpt_token.rs | 2 +- codex-rs/chatgpt/src/connectors.rs | 2 +- codex-rs/core/src/api_bridge.rs | 2 +- codex-rs/core/src/connectors.rs | 2 +- codex-rs/core/src/error.rs | 4 ++-- codex-rs/core/src/lib.rs | 1 - codex-rs/core/tests/suite/auth_refresh.rs | 4 ++-- codex-rs/tui_app_server/src/local_chatgpt_auth.rs | 4 ++-- 12 files changed, 16 insertions(+), 13 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 9ae4e7c463..b084aec05f 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -410,6 +410,7 @@ dependencies = [ "codex-app-server-protocol", "codex-core", "codex-features", + "codex-login", "codex-protocol", "codex-utils-cargo-bin", "core_test_support", @@ -1594,6 +1595,7 @@ dependencies = [ "codex-connectors", "codex-core", "codex-git-utils", + "codex-login", "codex-utils-cargo-bin", "codex-utils-cli", "pretty_assertions", diff --git a/codex-rs/app-server/tests/common/Cargo.toml b/codex-rs/app-server/tests/common/Cargo.toml index 851ba9556d..4eef03e969 100644 --- a/codex-rs/app-server/tests/common/Cargo.toml +++ b/codex-rs/app-server/tests/common/Cargo.toml @@ -14,6 +14,7 @@ chrono = { workspace = true } codex-app-server-protocol = { workspace = true } codex-core = { workspace = true } codex-features = { workspace = true } +codex-login = { workspace = true } codex-protocol = { workspace = true } codex-utils-cargo-bin = { workspace = true } serde = { workspace = true } diff --git a/codex-rs/app-server/tests/common/auth_fixtures.rs b/codex-rs/app-server/tests/common/auth_fixtures.rs index c52109ac5b..d814dece46 100644 --- a/codex-rs/app-server/tests/common/auth_fixtures.rs +++ b/codex-rs/app-server/tests/common/auth_fixtures.rs @@ -10,8 +10,8 @@ use codex_app_server_protocol::AuthMode; use codex_core::auth::AuthCredentialsStoreMode; use codex_core::auth::AuthDotJson; use codex_core::auth::save_auth; -use codex_core::token_data::TokenData; -use codex_core::token_data::parse_chatgpt_jwt_claims; +use codex_login::token_data::TokenData; +use codex_login::token_data::parse_chatgpt_jwt_claims; use serde_json::json; /// Builder for writing a fake ChatGPT auth.json in tests. diff --git a/codex-rs/chatgpt/Cargo.toml b/codex-rs/chatgpt/Cargo.toml index cd14a67009..84c793b536 100644 --- a/codex-rs/chatgpt/Cargo.toml +++ b/codex-rs/chatgpt/Cargo.toml @@ -12,6 +12,7 @@ anyhow = { workspace = true } clap = { workspace = true, features = ["derive"] } codex-connectors = { workspace = true } codex-core = { workspace = true } +codex-login = { workspace = true } codex-utils-cli = { workspace = true } codex-utils-cargo-bin = { workspace = true } serde = { workspace = true, features = ["derive"] } diff --git a/codex-rs/chatgpt/src/chatgpt_token.rs b/codex-rs/chatgpt/src/chatgpt_token.rs index 93aaef7ed2..ceca132e9b 100644 --- a/codex-rs/chatgpt/src/chatgpt_token.rs +++ b/codex-rs/chatgpt/src/chatgpt_token.rs @@ -1,10 +1,10 @@ use codex_core::AuthManager; +use codex_login::token_data::TokenData; use std::path::Path; use std::sync::LazyLock; use std::sync::RwLock; use codex_core::auth::AuthCredentialsStoreMode; -use codex_core::token_data::TokenData; static CHATGPT_TOKEN: LazyLock>> = LazyLock::new(|| RwLock::new(None)); diff --git a/codex-rs/chatgpt/src/connectors.rs b/codex-rs/chatgpt/src/connectors.rs index 4dcf8886c7..3c2b2fe4ef 100644 --- a/codex-rs/chatgpt/src/connectors.rs +++ b/codex-rs/chatgpt/src/connectors.rs @@ -1,6 +1,6 @@ use codex_core::AuthManager; use codex_core::config::Config; -use codex_core::token_data::TokenData; +use codex_login::token_data::TokenData; use std::collections::HashSet; use std::time::Duration; diff --git a/codex-rs/core/src/api_bridge.rs b/codex-rs/core/src/api_bridge.rs index 2060b78cf7..e7826f9ac6 100644 --- a/codex-rs/core/src/api_bridge.rs +++ b/codex-rs/core/src/api_bridge.rs @@ -6,6 +6,7 @@ use codex_api::TransportError; use codex_api::error::ApiError; use codex_api::rate_limits::parse_promo_message; use codex_api::rate_limits::parse_rate_limit_for_limit; +use codex_login::token_data::PlanType; use http::HeaderMap; use serde::Deserialize; use serde_json::Value; @@ -16,7 +17,6 @@ use crate::error::RetryLimitReachedError; use crate::error::UnexpectedResponseError; use crate::error::UsageLimitReachedError; use crate::model_provider_info::ModelProviderInfo; -use crate::token_data::PlanType; pub(crate) fn map_api_error(err: ApiError) -> CodexErr { match err { diff --git a/codex-rs/core/src/connectors.rs b/codex-rs/core/src/connectors.rs index fdb0f43222..e4999b9b41 100644 --- a/codex-rs/core/src/connectors.rs +++ b/codex-rs/core/src/connectors.rs @@ -16,6 +16,7 @@ pub use codex_app_server_protocol::AppInfo; pub use codex_app_server_protocol::AppMetadata; use codex_connectors::AllConnectorsCacheKey; use codex_connectors::DirectoryListResponse; +use codex_login::token_data::TokenData; use codex_protocol::protocol::SandboxPolicy; use rmcp::model::ToolAnnotations; use serde::Deserialize; @@ -43,7 +44,6 @@ use crate::mcp_connection_manager::codex_apps_tools_cache_key; use crate::plugins::AppConnectorId; use crate::plugins::PluginsManager; use crate::plugins::list_tool_suggest_discoverable_plugins; -use crate::token_data::TokenData; use crate::tools::discoverable::DiscoverablePluginInfo; use crate::tools::discoverable::DiscoverableTool; use codex_features::Feature; diff --git a/codex-rs/core/src/error.rs b/codex-rs/core/src/error.rs index b0c55a55c6..1fef42e0ad 100644 --- a/codex-rs/core/src/error.rs +++ b/codex-rs/core/src/error.rs @@ -1,7 +1,5 @@ use crate::exec::ExecToolCallOutput; use crate::network_policy_decision::NetworkPolicyDecisionPayload; -use crate::token_data::KnownPlan; -use crate::token_data::PlanType; use chrono::DateTime; use chrono::Datelike; use chrono::Local; @@ -9,6 +7,8 @@ use chrono::Utc; use codex_async_utils::CancelErr; pub use codex_login::auth::RefreshTokenFailedError; pub use codex_login::auth::RefreshTokenFailedReason; +use codex_login::token_data::KnownPlan; +use codex_login::token_data::PlanType; use codex_protocol::ThreadId; use codex_protocol::protocol::CodexErrorInfo; use codex_protocol::protocol::ErrorEvent; diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 1ed3250f3d..ea2651f92e 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -102,7 +102,6 @@ mod skills_watcher; mod stream_events_utils; pub mod test_support; mod text_encoding; -pub use codex_login::token_data; mod unified_exec; pub mod windows_sandbox; pub use client::X_RESPONSESAPI_INCLUDE_TIMING_METRICS_HEADER; diff --git a/codex-rs/core/tests/suite/auth_refresh.rs b/codex-rs/core/tests/suite/auth_refresh.rs index 3390a93197..c5302bbace 100644 --- a/codex-rs/core/tests/suite/auth_refresh.rs +++ b/codex-rs/core/tests/suite/auth_refresh.rs @@ -12,8 +12,8 @@ use codex_core::auth::RefreshTokenError; use codex_core::auth::load_auth_dot_json; use codex_core::auth::save_auth; use codex_core::error::RefreshTokenFailedReason; -use codex_core::token_data::IdTokenInfo; -use codex_core::token_data::TokenData; +use codex_login::token_data::IdTokenInfo; +use codex_login::token_data::TokenData; use core_test_support::skip_if_no_network; use pretty_assertions::assert_eq; use serde::Serialize; diff --git a/codex-rs/tui_app_server/src/local_chatgpt_auth.rs b/codex-rs/tui_app_server/src/local_chatgpt_auth.rs index 33d6e508a9..cc4af28ea7 100644 --- a/codex-rs/tui_app_server/src/local_chatgpt_auth.rs +++ b/codex-rs/tui_app_server/src/local_chatgpt_auth.rs @@ -60,8 +60,8 @@ mod tests { use codex_app_server_protocol::AuthMode; use codex_core::auth::AuthDotJson; use codex_core::auth::save_auth; - use codex_core::token_data::TokenData; use codex_login::auth::login_with_chatgpt_auth_tokens; + use codex_login::token_data::TokenData; use pretty_assertions::assert_eq; use serde::Serialize; use serde_json::json; @@ -99,7 +99,7 @@ mod tests { auth_mode: Some(AuthMode::Chatgpt), openai_api_key: None, tokens: Some(TokenData { - id_token: codex_core::token_data::parse_chatgpt_jwt_claims(&id_token) + id_token: codex_login::token_data::parse_chatgpt_jwt_claims(&id_token) .expect("id token should parse"), access_token, refresh_token: "refresh-token".to_string(), From dfb36573cd0c6aed5a61d6e11ed6d34a34e160ff Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 13:38:33 -0700 Subject: [PATCH 015/191] sandboxing: use OsString for SandboxCommand.program (#15897) ## Why `SandboxCommand.program` represents an executable path, but keeping it as `String` forced path-backed callers to run `to_string_lossy()` before the sandbox layer ever touched the command. That loses fidelity earlier than necessary and adds avoidable conversions in runtimes that already have a `PathBuf`. ## What changed - Changed `SandboxCommand.program` to `OsString`. - Updated `SandboxManager::transform` to keep the program and argv in `OsString` form until the `SandboxExecRequest` conversion boundary. - Switched the path-backed `apply_patch` and `js_repl` runtimes to pass `into_os_string()` instead of `to_string_lossy()`. - Updated the remaining string-backed builders and tests to match the new type while preserving the existing Linux helper `arg0` behavior. ## Verification - `cargo test -p codex-sandboxing` - `just argument-comment-lint -p codex-core -p codex-sandboxing` - `cargo test -p codex-core` currently fails in unrelated existing config tests: `config::tests::approvals_reviewer_*` and `config::tests::smart_approvals_alias_*` --- codex-rs/core/src/exec.rs | 2 +- codex-rs/core/src/tools/js_repl/mod.rs | 2 +- .../core/src/tools/runtimes/apply_patch.rs | 2 +- .../src/tools/runtimes/apply_patch_tests.rs | 8 ++--- codex-rs/core/src/tools/runtimes/mod.rs | 2 +- .../tools/runtimes/shell/unix_escalation.rs | 2 +- codex-rs/sandboxing/src/manager.rs | 31 +++++++++++++------ codex-rs/sandboxing/src/manager_tests.rs | 8 ++--- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/codex-rs/core/src/exec.rs b/codex-rs/core/src/exec.rs index a4399ddc46..4291b6cca3 100644 --- a/codex-rs/core/src/exec.rs +++ b/codex-rs/core/src/exec.rs @@ -272,7 +272,7 @@ pub fn build_exec_request( let manager = SandboxManager::new(); let command = SandboxCommand { - program: program.clone(), + program: program.clone().into(), args: args.to_vec(), cwd, env, diff --git a/codex-rs/core/src/tools/js_repl/mod.rs b/codex-rs/core/src/tools/js_repl/mod.rs index 266aa2a1ce..a9e3aa0f20 100644 --- a/codex-rs/core/src/tools/js_repl/mod.rs +++ b/codex-rs/core/src/tools/js_repl/mod.rs @@ -1045,7 +1045,7 @@ impl JsReplManager { has_managed_network_requirements, ); let command = SandboxCommand { - program: node_path.to_string_lossy().to_string(), + program: node_path.into_os_string(), args: vec![ "--experimental-vm-modules".to_string(), kernel_path.to_string_lossy().to_string(), diff --git a/codex-rs/core/src/tools/runtimes/apply_patch.rs b/codex-rs/core/src/tools/runtimes/apply_patch.rs index c476d47738..65a3981cca 100644 --- a/codex-rs/core/src/tools/runtimes/apply_patch.rs +++ b/codex-rs/core/src/tools/runtimes/apply_patch.rs @@ -97,7 +97,7 @@ impl ApplyPatchRuntime { fn build_sandbox_command_with_program(req: &ApplyPatchRequest, exe: PathBuf) -> SandboxCommand { SandboxCommand { - program: exe.to_string_lossy().to_string(), + program: exe.into_os_string(), args: vec![ CODEX_CORE_APPLY_PATCH_ARG1.to_string(), req.action.patch.clone(), diff --git a/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs b/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs index e38bf5bb05..589e5216df 100644 --- a/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs +++ b/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs @@ -98,10 +98,7 @@ fn build_sandbox_command_prefers_configured_codex_self_exe_for_apply_patch() { let command = ApplyPatchRuntime::build_sandbox_command(&request, Some(&codex_self_exe)) .expect("build sandbox command"); - assert_eq!( - command.program, - codex_self_exe.to_string_lossy().to_string() - ); + assert_eq!(command.program, codex_self_exe.into_os_string()); } #[cfg(not(target_os = "windows"))] @@ -136,7 +133,6 @@ fn build_sandbox_command_falls_back_to_current_exe_for_apply_patch() { command.program, std::env::current_exe() .expect("current exe") - .to_string_lossy() - .to_string() + .into_os_string() ); } diff --git a/codex-rs/core/src/tools/runtimes/mod.rs b/codex-rs/core/src/tools/runtimes/mod.rs index c1a5ad56f2..53890a89b7 100644 --- a/codex-rs/core/src/tools/runtimes/mod.rs +++ b/codex-rs/core/src/tools/runtimes/mod.rs @@ -28,7 +28,7 @@ pub(crate) fn build_sandbox_command( .split_first() .ok_or_else(|| ToolError::Rejected("command args are empty".to_string()))?; Ok(SandboxCommand { - program: program.clone(), + program: program.clone().into(), args: args.to_vec(), cwd: cwd.to_path_buf(), env: env.clone(), diff --git a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs index baef00d339..b2eaa7a032 100644 --- a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs +++ b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs @@ -864,7 +864,7 @@ impl CoreShellCommandExecutor { self.network.is_some(), ); let command = SandboxCommand { - program: program.clone(), + program: program.clone().into(), args: args.to_vec(), cwd: workdir.to_path_buf(), env, diff --git a/codex-rs/sandboxing/src/manager.rs b/codex-rs/sandboxing/src/manager.rs index 3d286eb64c..3f121ba413 100644 --- a/codex-rs/sandboxing/src/manager.rs +++ b/codex-rs/sandboxing/src/manager.rs @@ -18,6 +18,7 @@ use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_protocol::protocol::SandboxPolicy; use std::collections::HashMap; +use std::ffi::OsString; use std::path::Path; use std::path::PathBuf; @@ -65,7 +66,7 @@ pub fn get_platform_sandbox(windows_sandbox_enabled: bool) -> Option, pub cwd: PathBuf, pub env: HashMap, @@ -209,14 +210,14 @@ impl SandboxManager { effective_network_sandbox_policy(network_policy, additional_permissions.as_ref()); let mut argv = Vec::with_capacity(1 + command.args.len()); argv.push(command.program); - argv.append(&mut command.args); + argv.extend(command.args.into_iter().map(OsString::from)); let (argv, arg0_override) = match sandbox { - SandboxType::None => (argv, None), + SandboxType::None => (os_argv_to_strings(argv), None), #[cfg(target_os = "macos")] SandboxType::MacosSeatbelt => { let mut args = create_seatbelt_command_args_for_policies_with_extensions( - argv.clone(), + os_argv_to_strings(argv), &effective_file_system_policy, effective_network_policy, sandbox_policy_cwd, @@ -236,7 +237,7 @@ impl SandboxManager { .ok_or(SandboxTransformError::MissingLinuxSandboxExecutable)?; let allow_proxy_network = allow_network_for_proxy(enforce_managed_network); let mut args = create_linux_sandbox_command_args_for_policies( - argv.clone(), + os_argv_to_strings(argv), command.cwd.as_path(), &effective_policy, &effective_file_system_policy, @@ -246,7 +247,7 @@ impl SandboxManager { allow_proxy_network, ); let mut full_command = Vec::with_capacity(1 + args.len()); - full_command.push(exe.to_string_lossy().to_string()); + full_command.push(os_string_to_command_component(exe.as_os_str().to_owned())); full_command.append(&mut args); ( full_command, @@ -254,9 +255,9 @@ impl SandboxManager { ) } #[cfg(target_os = "windows")] - SandboxType::WindowsRestrictedToken => (argv, None), + SandboxType::WindowsRestrictedToken => (os_argv_to_strings(argv), None), #[cfg(not(target_os = "windows"))] - SandboxType::WindowsRestrictedToken => (argv, None), + SandboxType::WindowsRestrictedToken => (os_argv_to_strings(argv), None), }; Ok(SandboxExecRequest { @@ -275,9 +276,21 @@ impl SandboxManager { } } +fn os_argv_to_strings(argv: Vec) -> Vec { + argv.into_iter() + .map(os_string_to_command_component) + .collect() +} + +fn os_string_to_command_component(value: OsString) -> String { + value + .into_string() + .unwrap_or_else(|value| value.to_string_lossy().into_owned()) +} + fn linux_sandbox_arg0_override(exe: &Path) -> String { if exe.file_name().and_then(|name| name.to_str()) == Some(CODEX_LINUX_SANDBOX_ARG0) { - exe.to_string_lossy().into_owned() + os_string_to_command_component(exe.as_os_str().to_owned()) } else { CODEX_LINUX_SANDBOX_ARG0.to_string() } diff --git a/codex-rs/sandboxing/src/manager_tests.rs b/codex-rs/sandboxing/src/manager_tests.rs index 690aec89d5..67911bff6a 100644 --- a/codex-rs/sandboxing/src/manager_tests.rs +++ b/codex-rs/sandboxing/src/manager_tests.rs @@ -76,7 +76,7 @@ fn transform_preserves_unrestricted_file_system_policy_for_restricted_network() let exec_request = manager .transform(SandboxTransformRequest { command: SandboxCommand { - program: "true".to_string(), + program: "true".into(), args: Vec::new(), cwd: cwd.clone(), env: HashMap::new(), @@ -122,7 +122,7 @@ fn transform_additional_permissions_enable_network_for_external_sandbox() { let exec_request = manager .transform(SandboxTransformRequest { command: SandboxCommand { - program: "true".to_string(), + program: "true".into(), args: Vec::new(), cwd: cwd.clone(), env: HashMap::new(), @@ -181,7 +181,7 @@ fn transform_additional_permissions_preserves_denied_entries() { let exec_request = manager .transform(SandboxTransformRequest { command: SandboxCommand { - program: "true".to_string(), + program: "true".into(), args: Vec::new(), cwd: cwd.clone(), env: HashMap::new(), @@ -259,7 +259,7 @@ fn transform_linux_seccomp_request( manager .transform(SandboxTransformRequest { command: SandboxCommand { - program: "true".to_string(), + program: "true".into(), args: Vec::new(), cwd: cwd.clone(), env: HashMap::new(), From 609019c6e5a055914dc7d51b111d93db4bd342e1 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 14:56:43 -0700 Subject: [PATCH 016/191] docs: update AGENTS.md to discourage adding code to codex-core (#15910) ## Why `codex-core` is already the largest crate in `codex-rs`, so defaulting to it for new functionality makes it harder to keep the workspace modular. The repo guidance should make it explicit that contributors are expected to look for an existing non-`codex-core` crate, or introduce a new crate, before growing `codex-core` further. ## What Changed - Added a dedicated `The \`codex-core\` crate` section to `AGENTS.md`. - Documented why `codex-core` should be treated as a last resort for new functionality. - Added concrete guidance for both implementation and review: prefer an existing non-`codex-core` crate when possible, introduce a new workspace crate when that is the cleaner boundary, and push back on PRs that grow `codex-core` unnecessarily. --- AGENTS.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index b1dbd53f39..7a81eab40c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,6 +51,19 @@ Before finalizing a large change to `codex-rs`, run `just fix -p ` (in Also run `just argument-comment-lint` to ensure the codebase is clean of comment lint errors. +## The `codex-core` crate + +Over time, the `codex-core` crate (defined in `codex-rs/core/`) has become bloated because it is the largest crate, so it is often easier to add something new to `codex-core` rather than refactor out the library code you need so your new code neither takes a dependency on, nor contributes to the size of, `codex-core`. + +To that end: **resist adding code to codex-core**! + +Particularly when introducing a new concept/feature/API, before adding to `codex-core`, consider whether: + +- There is an existing crate other than `codex-core` that is an appropriate place for your new code to live. +- It is time to introduce a new crate to the Cargo workspace for your new functionality. Refactor existing code as necessary to make this happen. + +Likewise, when reviewing code, do not hesitate to push back on PRs that would unnecessarily add code to `codex-core`. + ## TUI style conventions See `codex-rs/tui/styles.md`. From b52abff279ffe57de90cabc536e30737002b7a73 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Thu, 26 Mar 2026 15:15:59 -0700 Subject: [PATCH 017/191] chore: move bwrap config helpers into dedicated module (#15898) ## Summary - move the bwrap PATH lookup and warning helpers out of config/mod.rs - move the related tests into a dedicated bwrap_tests.rs file ## Validation - git diff --check - skipped heavier local tests per request Follow-up to #15791. --- codex-rs/Cargo.lock | 1 + codex-rs/core/src/config/config_tests.rs | 112 ----------------------- codex-rs/core/src/config/mod.rs | 52 +---------- codex-rs/linux-sandbox/src/launcher.rs | 3 +- codex-rs/sandboxing/Cargo.toml | 1 + codex-rs/sandboxing/src/bwrap.rs | 46 ++++++++++ codex-rs/sandboxing/src/bwrap_tests.rs | 104 +++++++++++++++++++++ codex-rs/sandboxing/src/lib.rs | 11 +++ 8 files changed, 167 insertions(+), 163 deletions(-) create mode 100644 codex-rs/sandboxing/src/bwrap.rs create mode 100644 codex-rs/sandboxing/src/bwrap_tests.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index b084aec05f..9487d74a10 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2511,6 +2511,7 @@ dependencies = [ "tempfile", "tracing", "url", + "which 8.0.0", ] [[package]] diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 421bfa6bd6..a62919a444 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -5692,118 +5692,6 @@ shell_tool = true Ok(()) } -#[cfg(target_os = "linux")] -#[test] -fn system_bwrap_warning_reports_missing_system_bwrap() { - let warning = - system_bwrap_warning_for_lookup(None).expect("missing system bwrap should emit a warning"); - - assert!(warning.contains("could not find system bubblewrap")); -} - -#[cfg(target_os = "linux")] -#[test] -fn system_bwrap_warning_skips_too_old_system_bwrap() { - let fake_bwrap = write_fake_bwrap( - r#"#!/bin/sh -if [ "$1" = "--help" ]; then - echo 'usage: bwrap [OPTION...] COMMAND' - exit 0 -fi -exit 1 -"#, - ); - let fake_bwrap_path: &Path = fake_bwrap.as_ref(); - - assert_eq!( - system_bwrap_warning_for_lookup(Some(fake_bwrap_path.to_path_buf())), - None, - "Do not warn even if bwrap does not support `--argv0`", - ); -} - -#[cfg(target_os = "linux")] -#[test] -fn finds_first_executable_bwrap_in_search_paths() { - let temp_dir = tempdir().expect("temp dir"); - let cwd = temp_dir.path().join("cwd"); - let first_dir = temp_dir.path().join("first"); - let second_dir = temp_dir.path().join("second"); - std::fs::create_dir_all(&cwd).expect("create cwd"); - std::fs::create_dir_all(&first_dir).expect("create first dir"); - std::fs::create_dir_all(&second_dir).expect("create second dir"); - std::fs::write(first_dir.join("bwrap"), "not executable").expect("write non-executable bwrap"); - let expected_bwrap = write_named_fake_bwrap_in(&second_dir); - - assert_eq!( - find_system_bwrap_in_search_paths(vec![first_dir, second_dir], &cwd), - Some(expected_bwrap) - ); -} - -#[cfg(target_os = "linux")] -#[test] -fn skips_workspace_local_bwrap_in_search_paths() { - let temp_dir = tempdir().expect("temp dir"); - let cwd = temp_dir.path().join("cwd"); - let trusted_dir = temp_dir.path().join("trusted"); - std::fs::create_dir_all(&cwd).expect("create cwd"); - std::fs::create_dir_all(&trusted_dir).expect("create trusted dir"); - let _workspace_bwrap = write_named_fake_bwrap_in(&cwd); - let expected_bwrap = write_named_fake_bwrap_in(&trusted_dir); - - assert_eq!( - find_system_bwrap_in_search_paths(vec![cwd.clone(), trusted_dir], &cwd), - Some(expected_bwrap) - ); -} - -#[cfg(not(target_os = "linux"))] -#[test] -fn system_bwrap_warning_is_disabled_off_linux() { - assert!(system_bwrap_warning().is_none()); -} - -#[cfg(target_os = "linux")] -fn write_fake_bwrap(contents: &str) -> tempfile::TempPath { - write_fake_bwrap_in( - &std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), - contents, - ) -} - -#[cfg(target_os = "linux")] -fn write_fake_bwrap_in(dir: &Path, contents: &str) -> tempfile::TempPath { - use std::fs; - use std::os::unix::fs::PermissionsExt; - use tempfile::NamedTempFile; - - // Bazel can mount the OS temp directory `noexec`, so prefer the current - // working directory for fake executables and fall back to the default temp - // dir outside that environment. - let temp_file = NamedTempFile::new_in(dir) - .ok() - .unwrap_or_else(|| NamedTempFile::new().expect("temp file")); - // Linux rejects exec-ing a file that is still open for writing. - let path = temp_file.into_temp_path(); - fs::write(&path, contents).expect("write fake bwrap"); - let permissions = fs::Permissions::from_mode(0o755); - fs::set_permissions(&path, permissions).expect("chmod fake bwrap"); - path -} - -#[cfg(target_os = "linux")] -fn write_named_fake_bwrap_in(dir: &Path) -> PathBuf { - use std::fs; - use std::os::unix::fs::PermissionsExt; - - let path = dir.join("bwrap"); - fs::write(&path, "#!/bin/sh\n").expect("write fake bwrap"); - let permissions = fs::Permissions::from_mode(0o755); - fs::set_permissions(&path, permissions).expect("chmod fake bwrap"); - path -} - #[tokio::test] async fn approvals_reviewer_defaults_to_manual_only_without_guardian_feature() -> std::io::Result<()> { diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 6db6f0e613..8435e61325 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -118,7 +118,7 @@ pub use codex_config::Constrained; pub use codex_config::ConstraintError; pub use codex_config::ConstraintResult; pub use codex_network_proxy::NetworkProxyAuditMetadata; - +pub use codex_sandboxing::system_bwrap_warning; pub use managed_features::ManagedFeatures; pub use network_proxy_spec::NetworkProxySpec; pub use network_proxy_spec::StartedNetworkProxy; @@ -144,61 +144,12 @@ pub(crate) const DEFAULT_AGENT_JOB_MAX_RUNTIME_SECONDS: Option = None; pub const CONFIG_TOML_FILE: &str = "config.toml"; const OPENAI_BASE_URL_ENV_VAR: &str = "OPENAI_BASE_URL"; -#[cfg(target_os = "linux")] -const SYSTEM_BWRAP_PROGRAM: &str = "bwrap"; const RESERVED_MODEL_PROVIDER_IDS: [&str; 3] = [ OPENAI_PROVIDER_ID, OLLAMA_OSS_PROVIDER_ID, LMSTUDIO_OSS_PROVIDER_ID, ]; -#[cfg(target_os = "linux")] -pub fn system_bwrap_warning() -> Option { - system_bwrap_warning_for_lookup(find_system_bwrap_in_path()) -} - -#[cfg(not(target_os = "linux"))] -pub fn system_bwrap_warning() -> Option { - None -} - -#[cfg(target_os = "linux")] -fn system_bwrap_warning_for_lookup(system_bwrap_path: Option) -> Option { - match system_bwrap_path { - Some(_) => None, - None => Some( - "Codex could not find system bubblewrap on PATH. Please install bubblewrap with your package manager. Codex will use the vendored bubblewrap in the meantime." - .to_string(), - ), - } -} - -#[cfg(target_os = "linux")] -pub fn find_system_bwrap_in_path() -> Option { - let search_path = std::env::var_os("PATH")?; - let cwd = std::env::current_dir().ok()?; - find_system_bwrap_in_search_paths(std::iter::once(PathBuf::from(search_path)), &cwd) -} - -#[cfg(target_os = "linux")] -fn find_system_bwrap_in_search_paths( - search_paths: impl IntoIterator, - cwd: &Path, -) -> Option { - let search_path = std::env::join_paths(search_paths).ok()?; - let cwd = std::fs::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf()); - which::which_in_all(SYSTEM_BWRAP_PROGRAM, Some(search_path), &cwd) - .ok()? - .find_map(|path| { - let path = std::fs::canonicalize(path).ok()?; - if path.starts_with(&cwd) { - None - } else { - Some(path) - } - }) -} - fn resolve_sqlite_home_env(resolved_cwd: &Path) -> Option { let raw = std::env::var(codex_state::SQLITE_HOME_ENV).ok()?; let trimmed = raw.trim(); @@ -212,6 +163,7 @@ fn resolve_sqlite_home_env(resolved_cwd: &Path) -> Option { Some(resolved_cwd.join(path)) } } + #[cfg(test)] pub(crate) fn test_config() -> Config { let codex_home = tempfile::tempdir().expect("create temp dir"); diff --git a/codex-rs/linux-sandbox/src/launcher.rs b/codex-rs/linux-sandbox/src/launcher.rs index c36f1c5ff1..cfaa70f938 100644 --- a/codex-rs/linux-sandbox/src/launcher.rs +++ b/codex-rs/linux-sandbox/src/launcher.rs @@ -8,6 +8,7 @@ use std::process::Command; use std::sync::OnceLock; use crate::vendored_bwrap::exec_vendored_bwrap; +use codex_sandboxing::find_system_bwrap_in_path; use codex_utils_absolute_path::AbsolutePathBuf; #[derive(Debug, Clone, PartialEq, Eq)] @@ -34,7 +35,7 @@ pub(crate) fn exec_bwrap(argv: Vec, preserved_files: Vec) -> ! { fn preferred_bwrap_launcher() -> BubblewrapLauncher { static LAUNCHER: OnceLock = OnceLock::new(); LAUNCHER - .get_or_init(|| match codex_core::config::find_system_bwrap_in_path() { + .get_or_init(|| match find_system_bwrap_in_path() { Some(path) => preferred_bwrap_launcher_for_path(&path), None => BubblewrapLauncher::Vendored, }) diff --git a/codex-rs/sandboxing/Cargo.toml b/codex-rs/sandboxing/Cargo.toml index fc340d6502..cd7558ec90 100644 --- a/codex-rs/sandboxing/Cargo.toml +++ b/codex-rs/sandboxing/Cargo.toml @@ -21,6 +21,7 @@ libc = { workspace = true } serde_json = { workspace = true } tracing = { workspace = true, features = ["log"] } url = { workspace = true } +which = { workspace = true } [dev-dependencies] pretty_assertions = { workspace = true } diff --git a/codex-rs/sandboxing/src/bwrap.rs b/codex-rs/sandboxing/src/bwrap.rs new file mode 100644 index 0000000000..00cf55f7f5 --- /dev/null +++ b/codex-rs/sandboxing/src/bwrap.rs @@ -0,0 +1,46 @@ +use std::path::Path; +use std::path::PathBuf; + +const SYSTEM_BWRAP_PROGRAM: &str = "bwrap"; + +pub fn system_bwrap_warning() -> Option { + system_bwrap_warning_for_lookup(find_system_bwrap_in_path()) +} + +fn system_bwrap_warning_for_lookup(system_bwrap_path: Option) -> Option { + match system_bwrap_path { + Some(_) => None, + None => Some( + "Codex could not find system bubblewrap on PATH. Please install bubblewrap with your package manager. Codex will use the vendored bubblewrap in the meantime." + .to_string(), + ), + } +} + +pub fn find_system_bwrap_in_path() -> Option { + let search_path = std::env::var_os("PATH")?; + let cwd = std::env::current_dir().ok()?; + find_system_bwrap_in_search_paths(std::iter::once(PathBuf::from(search_path)), &cwd) +} + +fn find_system_bwrap_in_search_paths( + search_paths: impl IntoIterator, + cwd: &Path, +) -> Option { + let search_path = std::env::join_paths(search_paths).ok()?; + let cwd = std::fs::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf()); + which::which_in_all(SYSTEM_BWRAP_PROGRAM, Some(search_path), &cwd) + .ok()? + .find_map(|path| { + let path = std::fs::canonicalize(path).ok()?; + if path.starts_with(&cwd) { + None + } else { + Some(path) + } + }) +} + +#[cfg(test)] +#[path = "bwrap_tests.rs"] +mod tests; diff --git a/codex-rs/sandboxing/src/bwrap_tests.rs b/codex-rs/sandboxing/src/bwrap_tests.rs new file mode 100644 index 0000000000..e379de3fb1 --- /dev/null +++ b/codex-rs/sandboxing/src/bwrap_tests.rs @@ -0,0 +1,104 @@ +use super::*; +use pretty_assertions::assert_eq; +use std::path::Path; +use std::path::PathBuf; +use tempfile::tempdir; + +#[test] +fn system_bwrap_warning_reports_missing_system_bwrap() { + let warning = + system_bwrap_warning_for_lookup(None).expect("missing system bwrap should emit a warning"); + + assert!(warning.contains("could not find system bubblewrap")); +} + +#[test] +fn system_bwrap_warning_skips_too_old_system_bwrap() { + let fake_bwrap = write_fake_bwrap( + r#"#!/bin/sh +if [ "$1" = "--help" ]; then + echo 'usage: bwrap [OPTION...] COMMAND' + exit 0 +fi +exit 1 +"#, + ); + let fake_bwrap_path: &Path = fake_bwrap.as_ref(); + + assert_eq!( + system_bwrap_warning_for_lookup(Some(fake_bwrap_path.to_path_buf())), + None, + "Do not warn even if bwrap does not support `--argv0`", + ); +} + +#[test] +fn finds_first_executable_bwrap_in_search_paths() { + let temp_dir = tempdir().expect("temp dir"); + let cwd = temp_dir.path().join("cwd"); + let first_dir = temp_dir.path().join("first"); + let second_dir = temp_dir.path().join("second"); + std::fs::create_dir_all(&cwd).expect("create cwd"); + std::fs::create_dir_all(&first_dir).expect("create first dir"); + std::fs::create_dir_all(&second_dir).expect("create second dir"); + std::fs::write(first_dir.join("bwrap"), "not executable").expect("write non-executable bwrap"); + let expected_bwrap = write_named_fake_bwrap_in(&second_dir); + + assert_eq!( + find_system_bwrap_in_search_paths(vec![first_dir, second_dir], &cwd), + Some(expected_bwrap) + ); +} + +#[test] +fn skips_workspace_local_bwrap_in_search_paths() { + let temp_dir = tempdir().expect("temp dir"); + let cwd = temp_dir.path().join("cwd"); + let trusted_dir = temp_dir.path().join("trusted"); + std::fs::create_dir_all(&cwd).expect("create cwd"); + std::fs::create_dir_all(&trusted_dir).expect("create trusted dir"); + let _workspace_bwrap = write_named_fake_bwrap_in(&cwd); + let expected_bwrap = write_named_fake_bwrap_in(&trusted_dir); + + assert_eq!( + find_system_bwrap_in_search_paths(vec![cwd.clone(), trusted_dir], &cwd), + Some(expected_bwrap) + ); +} + +fn write_fake_bwrap(contents: &str) -> tempfile::TempPath { + write_fake_bwrap_in( + &std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), + contents, + ) +} + +fn write_fake_bwrap_in(dir: &Path, contents: &str) -> tempfile::TempPath { + use std::fs; + use std::os::unix::fs::PermissionsExt; + use tempfile::NamedTempFile; + + // Bazel can mount the OS temp directory `noexec`, so prefer the current + // working directory for fake executables and fall back to the default temp + // dir outside that environment. + let temp_file = NamedTempFile::new_in(dir) + .ok() + .unwrap_or_else(|| NamedTempFile::new().expect("temp file")); + // Linux rejects exec-ing a file that is still open for writing. + let path = temp_file.into_temp_path(); + fs::write(&path, contents).expect("write fake bwrap"); + let permissions = fs::Permissions::from_mode(0o755); + fs::set_permissions(&path, permissions).expect("chmod fake bwrap"); + path +} + +fn write_named_fake_bwrap_in(dir: &Path) -> PathBuf { + use std::fs; + use std::os::unix::fs::PermissionsExt; + + let path = dir.join("bwrap"); + fs::write(&path, "#!/bin/sh\n").expect("write fake bwrap"); + let permissions = fs::Permissions::from_mode(0o755); + fs::set_permissions(&path, permissions).expect("chmod fake bwrap"); + path +} diff --git a/codex-rs/sandboxing/src/lib.rs b/codex-rs/sandboxing/src/lib.rs index 5eca9d480c..eb6b4e33b8 100644 --- a/codex-rs/sandboxing/src/lib.rs +++ b/codex-rs/sandboxing/src/lib.rs @@ -1,3 +1,5 @@ +#[cfg(target_os = "linux")] +mod bwrap; pub mod landlock; pub mod macos_permissions; mod manager; @@ -7,6 +9,10 @@ pub mod seatbelt; #[cfg(target_os = "macos")] mod seatbelt_permissions; +#[cfg(target_os = "linux")] +pub use bwrap::find_system_bwrap_in_path; +#[cfg(target_os = "linux")] +pub use bwrap::system_bwrap_warning; pub use manager::SandboxCommand; pub use manager::SandboxExecRequest; pub use manager::SandboxManager; @@ -15,3 +21,8 @@ pub use manager::SandboxTransformRequest; pub use manager::SandboxType; pub use manager::SandboxablePreference; pub use manager::get_platform_sandbox; + +#[cfg(not(target_os = "linux"))] +pub fn system_bwrap_warning() -> Option { + None +} From 5906c6a658d6b91ccd0963ee6741bdb559b8f78e Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 15:32:03 -0700 Subject: [PATCH 018/191] chore: remove skill metadata from command approval payloads (#15906) ## Why This is effectively a follow-up to [#15812](https://github.com/openai/codex/pull/15812). That change removed the special skill-script exec path, but `skill_metadata` was still being threaded through command-approval payloads even though the approval flow no longer uses it to render prompts or resolve decisions. Keeping it around added extra protocol, schema, and client surface area without changing behavior. Removing it keeps the command-approval contract smaller and avoids carrying a dead field through app-server, TUI, and MCP boundaries. ## What changed - removed `ExecApprovalRequestSkillMetadata` and the corresponding `skillMetadata` field from core approval events and the v2 app-server protocol - removed the generated JSON and TypeScript schema output for that field - updated app-server, MCP server, TUI, and TUI app-server approval plumbing to stop forwarding the field - cleaned up tests that previously constructed or asserted `skillMetadata` ## Testing - `cargo test -p codex-app-server-protocol` - `cargo test -p codex-protocol` - `cargo test -p codex-app-server-test-client` - `cargo test -p codex-mcp-server` - `just argument-comment-lint` --- ...CommandExecutionRequestApprovalParams.json | 11 ---- .../schema/json/ServerRequest.json | 11 ---- .../codex_app_server_protocol.schemas.json | 11 ---- .../CommandExecutionRequestApprovalParams.ts | 5 -- ...ndExecutionRequestApprovalSkillMetadata.ts | 5 -- .../schema/typescript/v2/index.ts | 1 - codex-rs/app-server-protocol/src/export.rs | 10 ---- .../src/protocol/common.rs | 28 ---------- .../app-server-protocol/src/protocol/v2.rs | 54 ------------------- codex-rs/app-server-test-client/src/lib.rs | 4 -- .../app-server/src/bespoke_event_handling.rs | 5 -- codex-rs/app-server/src/transport/mod.rs | 20 ++----- codex-rs/core/src/codex.rs | 3 -- codex-rs/core/src/codex_delegate.rs | 2 - codex-rs/core/src/codex_delegate_tests.rs | 1 - codex-rs/core/src/tools/network_approval.rs | 1 - codex-rs/core/src/tools/runtimes/shell.rs | 1 - .../tools/runtimes/shell/unix_escalation.rs | 1 - .../core/src/tools/runtimes/unified_exec.rs | 1 - codex-rs/mcp-server/src/codex_tool_runner.rs | 1 - codex-rs/protocol/src/approvals.rs | 16 +----- codex-rs/protocol/src/protocol.rs | 1 - codex-rs/tui/src/app.rs | 3 -- .../tui/src/app/pending_interactive_replay.rs | 3 -- codex-rs/tui/src/chatwidget/tests.rs | 8 --- codex-rs/tui_app_server/src/app.rs | 1 - .../src/app/app_server_requests.rs | 1 - .../src/app/pending_interactive_replay.rs | 1 - codex-rs/tui_app_server/src/chatwidget.rs | 5 -- .../tui_app_server/src/chatwidget/tests.rs | 9 ---- 30 files changed, 5 insertions(+), 219 deletions(-) delete mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalSkillMetadata.ts diff --git a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json index 2c146b9522..66dccf15e0 100644 --- a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json @@ -298,17 +298,6 @@ } ] }, - "CommandExecutionRequestApprovalSkillMetadata": { - "properties": { - "pathToSkillsMd": { - "type": "string" - } - }, - "required": [ - "pathToSkillsMd" - ], - "type": "object" - }, "MacOsAutomationPermission": { "oneOf": [ { diff --git a/codex-rs/app-server-protocol/schema/json/ServerRequest.json b/codex-rs/app-server-protocol/schema/json/ServerRequest.json index 1fbbfb1b0a..6c63d36a37 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ServerRequest.json @@ -452,17 +452,6 @@ ], "type": "object" }, - "CommandExecutionRequestApprovalSkillMetadata": { - "properties": { - "pathToSkillsMd": { - "type": "string" - } - }, - "required": [ - "pathToSkillsMd" - ], - "type": "object" - }, "DynamicToolCallParams": { "properties": { "arguments": true, diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 6253c39297..28be3e5b35 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -1864,17 +1864,6 @@ "title": "CommandExecutionRequestApprovalResponse", "type": "object" }, - "CommandExecutionRequestApprovalSkillMetadata": { - "properties": { - "pathToSkillsMd": { - "type": "string" - } - }, - "required": [ - "pathToSkillsMd" - ], - "type": "object" - }, "DynamicToolCallParams": { "$schema": "http://json-schema.org/draft-07/schema#", "properties": { diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalParams.ts index 623fb971c1..8fb6375e69 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalParams.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalParams.ts @@ -4,7 +4,6 @@ import type { AdditionalPermissionProfile } from "./AdditionalPermissionProfile"; import type { CommandAction } from "./CommandAction"; import type { CommandExecutionApprovalDecision } from "./CommandExecutionApprovalDecision"; -import type { CommandExecutionRequestApprovalSkillMetadata } from "./CommandExecutionRequestApprovalSkillMetadata"; import type { ExecPolicyAmendment } from "./ExecPolicyAmendment"; import type { NetworkApprovalContext } from "./NetworkApprovalContext"; import type { NetworkPolicyAmendment } from "./NetworkPolicyAmendment"; @@ -44,10 +43,6 @@ commandActions?: Array | null, * Optional additional permissions requested for this command. */ additionalPermissions?: AdditionalPermissionProfile | null, -/** - * Optional skill metadata when the approval was triggered by a skill script. - */ -skillMetadata?: CommandExecutionRequestApprovalSkillMetadata | null, /** * Optional proposed execpolicy amendment to allow similar commands without prompting. */ diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalSkillMetadata.ts b/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalSkillMetadata.ts deleted file mode 100644 index dcfd7c2916..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/v2/CommandExecutionRequestApprovalSkillMetadata.ts +++ /dev/null @@ -1,5 +0,0 @@ -// GENERATED CODE! DO NOT MODIFY BY HAND! - -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type CommandExecutionRequestApprovalSkillMetadata = { pathToSkillsMd: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index d0b1b88198..b545f7986f 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -54,7 +54,6 @@ export type { CommandExecutionApprovalDecision } from "./CommandExecutionApprova export type { CommandExecutionOutputDeltaNotification } from "./CommandExecutionOutputDeltaNotification"; export type { CommandExecutionRequestApprovalParams } from "./CommandExecutionRequestApprovalParams"; export type { CommandExecutionRequestApprovalResponse } from "./CommandExecutionRequestApprovalResponse"; -export type { CommandExecutionRequestApprovalSkillMetadata } from "./CommandExecutionRequestApprovalSkillMetadata"; export type { CommandExecutionSource } from "./CommandExecutionSource"; export type { CommandExecutionStatus } from "./CommandExecutionStatus"; export type { Config } from "./Config"; diff --git a/codex-rs/app-server-protocol/src/export.rs b/codex-rs/app-server-protocol/src/export.rs index b89f23c666..033f605973 100644 --- a/codex-rs/app-server-protocol/src/export.rs +++ b/codex-rs/app-server-protocol/src/export.rs @@ -2298,10 +2298,6 @@ mod tests { command_execution_request_approval_ts.contains("additionalPermissions"), true ); - assert_eq!( - command_execution_request_approval_ts.contains("skillMetadata"), - true - ); Ok(()) } @@ -2705,10 +2701,6 @@ export type Config = { stableField: Keep, unstableField: string | null } & ({ [k command_execution_request_approval_json.contains("additionalPermissions"), false ); - assert_eq!( - command_execution_request_approval_json.contains("skillMetadata"), - false - ); let client_request_json = fs::read_to_string(output_dir.join("ClientRequest.json"))?; assert_eq!( @@ -2721,7 +2713,6 @@ export type Config = { stableField: Keep, unstableField: string | null } & ({ [k fs::read_to_string(output_dir.join("codex_app_server_protocol.schemas.json"))?; assert_eq!(bundle_json.contains("mockExperimentalField"), false); assert_eq!(bundle_json.contains("additionalPermissions"), false); - assert_eq!(bundle_json.contains("skillMetadata"), false); assert_eq!(bundle_json.contains("MockExperimentalMethodParams"), false); assert_eq!( bundle_json.contains("MockExperimentalMethodResponse"), @@ -2731,7 +2722,6 @@ export type Config = { stableField: Keep, unstableField: string | null } & ({ [k fs::read_to_string(output_dir.join("codex_app_server_protocol.v2.schemas.json"))?; assert_eq!(flat_v2_bundle_json.contains("mockExperimentalField"), false); assert_eq!(flat_v2_bundle_json.contains("additionalPermissions"), false); - assert_eq!(flat_v2_bundle_json.contains("skillMetadata"), false); assert_eq!( flat_v2_bundle_json.contains("MockExperimentalMethodParams"), false diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index 5a9215f2ee..4492a14944 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -1705,7 +1705,6 @@ mod tests { }), macos: None, }), - skill_metadata: None, proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, @@ -1716,31 +1715,4 @@ mod tests { Some("item/commandExecution/requestApproval.additionalPermissions") ); } - - #[test] - fn command_execution_request_approval_skill_metadata_is_marked_experimental() { - let params = v2::CommandExecutionRequestApprovalParams { - thread_id: "thr_123".to_string(), - turn_id: "turn_123".to_string(), - item_id: "call_123".to_string(), - approval_id: None, - reason: None, - network_approval_context: None, - command: Some("cat file".to_string()), - cwd: None, - command_actions: None, - additional_permissions: None, - skill_metadata: Some(v2::CommandExecutionRequestApprovalSkillMetadata { - path_to_skills_md: PathBuf::from("/tmp/SKILLS.md"), - }), - proposed_execpolicy_amendment: None, - proposed_network_policy_amendments: None, - available_decisions: None, - }; - let reason = crate::experimental_api::ExperimentalApi::experimental_reason(¶ms); - assert_eq!( - reason, - Some("item/commandExecution/requestApproval.skillMetadata") - ); - } } diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 147c91db2c..559d825aa5 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -7,7 +7,6 @@ use crate::protocol::common::AuthMode; use codex_experimental_api_macros::ExperimentalApi; use codex_protocol::account::PlanType; use codex_protocol::approvals::ElicitationRequest as CoreElicitationRequest; -use codex_protocol::approvals::ExecApprovalRequestSkillMetadata as CoreExecApprovalRequestSkillMetadata; use codex_protocol::approvals::ExecPolicyAmendment as CoreExecPolicyAmendment; use codex_protocol::approvals::NetworkApprovalContext as CoreNetworkApprovalContext; use codex_protocol::approvals::NetworkApprovalProtocol as CoreNetworkApprovalProtocol; @@ -3519,14 +3518,6 @@ impl From for SkillMetadata { } } -impl From for CommandExecutionRequestApprovalSkillMetadata { - fn from(value: CoreExecApprovalRequestSkillMetadata) -> Self { - Self { - path_to_skills_md: value.path_to_skills_md, - } - } -} - impl From for SkillInterface { fn from(value: CoreSkillInterface) -> Self { Self { @@ -5240,11 +5231,6 @@ pub struct CommandExecutionRequestApprovalParams { #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional = nullable)] pub additional_permissions: Option, - /// Optional skill metadata when the approval was triggered by a skill script. - #[experimental("item/commandExecution/requestApproval.skillMetadata")] - #[serde(default, skip_serializing_if = "Option::is_none")] - #[ts(optional = nullable)] - pub skill_metadata: Option, /// Optional proposed execpolicy amendment to allow similar commands without prompting. #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional = nullable)] @@ -5266,17 +5252,9 @@ impl CommandExecutionRequestApprovalParams { // We need a generic outbound compatibility design for stripping or // otherwise handling experimental server->client payloads. self.additional_permissions = None; - self.skill_metadata = None; } } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export_to = "v2/")] -pub struct CommandExecutionRequestApprovalSkillMetadata { - pub path_to_skills_md: PathBuf, -} - #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] @@ -6100,7 +6078,6 @@ mod tests { }, "macos": null }, - "skillMetadata": null, "proposedExecpolicyAmendment": null, "proposedNetworkPolicyAmendments": null, "availableDecisions": null @@ -6139,7 +6116,6 @@ mod tests { "contacts": "read_only" } }, - "skillMetadata": null, "proposedExecpolicyAmendment": null, "proposedNetworkPolicyAmendments": null, "availableDecisions": null @@ -6183,7 +6159,6 @@ mod tests { "contacts": "none" } }, - "skillMetadata": null, "proposedExecpolicyAmendment": null, "proposedNetworkPolicyAmendments": null, "availableDecisions": null @@ -6199,35 +6174,6 @@ mod tests { ); } - #[test] - fn command_execution_request_approval_accepts_skill_metadata() { - let params = serde_json::from_value::(json!({ - "threadId": "thr_123", - "turnId": "turn_123", - "itemId": "call_123", - "command": "cat file", - "cwd": "/tmp", - "commandActions": null, - "reason": null, - "networkApprovalContext": null, - "additionalPermissions": null, - "skillMetadata": { - "pathToSkillsMd": "/tmp/SKILLS.md" - }, - "proposedExecpolicyAmendment": null, - "proposedNetworkPolicyAmendments": null, - "availableDecisions": null - })) - .expect("skill metadata should deserialize"); - - assert_eq!( - params.skill_metadata, - Some(CommandExecutionRequestApprovalSkillMetadata { - path_to_skills_md: PathBuf::from("/tmp/SKILLS.md"), - }) - ); - } - #[test] fn permissions_request_approval_uses_request_permission_profile() { let read_only_path = if cfg!(windows) { diff --git a/codex-rs/app-server-test-client/src/lib.rs b/codex-rs/app-server-test-client/src/lib.rs index a479f96529..82954d3cb6 100644 --- a/codex-rs/app-server-test-client/src/lib.rs +++ b/codex-rs/app-server-test-client/src/lib.rs @@ -1917,7 +1917,6 @@ impl CodexClient { cwd, command_actions, additional_permissions, - skill_metadata, proposed_execpolicy_amendment, proposed_network_policy_amendments, available_decisions, @@ -1952,9 +1951,6 @@ impl CodexClient { if let Some(additional_permissions) = additional_permissions.as_ref() { println!("< additional permissions: {additional_permissions:?}"); } - if let Some(skill_metadata) = skill_metadata.as_ref() { - println!("< skill metadata: {skill_metadata:?}"); - } if let Some(execpolicy_amendment) = proposed_execpolicy_amendment.as_ref() { println!("< proposed execpolicy amendment: {execpolicy_amendment:?}"); } diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 15484d3d5f..9a850c0b1e 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -26,7 +26,6 @@ use codex_app_server_protocol::CommandExecutionApprovalDecision; use codex_app_server_protocol::CommandExecutionOutputDeltaNotification; use codex_app_server_protocol::CommandExecutionRequestApprovalParams; use codex_app_server_protocol::CommandExecutionRequestApprovalResponse; -use codex_app_server_protocol::CommandExecutionRequestApprovalSkillMetadata; use codex_app_server_protocol::CommandExecutionSource; use codex_app_server_protocol::CommandExecutionStatus; use codex_app_server_protocol::ContextCompactedNotification; @@ -608,7 +607,6 @@ pub(crate) async fn apply_bespoke_event_handling( proposed_execpolicy_amendment, proposed_network_policy_amendments, additional_permissions, - skill_metadata, parsed_cmd, .. } = ev; @@ -680,8 +678,6 @@ pub(crate) async fn apply_bespoke_event_handling( }); let additional_permissions = additional_permissions.map(V2AdditionalPermissionProfile::from); - let skill_metadata = - skill_metadata.map(CommandExecutionRequestApprovalSkillMetadata::from); let params = CommandExecutionRequestApprovalParams { thread_id: conversation_id.to_string(), @@ -694,7 +690,6 @@ pub(crate) async fn apply_bespoke_event_handling( cwd, command_actions, additional_permissions, - skill_metadata, proposed_execpolicy_amendment: proposed_execpolicy_amendment_v2, proposed_network_policy_amendments: proposed_network_policy_amendments_v2, available_decisions: Some(available_decisions), diff --git a/codex-rs/app-server/src/transport/mod.rs b/codex-rs/app-server/src/transport/mod.rs index 2f80a009d3..9e02e77239 100644 --- a/codex-rs/app-server/src/transport/mod.rs +++ b/codex-rs/app-server/src/transport/mod.rs @@ -379,7 +379,6 @@ pub(crate) async fn route_outgoing_envelope( mod tests { use super::*; use crate::error_code::OVERLOADED_ERROR_CODE; - use codex_app_server_protocol::CommandExecutionRequestApprovalSkillMetadata; use codex_app_server_protocol::ConfigWarningNotification; use codex_app_server_protocol::ServerNotification; use codex_utils_absolute_path::AbsolutePathBuf; @@ -761,7 +760,7 @@ mod tests { } #[tokio::test] - async fn command_execution_request_approval_strips_experimental_fields_without_capability() { + async fn command_execution_request_approval_strips_additional_permissions_without_capability() { let connection_id = ConnectionId(8); let (writer_tx, mut writer_rx) = mpsc::channel(1); @@ -805,9 +804,6 @@ mod tests { macos: None, }, ), - skill_metadata: Some(CommandExecutionRequestApprovalSkillMetadata { - path_to_skills_md: PathBuf::from("/tmp/SKILLS.md"), - }), proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, @@ -824,11 +820,10 @@ mod tests { .expect("request should be delivered to the connection"); let json = serde_json::to_value(message.message).expect("request should serialize"); assert_eq!(json["params"].get("additionalPermissions"), None); - assert_eq!(json["params"].get("skillMetadata"), None); } #[tokio::test] - async fn command_execution_request_approval_keeps_experimental_fields_with_capability() { + async fn command_execution_request_approval_keeps_additional_permissions_with_capability() { let connection_id = ConnectionId(9); let (writer_tx, mut writer_rx) = mpsc::channel(1); @@ -872,9 +867,6 @@ mod tests { macos: None, }, ), - skill_metadata: Some(CommandExecutionRequestApprovalSkillMetadata { - path_to_skills_md: PathBuf::from("/tmp/SKILLS.md"), - }), proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, @@ -897,17 +889,11 @@ mod tests { "network": null, "fileSystem": { "read": [allowed_path], - "write": null, + "write": null, }, "macos": null, }) ); - assert_eq!( - json["params"]["skillMetadata"], - json!({ - "pathToSkillsMd": "/tmp/SKILLS.md", - }) - ); } #[tokio::test] diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 3a27a8839b..ed24d28466 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -75,7 +75,6 @@ use codex_otel::current_span_w3c_trace_context; use codex_otel::set_parent_from_w3c_trace_context; use codex_protocol::ThreadId; use codex_protocol::approvals::ElicitationRequestEvent; -use codex_protocol::approvals::ExecApprovalRequestSkillMetadata; use codex_protocol::approvals::ExecPolicyAmendment; use codex_protocol::approvals::NetworkPolicyAmendment; use codex_protocol::approvals::NetworkPolicyRuleAction; @@ -2897,7 +2896,6 @@ impl Session { network_approval_context: Option, proposed_execpolicy_amendment: Option, additional_permissions: Option, - skill_metadata: Option, available_decisions: Option>, ) -> ReviewDecision { // command-level approvals use `call_id`. @@ -2951,7 +2949,6 @@ impl Session { proposed_execpolicy_amendment, proposed_network_policy_amendments, additional_permissions, - skill_metadata, available_decisions: Some(available_decisions), parsed_cmd, }); diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs index 9f491bbcb8..3f1508cca1 100644 --- a/codex-rs/core/src/codex_delegate.rs +++ b/codex-rs/core/src/codex_delegate.rs @@ -436,7 +436,6 @@ async fn handle_exec_approval( network_approval_context, proposed_execpolicy_amendment, additional_permissions, - skill_metadata, available_decisions, .. } = event; @@ -480,7 +479,6 @@ async fn handle_exec_approval( network_approval_context, proposed_execpolicy_amendment, additional_permissions, - skill_metadata, available_decisions, ), parent_session, diff --git a/codex-rs/core/src/codex_delegate_tests.rs b/codex-rs/core/src/codex_delegate_tests.rs index 8201424d8e..29efd92d71 100644 --- a/codex-rs/core/src/codex_delegate_tests.rs +++ b/codex-rs/core/src/codex_delegate_tests.rs @@ -286,7 +286,6 @@ async fn handle_exec_approval_uses_call_id_for_guardian_review_and_approval_id_f proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: Some(vec![ ReviewDecision::Approved, ReviewDecision::Abort, diff --git a/codex-rs/core/src/tools/network_approval.rs b/codex-rs/core/src/tools/network_approval.rs index 02b6bbe601..d3c526e99e 100644 --- a/codex-rs/core/src/tools/network_approval.rs +++ b/codex-rs/core/src/tools/network_approval.rs @@ -384,7 +384,6 @@ impl NetworkApprovalService { Some(network_approval_context.clone()), /*proposed_execpolicy_amendment*/ None, /*additional_permissions*/ None, - /*skill_metadata*/ None, available_decisions, ) .await diff --git a/codex-rs/core/src/tools/runtimes/shell.rs b/codex-rs/core/src/tools/runtimes/shell.rs index ecb5822124..3916a86301 100644 --- a/codex-rs/core/src/tools/runtimes/shell.rs +++ b/codex-rs/core/src/tools/runtimes/shell.rs @@ -184,7 +184,6 @@ impl Approvable for ShellRuntime { .proposed_execpolicy_amendment() .cloned(), req.additional_permissions.clone(), - /*skill_metadata*/ None, available_decisions, ) .await diff --git a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs index b2eaa7a032..8cfb551a1a 100644 --- a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs +++ b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs @@ -426,7 +426,6 @@ impl CoreShellActionProvider { /*network_approval_context*/ None, /*proposed_execpolicy_amendment*/ None, additional_permissions, - /*skill_metadata*/ None, Some(vec![ReviewDecision::Approved, ReviewDecision::Abort]), ) .await diff --git a/codex-rs/core/src/tools/runtimes/unified_exec.rs b/codex-rs/core/src/tools/runtimes/unified_exec.rs index 96a3e09fb8..9211fb08a7 100644 --- a/codex-rs/core/src/tools/runtimes/unified_exec.rs +++ b/codex-rs/core/src/tools/runtimes/unified_exec.rs @@ -161,7 +161,6 @@ impl Approvable for UnifiedExecRuntime<'_> { .proposed_execpolicy_amendment() .cloned(), req.additional_permissions.clone(), - /*skill_metadata*/ None, available_decisions, ) .await diff --git a/codex-rs/mcp-server/src/codex_tool_runner.rs b/codex-rs/mcp-server/src/codex_tool_runner.rs index 780a808038..ae9d26e99e 100644 --- a/codex-rs/mcp-server/src/codex_tool_runner.rs +++ b/codex-rs/mcp-server/src/codex_tool_runner.rs @@ -228,7 +228,6 @@ async fn run_codex_tool_session_inner( parsed_cmd, network_approval_context: _, additional_permissions: _, - skill_metadata: _, available_decisions: _, } = ev; handle_exec_approval_request( diff --git a/codex-rs/protocol/src/approvals.rs b/codex-rs/protocol/src/approvals.rs index 848ea31c02..95ccdd624c 100644 --- a/codex-rs/protocol/src/approvals.rs +++ b/codex-rs/protocol/src/approvals.rs @@ -1,6 +1,3 @@ -use std::collections::HashMap; -use std::path::PathBuf; - use crate::mcp::RequestId; use crate::models::MacOsSeatbeltProfileExtensions; use crate::models::PermissionProfile; @@ -14,6 +11,8 @@ use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; use serde_json::Value as JsonValue; +use std::collections::HashMap; +use std::path::PathBuf; use ts_rs::TS; #[derive(Debug, Clone, PartialEq, Eq)] @@ -107,13 +106,6 @@ pub struct NetworkPolicyAmendment { pub action: NetworkPolicyRuleAction, } -#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] -#[serde(rename_all = "snake_case")] -#[ts(rename_all = "snake_case")] -pub struct ExecApprovalRequestSkillMetadata { - pub path_to_skills_md: PathBuf, -} - #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, TS)] pub struct GuardianAssessmentEvent { /// Stable identifier for this guardian review lifecycle. @@ -181,10 +173,6 @@ pub struct ExecApprovalRequestEvent { #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] pub additional_permissions: Option, - /// Optional skill metadata when the approval was triggered by a skill script. - #[serde(default, skip_serializing_if = "Option::is_none")] - #[ts(optional)] - pub skill_metadata: Option, /// Ordered list of decisions the client may present for this prompt. /// /// When absent, clients should derive the legacy default set from the diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 71c9cbda64..9a4f2b90b2 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -64,7 +64,6 @@ use ts_rs::TS; pub use crate::approvals::ApplyPatchApprovalRequestEvent; pub use crate::approvals::ElicitationAction; pub use crate::approvals::ExecApprovalRequestEvent; -pub use crate::approvals::ExecApprovalRequestSkillMetadata; pub use crate::approvals::ExecPolicyAmendment; pub use crate::approvals::GuardianAssessmentEvent; pub use crate::approvals::GuardianAssessmentStatus; diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index fe1cb6ae0c..ff5d6d47dd 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -4979,7 +4979,6 @@ mod tests { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, @@ -6626,7 +6625,6 @@ guardian_approval = true proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, @@ -6715,7 +6713,6 @@ guardian_approval = true proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, diff --git a/codex-rs/tui/src/app/pending_interactive_replay.rs b/codex-rs/tui/src/app/pending_interactive_replay.rs index 02efb2ec1b..dba5f2f018 100644 --- a/codex-rs/tui/src/app/pending_interactive_replay.rs +++ b/codex-rs/tui/src/app/pending_interactive_replay.rs @@ -453,7 +453,6 @@ mod tests { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, @@ -597,7 +596,6 @@ mod tests { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, @@ -687,7 +685,6 @@ mod tests { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: Vec::new(), }, diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 90fea87e7f..8e3485daa7 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -3456,7 +3456,6 @@ async fn exec_approval_emits_proposed_command_and_decision_history() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -3507,7 +3506,6 @@ async fn exec_approval_uses_approval_id_when_present() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }), @@ -3549,7 +3547,6 @@ async fn exec_approval_decision_truncates_multiline_and_long_commands() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -3605,7 +3602,6 @@ async fn exec_approval_decision_truncates_multiline_and_long_commands() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -9966,7 +9962,6 @@ async fn approval_modal_exec_snapshot() -> anyhow::Result<()> { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10028,7 +10023,6 @@ async fn approval_modal_exec_without_reason_snapshot() -> anyhow::Result<()> { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10077,7 +10071,6 @@ async fn approval_modal_exec_multiline_prefix_hides_execpolicy_option_snapshot() proposed_execpolicy_amendment: Some(ExecPolicyAmendment::new(command)), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10445,7 +10438,6 @@ async fn status_widget_and_approval_modal_snapshot() { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; diff --git a/codex-rs/tui_app_server/src/app.rs b/codex-rs/tui_app_server/src/app.rs index 95e231b970..a86a8c9388 100644 --- a/codex-rs/tui_app_server/src/app.rs +++ b/codex-rs/tui_app_server/src/app.rs @@ -8371,7 +8371,6 @@ guardian_approval = true cwd: Some(PathBuf::from("/tmp/project")), command_actions: None, additional_permissions: None, - skill_metadata: None, proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, diff --git a/codex-rs/tui_app_server/src/app/app_server_requests.rs b/codex-rs/tui_app_server/src/app/app_server_requests.rs index 4381e883c0..29f0032b65 100644 --- a/codex-rs/tui_app_server/src/app/app_server_requests.rs +++ b/codex-rs/tui_app_server/src/app/app_server_requests.rs @@ -318,7 +318,6 @@ mod tests { cwd: None, command_actions: None, additional_permissions: None, - skill_metadata: None, proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, diff --git a/codex-rs/tui_app_server/src/app/pending_interactive_replay.rs b/codex-rs/tui_app_server/src/app/pending_interactive_replay.rs index 67c88d5f90..0110c8795b 100644 --- a/codex-rs/tui_app_server/src/app/pending_interactive_replay.rs +++ b/codex-rs/tui_app_server/src/app/pending_interactive_replay.rs @@ -627,7 +627,6 @@ mod tests { cwd: Some(PathBuf::from("/tmp")), command_actions: None, additional_permissions: None, - skill_metadata: None, proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, diff --git a/codex-rs/tui_app_server/src/chatwidget.rs b/codex-rs/tui_app_server/src/chatwidget.rs index 636349aae5..5799cc4dd3 100644 --- a/codex-rs/tui_app_server/src/chatwidget.rs +++ b/codex-rs/tui_app_server/src/chatwidget.rs @@ -1317,11 +1317,6 @@ fn exec_approval_request_from_params( .collect() }, ), - skill_metadata: params.skill_metadata.map(|metadata| { - codex_protocol::approvals::ExecApprovalRequestSkillMetadata { - path_to_skills_md: metadata.path_to_skills_md, - } - }), available_decisions: params.available_decisions.map(|decisions| { decisions .into_iter() diff --git a/codex-rs/tui_app_server/src/chatwidget/tests.rs b/codex-rs/tui_app_server/src/chatwidget/tests.rs index 7dadb963d8..58ff60eb03 100644 --- a/codex-rs/tui_app_server/src/chatwidget/tests.rs +++ b/codex-rs/tui_app_server/src/chatwidget/tests.rs @@ -3479,7 +3479,6 @@ async fn exec_approval_emits_proposed_command_and_decision_history() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -3529,7 +3528,6 @@ fn app_server_exec_approval_request_splits_shell_wrapped_command() { cwd: Some(PathBuf::from("/tmp")), command_actions: None, additional_permissions: None, - skill_metadata: None, proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, available_decisions: None, @@ -3564,7 +3562,6 @@ async fn exec_approval_uses_approval_id_when_present() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }), @@ -3606,7 +3603,6 @@ async fn exec_approval_decision_truncates_multiline_and_long_commands() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -3662,7 +3658,6 @@ async fn exec_approval_decision_truncates_multiline_and_long_commands() { proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10680,7 +10675,6 @@ async fn approval_modal_exec_snapshot() -> anyhow::Result<()> { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10742,7 +10736,6 @@ async fn approval_modal_exec_without_reason_snapshot() -> anyhow::Result<()> { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -10791,7 +10784,6 @@ async fn approval_modal_exec_multiline_prefix_hides_execpolicy_option_snapshot() proposed_execpolicy_amendment: Some(ExecPolicyAmendment::new(command)), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; @@ -11175,7 +11167,6 @@ async fn status_widget_and_approval_modal_snapshot() { ])), proposed_network_policy_amendments: None, additional_permissions: None, - skill_metadata: None, available_decisions: None, parsed_cmd: vec![], }; From aea82c63eac85f5d5fd85de5ac79a0b1ff8369d4 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Thu, 26 Mar 2026 16:18:04 -0700 Subject: [PATCH 019/191] fix(network-proxy): fail closed on network-proxy DNS lookup errors (#15909) ## Summary Fail closed when the network proxy's local/private IP pre-check hits a DNS lookup error or timeout, instead of treating the hostname as public and allowing the request. ## Root cause `host_resolves_to_non_public_ip()` returned `false` on resolver failure, which created a fail-open path in the `allow_local_binding = false` boundary. The eventual connect path performs its own DNS resolution later, so a transient pre-check failure is not evidence that the destination is public. ## Changes - Treat DNS lookup errors/timeouts as local/private for blocking purposes - Add a regression test for an allowlisted hostname that fails DNS resolution ## Validation - `cargo test -p codex-network-proxy` - `cargo clippy -p codex-network-proxy --all-targets -- -D warnings` - `just fmt` - `just argument-comment-lint` --- codex-rs/network-proxy/src/runtime.rs | 44 ++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/codex-rs/network-proxy/src/runtime.rs b/codex-rs/network-proxy/src/runtime.rs index 9381fab7f4..ec43a54792 100644 --- a/codex-rs/network-proxy/src/runtime.rs +++ b/codex-rs/network-proxy/src/runtime.rs @@ -698,12 +698,22 @@ async fn host_resolves_to_non_public_ip(host: &str, port: u16) -> bool { return is_non_public_ip(ip); } - // If DNS lookup fails, default to "not local/private" rather than blocking. In practice, the - // subsequent connect attempt will fail anyway, and blocking on transient resolver issues would - // make the proxy fragile. The allowlist/denylist remains the primary control plane. + // Block the request if this DNS lookup fails. We resolve the hostname again when we connect, + // so a failed check here does not prove the destination is public. let addrs = match timeout(DNS_LOOKUP_TIMEOUT, lookup_host((host, port))).await { Ok(Ok(addrs)) => addrs, - Ok(Err(_)) | Err(_) => return false, + Ok(Err(err)) => { + debug!( + "blocking host because DNS lookup failed during local/private IP check (host={host}, port={port}): {err}" + ); + return true; + } + Err(_) => { + debug!( + "blocking host because DNS lookup timed out during local/private IP check (host={host}, port={port})" + ); + return true; + } }; for addr in addrs { @@ -903,17 +913,18 @@ mod tests { }); assert_eq!( - state.host_blocked("evil.example", 80).await.unwrap(), + // Use a public IP literal to avoid relying on ambient DNS behavior. + state.host_blocked("8.8.8.8", 80).await.unwrap(), HostBlockDecision::Allowed ); - state.add_denied_domain("evil.example").await.unwrap(); + state.add_denied_domain("8.8.8.8").await.unwrap(); let (allowed, denied) = state.current_patterns().await.unwrap(); assert_eq!(allowed, vec!["*".to_string()]); - assert_eq!(denied, vec!["evil.example".to_string()]); + assert_eq!(denied, vec!["8.8.8.8".to_string()]); assert_eq!( - state.host_blocked("evil.example", 80).await.unwrap(), + state.host_blocked("8.8.8.8", 80).await.unwrap(), HostBlockDecision::Blocked(HostBlockReason::Denied) ); } @@ -1237,6 +1248,23 @@ mod tests { ); } + #[tokio::test] + async fn host_blocked_rejects_allowlisted_hostname_when_dns_lookup_fails() { + let state = network_proxy_state_for_policy(NetworkProxySettings { + allowed_domains: vec!["does-not-resolve.invalid".to_string()], + allow_local_binding: false, + ..NetworkProxySettings::default() + }); + + assert_eq!( + state + .host_blocked("does-not-resolve.invalid", 80) + .await + .unwrap(), + HostBlockDecision::Blocked(HostBlockReason::NotAllowedLocal) + ); + } + #[test] fn validate_policy_against_constraints_disallows_widening_allowed_domains() { let constraints = NetworkProxyConstraints { From c264c6eef975d4abaf67beefd60added71c90950 Mon Sep 17 00:00:00 2001 From: Siggi Simonarson Date: Thu, 26 Mar 2026 16:41:15 -0700 Subject: [PATCH 020/191] Preserve bazel repository cache in github actions (#14495) Highlights: - Trimmed down to just the repository cache for faster upload / download - Made the cache key only include files that affect external dependencies (since that's what the repository cache caches) - MODULE.bazel, codex-rs/Cargo.lock, codex-rs/Cargo.toml - Split the caching action in to explicit restore / save steps (similar to your rust CI) which allows us to skip uploads on cache hit, and not fail the build if upload fails This should get rid of 842 network fetches that are happening on every Bazel CI run, while also reducing the Github flakiness @bolinfest reported. Uploading should be faster (since we're not caching many small files), and will only happen when MODULE.bazel or Cargo.lock / Cargo.toml files change. In my testing, it [took 3s to save the repository cache](https://github.com/siggisim/codex/actions/runs/23014186143/job/66832859781). --- .github/workflows/bazel.yml | 46 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index b2ef107ca7..0497cd54ea 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -78,30 +78,17 @@ jobs: shell: bash run: ./scripts/check-module-bazel-lock.sh - # TODO(mbolin): Bring this back once we have caching working. Currently, - # we never seem to get a cache hit but we still end up paying the cost of - # uploading at the end of the build, which takes over a minute! - # - # Cache build and external artifacts so that the next ci build is incremental. - # Because github action caches cannot be updated after a build, we need to - # store the contents of each build in a unique cache key, then fall back to loading - # it on the next ci run. We use hashFiles(...) in the key and restore-keys- with - # the prefix to load the most recent cache for the branch on a cache miss. You - # should customize the contents of hashFiles to capture any bazel input sources, - # although this doesn't need to be perfect. If none of the input sources change - # then a cache hit will load an existing cache and bazel won't have to do any work. - # In the case of a cache miss, you want the fallback cache to contain most of the - # previously built artifacts to minimize build time. The more precise you are with - # hashFiles sources the less work bazel will have to do. - # - name: Mount bazel caches - # uses: actions/cache@v5 - # with: - # path: | - # ~/.cache/bazel-repo-cache - # ~/.cache/bazel-repo-contents-cache - # key: bazel-cache-${{ matrix.os }}-${{ hashFiles('**/BUILD.bazel', '**/*.bzl', 'MODULE.bazel') }} - # restore-keys: | - # bazel-cache-${{ matrix.os }} + # Restore bazel repository cache so we don't have to redownload all the external dependencies + # on every CI run. + - name: Restore bazel repository cache + id: cache_bazel_repository_restore + uses: actions/cache/restore@v5 + with: + path: | + ~/.cache/bazel-repo-cache + key: bazel-cache-${{ matrix.os }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} + restore-keys: | + bazel-cache-${{ matrix.os }} - name: Configure Bazel startup args (Windows) if: runner.os == 'Windows' @@ -229,3 +216,14 @@ jobs: print_failed_bazel_test_logs "$bazel_console_log" exit "$bazel_status" fi + + # Save bazel repository cache explicitly; make non-fatal so cache uploading + # never fails the overall job. Only save when key wasn't hit. + - name: Save bazel repository cache + if: always() && !cancelled() && steps.cache_bazel_repository_restore.outputs.cache-hit != 'true' + continue-on-error: true + uses: actions/cache/save@v5 + with: + path: | + ~/.cache/bazel-repo-cache + key: bazel-cache-${{ matrix.os }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} From a27cd2d28150b71882847a8bfdc03d275e29a1eb Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Fri, 27 Mar 2026 06:50:07 +0700 Subject: [PATCH 021/191] bazel: re-organize bazelrc (#15522) Replaced ci.bazelrc and v8-ci.bazelrc by custom configs inside the main .bazelrc file. As a result, github workflows setup is simplified down to a single '--config=' flag usage. Moved the build metadata flags to config=ci. Added custom tags metadata to help differentiate invocations based on workflow (bazel vs v8) and os (linux/macos/windows). Enabled users to override the default values in .bazelrc by using a user.bazelrc file locally. Added user.bazelrc to gitignore. --- .bazelrc | 48 ++++++++++++++++++++++++++ .github/workflows/bazel.yml | 12 ++++--- .github/workflows/ci.bazelrc | 27 --------------- .github/workflows/rusty-v8-release.yml | 2 +- .github/workflows/v8-canary.yml | 2 +- .github/workflows/v8-ci.bazelrc | 5 --- .gitignore | 1 + 7 files changed, 59 insertions(+), 38 deletions(-) delete mode 100644 .github/workflows/ci.bazelrc delete mode 100644 .github/workflows/v8-ci.bazelrc diff --git a/.bazelrc b/.bazelrc index 2ff9cbc28e..ce7c1e1d43 100644 --- a/.bazelrc +++ b/.bazelrc @@ -60,3 +60,51 @@ common:remote --jobs=800 # Enable pipelined compilation since we are not bound by local CPU count. #common:remote --@rules_rust//rust/settings:pipelined_compilation +# GitHub Actions CI configs. +common:ci --remote_download_minimal +common:ci --keep_going +common:ci --verbose_failures +common:ci --build_metadata=REPO_URL=https://github.com/openai/codex.git +common:ci --build_metadata=ROLE=CI +common:ci --build_metadata=VISIBILITY=PUBLIC + +# Disable disk cache in CI since we have a remote one and aren't using persistent workers. +common:ci --disk_cache= + +# Shared config for the main Bazel CI workflow. +common:ci-bazel --config=ci +common:ci-bazel --build_metadata=TAG_workflow=bazel + +# Rearrange caches on Windows so they're on the same volume as the checkout. +common:ci-windows --config=ci-bazel +common:ci-windows --build_metadata=TAG_os=windows +common:ci-windows --repo_contents_cache=D:/a/.cache/bazel-repo-contents-cache +common:ci-windows --repository_cache=D:/a/.cache/bazel-repo-cache + +# We prefer to run the build actions entirely remotely so we can dial up the concurrency. +# We have platform-specific tests, so we want to execute the tests on all platforms using the strongest sandboxing available on each platform. + +# On linux, we can do a full remote build/test, by targeting the right (x86/arm) runners, so we have coverage of both. +# Linux crossbuilds don't work until we untangle the libc constraint mess. +common:ci-linux --config=ci-bazel +common:ci-linux --build_metadata=TAG_os=linux +common:ci-linux --config=remote +common:ci-linux --strategy=remote +common:ci-linux --platforms=//:rbe + +# On mac, we can run all the build actions remotely but test actions locally. +common:ci-macos --config=ci-bazel +common:ci-macos --build_metadata=TAG_os=macos +common:ci-macos --config=remote +common:ci-macos --strategy=remote +common:ci-macos --strategy=TestRunner=darwin-sandbox,local + +# Linux-only V8 CI config. +common:ci-v8 --config=ci +common:ci-v8 --build_metadata=TAG_workflow=v8 +common:ci-v8 --build_metadata=TAG_os=linux +common:ci-v8 --config=remote +common:ci-v8 --strategy=remote + +# Optional per-user local overrides. +try-import %workspace%/user.bazelrc diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 0497cd54ea..1c12db52b1 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -144,10 +144,7 @@ jobs: bazel_args=( test --test_verbose_timeout_warnings - --build_metadata=REPO_URL=https://github.com/openai/codex.git --build_metadata=COMMIT_SHA=$(git rev-parse HEAD) - --build_metadata=ROLE=CI - --build_metadata=VISIBILITY=PUBLIC ) bazel_targets=( @@ -164,6 +161,13 @@ jobs: bazel_args+=("--test_env=CODEX_JS_REPL_NODE_PATH=${node_bin}") fi + ci_config=ci-linux + if [[ "${RUNNER_OS:-}" == "macOS" ]]; then + ci_config=ci-macos + elif [[ "${RUNNER_OS:-}" == "Windows" ]]; then + ci_config=ci-windows + fi + if [[ -n "${BUILDBUDDY_API_KEY:-}" ]]; then echo "BuildBuddy API key is available; using remote Bazel configuration." # Work around Bazel 9 remote repo contents cache / overlay materialization failures @@ -173,8 +177,8 @@ jobs: set +e bazel $BAZEL_STARTUP_ARGS \ --noexperimental_remote_repo_contents_cache \ - --bazelrc=.github/workflows/ci.bazelrc \ "${bazel_args[@]}" \ + "--config=${ci_config}" \ "--remote_header=x-buildbuddy-api-key=$BUILDBUDDY_API_KEY" \ -- \ "${bazel_targets[@]}" \ diff --git a/.github/workflows/ci.bazelrc b/.github/workflows/ci.bazelrc deleted file mode 100644 index 997a774eb3..0000000000 --- a/.github/workflows/ci.bazelrc +++ /dev/null @@ -1,27 +0,0 @@ -common --remote_download_minimal -common --keep_going -common --verbose_failures - -# Disable disk cache since we have remote one and aren't using persistent workers. -common --disk_cache= - -# Rearrange caches on Windows so they're on the same volume as the checkout. -common:windows --repo_contents_cache=D:/a/.cache/bazel-repo-contents-cache -common:windows --repository_cache=D:/a/.cache/bazel-repo-cache - -# We prefer to run the build actions entirely remotely so we can dial up the concurrency. -# We have platform-specific tests, so we want to execute the tests on all platforms using the strongest sandboxing available on each platform. - -# On linux, we can do a full remote build/test, by targeting the right (x86/arm) runners, so we have coverage of both. -# Linux crossbuilds don't work until we untangle the libc constraint mess. -common:linux --config=remote -common:linux --strategy=remote -common:linux --platforms=//:rbe - -# On mac, we can run all the build actions remotely but test actions locally. -common:macos --config=remote -common:macos --strategy=remote -common:macos --strategy=TestRunner=darwin-sandbox,local - -# On windows we cannot cross-build the tests but run them locally due to what appears to be a Bazel bug -# (windows vs unix path confusion) diff --git a/.github/workflows/rusty-v8-release.yml b/.github/workflows/rusty-v8-release.yml index bb191b88cb..60aac24366 100644 --- a/.github/workflows/rusty-v8-release.yml +++ b/.github/workflows/rusty-v8-release.yml @@ -116,8 +116,8 @@ jobs: bazel \ --noexperimental_remote_repo_contents_cache \ - --bazelrc=.github/workflows/v8-ci.bazelrc \ "${bazel_args[@]}" \ + --config=ci-v8 \ "--remote_header=x-buildbuddy-api-key=${BUILDBUDDY_API_KEY}" - name: Stage release pair diff --git a/.github/workflows/v8-canary.yml b/.github/workflows/v8-canary.yml index 213c6a7b60..6e068e8021 100644 --- a/.github/workflows/v8-canary.yml +++ b/.github/workflows/v8-canary.yml @@ -108,8 +108,8 @@ jobs: bazel \ --noexperimental_remote_repo_contents_cache \ - --bazelrc=.github/workflows/v8-ci.bazelrc \ "${bazel_args[@]}" \ + --config=ci-v8 \ "--remote_header=x-buildbuddy-api-key=${BUILDBUDDY_API_KEY}" - name: Stage release pair diff --git a/.github/workflows/v8-ci.bazelrc b/.github/workflows/v8-ci.bazelrc deleted file mode 100644 index df1b4bec3d..0000000000 --- a/.github/workflows/v8-ci.bazelrc +++ /dev/null @@ -1,5 +0,0 @@ -import %workspace%/.github/workflows/ci.bazelrc - -common --build_metadata=REPO_URL=https://github.com/openai/codex.git -common --build_metadata=ROLE=CI -common --build_metadata=VISIBILITY=PUBLIC diff --git a/.gitignore b/.gitignore index 8f39b7b1c8..82269594bb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ node_modules # build dist/ bazel-* +user.bazelrc build/ out/ storybook-static/ From 44d28f500f7f21fa13f0e9eea87544a6269efde7 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 17:03:35 -0700 Subject: [PATCH 022/191] codex-tools: extract shared tool schema parsing (#15923) ## Why `parse_tool_input_schema` and the supporting `JsonSchema` model were living in `core/src/tools/spec.rs`, but they already serve callers outside `codex-core`. Keeping that shared schema parsing logic inside `codex-core` makes the crate boundary harder to reason about and works against the guidance in `AGENTS.md` to avoid growing `codex-core` when reusable code can live elsewhere. This change takes the first extraction step by moving the schema parsing primitive into its own crate while keeping the rest of the tool-spec assembly in `codex-core`. ## What changed - added a new `codex-tools` crate under `codex-rs/tools` - moved the shared tool input schema model and sanitizer/parser into `tools/src/json_schema.rs` - kept `tools/src/lib.rs` exports-only, with the module-level unit tests split into `json_schema_tests.rs` - updated `codex-core` to use `codex-tools::JsonSchema` and re-export `parse_tool_input_schema` - updated `codex-app-server` dynamic tool validation to depend on `codex-tools` directly instead of reaching through `codex-core` - wired the new crate into the Cargo workspace and Bazel build graph --- codex-rs/Cargo.lock | 11 ++ codex-rs/Cargo.toml | 2 + codex-rs/app-server/Cargo.toml | 1 + .../app-server/src/codex_message_processor.rs | 2 +- codex-rs/core/Cargo.toml | 1 + codex-rs/core/src/lib.rs | 2 +- codex-rs/core/src/tools/spec.rs | 185 +----------------- codex-rs/core/src/tools/spec_tests.rs | 1 + codex-rs/tools/BUILD.bazel | 6 + codex-rs/tools/Cargo.toml | 15 ++ codex-rs/tools/README.md | 70 +++++++ codex-rs/tools/src/json_schema.rs | 176 +++++++++++++++++ codex-rs/tools/src/json_schema_tests.rs | 98 ++++++++++ codex-rs/tools/src/lib.rs | 7 + 14 files changed, 394 insertions(+), 183 deletions(-) create mode 100644 codex-rs/tools/BUILD.bazel create mode 100644 codex-rs/tools/Cargo.toml create mode 100644 codex-rs/tools/README.md create mode 100644 codex-rs/tools/src/json_schema.rs create mode 100644 codex-rs/tools/src/json_schema_tests.rs create mode 100644 codex-rs/tools/src/lib.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 9487d74a10..9f62e87eea 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1417,6 +1417,7 @@ dependencies = [ "codex-sandboxing", "codex-shell-command", "codex-state", + "codex-tools", "codex-utils-absolute-path", "codex-utils-cargo-bin", "codex-utils-cli", @@ -1848,6 +1849,7 @@ dependencies = [ "codex-shell-escalation", "codex-state", "codex-terminal-detection", + "codex-tools", "codex-utils-absolute-path", "codex-utils-cache", "codex-utils-cargo-bin", @@ -2627,6 +2629,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "codex-tools" +version = "0.0.0" +dependencies = [ + "pretty_assertions", + "serde", + "serde_json", +] + [[package]] name = "codex-tui" version = "0.0.0" diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index ba287de5c5..f05355e393 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -51,6 +51,7 @@ members = [ "otel", "tui", "tui_app_server", + "tools", "v8-poc", "utils/absolute-path", "utils/cargo-bin", @@ -150,6 +151,7 @@ codex-skills = { path = "skills" } codex-state = { path = "state" } codex-stdio-to-uds = { path = "stdio-to-uds" } codex-terminal-detection = { path = "terminal-detection" } +codex-tools = { path = "tools" } codex-tui = { path = "tui" } codex-tui-app-server = { path = "tui_app_server" } codex-v8-poc = { path = "v8-poc" } diff --git a/codex-rs/app-server/Cargo.toml b/codex-rs/app-server/Cargo.toml index f54bb7ad03..ee12e87acb 100644 --- a/codex-rs/app-server/Cargo.toml +++ b/codex-rs/app-server/Cargo.toml @@ -49,6 +49,7 @@ codex-feedback = { workspace = true } codex-rmcp-client = { workspace = true } codex-sandboxing = { workspace = true } codex-state = { workspace = true } +codex-tools = { workspace = true } codex-utils-absolute-path = { workspace = true } codex-utils-json-to-toml = { workspace = true } chrono = { workspace = true } diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 620a85a095..875eb2b87f 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -7903,7 +7903,7 @@ fn validate_dynamic_tools(tools: &[ApiDynamicToolSpec]) -> Result<(), String> { return Err(format!("duplicate dynamic tool name: {name}")); } - if let Err(err) = codex_core::parse_tool_input_schema(&tool.input_schema) { + if let Err(err) = codex_tools::parse_tool_input_schema(&tool.input_schema) { return Err(format!( "dynamic tool input schema is not supported for {name}: {err}" )); diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index f64a0a5280..e40066027d 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -52,6 +52,7 @@ codex-rmcp-client = { workspace = true } codex-sandboxing = { workspace = true } codex-state = { workspace = true } codex-terminal-detection = { workspace = true } +codex-tools = { workspace = true } codex-utils-absolute-path = { workspace = true } codex-utils-cache = { workspace = true } codex-utils-image = { workspace = true } diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index ea2651f92e..f0390c805a 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -201,6 +201,7 @@ pub use client_common::REVIEW_PROMPT; pub use client_common::ResponseEvent; pub use client_common::ResponseStream; pub use codex_sandboxing::get_platform_sandbox; +pub use codex_tools::parse_tool_input_schema; pub use compact::content_items_to_text; pub use event_mapping::parse_turn_item; pub use exec_policy::ExecPolicyError; @@ -208,7 +209,6 @@ pub use exec_policy::check_execpolicy_for_warnings; pub use exec_policy::format_exec_policy_error_with_source; pub use exec_policy::load_exec_policy; pub use file_watcher::FileWatcherEvent; -pub use tools::spec::parse_tool_input_schema; pub use turn_metadata::build_turn_metadata_header; pub mod compact; pub mod memory_trace; diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index 82aba1a8cb..a81bf5a3c3 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -46,6 +46,7 @@ use codex_protocol::openai_models::WebSearchToolType; use codex_protocol::protocol::SandboxPolicy; use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SubAgentSource; +pub use codex_tools::parse_tool_input_schema; use codex_utils_absolute_path::AbsolutePathBuf; use serde::Deserialize; use serde::Serialize; @@ -55,6 +56,8 @@ use std::collections::BTreeMap; use std::collections::HashMap; use std::path::PathBuf; +pub type JsonSchema = codex_tools::JsonSchema; + const TOOL_SEARCH_DESCRIPTION_TEMPLATE: &str = include_str!("../../templates/search_tool/tool_description.md"); const TOOL_SUGGEST_DESCRIPTION_TEMPLATE: &str = @@ -539,62 +542,6 @@ fn supports_image_generation(model_info: &ModelInfo) -> bool { model_info.input_modalities.contains(&InputModality::Image) } -/// Generic JSON‑Schema subset needed for our tool definitions -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -#[serde(tag = "type", rename_all = "lowercase")] -pub enum JsonSchema { - Boolean { - #[serde(skip_serializing_if = "Option::is_none")] - description: Option, - }, - String { - #[serde(skip_serializing_if = "Option::is_none")] - description: Option, - }, - /// MCP schema allows "number" | "integer" for Number - #[serde(alias = "integer")] - Number { - #[serde(skip_serializing_if = "Option::is_none")] - description: Option, - }, - Array { - items: Box, - - #[serde(skip_serializing_if = "Option::is_none")] - description: Option, - }, - Object { - properties: BTreeMap, - #[serde(skip_serializing_if = "Option::is_none")] - required: Option>, - #[serde( - rename = "additionalProperties", - skip_serializing_if = "Option::is_none" - )] - additional_properties: Option, - }, -} - -/// Whether additional properties are allowed, and if so, any required schema -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -#[serde(untagged)] -pub enum AdditionalProperties { - Boolean(bool), - Schema(Box), -} - -impl From for AdditionalProperties { - fn from(b: bool) -> Self { - Self::Boolean(b) - } -} - -impl From for AdditionalProperties { - fn from(s: JsonSchema) -> Self { - Self::Schema(Box::new(s)) - } -} - fn create_network_permissions_schema() -> JsonSchema { JsonSchema::Object { properties: BTreeMap::from([( @@ -2458,13 +2405,6 @@ fn dynamic_tool_to_openai_tool( }) } -/// Parse the tool input_schema or return an error for invalid schema -pub fn parse_tool_input_schema(input_schema: &JsonValue) -> Result { - let mut input_schema = input_schema.clone(); - sanitize_json_schema(&mut input_schema); - serde_json::from_value::(input_schema) -} - fn mcp_tool_to_openai_tool_parts( tool: rmcp::model::Tool, ) -> Result<(String, JsonSchema, Option), serde_json::Error> { @@ -2489,13 +2429,7 @@ fn mcp_tool_to_openai_tool_parts( ); } - // Serialize to a raw JSON value so we can sanitize schemas coming from MCP - // servers. Some servers omit the top-level or nested `type` in JSON - // Schemas (e.g. using enum/anyOf), or use unsupported variants like - // `integer`. Our internal JsonSchema is a small subset and requires - // `type`, so we coerce/sanitize here for compatibility. - sanitize_json_schema(&mut serialized_input_schema); - let input_schema = serde_json::from_value::(serialized_input_schema)?; + let input_schema = parse_tool_input_schema(&serialized_input_schema)?; let structured_content_schema = output_schema .map(|output_schema| serde_json::Value::Object(output_schema.as_ref().clone())) .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())); @@ -2526,117 +2460,6 @@ fn mcp_call_tool_result_output_schema(structured_content_schema: JsonValue) -> J }) } -/// Sanitize a JSON Schema (as serde_json::Value) so it can fit our limited -/// JsonSchema enum. This function: -/// - Ensures every schema object has a "type". If missing, infers it from -/// common keywords (properties => object, items => array, enum/const/format => string) -/// and otherwise defaults to "string". -/// - Fills required child fields (e.g. array items, object properties) with -/// permissive defaults when absent. -fn sanitize_json_schema(value: &mut JsonValue) { - match value { - JsonValue::Bool(_) => { - // JSON Schema boolean form: true/false. Coerce to an accept-all string. - *value = json!({ "type": "string" }); - } - JsonValue::Array(arr) => { - for v in arr.iter_mut() { - sanitize_json_schema(v); - } - } - JsonValue::Object(map) => { - // First, recursively sanitize known nested schema holders - if let Some(props) = map.get_mut("properties") - && let Some(props_map) = props.as_object_mut() - { - for (_k, v) in props_map.iter_mut() { - sanitize_json_schema(v); - } - } - if let Some(items) = map.get_mut("items") { - sanitize_json_schema(items); - } - // Some schemas use oneOf/anyOf/allOf - sanitize their entries - for combiner in ["oneOf", "anyOf", "allOf", "prefixItems"] { - if let Some(v) = map.get_mut(combiner) { - sanitize_json_schema(v); - } - } - - // Normalize/ensure type - let mut ty = map.get("type").and_then(|v| v.as_str()).map(str::to_string); - - // If type is an array (union), pick first supported; else leave to inference - if ty.is_none() - && let Some(JsonValue::Array(types)) = map.get("type") - { - for t in types { - if let Some(tt) = t.as_str() - && matches!( - tt, - "object" | "array" | "string" | "number" | "integer" | "boolean" - ) - { - ty = Some(tt.to_string()); - break; - } - } - } - - // Infer type if still missing - if ty.is_none() { - if map.contains_key("properties") - || map.contains_key("required") - || map.contains_key("additionalProperties") - { - ty = Some("object".to_string()); - } else if map.contains_key("items") || map.contains_key("prefixItems") { - ty = Some("array".to_string()); - } else if map.contains_key("enum") - || map.contains_key("const") - || map.contains_key("format") - { - ty = Some("string".to_string()); - } else if map.contains_key("minimum") - || map.contains_key("maximum") - || map.contains_key("exclusiveMinimum") - || map.contains_key("exclusiveMaximum") - || map.contains_key("multipleOf") - { - ty = Some("number".to_string()); - } - } - // If we still couldn't infer, default to string - let ty = ty.unwrap_or_else(|| "string".to_string()); - map.insert("type".to_string(), JsonValue::String(ty.to_string())); - - // Ensure object schemas have properties map - if ty == "object" { - if !map.contains_key("properties") { - map.insert( - "properties".to_string(), - JsonValue::Object(serde_json::Map::new()), - ); - } - // If additionalProperties is an object schema, sanitize it too. - // Leave booleans as-is, since JSON Schema allows boolean here. - if let Some(ap) = map.get_mut("additionalProperties") { - let is_bool = matches!(ap, JsonValue::Bool(_)); - if !is_bool { - sanitize_json_schema(ap); - } - } - } - - // Ensure array schemas have items - if ty == "array" && !map.contains_key("items") { - map.insert("items".to_string(), json!({ "type": "string" })); - } - } - _ => {} - } -} - /// Builds the tool registry builder while collecting tool specs for later serialization. #[cfg(test)] pub(crate) fn build_specs( diff --git a/codex-rs/core/src/tools/spec_tests.rs b/codex-rs/core/src/tools/spec_tests.rs index 6f0e7496b8..82310e8726 100644 --- a/codex-rs/core/src/tools/spec_tests.rs +++ b/codex-rs/core/src/tools/spec_tests.rs @@ -11,6 +11,7 @@ use codex_app_server_protocol::AppInfo; use codex_protocol::openai_models::InputModality; use codex_protocol::openai_models::ModelInfo; use codex_protocol::openai_models::ModelsResponse; +use codex_tools::AdditionalProperties; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; use std::path::PathBuf; diff --git a/codex-rs/tools/BUILD.bazel b/codex-rs/tools/BUILD.bazel new file mode 100644 index 0000000000..d2e730cfa9 --- /dev/null +++ b/codex-rs/tools/BUILD.bazel @@ -0,0 +1,6 @@ +load("//:defs.bzl", "codex_rust_crate") + +codex_rust_crate( + name = "tools", + crate_name = "codex_tools", +) diff --git a/codex-rs/tools/Cargo.toml b/codex-rs/tools/Cargo.toml new file mode 100644 index 0000000000..c596520c57 --- /dev/null +++ b/codex-rs/tools/Cargo.toml @@ -0,0 +1,15 @@ +[package] +edition.workspace = true +license.workspace = true +name = "codex-tools" +version.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } + +[dev-dependencies] +pretty_assertions = { workspace = true } diff --git a/codex-rs/tools/README.md b/codex-rs/tools/README.md new file mode 100644 index 0000000000..d4c2f28e91 --- /dev/null +++ b/codex-rs/tools/README.md @@ -0,0 +1,70 @@ +# codex-tools + +`codex-tools` is intended to become the home for tool-related code that is +shared across multiple crates and does not need to stay coupled to +`codex-core`. + +Today this crate is intentionally small. It only owns the shared tool input +schema model and parser that were previously defined in `core/src/tools/spec.rs`: + +- `JsonSchema` +- `AdditionalProperties` +- `parse_tool_input_schema()` + +That extraction is the first step in a longer migration. The goal is not to +move all of `core/src/tools` into this crate in one shot. Instead, the plan is +to peel off reusable pieces in reviewable increments while keeping +compatibility-sensitive orchestration in `codex-core` until the surrounding +boundaries are ready. + +## Vision + +Over time, this crate should hold tool-facing primitives that are shared by +multiple consumers, for example: + +- schema and spec data models +- tool input/output parsing helpers +- tool metadata and compatibility shims that do not depend on `codex-core` +- other narrowly scoped utility code that multiple crates need + +The corresponding non-goals are just as important: + +- do not move `codex-core` orchestration here prematurely +- do not pull `Session` / `TurnContext` / approval flow / runtime execution + logic into this crate unless those dependencies have first been split into + stable shared interfaces +- do not turn this crate into a grab-bag for unrelated helper code + +## Migration approach + +The expected migration shape is: + +1. Move low-coupling tool primitives here. +2. Switch non-core consumers to depend on `codex-tools` directly. +3. Leave compatibility-sensitive adapters in `codex-core` while downstream + call sites are updated. +4. Only extract higher-level tool infrastructure after the crate boundaries are + clear and independently testable. + +That means it is normal for `codex-core` to temporarily re-export types or +helpers from `codex-tools` during the transition. + +## Crate conventions + +This crate should start with stricter structure than `core/src/tools` so it +stays easy to grow: + +- `src/lib.rs` should remain exports-only. +- Business logic should live in named module files such as `foo.rs`. +- Unit tests for `foo.rs` should live in a sibling `foo_tests.rs`. +- The implementation file should wire tests with: + +```rust +#[cfg(test)] +#[path = "foo_tests.rs"] +mod tests; +``` + +If this crate starts accumulating code that needs runtime state from +`codex-core`, that is a sign to revisit the extraction boundary before adding +more here. diff --git a/codex-rs/tools/src/json_schema.rs b/codex-rs/tools/src/json_schema.rs new file mode 100644 index 0000000000..727a085af1 --- /dev/null +++ b/codex-rs/tools/src/json_schema.rs @@ -0,0 +1,176 @@ +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value as JsonValue; +use serde_json::json; +use std::collections::BTreeMap; + +/// Generic JSON-Schema subset needed for our tool definitions. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(tag = "type", rename_all = "lowercase")] +pub enum JsonSchema { + Boolean { + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + }, + String { + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + }, + /// MCP schema allows "number" | "integer" for Number. + #[serde(alias = "integer")] + Number { + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + }, + Array { + items: Box, + + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + }, + Object { + properties: BTreeMap, + #[serde(skip_serializing_if = "Option::is_none")] + required: Option>, + #[serde( + rename = "additionalProperties", + skip_serializing_if = "Option::is_none" + )] + additional_properties: Option, + }, +} + +/// Whether additional properties are allowed, and if so, any required schema. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(untagged)] +pub enum AdditionalProperties { + Boolean(bool), + Schema(Box), +} + +impl From for AdditionalProperties { + fn from(value: bool) -> Self { + Self::Boolean(value) + } +} + +impl From for AdditionalProperties { + fn from(value: JsonSchema) -> Self { + Self::Schema(Box::new(value)) + } +} + +/// Parse the tool `input_schema` or return an error for invalid schema. +pub fn parse_tool_input_schema(input_schema: &JsonValue) -> Result { + let mut input_schema = input_schema.clone(); + sanitize_json_schema(&mut input_schema); + serde_json::from_value::(input_schema) +} + +/// Sanitize a JSON Schema (as serde_json::Value) so it can fit our limited +/// JsonSchema enum. This function: +/// - Ensures every schema object has a "type". If missing, infers it from +/// common keywords (properties => object, items => array, enum/const/format => string) +/// and otherwise defaults to "string". +/// - Fills required child fields (e.g. array items, object properties) with +/// permissive defaults when absent. +fn sanitize_json_schema(value: &mut JsonValue) { + match value { + JsonValue::Bool(_) => { + // JSON Schema boolean form: true/false. Coerce to an accept-all string. + *value = json!({ "type": "string" }); + } + JsonValue::Array(values) => { + for value in values { + sanitize_json_schema(value); + } + } + JsonValue::Object(map) => { + if let Some(properties) = map.get_mut("properties") + && let Some(properties_map) = properties.as_object_mut() + { + for value in properties_map.values_mut() { + sanitize_json_schema(value); + } + } + if let Some(items) = map.get_mut("items") { + sanitize_json_schema(items); + } + for combiner in ["oneOf", "anyOf", "allOf", "prefixItems"] { + if let Some(value) = map.get_mut(combiner) { + sanitize_json_schema(value); + } + } + + let mut schema_type = map + .get("type") + .and_then(|value| value.as_str()) + .map(str::to_string); + + if schema_type.is_none() + && let Some(JsonValue::Array(types)) = map.get("type") + { + for candidate in types { + if let Some(candidate_type) = candidate.as_str() + && matches!( + candidate_type, + "object" | "array" | "string" | "number" | "integer" | "boolean" + ) + { + schema_type = Some(candidate_type.to_string()); + break; + } + } + } + + if schema_type.is_none() { + if map.contains_key("properties") + || map.contains_key("required") + || map.contains_key("additionalProperties") + { + schema_type = Some("object".to_string()); + } else if map.contains_key("items") || map.contains_key("prefixItems") { + schema_type = Some("array".to_string()); + } else if map.contains_key("enum") + || map.contains_key("const") + || map.contains_key("format") + { + schema_type = Some("string".to_string()); + } else if map.contains_key("minimum") + || map.contains_key("maximum") + || map.contains_key("exclusiveMinimum") + || map.contains_key("exclusiveMaximum") + || map.contains_key("multipleOf") + { + schema_type = Some("number".to_string()); + } + } + + let schema_type = schema_type.unwrap_or_else(|| "string".to_string()); + map.insert("type".to_string(), JsonValue::String(schema_type.clone())); + + if schema_type == "object" { + if !map.contains_key("properties") { + map.insert( + "properties".to_string(), + JsonValue::Object(serde_json::Map::new()), + ); + } + if let Some(additional_properties) = map.get_mut("additionalProperties") + && !matches!(additional_properties, JsonValue::Bool(_)) + { + sanitize_json_schema(additional_properties); + } + } + + if schema_type == "array" && !map.contains_key("items") { + map.insert("items".to_string(), json!({ "type": "string" })); + } + } + _ => {} + } +} + +#[cfg(test)] +#[path = "json_schema_tests.rs"] +mod tests; diff --git a/codex-rs/tools/src/json_schema_tests.rs b/codex-rs/tools/src/json_schema_tests.rs new file mode 100644 index 0000000000..7d2cd25c9d --- /dev/null +++ b/codex-rs/tools/src/json_schema_tests.rs @@ -0,0 +1,98 @@ +use super::AdditionalProperties; +use super::JsonSchema; +use super::parse_tool_input_schema; +use pretty_assertions::assert_eq; +use std::collections::BTreeMap; + +#[test] +fn parse_tool_input_schema_coerces_boolean_schemas() { + let schema = parse_tool_input_schema(&serde_json::json!(true)).expect("parse schema"); + + assert_eq!(schema, JsonSchema::String { description: None }); +} + +#[test] +fn parse_tool_input_schema_infers_object_shape_and_defaults_properties() { + let schema = parse_tool_input_schema(&serde_json::json!({ + "properties": { + "query": {"description": "search query"} + } + })) + .expect("parse schema"); + + assert_eq!( + schema, + JsonSchema::Object { + properties: BTreeMap::from([( + "query".to_string(), + JsonSchema::String { + description: Some("search query".to_string()), + }, + )]), + required: None, + additional_properties: None, + } + ); +} + +#[test] +fn parse_tool_input_schema_normalizes_integer_and_missing_array_items() { + let schema = parse_tool_input_schema(&serde_json::json!({ + "type": "object", + "properties": { + "page": {"type": "integer"}, + "tags": {"type": "array"} + } + })) + .expect("parse schema"); + + assert_eq!( + schema, + JsonSchema::Object { + properties: BTreeMap::from([ + ("page".to_string(), JsonSchema::Number { description: None },), + ( + "tags".to_string(), + JsonSchema::Array { + items: Box::new(JsonSchema::String { description: None }), + description: None, + }, + ), + ]), + required: None, + additional_properties: None, + } + ); +} + +#[test] +fn parse_tool_input_schema_sanitizes_additional_properties_schema() { + let schema = parse_tool_input_schema(&serde_json::json!({ + "type": "object", + "additionalProperties": { + "required": ["value"], + "properties": { + "value": {"anyOf": [{"type": "string"}, {"type": "number"}]} + } + } + })) + .expect("parse schema"); + + assert_eq!( + schema, + JsonSchema::Object { + properties: BTreeMap::new(), + required: None, + additional_properties: Some(AdditionalProperties::Schema(Box::new( + JsonSchema::Object { + properties: BTreeMap::from([( + "value".to_string(), + JsonSchema::String { description: None }, + )]), + required: Some(vec!["value".to_string()]), + additional_properties: None, + }, + ))), + } + ); +} diff --git a/codex-rs/tools/src/lib.rs b/codex-rs/tools/src/lib.rs new file mode 100644 index 0000000000..90d3af10c8 --- /dev/null +++ b/codex-rs/tools/src/lib.rs @@ -0,0 +1,7 @@ +//! Shared tool-schema parsing primitives that can live outside `codex-core`. + +mod json_schema; + +pub use json_schema::AdditionalProperties; +pub use json_schema::JsonSchema; +pub use json_schema::parse_tool_input_schema; From e6e2999209a9a0c3c25a4d2f37aa73d0c400862e Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 17:12:45 -0700 Subject: [PATCH 023/191] permissions: remove macOS seatbelt extension profiles (#15918) ## Why `PermissionProfile` should only describe the per-command permissions we still want to grant dynamically. Keeping `MacOsSeatbeltProfileExtensions` in that surface forced extra macOS-only approval, protocol, schema, and TUI branches for a capability we no longer want to expose. ## What changed - Removed the macOS-specific permission-profile types from `codex-protocol`, the app-server v2 API, and the generated schema/TypeScript artifacts. - Deleted the core and sandboxing plumbing that threaded `MacOsSeatbeltProfileExtensions` through execution requests and seatbelt construction. - Simplified macOS seatbelt generation so it always includes the fixed read-only preferences allowlist instead of carrying a configurable profile extension. - Removed the macOS additional-permissions UI/docs/test coverage and deleted the obsolete macOS permission modules. - Tightened `request_permissions` intersection handling so explicitly empty requested read lists are preserved only when that field was actually granted, avoiding zero-grant responses being stored as active permissions. --- codex-rs/Cargo.lock | 1 - ...CommandExecutionRequestApprovalParams.json | 88 ------ .../schema/json/ServerRequest.json | 88 ------ .../codex_app_server_protocol.schemas.json | 88 ------ .../typescript/MacOsAutomationPermission.ts | 5 - .../typescript/MacOsContactsPermission.ts | 5 - .../typescript/MacOsPreferencesPermission.ts | 5 - .../schema/typescript/index.ts | 3 - .../v2/AdditionalMacOsPermissions.ts | 8 - .../v2/AdditionalPermissionProfile.ts | 3 +- .../schema/typescript/v2/index.ts | 1 - .../src/protocol/common.rs | 1 - .../app-server-protocol/src/protocol/v2.rs | 137 +-------- codex-rs/app-server/README.md | 4 +- codex-rs/app-server/src/transport/mod.rs | 3 - codex-rs/cli/src/debug_sandbox.rs | 5 +- codex-rs/core/README.md | 28 +- codex-rs/core/src/codex_tests_guardian.rs | 3 +- codex-rs/core/src/config/config_tests.rs | 4 - codex-rs/core/src/config/mod.rs | 5 - codex-rs/core/src/exec.rs | 2 - codex-rs/core/src/exec_policy_tests.rs | 27 ++ codex-rs/core/src/seatbelt.rs | 5 +- codex-rs/core/src/tools/handlers/mod.rs | 8 +- codex-rs/core/src/tools/js_repl/mod.rs | 2 - .../tools/runtimes/shell/unix_escalation.rs | 42 --- .../runtimes/shell/unix_escalation_tests.rs | 219 ------------- codex-rs/core/src/tools/sandboxing.rs | 2 - .../core/tests/suite/request_permissions.rs | 4 +- codex-rs/protocol/src/approvals.rs | 2 - codex-rs/protocol/src/models.rs | 287 +----------------- codex-rs/protocol/src/request_permissions.rs | 1 - codex-rs/sandboxing/Cargo.toml | 1 - codex-rs/sandboxing/src/lib.rs | 3 - codex-rs/sandboxing/src/macos_permissions.rs | 154 ---------- .../sandboxing/src/macos_permissions_tests.rs | 121 -------- codex-rs/sandboxing/src/manager.rs | 23 +- codex-rs/sandboxing/src/manager_tests.rs | 7 - codex-rs/sandboxing/src/policy_transforms.rs | 65 ++-- .../sandboxing/src/policy_transforms_tests.rs | 199 +++--------- codex-rs/sandboxing/src/seatbelt.rs | 32 +- .../sandboxing/src/seatbelt_permissions.rs | 190 ------------ .../src/seatbelt_permissions_tests.rs | 154 ---------- codex-rs/sandboxing/src/seatbelt_tests.rs | 154 ++-------- .../tui/src/bottom_pane/approval_overlay.rs | 86 ------ ...y_additional_permissions_macos_prompt.snap | 18 -- codex-rs/tui_app_server/src/app.rs | 2 - .../src/bottom_pane/approval_overlay.rs | 86 ------ ...y_additional_permissions_macos_prompt.snap | 18 -- ...y_additional_permissions_macos_prompt.snap | 18 -- 50 files changed, 148 insertions(+), 2269 deletions(-) delete mode 100644 codex-rs/app-server-protocol/schema/typescript/MacOsAutomationPermission.ts delete mode 100644 codex-rs/app-server-protocol/schema/typescript/MacOsContactsPermission.ts delete mode 100644 codex-rs/app-server-protocol/schema/typescript/MacOsPreferencesPermission.ts delete mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/AdditionalMacOsPermissions.ts delete mode 100644 codex-rs/sandboxing/src/macos_permissions.rs delete mode 100644 codex-rs/sandboxing/src/macos_permissions_tests.rs delete mode 100644 codex-rs/sandboxing/src/seatbelt_permissions.rs delete mode 100644 codex-rs/sandboxing/src/seatbelt_permissions_tests.rs delete mode 100644 codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap delete mode 100644 codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap delete mode 100644 codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 9f62e87eea..f494c8528b 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2505,7 +2505,6 @@ dependencies = [ "codex-network-proxy", "codex-protocol", "codex-utils-absolute-path", - "dirs", "dunce", "libc", "pretty_assertions", diff --git a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json index 66dccf15e0..617fa1f3cb 100644 --- a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json @@ -28,41 +28,6 @@ }, "type": "object" }, - "AdditionalMacOsPermissions": { - "properties": { - "accessibility": { - "type": "boolean" - }, - "automations": { - "$ref": "#/definitions/MacOsAutomationPermission" - }, - "calendar": { - "type": "boolean" - }, - "contacts": { - "$ref": "#/definitions/MacOsContactsPermission" - }, - "launchServices": { - "type": "boolean" - }, - "preferences": { - "$ref": "#/definitions/MacOsPreferencesPermission" - }, - "reminders": { - "type": "boolean" - } - }, - "required": [ - "accessibility", - "automations", - "calendar", - "contacts", - "launchServices", - "preferences", - "reminders" - ], - "type": "object" - }, "AdditionalNetworkPermissions": { "properties": { "enabled": { @@ -86,16 +51,6 @@ } ] }, - "macos": { - "anyOf": [ - { - "$ref": "#/definitions/AdditionalMacOsPermissions" - }, - { - "type": "null" - } - ] - }, "network": { "anyOf": [ { @@ -298,49 +253,6 @@ } ] }, - "MacOsAutomationPermission": { - "oneOf": [ - { - "enum": [ - "none", - "all" - ], - "type": "string" - }, - { - "additionalProperties": false, - "properties": { - "bundle_ids": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "required": [ - "bundle_ids" - ], - "title": "BundleIdsMacOsAutomationPermission", - "type": "object" - } - ] - }, - "MacOsContactsPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, - "MacOsPreferencesPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, "NetworkApprovalContext": { "properties": { "host": { diff --git a/codex-rs/app-server-protocol/schema/json/ServerRequest.json b/codex-rs/app-server-protocol/schema/json/ServerRequest.json index 6c63d36a37..7c11a4c02b 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ServerRequest.json @@ -28,41 +28,6 @@ }, "type": "object" }, - "AdditionalMacOsPermissions": { - "properties": { - "accessibility": { - "type": "boolean" - }, - "automations": { - "$ref": "#/definitions/MacOsAutomationPermission" - }, - "calendar": { - "type": "boolean" - }, - "contacts": { - "$ref": "#/definitions/MacOsContactsPermission" - }, - "launchServices": { - "type": "boolean" - }, - "preferences": { - "$ref": "#/definitions/MacOsPreferencesPermission" - }, - "reminders": { - "type": "boolean" - } - }, - "required": [ - "accessibility", - "automations", - "calendar", - "contacts", - "launchServices", - "preferences", - "reminders" - ], - "type": "object" - }, "AdditionalNetworkPermissions": { "properties": { "enabled": { @@ -86,16 +51,6 @@ } ] }, - "macos": { - "anyOf": [ - { - "$ref": "#/definitions/AdditionalMacOsPermissions" - }, - { - "type": "null" - } - ] - }, "network": { "anyOf": [ { @@ -627,49 +582,6 @@ ], "type": "object" }, - "MacOsAutomationPermission": { - "oneOf": [ - { - "enum": [ - "none", - "all" - ], - "type": "string" - }, - { - "additionalProperties": false, - "properties": { - "bundle_ids": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "required": [ - "bundle_ids" - ], - "title": "BundleIdsMacOsAutomationPermission", - "type": "object" - } - ] - }, - "MacOsContactsPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, - "MacOsPreferencesPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, "McpElicitationArrayType": { "enum": [ "array" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 28be3e5b35..f52faf5b15 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -28,41 +28,6 @@ }, "type": "object" }, - "AdditionalMacOsPermissions": { - "properties": { - "accessibility": { - "type": "boolean" - }, - "automations": { - "$ref": "#/definitions/MacOsAutomationPermission" - }, - "calendar": { - "type": "boolean" - }, - "contacts": { - "$ref": "#/definitions/MacOsContactsPermission" - }, - "launchServices": { - "type": "boolean" - }, - "preferences": { - "$ref": "#/definitions/MacOsPreferencesPermission" - }, - "reminders": { - "type": "boolean" - } - }, - "required": [ - "accessibility", - "automations", - "calendar", - "contacts", - "launchServices", - "preferences", - "reminders" - ], - "type": "object" - }, "AdditionalNetworkPermissions": { "properties": { "enabled": { @@ -86,16 +51,6 @@ } ] }, - "macos": { - "anyOf": [ - { - "$ref": "#/definitions/AdditionalMacOsPermissions" - }, - { - "type": "null" - } - ] - }, "network": { "anyOf": [ { @@ -2468,49 +2423,6 @@ "title": "JSONRPCResponse", "type": "object" }, - "MacOsAutomationPermission": { - "oneOf": [ - { - "enum": [ - "none", - "all" - ], - "type": "string" - }, - { - "additionalProperties": false, - "properties": { - "bundle_ids": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "required": [ - "bundle_ids" - ], - "title": "BundleIdsMacOsAutomationPermission", - "type": "object" - } - ] - }, - "MacOsContactsPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, - "MacOsPreferencesPermission": { - "enum": [ - "none", - "read_only", - "read_write" - ], - "type": "string" - }, "McpElicitationArrayType": { "enum": [ "array" diff --git a/codex-rs/app-server-protocol/schema/typescript/MacOsAutomationPermission.ts b/codex-rs/app-server-protocol/schema/typescript/MacOsAutomationPermission.ts deleted file mode 100644 index 31036b23ee..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/MacOsAutomationPermission.ts +++ /dev/null @@ -1,5 +0,0 @@ -// GENERATED CODE! DO NOT MODIFY BY HAND! - -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type MacOsAutomationPermission = "none" | "all" | { "bundle_ids": Array }; diff --git a/codex-rs/app-server-protocol/schema/typescript/MacOsContactsPermission.ts b/codex-rs/app-server-protocol/schema/typescript/MacOsContactsPermission.ts deleted file mode 100644 index dd6d7b59ef..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/MacOsContactsPermission.ts +++ /dev/null @@ -1,5 +0,0 @@ -// GENERATED CODE! DO NOT MODIFY BY HAND! - -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type MacOsContactsPermission = "none" | "read_only" | "read_write"; diff --git a/codex-rs/app-server-protocol/schema/typescript/MacOsPreferencesPermission.ts b/codex-rs/app-server-protocol/schema/typescript/MacOsPreferencesPermission.ts deleted file mode 100644 index 2f5234a268..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/MacOsPreferencesPermission.ts +++ /dev/null @@ -1,5 +0,0 @@ -// GENERATED CODE! DO NOT MODIFY BY HAND! - -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type MacOsPreferencesPermission = "none" | "read_only" | "read_write"; diff --git a/codex-rs/app-server-protocol/schema/typescript/index.ts b/codex-rs/app-server-protocol/schema/typescript/index.ts index 777feaa56e..09c388337f 100644 --- a/codex-rs/app-server-protocol/schema/typescript/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/index.ts @@ -41,9 +41,6 @@ export type { InputModality } from "./InputModality"; export type { LocalShellAction } from "./LocalShellAction"; export type { LocalShellExecAction } from "./LocalShellExecAction"; export type { LocalShellStatus } from "./LocalShellStatus"; -export type { MacOsAutomationPermission } from "./MacOsAutomationPermission"; -export type { MacOsContactsPermission } from "./MacOsContactsPermission"; -export type { MacOsPreferencesPermission } from "./MacOsPreferencesPermission"; export type { MessagePhase } from "./MessagePhase"; export type { ModeKind } from "./ModeKind"; export type { NetworkPolicyAmendment } from "./NetworkPolicyAmendment"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalMacOsPermissions.ts b/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalMacOsPermissions.ts deleted file mode 100644 index 177661bb0e..0000000000 --- a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalMacOsPermissions.ts +++ /dev/null @@ -1,8 +0,0 @@ -// GENERATED CODE! DO NOT MODIFY BY HAND! - -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { MacOsAutomationPermission } from "../MacOsAutomationPermission"; -import type { MacOsContactsPermission } from "../MacOsContactsPermission"; -import type { MacOsPreferencesPermission } from "../MacOsPreferencesPermission"; - -export type AdditionalMacOsPermissions = { preferences: MacOsPreferencesPermission, automations: MacOsAutomationPermission, launchServices: boolean, accessibility: boolean, calendar: boolean, reminders: boolean, contacts: MacOsContactsPermission, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalPermissionProfile.ts b/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalPermissionProfile.ts index 701ba54316..65836c119d 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalPermissionProfile.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalPermissionProfile.ts @@ -2,7 +2,6 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. import type { AdditionalFileSystemPermissions } from "./AdditionalFileSystemPermissions"; -import type { AdditionalMacOsPermissions } from "./AdditionalMacOsPermissions"; import type { AdditionalNetworkPermissions } from "./AdditionalNetworkPermissions"; -export type AdditionalPermissionProfile = { network: AdditionalNetworkPermissions | null, fileSystem: AdditionalFileSystemPermissions | null, macos: AdditionalMacOsPermissions | null, }; +export type AdditionalPermissionProfile = { network: AdditionalNetworkPermissions | null, fileSystem: AdditionalFileSystemPermissions | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index b545f7986f..73fe05eaeb 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -5,7 +5,6 @@ export type { AccountLoginCompletedNotification } from "./AccountLoginCompletedN export type { AccountRateLimitsUpdatedNotification } from "./AccountRateLimitsUpdatedNotification"; export type { AccountUpdatedNotification } from "./AccountUpdatedNotification"; export type { AdditionalFileSystemPermissions } from "./AdditionalFileSystemPermissions"; -export type { AdditionalMacOsPermissions } from "./AdditionalMacOsPermissions"; export type { AdditionalNetworkPermissions } from "./AdditionalNetworkPermissions"; export type { AdditionalPermissionProfile } from "./AdditionalPermissionProfile"; export type { AgentMessageDeltaNotification } from "./AgentMessageDeltaNotification"; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index 4492a14944..f3e56f4ffe 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -1703,7 +1703,6 @@ mod tests { read: Some(vec![absolute_path("/tmp/allowed")]), write: None, }), - macos: None, }), proposed_execpolicy_amendment: None, proposed_network_policy_amendments: None, diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 559d825aa5..2211e7aa15 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -32,10 +32,6 @@ use codex_protocol::mcp::Tool as McpTool; use codex_protocol::memory_citation::MemoryCitation as CoreMemoryCitation; use codex_protocol::memory_citation::MemoryCitationEntry as CoreMemoryCitationEntry; use codex_protocol::models::FileSystemPermissions as CoreFileSystemPermissions; -use codex_protocol::models::MacOsAutomationPermission as CoreMacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission as CoreMacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission as CoreMacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions as CoreMacOsSeatbeltProfileExtensions; use codex_protocol::models::MessagePhase; use codex_protocol::models::NetworkPermissions as CoreNetworkPermissions; use codex_protocol::models::PermissionProfile as CorePermissionProfile; @@ -1085,47 +1081,6 @@ impl From for CoreFileSystemPermissions { } } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export_to = "v2/")] -pub struct AdditionalMacOsPermissions { - pub preferences: CoreMacOsPreferencesPermission, - pub automations: CoreMacOsAutomationPermission, - pub launch_services: bool, - pub accessibility: bool, - pub calendar: bool, - pub reminders: bool, - pub contacts: CoreMacOsContactsPermission, -} - -impl From for AdditionalMacOsPermissions { - fn from(value: CoreMacOsSeatbeltProfileExtensions) -> Self { - Self { - preferences: value.macos_preferences, - automations: value.macos_automation, - launch_services: value.macos_launch_services, - accessibility: value.macos_accessibility, - calendar: value.macos_calendar, - reminders: value.macos_reminders, - contacts: value.macos_contacts, - } - } -} - -impl From for CoreMacOsSeatbeltProfileExtensions { - fn from(value: AdditionalMacOsPermissions) -> Self { - Self { - macos_preferences: value.preferences, - macos_automation: value.automations, - macos_launch_services: value.launch_services, - macos_accessibility: value.accessibility, - macos_calendar: value.calendar, - macos_reminders: value.reminders, - macos_contacts: value.contacts, - } - } -} - #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] @@ -1182,7 +1137,6 @@ impl From for CoreRequestPermissionProfile { pub struct AdditionalPermissionProfile { pub network: Option, pub file_system: Option, - pub macos: Option, } impl From for AdditionalPermissionProfile { @@ -1190,7 +1144,6 @@ impl From for AdditionalPermissionProfile { Self { network: value.network.map(AdditionalNetworkPermissions::from), file_system: value.file_system.map(AdditionalFileSystemPermissions::from), - macos: value.macos.map(AdditionalMacOsPermissions::from), } } } @@ -1200,7 +1153,6 @@ impl From for CorePermissionProfile { Self { network: value.network.map(CoreNetworkPermissions::from), file_system: value.file_system.map(CoreFileSystemPermissions::from), - macos: value.macos.map(CoreMacOsSeatbeltProfileExtensions::from), } } } @@ -1222,7 +1174,6 @@ impl From for CorePermissionProfile { Self { network: value.network.map(CoreNetworkPermissions::from), file_system: value.file_system.map(CoreFileSystemPermissions::from), - macos: None, } } } @@ -6075,8 +6026,7 @@ mod tests { "fileSystem": { "read": ["relative/path"], "write": null - }, - "macos": null + } }, "proposedExecpolicyAmendment": null, "proposedNetworkPolicyAmendments": null, @@ -6090,90 +6040,6 @@ mod tests { ); } - #[test] - fn command_execution_request_approval_accepts_macos_automation_bundle_ids_object() { - let params = serde_json::from_value::(json!({ - "threadId": "thr_123", - "turnId": "turn_123", - "itemId": "call_123", - "command": "cat file", - "cwd": "/tmp", - "commandActions": null, - "reason": null, - "networkApprovalContext": null, - "additionalPermissions": { - "network": null, - "fileSystem": null, - "macos": { - "preferences": "read_only", - "automations": { - "bundle_ids": ["com.apple.Notes"] - }, - "launchServices": false, - "accessibility": false, - "calendar": false, - "reminders": false, - "contacts": "read_only" - } - }, - "proposedExecpolicyAmendment": null, - "proposedNetworkPolicyAmendments": null, - "availableDecisions": null - })) - .expect("bundle_ids object should deserialize"); - - assert_eq!( - params - .additional_permissions - .and_then(|permissions| permissions.macos) - .map(|macos| (macos.automations, macos.launch_services, macos.contacts)), - Some(( - CoreMacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string(),]), - false, - CoreMacOsContactsPermission::ReadOnly, - )) - ); - } - - #[test] - fn command_execution_request_approval_accepts_macos_reminders_permission() { - let params = serde_json::from_value::(json!({ - "threadId": "thr_123", - "turnId": "turn_123", - "itemId": "call_123", - "command": "cat file", - "cwd": "/tmp", - "commandActions": null, - "reason": null, - "networkApprovalContext": null, - "additionalPermissions": { - "network": null, - "fileSystem": null, - "macos": { - "preferences": "read_only", - "automations": "none", - "launchServices": false, - "accessibility": false, - "calendar": false, - "reminders": true, - "contacts": "none" - } - }, - "proposedExecpolicyAmendment": null, - "proposedNetworkPolicyAmendments": null, - "availableDecisions": null - })) - .expect("reminders permission should deserialize"); - - assert_eq!( - params - .additional_permissions - .and_then(|permissions| permissions.macos) - .map(|macos| macos.reminders), - Some(true) - ); - } - #[test] fn permissions_request_approval_uses_request_permission_profile() { let read_only_path = if cfg!(windows) { @@ -6331,7 +6197,6 @@ mod tests { .expect("path must be absolute"), ]), }), - macos: None, } ); } diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 1ca4688272..cabea022a1 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -1019,7 +1019,7 @@ the client can offer session-scoped and/or persistent approval choices. ### Permission requests -The built-in `request_permissions` tool sends an `item/permissions/requestApproval` JSON-RPC request to the client with the requested permission profile. This v2 payload mirrors the standalone tool's narrower permission shape, so it can request network access and additional filesystem access but does not include the broader `macos` branch used by command-execution `additionalPermissions`. +The built-in `request_permissions` tool sends an `item/permissions/requestApproval` JSON-RPC request to the client with the requested permission profile. This v2 payload mirrors the command-execution `additionalPermissions` shape: it can request network access and additional filesystem access. ```json { @@ -1055,7 +1055,7 @@ The client responds with `result.permissions`, which should be the granted subse } ``` -Only the granted subset matters on the wire. Any permissions omitted from `result.permissions` are treated as denied, including omitted nested keys inside `result.permissions.macos`, so a sparse response like `{ "permissions": { "macos": { "accessibility": true } } }` grants only accessibility. Any permissions not present in the original request are ignored by the server. +Only the granted subset matters on the wire. Any permissions omitted from `result.permissions` are treated as denied. Any permissions not present in the original request are ignored by the server. Within the same turn, granted permissions are sticky: later shell-like tool calls can automatically reuse the granted subset without reissuing a separate permission request. diff --git a/codex-rs/app-server/src/transport/mod.rs b/codex-rs/app-server/src/transport/mod.rs index 9e02e77239..c0653b903b 100644 --- a/codex-rs/app-server/src/transport/mod.rs +++ b/codex-rs/app-server/src/transport/mod.rs @@ -801,7 +801,6 @@ mod tests { write: None, }, ), - macos: None, }, ), proposed_execpolicy_amendment: None, @@ -864,7 +863,6 @@ mod tests { write: None, }, ), - macos: None, }, ), proposed_execpolicy_amendment: None, @@ -891,7 +889,6 @@ mod tests { "read": [allowed_path], "write": null, }, - "macos": null, }) ); } diff --git a/codex-rs/cli/src/debug_sandbox.rs b/codex-rs/cli/src/debug_sandbox.rs index d519bd5f67..e5cccfc53a 100644 --- a/codex-rs/cli/src/debug_sandbox.rs +++ b/codex-rs/cli/src/debug_sandbox.rs @@ -18,7 +18,7 @@ use codex_protocol::config_types::SandboxMode; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_sandboxing::landlock::create_linux_sandbox_command_args_for_policies; #[cfg(target_os = "macos")] -use codex_sandboxing::seatbelt::create_seatbelt_command_args_for_policies_with_extensions; +use codex_sandboxing::seatbelt::create_seatbelt_command_args_for_policies; use codex_utils_cli::CliConfigOverrides; use tokio::process::Child; use tokio::process::Command as TokioCommand; @@ -246,14 +246,13 @@ async fn run_command_under_sandbox( let mut child = match sandbox_type { #[cfg(target_os = "macos")] SandboxType::Seatbelt => { - let args = create_seatbelt_command_args_for_policies_with_extensions( + let args = create_seatbelt_command_args_for_policies( command, &config.permissions.file_system_sandbox_policy, config.permissions.network_sandbox_policy, sandbox_policy_cwd.as_path(), /*enforce_managed_network*/ false, network.as_ref(), - /*extensions*/ None, ); let network_policy = config.permissions.network_sandbox_policy; spawn_debug_sandbox_child( diff --git a/codex-rs/core/README.md b/codex-rs/core/README.md index 1605d28a07..63cd3b6f36 100644 --- a/codex-rs/core/README.md +++ b/codex-rs/core/README.md @@ -17,32 +17,8 @@ pointer file), the resolved `gitdir:` target, and `.codex` read-only. Network access and filesystem read/write roots are controlled by `SandboxPolicy`. Seatbelt consumes the resolved policy and enforces it. -Seatbelt also supports macOS permission-profile extensions layered on top of -`SandboxPolicy`: - -- no extension profile provided: - keeps legacy default preferences read access (`user-preference-read`). -- extension profile provided with no `macos_preferences` grant: - does not add preferences access clauses. -- `macos_preferences = "readonly"`: - enables cfprefs read clauses and `user-preference-read`. -- `macos_preferences = "readwrite"`: - includes readonly clauses plus `user-preference-write` and cfprefs shm write - clauses. -- `macos_automation = true`: - enables broad Apple Events send permissions. -- `macos_automation = ["com.apple.Notes", ...]`: - enables Apple Events send only to listed bundle IDs. -- `macos_launch_services = true`: - enables LaunchServices lookups and open/launch operations. -- `macos_accessibility = true`: - enables `com.apple.axserver` mach lookup. -- `macos_calendar = true`: - enables `com.apple.CalendarAgent` mach lookup. -- `macos_contacts = "read_only"`: - enables Address Book read access and Contacts read services. -- `macos_contacts = "read_write"`: - includes the readonly Contacts clauses plus Address Book writes and keychain/temp helpers required for writes. +Seatbelt also keeps the legacy default preferences read access +(`user-preference-read`) needed for cfprefs-backed macOS behavior. ### Linux diff --git a/codex-rs/core/src/codex_tests_guardian.rs b/codex-rs/core/src/codex_tests_guardian.rs index 33b8d15d19..18eb3d177b 100644 --- a/codex-rs/core/src/codex_tests_guardian.rs +++ b/codex-rs/core/src/codex_tests_guardian.rs @@ -160,7 +160,6 @@ async fn guardian_allows_shell_additional_permissions_requests_past_policy_valid enabled: Some(true), }), file_system: None, - macos: None, }, "justification": params.justification.clone(), }) @@ -234,7 +233,7 @@ async fn guardian_allows_unified_exec_additional_permissions_requests_past_polic assert_eq!( output, - "missing `additional_permissions`; provide at least one of `network`, `file_system`, or `macos` when using `with_additional_permissions`" + "missing `additional_permissions`; provide at least one of `network` or `file_system` when using `with_additional_permissions`" ); } diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index a62919a444..4d6527ab80 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -4379,7 +4379,6 @@ fn test_precedence_fixture_with_o3_profile() -> std::io::Result<()> { shell_environment_policy: ShellEnvironmentPolicy::default(), windows_sandbox_mode: None, windows_sandbox_private_desktop: true, - macos_seatbelt_profile_extensions: None, }, approvals_reviewer: ApprovalsReviewer::User, enforce_residency: Constrained::allow_any(None), @@ -4522,7 +4521,6 @@ fn test_precedence_fixture_with_gpt3_profile() -> std::io::Result<()> { shell_environment_policy: ShellEnvironmentPolicy::default(), windows_sandbox_mode: None, windows_sandbox_private_desktop: true, - macos_seatbelt_profile_extensions: None, }, approvals_reviewer: ApprovalsReviewer::User, enforce_residency: Constrained::allow_any(None), @@ -4663,7 +4661,6 @@ fn test_precedence_fixture_with_zdr_profile() -> std::io::Result<()> { shell_environment_policy: ShellEnvironmentPolicy::default(), windows_sandbox_mode: None, windows_sandbox_private_desktop: true, - macos_seatbelt_profile_extensions: None, }, approvals_reviewer: ApprovalsReviewer::User, enforce_residency: Constrained::allow_any(None), @@ -4790,7 +4787,6 @@ fn test_precedence_fixture_with_gpt5_profile() -> std::io::Result<()> { shell_environment_policy: ShellEnvironmentPolicy::default(), windows_sandbox_mode: None, windows_sandbox_private_desktop: true, - macos_seatbelt_profile_extensions: None, }, approvals_reviewer: ApprovalsReviewer::User, enforce_residency: Constrained::allow_any(None), diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 8435e61325..fa21cbad84 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -78,7 +78,6 @@ use codex_protocol::config_types::WebSearchConfig; use codex_protocol::config_types::WebSearchMode; use codex_protocol::config_types::WebSearchToolConfig; use codex_protocol::config_types::WindowsSandboxLevel; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::openai_models::ModelsResponse; use codex_protocol::openai_models::ReasoningEffort; use codex_protocol::permissions::FileSystemSandboxPolicy; @@ -206,9 +205,6 @@ pub struct Permissions { pub windows_sandbox_mode: Option, /// Whether the final Windows sandboxed child should run on a private desktop. pub windows_sandbox_private_desktop: bool, - /// Optional macOS seatbelt extension profile used to extend default - /// seatbelt permissions when running under seatbelt. - pub macos_seatbelt_profile_extensions: Option, } /// Application configuration loaded from disk and merged with overrides. @@ -2556,7 +2552,6 @@ impl Config { shell_environment_policy, windows_sandbox_mode, windows_sandbox_private_desktop, - macos_seatbelt_profile_extensions: None, }, approvals_reviewer, enforce_residency: enforce_residency.value, diff --git a/codex-rs/core/src/exec.rs b/codex-rs/core/src/exec.rs index 4291b6cca3..11d8600ba1 100644 --- a/codex-rs/core/src/exec.rs +++ b/codex-rs/core/src/exec.rs @@ -292,8 +292,6 @@ pub fn build_exec_request( enforce_managed_network, network: network.as_ref(), sandbox_policy_cwd: sandbox_cwd, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: codex_linux_sandbox_exe.as_ref(), use_legacy_landlock, windows_sandbox_level, diff --git a/codex-rs/core/src/exec_policy_tests.rs b/codex-rs/core/src/exec_policy_tests.rs index f062bcc741..200f302a20 100644 --- a/codex-rs/core/src/exec_policy_tests.rs +++ b/codex-rs/core/src/exec_policy_tests.rs @@ -828,6 +828,33 @@ fn unmatched_on_request_uses_split_filesystem_policy_for_escalation_prompts() { ); } +#[tokio::test] +async fn exec_approval_requirement_prompts_for_inline_additional_permissions_under_on_request() { + assert_exec_approval_requirement_for_command( + ExecApprovalRequirementScenario { + policy_src: None, + command: vec![ + "zsh".to_string(), + "-lc".to_string(), + "touch requested-dir/requested-but-unused.txt".to_string(), + ], + approval_policy: AskForApproval::OnRequest, + sandbox_policy: SandboxPolicy::new_read_only_policy(), + file_system_sandbox_policy: read_only_file_system_sandbox_policy(), + sandbox_permissions: SandboxPermissions::WithAdditionalPermissions, + prefix_rule: None, + }, + ExecApprovalRequirement::NeedsApproval { + reason: None, + proposed_execpolicy_amendment: Some(ExecPolicyAmendment::new(vec![ + "touch".to_string(), + "requested-dir/requested-but-unused.txt".to_string(), + ])), + }, + ) + .await; +} + #[tokio::test] async fn exec_approval_requirement_rejects_unmatched_sandbox_escalation_when_granular_sandbox_is_disabled() { diff --git a/codex-rs/core/src/seatbelt.rs b/codex-rs/core/src/seatbelt.rs index f81016a990..353c9708f5 100644 --- a/codex-rs/core/src/seatbelt.rs +++ b/codex-rs/core/src/seatbelt.rs @@ -9,7 +9,7 @@ use codex_network_proxy::NetworkProxy; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_sandboxing::seatbelt::MACOS_PATH_TO_SEATBELT_EXECUTABLE; -use codex_sandboxing::seatbelt::create_seatbelt_command_args_for_policies_with_extensions; +use codex_sandboxing::seatbelt::create_seatbelt_command_args_for_policies; use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; @@ -24,14 +24,13 @@ pub async fn spawn_command_under_seatbelt( network: Option<&NetworkProxy>, mut env: HashMap, ) -> std::io::Result { - let args = create_seatbelt_command_args_for_policies_with_extensions( + let args = create_seatbelt_command_args_for_policies( command, &FileSystemSandboxPolicy::from_legacy_sandbox_policy(sandbox_policy, sandbox_policy_cwd), NetworkSandboxPolicy::from(sandbox_policy), sandbox_policy_cwd, /*enforce_managed_network*/ false, network, - /*extensions*/ None, ); let arg0 = None; env.insert(CODEX_SANDBOX_ENV_VAR.to_string(), "seatbelt".to_string()); diff --git a/codex-rs/core/src/tools/handlers/mod.rs b/codex-rs/core/src/tools/handlers/mod.rs index 3241b323b8..1afab676ab 100644 --- a/codex-rs/core/src/tools/handlers/mod.rs +++ b/codex-rs/core/src/tools/handlers/mod.rs @@ -127,18 +127,14 @@ pub(crate) fn normalize_and_validate_additional_permissions( } let Some(additional_permissions) = additional_permissions else { return Err( - "missing `additional_permissions`; provide at least one of `network`, `file_system`, or `macos` when using `with_additional_permissions`" + "missing `additional_permissions`; provide at least one of `network` or `file_system` when using `with_additional_permissions`" .to_string(), ); }; - #[cfg(not(target_os = "macos"))] - if additional_permissions.macos.is_some() { - return Err("`additional_permissions.macos` is only supported on macOS".to_string()); - } let normalized = normalize_additional_permissions(additional_permissions)?; if normalized.is_empty() { return Err( - "`additional_permissions` must include at least one requested permission in `network`, `file_system`, or `macos`" + "`additional_permissions` must include at least one requested permission in `network` or `file_system`" .to_string(), ); } diff --git a/codex-rs/core/src/tools/js_repl/mod.rs b/codex-rs/core/src/tools/js_repl/mod.rs index a9e3aa0f20..beb2930b22 100644 --- a/codex-rs/core/src/tools/js_repl/mod.rs +++ b/codex-rs/core/src/tools/js_repl/mod.rs @@ -1068,8 +1068,6 @@ impl JsReplManager { enforce_managed_network: has_managed_network_requirements, network: None, sandbox_policy_cwd: &turn.cwd, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: turn.codex_linux_sandbox_exe.as_ref(), use_legacy_landlock: turn.features.use_legacy_landlock(), windows_sandbox_level: turn.windows_sandbox_level, diff --git a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs index 8cfb551a1a..6ceef4c2c7 100644 --- a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs +++ b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs @@ -23,7 +23,6 @@ use codex_execpolicy::Policy; use codex_execpolicy::RuleMatch; use codex_features::Feature; use codex_protocol::config_types::WindowsSandboxLevel; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::NetworkSandboxPolicy; @@ -154,12 +153,6 @@ pub(super) async fn try_run_zsh_fork( windows_sandbox_level, arg0, sandbox_policy_cwd: ctx.turn.cwd.to_path_buf(), - macos_seatbelt_profile_extensions: ctx - .turn - .config - .permissions - .macos_seatbelt_profile_extensions - .clone(), codex_linux_sandbox_exe: ctx.turn.codex_linux_sandbox_exe.clone(), use_legacy_landlock: ctx.turn.features.use_legacy_landlock(), }; @@ -258,12 +251,6 @@ pub(crate) async fn prepare_unified_exec_zsh_fork( windows_sandbox_level: exec_request.windows_sandbox_level, arg0: exec_request.arg0.clone(), sandbox_policy_cwd: ctx.turn.cwd.to_path_buf(), - macos_seatbelt_profile_extensions: ctx - .turn - .config - .permissions - .macos_seatbelt_profile_extensions - .clone(), codex_linux_sandbox_exe: ctx.turn.codex_linux_sandbox_exe.clone(), use_legacy_landlock: ctx.turn.features.use_legacy_landlock(), }; @@ -359,7 +346,6 @@ impl CoreShellActionProvider { file_system_sandbox_policy: &FileSystemSandboxPolicy, network_sandbox_policy: NetworkSandboxPolicy, additional_permissions: Option<&PermissionProfile>, - macos_seatbelt_profile_extensions: Option<&MacOsSeatbeltProfileExtensions>, ) -> EscalationExecution { match sandbox_permissions { SandboxPermissions::UseDefault => EscalationExecution::TurnDefault, @@ -373,8 +359,6 @@ impl CoreShellActionProvider { sandbox_policy: sandbox_policy.clone(), file_system_sandbox_policy: file_system_sandbox_policy.clone(), network_sandbox_policy, - macos_seatbelt_profile_extensions: macos_seatbelt_profile_extensions - .cloned(), }, )) }) @@ -560,11 +544,6 @@ impl EscalationPolicy for CoreShellActionProvider { &self.file_system_sandbox_policy, self.network_sandbox_policy, self.prompt_permissions.as_ref(), - self.turn - .config - .permissions - .macos_seatbelt_profile_extensions - .as_ref(), ), }; self.process_decision( @@ -687,8 +666,6 @@ struct CoreShellCommandExecutor { windows_sandbox_level: WindowsSandboxLevel, arg0: Option, sandbox_policy_cwd: PathBuf, - #[cfg_attr(not(target_os = "macos"), allow(dead_code))] - macos_seatbelt_profile_extensions: Option, codex_linux_sandbox_exe: Option, use_legacy_landlock: bool, } @@ -701,8 +678,6 @@ struct PrepareSandboxedExecParams<'a> { file_system_sandbox_policy: &'a FileSystemSandboxPolicy, network_sandbox_policy: NetworkSandboxPolicy, additional_permissions: Option, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: Option<&'a MacOsSeatbeltProfileExtensions>, } #[async_trait::async_trait] @@ -787,17 +762,12 @@ impl ShellCommandExecutor for CoreShellCommandExecutor { file_system_sandbox_policy: &self.file_system_sandbox_policy, network_sandbox_policy: self.network_sandbox_policy, additional_permissions: None, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: self - .macos_seatbelt_profile_extensions - .as_ref(), })? } EscalationExecution::Permissions(EscalationPermissions::PermissionProfile( permission_profile, )) => { // Merge additive permissions into the existing turn/request sandbox policy. - // On macOS, additional profile extensions are unioned with the turn defaults. self.prepare_sandboxed_exec(PrepareSandboxedExecParams { command, workdir, @@ -806,10 +776,6 @@ impl ShellCommandExecutor for CoreShellCommandExecutor { file_system_sandbox_policy: &self.file_system_sandbox_policy, network_sandbox_policy: self.network_sandbox_policy, additional_permissions: Some(permission_profile), - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: self - .macos_seatbelt_profile_extensions - .as_ref(), })? } EscalationExecution::Permissions(EscalationPermissions::Permissions(permissions)) => { @@ -822,10 +788,6 @@ impl ShellCommandExecutor for CoreShellCommandExecutor { file_system_sandbox_policy: &permissions.file_system_sandbox_policy, network_sandbox_policy: permissions.network_sandbox_policy, additional_permissions: None, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: permissions - .macos_seatbelt_profile_extensions - .as_ref(), })? } }; @@ -848,8 +810,6 @@ impl CoreShellCommandExecutor { file_system_sandbox_policy, network_sandbox_policy, additional_permissions, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions, } = params; let (program, args) = command .split_first() @@ -882,8 +842,6 @@ impl CoreShellCommandExecutor { enforce_managed_network: self.network.is_some(), network: self.network.as_ref(), sandbox_policy_cwd: &self.sandbox_policy_cwd, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions, codex_linux_sandbox_exe: self.codex_linux_sandbox_exe.as_ref(), use_legacy_landlock: self.use_legacy_landlock, windows_sandbox_level: self.windows_sandbox_level, diff --git a/codex-rs/core/src/tools/runtimes/shell/unix_escalation_tests.rs b/codex-rs/core/src/tools/runtimes/shell/unix_escalation_tests.rs index 46ba0cf908..41cfce5150 100644 --- a/codex-rs/core/src/tools/runtimes/shell/unix_escalation_tests.rs +++ b/codex-rs/core/src/tools/runtimes/shell/unix_escalation_tests.rs @@ -1,6 +1,4 @@ use super::CoreShellActionProvider; -#[cfg(target_os = "macos")] -use super::CoreShellCommandExecutor; use super::InterceptedExecPolicyContext; use super::ParsedShellCommand; use super::commands_for_intercepted_exec_policy; @@ -8,12 +6,6 @@ use super::evaluate_intercepted_exec_policy; use super::extract_shell_script; use super::join_program_and_argv; use super::map_exec_result; -#[cfg(target_os = "macos")] -use crate::config::Constrained; -#[cfg(target_os = "macos")] -use crate::config::Permissions; -#[cfg(target_os = "macos")] -use crate::config::types::ShellEnvironmentPolicy; use crate::protocol::AskForApproval; use crate::protocol::GranularApprovalConfig; use crate::protocol::ReadOnlyAccess; @@ -23,11 +15,7 @@ use codex_execpolicy::Decision; use codex_execpolicy::Evaluation; use codex_execpolicy::PolicyParser; use codex_execpolicy::RuleMatch; -#[cfg(target_os = "macos")] -use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::models::FileSystemPermissions; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; use codex_protocol::permissions::FileSystemPath; @@ -36,18 +24,12 @@ use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::FileSystemSpecialPath; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_sandboxing::SandboxType; -#[cfg(target_os = "macos")] -use codex_sandboxing::seatbelt::MACOS_PATH_TO_SEATBELT_EXECUTABLE; use codex_shell_escalation::EscalationExecution; use codex_shell_escalation::EscalationPermissions; use codex_shell_escalation::ExecResult; use codex_shell_escalation::Permissions as EscalatedPermissions; -#[cfg(target_os = "macos")] -use codex_shell_escalation::ShellCommandExecutor; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; -#[cfg(target_os = "macos")] -use std::collections::HashMap; use std::path::PathBuf; use std::time::Duration; @@ -76,11 +58,6 @@ fn read_only_file_system_sandbox_policy() -> FileSystemSandboxPolicy { }]) } -#[cfg(target_os = "macos")] -fn unrestricted_file_system_sandbox_policy() -> FileSystemSandboxPolicy { - FileSystemSandboxPolicy::unrestricted() -} - #[test] fn execve_prompt_rejection_keeps_prefix_rules_on_rules_flag() { assert_eq!( @@ -294,10 +271,6 @@ fn shell_request_escalation_execution_is_explicit() { }, ]); let network_sandbox_policy = NetworkSandboxPolicy::Restricted; - let macos_seatbelt_profile_extensions = MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - ..Default::default() - }; assert_eq!( CoreShellActionProvider::shell_request_escalation_execution( @@ -306,7 +279,6 @@ fn shell_request_escalation_execution_is_explicit() { &file_system_sandbox_policy, network_sandbox_policy, None, - Some(&macos_seatbelt_profile_extensions), ), EscalationExecution::TurnDefault, ); @@ -317,7 +289,6 @@ fn shell_request_escalation_execution_is_explicit() { &file_system_sandbox_policy, network_sandbox_policy, None, - Some(&macos_seatbelt_profile_extensions), ), EscalationExecution::Unsandboxed, ); @@ -328,14 +299,12 @@ fn shell_request_escalation_execution_is_explicit() { &file_system_sandbox_policy, network_sandbox_policy, Some(&requested_permissions), - Some(&macos_seatbelt_profile_extensions), ), EscalationExecution::Permissions(EscalationPermissions::Permissions( EscalatedPermissions { sandbox_policy, file_system_sandbox_policy, network_sandbox_policy, - macos_seatbelt_profile_extensions: Some(macos_seatbelt_profile_extensions), }, )), ); @@ -558,191 +527,3 @@ host_executable(name = "git", paths = ["{allowed_git_literal}"]) evaluation.decision )); } - -#[cfg(target_os = "macos")] -#[tokio::test] -async fn prepare_escalated_exec_turn_default_preserves_macos_seatbelt_extensions() { - let cwd = AbsolutePathBuf::from_absolute_path(std::env::temp_dir()).unwrap(); - let executor = CoreShellCommandExecutor { - command: vec!["echo".to_string(), "ok".to_string()], - cwd: cwd.to_path_buf(), - env: HashMap::new(), - network: None, - sandbox: SandboxType::None, - sandbox_policy: SandboxPolicy::new_read_only_policy(), - file_system_sandbox_policy: read_only_file_system_sandbox_policy(), - network_sandbox_policy: NetworkSandboxPolicy::Restricted, - windows_sandbox_level: WindowsSandboxLevel::Disabled, - arg0: None, - sandbox_policy_cwd: cwd.to_path_buf(), - macos_seatbelt_profile_extensions: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - ..Default::default() - }), - codex_linux_sandbox_exe: None, - use_legacy_landlock: false, - }; - - let prepared = executor - .prepare_escalated_exec( - &AbsolutePathBuf::from_absolute_path("/bin/echo").unwrap(), - &["echo".to_string(), "ok".to_string()], - &cwd, - HashMap::new(), - EscalationExecution::TurnDefault, - ) - .await - .unwrap(); - - assert_eq!( - prepared.command.first().map(String::as_str), - Some(MACOS_PATH_TO_SEATBELT_EXECUTABLE) - ); - assert_eq!(prepared.command.get(1).map(String::as_str), Some("-p")); - assert!( - prepared - .command - .get(2) - .is_some_and(|policy| policy.contains("(allow user-preference-write)")), - "expected seatbelt policy to include macOS extension profile: {:?}", - prepared.command - ); -} - -#[cfg(target_os = "macos")] -#[tokio::test] -async fn prepare_escalated_exec_permissions_preserve_macos_seatbelt_extensions() { - let cwd = AbsolutePathBuf::from_absolute_path(std::env::temp_dir()).unwrap(); - let executor = CoreShellCommandExecutor { - command: vec!["echo".to_string(), "ok".to_string()], - cwd: cwd.to_path_buf(), - env: HashMap::new(), - network: None, - sandbox: SandboxType::None, - sandbox_policy: SandboxPolicy::DangerFullAccess, - file_system_sandbox_policy: unrestricted_file_system_sandbox_policy(), - network_sandbox_policy: NetworkSandboxPolicy::Enabled, - windows_sandbox_level: WindowsSandboxLevel::Disabled, - arg0: None, - sandbox_policy_cwd: cwd.to_path_buf(), - macos_seatbelt_profile_extensions: None, - codex_linux_sandbox_exe: None, - use_legacy_landlock: false, - }; - - let permissions = Permissions { - approval_policy: Constrained::allow_any(AskForApproval::Never), - sandbox_policy: Constrained::allow_any(SandboxPolicy::new_read_only_policy()), - file_system_sandbox_policy: read_only_file_system_sandbox_policy(), - network_sandbox_policy: codex_protocol::permissions::NetworkSandboxPolicy::Restricted, - network: None, - allow_login_shell: true, - shell_environment_policy: ShellEnvironmentPolicy::default(), - windows_sandbox_mode: None, - windows_sandbox_private_desktop: false, - macos_seatbelt_profile_extensions: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - ..Default::default() - }), - }; - - let prepared = executor - .prepare_escalated_exec( - &AbsolutePathBuf::from_absolute_path("/bin/echo").unwrap(), - &["echo".to_string(), "ok".to_string()], - &cwd, - HashMap::new(), - EscalationExecution::Permissions(EscalationPermissions::Permissions( - EscalatedPermissions { - sandbox_policy: permissions.sandbox_policy.get().clone(), - file_system_sandbox_policy: permissions.file_system_sandbox_policy.clone(), - network_sandbox_policy: permissions.network_sandbox_policy, - macos_seatbelt_profile_extensions: permissions - .macos_seatbelt_profile_extensions - .clone(), - }, - )), - ) - .await - .unwrap(); - - assert_eq!( - prepared.command.first().map(String::as_str), - Some(MACOS_PATH_TO_SEATBELT_EXECUTABLE) - ); - assert_eq!(prepared.command.get(1).map(String::as_str), Some("-p")); - assert!( - prepared - .command - .get(2) - .is_some_and(|policy| policy.contains("(allow user-preference-write)")), - "expected seatbelt policy to include macOS extension profile: {:?}", - prepared.command - ); -} - -#[cfg(target_os = "macos")] -#[tokio::test] -async fn prepare_escalated_exec_permission_profile_unions_turn_and_requested_macos_extensions() { - let cwd = AbsolutePathBuf::from_absolute_path(std::env::temp_dir()).unwrap(); - let sandbox_policy = SandboxPolicy::new_read_only_policy(); - let executor = CoreShellCommandExecutor { - command: vec!["echo".to_string(), "ok".to_string()], - cwd: cwd.to_path_buf(), - env: HashMap::new(), - network: None, - sandbox: SandboxType::None, - sandbox_policy: sandbox_policy.clone(), - file_system_sandbox_policy: read_only_file_system_sandbox_policy(), - network_sandbox_policy: NetworkSandboxPolicy::from(&sandbox_policy), - windows_sandbox_level: WindowsSandboxLevel::Disabled, - arg0: None, - sandbox_policy_cwd: cwd.to_path_buf(), - macos_seatbelt_profile_extensions: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - ..Default::default() - }), - codex_linux_sandbox_exe: None, - use_legacy_landlock: false, - }; - - let prepared = executor - .prepare_escalated_exec( - &AbsolutePathBuf::from_absolute_path("/bin/echo").unwrap(), - &["echo".to_string(), "ok".to_string()], - &cwd, - HashMap::new(), - EscalationExecution::Permissions(EscalationPermissions::PermissionProfile( - PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_calendar: true, - macos_reminders: false, - ..Default::default() - }), - ..Default::default() - }, - )), - ) - .await - .unwrap(); - - let policy = prepared - .command - .get(2) - .expect("seatbelt policy should be present"); - assert_eq!( - prepared.command.first().map(String::as_str), - Some(MACOS_PATH_TO_SEATBELT_EXECUTABLE) - ); - assert_eq!(prepared.command.get(1).map(String::as_str), Some("-p")); - assert!( - policy.contains("(allow user-preference-read)"), - "expected turn macOS seatbelt extensions to be preserved: {:?}", - prepared.command - ); - assert!( - policy.contains("(allow mach-lookup (global-name \"com.apple.CalendarAgent\"))"), - "expected requested macOS seatbelt extensions to be included: {:?}", - prepared.command - ); -} diff --git a/codex-rs/core/src/tools/sandboxing.rs b/codex-rs/core/src/tools/sandboxing.rs index 6cad5a3293..5b7e950192 100644 --- a/codex-rs/core/src/tools/sandboxing.rs +++ b/codex-rs/core/src/tools/sandboxing.rs @@ -348,8 +348,6 @@ impl<'a> SandboxAttempt<'a> { enforce_managed_network: self.enforce_managed_network, network, sandbox_policy_cwd: self.sandbox_cwd, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: self.codex_linux_sandbox_exe, use_legacy_landlock: self.use_legacy_landlock, windows_sandbox_level: self.windows_sandbox_level, diff --git a/codex-rs/core/tests/suite/request_permissions.rs b/codex-rs/core/tests/suite/request_permissions.rs index 7c16599bf0..2cfd1cf6f7 100644 --- a/codex-rs/core/tests/suite/request_permissions.rs +++ b/codex-rs/core/tests/suite/request_permissions.rs @@ -4,6 +4,7 @@ use anyhow::Result; use codex_core::config::Constrained; use codex_core::sandboxing::SandboxPermissions; use codex_features::Feature; +use codex_protocol::config_types::ApprovalsReviewer; use codex_protocol::models::FileSystemPermissions; use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::AskForApproval; @@ -193,7 +194,7 @@ async fn submit_turn( final_output_json_schema: None, cwd: test.cwd.path().to_path_buf(), approval_policy, - approvals_reviewer: None, + approvals_reviewer: Some(ApprovalsReviewer::User), sandbox_policy, model: session_model, effort: None, @@ -1579,7 +1580,6 @@ async fn partial_request_permissions_grants_do_not_preapprove_new_permissions() .clone() .unwrap_or_else(|| panic!("expected merged additional permissions")); assert_eq!(approval_permissions.network, None); - assert_eq!(approval_permissions.macos, None); let approval_file_system = approval_permissions .file_system diff --git a/codex-rs/protocol/src/approvals.rs b/codex-rs/protocol/src/approvals.rs index 95ccdd624c..cd2b3e6952 100644 --- a/codex-rs/protocol/src/approvals.rs +++ b/codex-rs/protocol/src/approvals.rs @@ -1,5 +1,4 @@ use crate::mcp::RequestId; -use crate::models::MacOsSeatbeltProfileExtensions; use crate::models::PermissionProfile; use crate::parse_command::ParsedCommand; use crate::permissions::FileSystemSandboxPolicy; @@ -20,7 +19,6 @@ pub struct Permissions { pub sandbox_policy: SandboxPolicy, pub file_system_sandbox_policy: FileSystemSandboxPolicy, pub network_sandbox_policy: NetworkSandboxPolicy, - pub macos_seatbelt_profile_extensions: Option, } #[allow(clippy::large_enum_variant)] diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index 6fa8162bf7..4717907177 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -88,138 +88,15 @@ impl NetworkPermissions { } } -#[derive( - Debug, - Clone, - PartialEq, - Eq, - PartialOrd, - Ord, - Default, - Hash, - Serialize, - Deserialize, - JsonSchema, - TS, -)] -#[serde(rename_all = "snake_case")] -pub enum MacOsPreferencesPermission { - None, - // IMPORTANT: ReadOnly needs to be the default because it's the - // security-sensitive default and keeps cf prefs working. - #[default] - ReadOnly, - ReadWrite, -} - -#[derive( - Debug, - Clone, - PartialEq, - Eq, - PartialOrd, - Ord, - Default, - Hash, - Serialize, - Deserialize, - JsonSchema, - TS, -)] -#[serde(rename_all = "snake_case")] -pub enum MacOsContactsPermission { - #[default] - None, - ReadOnly, - ReadWrite, -} - -#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize, JsonSchema, TS)] -#[serde(rename_all = "snake_case", try_from = "MacOsAutomationPermissionDe")] -pub enum MacOsAutomationPermission { - #[default] - None, - All, - BundleIds(Vec), -} - -#[derive(Debug, Deserialize)] -#[serde(untagged)] -enum MacOsAutomationPermissionDe { - Mode(String), - BundleIds(Vec), - BundleIdsObject { bundle_ids: Vec }, -} - -impl TryFrom for MacOsAutomationPermission { - type Error = String; - - /// Accepts one of: - /// - `"none"` or `"all"` - /// - a plain list of bundle IDs, e.g. `["com.apple.Notes"]` - /// - an object with bundle IDs, e.g. `{"bundle_ids": ["com.apple.Notes"]}` - fn try_from(value: MacOsAutomationPermissionDe) -> Result { - let permission = match value { - MacOsAutomationPermissionDe::Mode(value) => { - let normalized = value.trim().to_ascii_lowercase(); - if normalized == "all" { - MacOsAutomationPermission::All - } else if normalized == "none" { - MacOsAutomationPermission::None - } else { - return Err(format!( - "invalid macOS automation permission: {value}; expected none, all, or bundle ids" - )); - } - } - MacOsAutomationPermissionDe::BundleIds(bundle_ids) - | MacOsAutomationPermissionDe::BundleIdsObject { bundle_ids } => { - let bundle_ids = bundle_ids - .into_iter() - .map(|bundle_id| bundle_id.trim().to_string()) - .filter(|bundle_id| !bundle_id.is_empty()) - .collect::>(); - if bundle_ids.is_empty() { - MacOsAutomationPermission::None - } else { - MacOsAutomationPermission::BundleIds(bundle_ids) - } - } - }; - - Ok(permission) - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize, JsonSchema, TS)] -#[serde(default)] -pub struct MacOsSeatbeltProfileExtensions { - #[serde(alias = "preferences")] - pub macos_preferences: MacOsPreferencesPermission, - #[serde(alias = "automations")] - pub macos_automation: MacOsAutomationPermission, - #[serde(alias = "launch_services")] - pub macos_launch_services: bool, - #[serde(alias = "accessibility")] - pub macos_accessibility: bool, - #[serde(alias = "calendar")] - pub macos_calendar: bool, - #[serde(alias = "reminders")] - pub macos_reminders: bool, - #[serde(alias = "contacts")] - pub macos_contacts: MacOsContactsPermission, -} - #[derive(Debug, Clone, Default, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, TS)] pub struct PermissionProfile { pub network: Option, pub file_system: Option, - pub macos: Option, } impl PermissionProfile { pub fn is_empty(&self) -> bool { - self.network.is_none() && self.file_system.is_none() && self.macos.is_none() + self.network.is_none() && self.file_system.is_none() } } @@ -726,7 +603,7 @@ fn granular_prompt_intro_text() -> &'static str { } fn request_permissions_tool_prompt_section() -> &'static str { - "# request_permissions Tool\n\nThe built-in `request_permissions` tool is available in this session. Invoke it when you need to request additional `network`, `file_system`, or `macos` permissions before later shell-like commands need them. Request only the specific permissions required for the task." + "# request_permissions Tool\n\nThe built-in `request_permissions` tool is available in this session. Invoke it when you need to request additional `network` or `file_system` permissions before later shell-like commands need them. Request only the specific permissions required for the task." } fn granular_instructions( @@ -1647,170 +1524,10 @@ mod tests { let permission_profile = PermissionProfile { network: Some(NetworkPermissions { enabled: None }), file_system: None, - macos: None, }; assert_eq!(permission_profile.is_empty(), false); } - #[test] - fn macos_preferences_permission_deserializes_read_write() { - let permission = serde_json::from_str::("\"read_write\"") - .expect("deserialize macos preferences permission"); - assert_eq!(permission, MacOsPreferencesPermission::ReadWrite); - } - - #[test] - fn macos_preferences_permission_order_matches_permissiveness() { - assert!(MacOsPreferencesPermission::None < MacOsPreferencesPermission::ReadOnly); - assert!(MacOsPreferencesPermission::ReadOnly < MacOsPreferencesPermission::ReadWrite); - } - - #[test] - fn macos_contacts_permission_order_matches_permissiveness() { - assert!(MacOsContactsPermission::None < MacOsContactsPermission::ReadOnly); - assert!(MacOsContactsPermission::ReadOnly < MacOsContactsPermission::ReadWrite); - } - - #[test] - fn permission_profile_deserializes_macos_seatbelt_profile_extensions() { - let permission_profile = serde_json::from_value::(serde_json::json!({ - "network": null, - "file_system": null, - "macos": { - "macos_preferences": "read_write", - "macos_automation": ["com.apple.Notes"], - "macos_launch_services": true, - "macos_accessibility": true, - "macos_calendar": true - } - })) - .expect("deserialize permission profile"); - - assert_eq!( - permission_profile, - PermissionProfile { - network: None, - file_system: None, - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - } - ); - } - - #[test] - fn permission_profile_deserializes_macos_reminders_permission() { - let permission_profile = serde_json::from_value::(serde_json::json!({ - "macos": { - "macos_reminders": true - } - })) - .expect("deserialize reminders permission profile"); - - assert_eq!( - permission_profile, - PermissionProfile { - network: None, - file_system: None, - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - macos_automation: MacOsAutomationPermission::None, - macos_launch_services: false, - macos_accessibility: false, - macos_calendar: false, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::None, - }), - } - ); - } - - #[test] - fn macos_seatbelt_profile_extensions_deserializes_missing_fields_to_defaults() { - let permissions = - serde_json::from_value::(serde_json::json!({ - "macos_automation": ["com.apple.Notes"] - })) - .expect("deserialize macos permissions"); - - assert_eq!( - permissions, - MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: false, - macos_calendar: false, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - } - ); - } - - #[test] - fn macos_seatbelt_profile_extensions_deserializes_tool_schema_aliases() { - let permissions = - serde_json::from_value::(serde_json::json!({ - "preferences": "read_write", - "automations": ["com.apple.Notes"], - "launch_services": true, - "accessibility": true, - "calendar": true, - "reminders": true, - "contacts": "read_only" - })) - .expect("deserialize macos permissions"); - - assert_eq!( - permissions, - MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::ReadOnly, - } - ); - } - - #[test] - fn macos_automation_permission_deserializes_all_and_none() { - let all = serde_json::from_str::("\"all\"") - .expect("deserialize all automation permission"); - let none = serde_json::from_str::("\"none\"") - .expect("deserialize none automation permission"); - - assert_eq!(all, MacOsAutomationPermission::All); - assert_eq!(none, MacOsAutomationPermission::None); - } - - #[test] - fn macos_automation_permission_deserializes_bundle_ids_object() { - let permission = serde_json::from_value::(serde_json::json!({ - "bundle_ids": ["com.apple.Notes"] - })) - .expect("deserialize bundle_ids object automation permission"); - - assert_eq!( - permission, - MacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string(),]) - ); - } - #[test] fn convert_mcp_content_to_items_builds_data_urls_when_missing_prefix() { let contents = vec![serde_json::json!({ diff --git a/codex-rs/protocol/src/request_permissions.rs b/codex-rs/protocol/src/request_permissions.rs index db5396c5e4..400fde4e20 100644 --- a/codex-rs/protocol/src/request_permissions.rs +++ b/codex-rs/protocol/src/request_permissions.rs @@ -32,7 +32,6 @@ impl From for PermissionProfile { Self { network: value.network, file_system: value.file_system, - macos: None, } } } diff --git a/codex-rs/sandboxing/Cargo.toml b/codex-rs/sandboxing/Cargo.toml index cd7558ec90..274e5bce22 100644 --- a/codex-rs/sandboxing/Cargo.toml +++ b/codex-rs/sandboxing/Cargo.toml @@ -15,7 +15,6 @@ workspace = true codex-network-proxy = { workspace = true } codex-protocol = { workspace = true } codex-utils-absolute-path = { workspace = true } -dirs = { workspace = true } dunce = { workspace = true } libc = { workspace = true } serde_json = { workspace = true } diff --git a/codex-rs/sandboxing/src/lib.rs b/codex-rs/sandboxing/src/lib.rs index eb6b4e33b8..75bddad5e9 100644 --- a/codex-rs/sandboxing/src/lib.rs +++ b/codex-rs/sandboxing/src/lib.rs @@ -1,13 +1,10 @@ #[cfg(target_os = "linux")] mod bwrap; pub mod landlock; -pub mod macos_permissions; mod manager; pub mod policy_transforms; #[cfg(target_os = "macos")] pub mod seatbelt; -#[cfg(target_os = "macos")] -mod seatbelt_permissions; #[cfg(target_os = "linux")] pub use bwrap::find_system_bwrap_in_path; diff --git a/codex-rs/sandboxing/src/macos_permissions.rs b/codex-rs/sandboxing/src/macos_permissions.rs deleted file mode 100644 index 22314937ef..0000000000 --- a/codex-rs/sandboxing/src/macos_permissions.rs +++ /dev/null @@ -1,154 +0,0 @@ -use std::collections::BTreeSet; - -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; - -/// Merges macOS seatbelt profile extensions by taking the permissive union of -/// each permission field. -pub fn merge_macos_seatbelt_profile_extensions( - base: Option<&MacOsSeatbeltProfileExtensions>, - permissions: Option<&MacOsSeatbeltProfileExtensions>, -) -> Option { - let Some(permissions) = permissions else { - return base.cloned(); - }; - - match base { - Some(base) => Some(MacOsSeatbeltProfileExtensions { - macos_preferences: union_macos_preferences_permission( - &base.macos_preferences, - &permissions.macos_preferences, - ), - macos_automation: union_macos_automation_permission( - &base.macos_automation, - &permissions.macos_automation, - ), - macos_launch_services: base.macos_launch_services || permissions.macos_launch_services, - macos_accessibility: base.macos_accessibility || permissions.macos_accessibility, - macos_calendar: base.macos_calendar || permissions.macos_calendar, - macos_reminders: base.macos_reminders || permissions.macos_reminders, - macos_contacts: union_macos_contacts_permission( - &base.macos_contacts, - &permissions.macos_contacts, - ), - }), - None => Some(permissions.clone()), - } -} - -pub fn intersect_macos_seatbelt_profile_extensions( - requested: Option, - granted: Option, -) -> Option { - match (requested, granted) { - (Some(requested), Some(granted)) => { - let macos_automation = intersect_macos_automation_permission( - &requested.macos_automation, - &granted.macos_automation, - ); - - Some(MacOsSeatbeltProfileExtensions { - macos_preferences: requested.macos_preferences.min(granted.macos_preferences), - macos_automation, - macos_launch_services: requested.macos_launch_services - && granted.macos_launch_services, - macos_accessibility: requested.macos_accessibility && granted.macos_accessibility, - macos_calendar: requested.macos_calendar && granted.macos_calendar, - macos_reminders: requested.macos_reminders && granted.macos_reminders, - macos_contacts: requested.macos_contacts.min(granted.macos_contacts), - }) - } - _ => None, - } -} - -/// Unions two preferences permissions by keeping the more permissive one. -/// -/// The larger rank wins: `None < ReadOnly < ReadWrite`. When both sides have -/// the same rank, this keeps `base`. -fn union_macos_preferences_permission( - base: &MacOsPreferencesPermission, - requested: &MacOsPreferencesPermission, -) -> MacOsPreferencesPermission { - if base < requested { - requested.clone() - } else { - base.clone() - } -} - -fn union_macos_contacts_permission( - base: &MacOsContactsPermission, - requested: &MacOsContactsPermission, -) -> MacOsContactsPermission { - if base < requested { - requested.clone() - } else { - base.clone() - } -} - -/// Unions two automation permissions by keeping the more permissive result. -/// -/// `All` wins over everything, `None` yields to the other side, and two bundle -/// ID allowlists are unioned together. -fn union_macos_automation_permission( - base: &MacOsAutomationPermission, - requested: &MacOsAutomationPermission, -) -> MacOsAutomationPermission { - match (base, requested) { - (MacOsAutomationPermission::All, _) | (_, MacOsAutomationPermission::All) => { - MacOsAutomationPermission::All - } - (MacOsAutomationPermission::None, _) => requested.clone(), - (_, MacOsAutomationPermission::None) => base.clone(), - ( - MacOsAutomationPermission::BundleIds(base_bundle_ids), - MacOsAutomationPermission::BundleIds(requested_bundle_ids), - ) => MacOsAutomationPermission::BundleIds( - base_bundle_ids - .iter() - .chain(requested_bundle_ids.iter()) - .cloned() - .collect::>() - .into_iter() - .collect(), - ), - } -} - -fn intersect_macos_automation_permission( - requested: &MacOsAutomationPermission, - granted: &MacOsAutomationPermission, -) -> MacOsAutomationPermission { - match (requested, granted) { - (_, MacOsAutomationPermission::None) | (MacOsAutomationPermission::None, _) => { - MacOsAutomationPermission::None - } - (MacOsAutomationPermission::All, granted) => granted.clone(), - (MacOsAutomationPermission::BundleIds(requested), MacOsAutomationPermission::All) => { - MacOsAutomationPermission::BundleIds(requested.clone()) - } - ( - MacOsAutomationPermission::BundleIds(requested), - MacOsAutomationPermission::BundleIds(granted), - ) => { - let bundle_ids = requested - .iter() - .filter(|bundle_id| granted.contains(bundle_id)) - .cloned() - .collect::>(); - if bundle_ids.is_empty() { - MacOsAutomationPermission::None - } else { - MacOsAutomationPermission::BundleIds(bundle_ids) - } - } - } -} - -#[cfg(all(test, target_os = "macos"))] -#[path = "macos_permissions_tests.rs"] -mod tests; diff --git a/codex-rs/sandboxing/src/macos_permissions_tests.rs b/codex-rs/sandboxing/src/macos_permissions_tests.rs deleted file mode 100644 index 97a2a2c753..0000000000 --- a/codex-rs/sandboxing/src/macos_permissions_tests.rs +++ /dev/null @@ -1,121 +0,0 @@ -use super::intersect_macos_automation_permission; -use super::intersect_macos_seatbelt_profile_extensions; -use super::merge_macos_seatbelt_profile_extensions; -use super::union_macos_automation_permission; -use super::union_macos_contacts_permission; -use super::union_macos_preferences_permission; -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; -use pretty_assertions::assert_eq; - -#[test] -fn merge_extensions_widens_permissions() { - let base = MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: false, - macos_calendar: false, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::ReadOnly, - }; - let requested = MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - "com.apple.Calendar".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::ReadWrite, - }; - - let merged = - merge_macos_seatbelt_profile_extensions(Some(&base), Some(&requested)).expect("merge"); - - assert_eq!( - merged, - MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::ReadWrite, - } - ); -} - -#[test] -fn union_macos_preferences_permission_does_not_downgrade() { - let base = MacOsPreferencesPermission::ReadWrite; - let requested = MacOsPreferencesPermission::ReadOnly; - - let merged = union_macos_preferences_permission(&base, &requested); - - assert_eq!(merged, MacOsPreferencesPermission::ReadWrite); -} - -#[test] -fn union_macos_automation_permission_all_is_dominant() { - let base = MacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string()]); - let requested = MacOsAutomationPermission::All; - - let merged = union_macos_automation_permission(&base, &requested); - - assert_eq!(merged, MacOsAutomationPermission::All); -} - -#[test] -fn intersect_macos_automation_permission_keeps_common_bundle_ids() { - let requested = MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - "com.apple.Calendar".to_string(), - ]); - let granted = MacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string()]); - - let intersected = intersect_macos_automation_permission(&requested, &granted); - - assert_eq!( - intersected, - MacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string()]) - ); -} - -#[test] -fn intersect_macos_seatbelt_profile_extensions_preserves_default_grant() { - let requested = MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec!["com.apple.Notes".to_string()]), - macos_launch_services: false, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }; - let granted = MacOsSeatbeltProfileExtensions::default(); - - let intersected = intersect_macos_seatbelt_profile_extensions(Some(requested), Some(granted)); - - assert_eq!(intersected, Some(MacOsSeatbeltProfileExtensions::default())); -} - -#[test] -fn union_macos_contacts_permission_does_not_downgrade() { - let base = MacOsContactsPermission::ReadWrite; - let requested = MacOsContactsPermission::ReadOnly; - - let merged = union_macos_contacts_permission(&base, &requested); - - assert_eq!(merged, MacOsContactsPermission::ReadWrite); -} diff --git a/codex-rs/sandboxing/src/manager.rs b/codex-rs/sandboxing/src/manager.rs index 3f121ba413..c89e7cd9be 100644 --- a/codex-rs/sandboxing/src/manager.rs +++ b/codex-rs/sandboxing/src/manager.rs @@ -8,11 +8,9 @@ use crate::policy_transforms::should_require_platform_sandbox; #[cfg(target_os = "macos")] use crate::seatbelt::MACOS_PATH_TO_SEATBELT_EXECUTABLE; #[cfg(target_os = "macos")] -use crate::seatbelt::create_seatbelt_command_args_for_policies_with_extensions; +use crate::seatbelt::create_seatbelt_command_args_for_policies; use codex_network_proxy::NetworkProxy; use codex_protocol::config_types::WindowsSandboxLevel; -#[cfg(target_os = "macos")] -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::NetworkSandboxPolicy; @@ -102,8 +100,6 @@ pub struct SandboxTransformRequest<'a> { // to make shared ownership explicit across runtime/sandbox plumbing. pub network: Option<&'a NetworkProxy>, pub sandbox_policy_cwd: &'a Path, - #[cfg(target_os = "macos")] - pub macos_seatbelt_profile_extensions: Option<&'a MacOsSeatbeltProfileExtensions>, pub codex_linux_sandbox_exe: Option<&'a PathBuf>, pub use_legacy_landlock: bool, pub windows_sandbox_level: WindowsSandboxLevel, @@ -181,27 +177,15 @@ impl SandboxManager { enforce_managed_network, network, sandbox_policy_cwd, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions, codex_linux_sandbox_exe, use_legacy_landlock, windows_sandbox_level, windows_sandbox_private_desktop, } = request; - #[cfg(not(target_os = "macos"))] - let macos_seatbelt_profile_extensions = None; let additional_permissions = command.additional_permissions.take(); let EffectiveSandboxPermissions { sandbox_policy: effective_policy, - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: effective_macos_seatbelt_profile_extensions, - #[cfg(not(target_os = "macos"))] - macos_seatbelt_profile_extensions: _, - } = EffectiveSandboxPermissions::new( - policy, - macos_seatbelt_profile_extensions, - additional_permissions.as_ref(), - ); + } = EffectiveSandboxPermissions::new(policy, additional_permissions.as_ref()); let effective_file_system_policy = effective_file_system_sandbox_policy( file_system_policy, additional_permissions.as_ref(), @@ -216,14 +200,13 @@ impl SandboxManager { SandboxType::None => (os_argv_to_strings(argv), None), #[cfg(target_os = "macos")] SandboxType::MacosSeatbelt => { - let mut args = create_seatbelt_command_args_for_policies_with_extensions( + let mut args = create_seatbelt_command_args_for_policies( os_argv_to_strings(argv), &effective_file_system_policy, effective_network_policy, sandbox_policy_cwd, enforce_managed_network, network, - effective_macos_seatbelt_profile_extensions.as_ref(), ); let mut full_command = Vec::with_capacity(1 + args.len()); full_command.push(MACOS_PATH_TO_SEATBELT_EXECUTABLE.to_string()); diff --git a/codex-rs/sandboxing/src/manager_tests.rs b/codex-rs/sandboxing/src/manager_tests.rs index 67911bff6a..a32c9e586a 100644 --- a/codex-rs/sandboxing/src/manager_tests.rs +++ b/codex-rs/sandboxing/src/manager_tests.rs @@ -91,8 +91,6 @@ fn transform_preserves_unrestricted_file_system_policy_for_restricted_network() enforce_managed_network: false, network: None, sandbox_policy_cwd: cwd.as_path(), - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: None, use_legacy_landlock: false, windows_sandbox_level: WindowsSandboxLevel::Disabled, @@ -134,7 +132,6 @@ fn transform_additional_permissions_enable_network_for_external_sandbox() { read: Some(vec![path]), write: Some(Vec::new()), }), - ..Default::default() }), }, policy: &SandboxPolicy::ExternalSandbox { @@ -146,8 +143,6 @@ fn transform_additional_permissions_enable_network_for_external_sandbox() { enforce_managed_network: false, network: None, sandbox_policy_cwd: cwd.as_path(), - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: None, use_legacy_landlock: false, windows_sandbox_level: WindowsSandboxLevel::Disabled, @@ -216,8 +211,6 @@ fn transform_additional_permissions_preserves_denied_entries() { enforce_managed_network: false, network: None, sandbox_policy_cwd: cwd.as_path(), - #[cfg(target_os = "macos")] - macos_seatbelt_profile_extensions: None, codex_linux_sandbox_exe: None, use_legacy_landlock: false, windows_sandbox_level: WindowsSandboxLevel::Disabled, diff --git a/codex-rs/sandboxing/src/policy_transforms.rs b/codex-rs/sandboxing/src/policy_transforms.rs index 045c7e3f90..25c79fcc9e 100644 --- a/codex-rs/sandboxing/src/policy_transforms.rs +++ b/codex-rs/sandboxing/src/policy_transforms.rs @@ -1,7 +1,4 @@ -use crate::macos_permissions::intersect_macos_seatbelt_profile_extensions; -use crate::macos_permissions::merge_macos_seatbelt_profile_extensions; use codex_protocol::models::FileSystemPermissions; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::NetworkPermissions; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; @@ -20,28 +17,21 @@ use std::collections::HashSet; #[derive(Debug, Clone, PartialEq, Eq)] pub struct EffectiveSandboxPermissions { pub sandbox_policy: SandboxPolicy, - pub macos_seatbelt_profile_extensions: Option, } impl EffectiveSandboxPermissions { pub fn new( sandbox_policy: &SandboxPolicy, - macos_seatbelt_profile_extensions: Option<&MacOsSeatbeltProfileExtensions>, additional_permissions: Option<&PermissionProfile>, ) -> Self { let Some(additional_permissions) = additional_permissions else { return Self { sandbox_policy: sandbox_policy.clone(), - macos_seatbelt_profile_extensions: macos_seatbelt_profile_extensions.cloned(), }; }; Self { sandbox_policy: effective_sandbox_policy(sandbox_policy, Some(additional_permissions)), - macos_seatbelt_profile_extensions: merge_macos_seatbelt_profile_extensions( - macos_seatbelt_profile_extensions, - additional_permissions.macos.as_ref(), - ), } } } @@ -64,12 +54,9 @@ pub fn normalize_additional_permissions( FileSystemPermissions { read, write } }) .filter(|file_system| !file_system.is_empty()); - let macos = additional_permissions.macos; - Ok(PermissionProfile { network, file_system, - macos, }) } @@ -110,15 +97,10 @@ pub fn merge_permission_profiles( (None, Some(permissions)) => Some(permissions.clone()), (None, None) => None, }; - let macos = merge_macos_seatbelt_profile_extensions( - base.macos.as_ref(), - permissions.macos.as_ref(), - ); Some(PermissionProfile { network, file_system, - macos, }) .filter(|permissions| !permissions.is_empty()) } @@ -134,26 +116,10 @@ pub fn intersect_permission_profiles( .file_system .map(|requested_file_system| { let granted_file_system = granted.file_system.unwrap_or_default(); - let read = requested_file_system - .read - .map(|requested_read| { - let granted_read = granted_file_system.read.unwrap_or_default(); - requested_read - .into_iter() - .filter(|path| granted_read.contains(path)) - .collect() - }) - .filter(|paths: &Vec<_>| !paths.is_empty()); - let write = requested_file_system - .write - .map(|requested_write| { - let granted_write = granted_file_system.write.unwrap_or_default(); - requested_write - .into_iter() - .filter(|path| granted_write.contains(path)) - .collect() - }) - .filter(|paths: &Vec<_>| !paths.is_empty()); + let read = + intersect_permission_paths(requested_file_system.read, granted_file_system.read); + let write = + intersect_permission_paths(requested_file_system.write, granted_file_system.write); FileSystemPermissions { read, write } }) .filter(|file_system| !file_system.is_empty()); @@ -171,15 +137,32 @@ pub fn intersect_permission_profiles( _ => None, }; - let macos = intersect_macos_seatbelt_profile_extensions(requested.macos, granted.macos); - PermissionProfile { network, file_system, - macos, } } +fn intersect_permission_paths( + requested: Option>, + granted: Option>, +) -> Option> { + requested.and_then(|requested_paths| { + if requested_paths.is_empty() { + return granted.map(|_| Vec::new()); + } + + let granted_paths = granted.unwrap_or_default(); + Some( + requested_paths + .into_iter() + .filter(|path| granted_paths.contains(path)) + .collect::>(), + ) + .filter(|paths| !paths.is_empty()) + }) +} + fn normalize_permission_paths( paths: Vec, _permission_kind: &str, diff --git a/codex-rs/sandboxing/src/policy_transforms_tests.rs b/codex-rs/sandboxing/src/policy_transforms_tests.rs index a9c759a205..30dbac9242 100644 --- a/codex-rs/sandboxing/src/policy_transforms_tests.rs +++ b/codex-rs/sandboxing/src/policy_transforms_tests.rs @@ -1,21 +1,10 @@ -#[cfg(target_os = "macos")] -use super::EffectiveSandboxPermissions; use super::effective_file_system_sandbox_policy; -#[cfg(target_os = "macos")] use super::intersect_permission_profiles; use super::merge_file_system_policy_with_additional_permissions; use super::normalize_additional_permissions; use super::sandbox_policy_with_additional_permissions; use super::should_require_platform_sandbox; use codex_protocol::models::FileSystemPermissions; -#[cfg(target_os = "macos")] -use codex_protocol::models::MacOsAutomationPermission; -#[cfg(target_os = "macos")] -use codex_protocol::models::MacOsContactsPermission; -#[cfg(target_os = "macos")] -use codex_protocol::models::MacOsPreferencesPermission; -#[cfg(target_os = "macos")] -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::NetworkPermissions; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; @@ -110,7 +99,6 @@ fn normalize_additional_permissions_preserves_network() { read: Some(vec![path.clone()]), write: Some(vec![path.clone()]), }), - ..Default::default() }) .expect("permissions"); @@ -172,104 +160,73 @@ fn normalize_additional_permissions_drops_empty_nested_profiles() { read: None, write: None, }), - macos: None, }) .expect("permissions"); assert_eq!(permissions, PermissionProfile::default()); } -#[cfg(target_os = "macos")] #[test] -fn normalize_additional_permissions_preserves_default_macos_preferences_permission() { - let permissions = normalize_additional_permissions(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions::default()), - ..Default::default() - }) - .expect("permissions"); - - assert_eq!( - permissions, - PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions::default()), - ..Default::default() - } - ); -} - -#[cfg(target_os = "macos")] -#[test] -fn intersect_permission_profiles_preserves_default_macos_grants() { +fn intersect_permission_profiles_preserves_explicit_empty_requested_reads() { + let temp_dir = TempDir::new().expect("create temp dir"); + let path = AbsolutePathBuf::from_absolute_path( + canonicalize(temp_dir.path()).expect("canonicalize temp dir"), + ) + .expect("absolute temp dir"); let requested = PermissionProfile { file_system: Some(FileSystemPermissions { - read: Some(Vec::from(["/tmp/requested" - .try_into() - .expect("absolute path")])), - write: None, - }), - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, + read: Some(vec![]), + write: Some(vec![path]), }), ..Default::default() }; - let granted = PermissionProfile { - file_system: Some(FileSystemPermissions { - read: Some(Vec::new()), - write: None, - }), - macos: Some(MacOsSeatbeltProfileExtensions::default()), - ..Default::default() - }; + let granted = requested.clone(); assert_eq!( - intersect_permission_profiles(requested, granted), - PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions::default()), - ..Default::default() - } + intersect_permission_profiles(requested.clone(), granted), + requested ); } -#[cfg(target_os = "macos")] #[test] -fn normalize_additional_permissions_preserves_macos_permissions() { - let permissions = normalize_additional_permissions(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, +fn intersect_permission_profiles_drops_ungranted_nonempty_path_requests() { + let temp_dir = TempDir::new().expect("create temp dir"); + let path = AbsolutePathBuf::from_absolute_path( + canonicalize(temp_dir.path()).expect("canonicalize temp dir"), + ) + .expect("absolute temp dir"); + let requested = PermissionProfile { + file_system: Some(FileSystemPermissions { + read: Some(vec![path]), + write: None, }), ..Default::default() - }) - .expect("permissions"); + }; assert_eq!( - permissions.macos, - Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }) + intersect_permission_profiles(requested, PermissionProfile::default()), + PermissionProfile::default() + ); +} + +#[test] +fn intersect_permission_profiles_drops_explicit_empty_reads_without_grant() { + let temp_dir = TempDir::new().expect("create temp dir"); + let path = AbsolutePathBuf::from_absolute_path( + canonicalize(temp_dir.path()).expect("canonicalize temp dir"), + ) + .expect("absolute temp dir"); + let requested = PermissionProfile { + file_system: Some(FileSystemPermissions { + read: Some(vec![]), + write: Some(vec![path]), + }), + ..Default::default() + }; + + assert_eq!( + intersect_permission_profiles(requested, PermissionProfile::default()), + PermissionProfile::default() ); } @@ -296,7 +253,6 @@ fn read_only_additional_permissions_can_enable_network_without_writes() { read: Some(vec![path.clone()]), write: Some(Vec::new()), }), - ..Default::default() }, ); @@ -312,70 +268,6 @@ fn read_only_additional_permissions_can_enable_network_without_writes() { ); } -#[cfg(target_os = "macos")] -#[test] -fn effective_permissions_merge_macos_extensions_with_additional_permissions() { - let temp_dir = TempDir::new().expect("create temp dir"); - let path = AbsolutePathBuf::from_absolute_path( - canonicalize(temp_dir.path()).expect("canonicalize temp dir"), - ) - .expect("absolute temp dir"); - let effective_permissions = EffectiveSandboxPermissions::new( - &SandboxPolicy::ReadOnly { - access: ReadOnlyAccess::Restricted { - include_platform_defaults: true, - readable_roots: vec![path.clone()], - }, - network_access: false, - }, - Some(&MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: false, - macos_calendar: false, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - Some(&PermissionProfile { - file_system: Some(FileSystemPermissions { - read: Some(vec![path]), - write: Some(Vec::new()), - }), - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - ..Default::default() - }), - ); - - assert_eq!( - effective_permissions.macos_seatbelt_profile_extensions, - Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }) - ); -} - #[test] fn external_sandbox_additional_permissions_can_enable_network() { let temp_dir = TempDir::new().expect("create temp dir"); @@ -395,7 +287,6 @@ fn external_sandbox_additional_permissions_can_enable_network() { read: Some(vec![path]), write: Some(Vec::new()), }), - ..Default::default() }, ); diff --git a/codex-rs/sandboxing/src/seatbelt.rs b/codex-rs/sandboxing/src/seatbelt.rs index e6ff71d0d3..5c11c4fc45 100644 --- a/codex-rs/sandboxing/src/seatbelt.rs +++ b/codex-rs/sandboxing/src/seatbelt.rs @@ -2,7 +2,6 @@ use codex_network_proxy::NetworkProxy; use codex_network_proxy::PROXY_URL_ENV_KEYS; use codex_network_proxy::has_proxy_url_env_vars; use codex_network_proxy::proxy_url_env_value; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_protocol::protocol::SandboxPolicy; @@ -16,12 +15,18 @@ use std::path::PathBuf; use tracing::warn; use url::Url; -use crate::seatbelt_permissions::build_seatbelt_extensions; - const MACOS_SEATBELT_BASE_POLICY: &str = include_str!("seatbelt_base_policy.sbpl"); const MACOS_SEATBELT_NETWORK_POLICY: &str = include_str!("seatbelt_network_policy.sbpl"); const MACOS_RESTRICTED_READ_ONLY_PLATFORM_DEFAULTS: &str = include_str!("restricted_read_only_platform_defaults.sbpl"); +const MACOS_DEFAULT_PREFERENCES_POLICY: &str = r#"; allow readonly user preferences +(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) +(allow mach-lookup + (global-name "com.apple.cfprefsd.daemon") + (global-name "com.apple.cfprefsd.agent") + (local-name "com.apple.cfprefsd.agent")) +(allow user-preference-read) +"#; /// When working with `sandbox-exec`, only consider `sandbox-exec` in `/usr/bin` /// to defend against an attacker trying to inject a malicious version on the @@ -356,33 +361,30 @@ fn build_seatbelt_access_policy( } #[cfg_attr(not(test), allow(dead_code))] -fn create_seatbelt_command_args_with_extensions( +fn create_seatbelt_command_args( command: Vec, sandbox_policy: &SandboxPolicy, sandbox_policy_cwd: &Path, enforce_managed_network: bool, network: Option<&NetworkProxy>, - extensions: Option<&MacOsSeatbeltProfileExtensions>, ) -> Vec { - create_seatbelt_command_args_for_policies_with_extensions( + create_seatbelt_command_args_for_policies( command, &FileSystemSandboxPolicy::from_legacy_sandbox_policy(sandbox_policy, sandbox_policy_cwd), NetworkSandboxPolicy::from(sandbox_policy), sandbox_policy_cwd, enforce_managed_network, network, - extensions, ) } -pub fn create_seatbelt_command_args_for_policies_with_extensions( +pub fn create_seatbelt_command_args_for_policies( command: Vec, file_system_sandbox_policy: &FileSystemSandboxPolicy, network_sandbox_policy: NetworkSandboxPolicy, sandbox_policy_cwd: &Path, enforce_managed_network: bool, network: Option<&NetworkProxy>, - extensions: Option<&MacOsSeatbeltProfileExtensions>, ) -> Vec { let unreadable_roots = file_system_sandbox_policy.get_unreadable_roots_with_cwd(sandbox_policy_cwd); @@ -470,17 +472,11 @@ pub fn create_seatbelt_command_args_for_policies_with_extensions( let proxy = proxy_policy_inputs(network); let network_policy = dynamic_network_policy_for_network(network_sandbox_policy, enforce_managed_network, &proxy); - let seatbelt_extensions = extensions.map_or_else( - || { - // Backward-compatibility default when no extension profile is provided. - build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions::default()) - }, - build_seatbelt_extensions, - ); let include_platform_defaults = file_system_sandbox_policy.include_platform_defaults(); let mut policy_sections = vec![ MACOS_SEATBELT_BASE_POLICY.to_string(), + MACOS_DEFAULT_PREFERENCES_POLICY.to_string(), file_read_policy, file_write_policy, network_policy, @@ -488,9 +484,6 @@ pub fn create_seatbelt_command_args_for_policies_with_extensions( if include_platform_defaults { policy_sections.push(MACOS_RESTRICTED_READ_ONLY_PLATFORM_DEFAULTS.to_string()); } - if !seatbelt_extensions.policy.is_empty() { - policy_sections.push(seatbelt_extensions.policy.clone()); - } let full_policy = policy_sections.join("\n"); @@ -499,7 +492,6 @@ pub fn create_seatbelt_command_args_for_policies_with_extensions( file_write_dir_params, macos_dir_params(), unix_socket_dir_params(&proxy), - seatbelt_extensions.dir_params, ] .concat(); diff --git a/codex-rs/sandboxing/src/seatbelt_permissions.rs b/codex-rs/sandboxing/src/seatbelt_permissions.rs deleted file mode 100644 index d53e7496ad..0000000000 --- a/codex-rs/sandboxing/src/seatbelt_permissions.rs +++ /dev/null @@ -1,190 +0,0 @@ -use std::collections::BTreeSet; -use std::path::PathBuf; - -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; - -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub(crate) struct SeatbeltExtensionPolicy { - pub(crate) policy: String, - pub(crate) dir_params: Vec<(String, PathBuf)>, -} - -fn normalized_extensions( - extensions: &MacOsSeatbeltProfileExtensions, -) -> MacOsSeatbeltProfileExtensions { - let mut normalized = extensions.clone(); - if let MacOsAutomationPermission::BundleIds(bundle_ids) = &extensions.macos_automation { - let bundle_ids = normalize_bundle_ids(bundle_ids); - normalized.macos_automation = if bundle_ids.is_empty() { - MacOsAutomationPermission::None - } else { - MacOsAutomationPermission::BundleIds(bundle_ids) - }; - } - - normalized -} - -pub(crate) fn build_seatbelt_extensions( - extensions: &MacOsSeatbeltProfileExtensions, -) -> SeatbeltExtensionPolicy { - let extensions = normalized_extensions(extensions); - let mut clauses = Vec::new(); - - match extensions.macos_preferences { - MacOsPreferencesPermission::None => {} - MacOsPreferencesPermission::ReadOnly => { - clauses.push( - "(allow ipc-posix-shm-read* (ipc-posix-name-prefix \"apple.cfprefs.\"))" - .to_string(), - ); - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.cfprefsd.daemon\")\n (global-name \"com.apple.cfprefsd.agent\")\n (local-name \"com.apple.cfprefsd.agent\"))" - .to_string(), - ); - clauses.push("(allow user-preference-read)".to_string()); - } - MacOsPreferencesPermission::ReadWrite => { - clauses.push( - "(allow ipc-posix-shm-read* (ipc-posix-name-prefix \"apple.cfprefs.\"))" - .to_string(), - ); - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.cfprefsd.daemon\")\n (global-name \"com.apple.cfprefsd.agent\")\n (local-name \"com.apple.cfprefsd.agent\"))" - .to_string(), - ); - clauses.push("(allow user-preference-read)".to_string()); - clauses.push("(allow user-preference-write)".to_string()); - clauses.push( - "(allow ipc-posix-shm-write-data (ipc-posix-name-prefix \"apple.cfprefs.\"))" - .to_string(), - ); - clauses.push( - "(allow ipc-posix-shm-write-create (ipc-posix-name-prefix \"apple.cfprefs.\"))" - .to_string(), - ); - } - } - - match extensions.macos_automation { - MacOsAutomationPermission::None => {} - MacOsAutomationPermission::All => { - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.coreservices.appleevents\"))" - .to_string(), - ); - clauses.push("(allow appleevent-send)".to_string()); - } - MacOsAutomationPermission::BundleIds(bundle_ids) => { - if !bundle_ids.is_empty() { - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.coreservices.appleevents\"))" - .to_string(), - ); - let destinations = bundle_ids - .iter() - .map(|bundle_id| format!(" (appleevent-destination \"{bundle_id}\")")) - .collect::>() - .join("\n"); - clauses.push(format!("(allow appleevent-send\n{destinations}\n)")); - } - } - } - - if extensions.macos_launch_services { - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.coreservices.launchservicesd\")\n (global-name \"com.apple.lsd.mapdb\")\n (global-name \"com.apple.coreservices.quarantine-resolver\")\n (global-name \"com.apple.lsd.modifydb\"))" - .to_string(), - ); - clauses.push("(allow lsopen)".to_string()); - } - - if extensions.macos_accessibility { - clauses.push("(allow mach-lookup (local-name \"com.apple.axserver\"))".to_string()); - } - - if extensions.macos_calendar { - clauses.push("(allow mach-lookup (global-name \"com.apple.CalendarAgent\"))".to_string()); - } - - if extensions.macos_reminders { - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.CalendarAgent\")\n (global-name \"com.apple.remindd\"))" - .to_string(), - ); - } - - let mut dir_params = Vec::new(); - match extensions.macos_contacts { - MacOsContactsPermission::None => {} - MacOsContactsPermission::ReadOnly => { - clauses.push( - "(allow file-read* file-test-existence\n (subpath \"/System/Library/Address Book Plug-Ins\")\n (subpath (param \"ADDRESSBOOK_DIR\")))" - .to_string(), - ); - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.tccd\")\n (global-name \"com.apple.tccd.system\")\n (global-name \"com.apple.contactsd.persistence\")\n (global-name \"com.apple.AddressBook.ContactsAccountsService\")\n (global-name \"com.apple.contacts.account-caching\")\n (global-name \"com.apple.accountsd.accountmanager\"))" - .to_string(), - ); - if let Some(addressbook_dir) = addressbook_dir() { - dir_params.push(("ADDRESSBOOK_DIR".to_string(), addressbook_dir)); - } - } - MacOsContactsPermission::ReadWrite => { - clauses.push( - "(allow file-read* file-write*\n (subpath \"/System/Library/Address Book Plug-Ins\")\n (subpath (param \"ADDRESSBOOK_DIR\"))\n (subpath \"/var/folders\")\n (subpath \"/private/var/folders\"))" - .to_string(), - ); - clauses.push( - "(allow mach-lookup\n (global-name \"com.apple.tccd\")\n (global-name \"com.apple.tccd.system\")\n (global-name \"com.apple.contactsd.persistence\")\n (global-name \"com.apple.AddressBook.ContactsAccountsService\")\n (global-name \"com.apple.contacts.account-caching\")\n (global-name \"com.apple.accountsd.accountmanager\")\n (global-name \"com.apple.securityd.xpc\"))" - .to_string(), - ); - if let Some(addressbook_dir) = addressbook_dir() { - dir_params.push(("ADDRESSBOOK_DIR".to_string(), addressbook_dir)); - } - } - } - - if clauses.is_empty() { - SeatbeltExtensionPolicy::default() - } else { - SeatbeltExtensionPolicy { - policy: format!( - "; macOS permission profile extensions\n{}\n", - clauses.join("\n") - ), - dir_params, - } - } -} - -fn addressbook_dir() -> Option { - Some(dirs::home_dir()?.join("Library/Application Support/AddressBook")) -} - -fn normalize_bundle_ids(bundle_ids: &[String]) -> Vec { - let mut unique = BTreeSet::new(); - for bundle_id in bundle_ids { - let candidate = bundle_id.trim(); - if is_valid_bundle_id(candidate) { - unique.insert(candidate.to_string()); - } - } - unique.into_iter().collect() -} - -fn is_valid_bundle_id(bundle_id: &str) -> bool { - if bundle_id.len() < 3 || !bundle_id.contains('.') { - return false; - } - bundle_id - .chars() - .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_') -} - -#[cfg(test)] -#[path = "seatbelt_permissions_tests.rs"] -mod tests; diff --git a/codex-rs/sandboxing/src/seatbelt_permissions_tests.rs b/codex-rs/sandboxing/src/seatbelt_permissions_tests.rs deleted file mode 100644 index b52ccdfcb2..0000000000 --- a/codex-rs/sandboxing/src/seatbelt_permissions_tests.rs +++ /dev/null @@ -1,154 +0,0 @@ -use super::MacOsAutomationPermission; -use super::MacOsContactsPermission; -use super::MacOsPreferencesPermission; -use super::MacOsSeatbeltProfileExtensions; -use super::build_seatbelt_extensions; - -#[test] -fn preferences_read_only_emits_read_clauses_only() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadOnly, - ..Default::default() - }); - assert!(policy.policy.contains("(allow user-preference-read)")); - assert!(!policy.policy.contains("(allow user-preference-write)")); -} - -#[test] -fn preferences_read_write_emits_write_clauses() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - ..Default::default() - }); - assert!(policy.policy.contains("(allow user-preference-read)")); - assert!(policy.policy.contains("(allow user-preference-write)")); - assert!( - policy.policy.contains( - "(allow ipc-posix-shm-write-create (ipc-posix-name-prefix \"apple.cfprefs.\"))" - ) - ); -} - -#[test] -fn automation_all_emits_unscoped_appleevents() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_automation: MacOsAutomationPermission::All, - ..Default::default() - }); - assert!(policy.policy.contains("(allow appleevent-send)")); - assert!(policy.policy.contains("com.apple.coreservices.appleevents")); -} - -#[test] -fn automation_bundle_ids_are_normalized_and_scoped() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - " com.apple.Notes ".to_string(), - "com.apple.Calendar".to_string(), - "bad bundle".to_string(), - "com.apple.Notes".to_string(), - ]), - ..Default::default() - }); - assert!( - policy - .policy - .contains("(appleevent-destination \"com.apple.Calendar\")") - ); - assert!( - policy - .policy - .contains("(appleevent-destination \"com.apple.Notes\")") - ); - assert!(!policy.policy.contains("bad bundle")); - assert!(policy.policy.contains("com.apple.coreservices.appleevents")); -} - -#[test] -fn launch_services_emit_launch_clauses() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_launch_services: true, - ..Default::default() - }); - assert!( - policy - .policy - .contains("com.apple.coreservices.launchservicesd") - ); - assert!(policy.policy.contains("com.apple.lsd.mapdb")); - assert!( - policy - .policy - .contains("com.apple.coreservices.quarantine-resolver") - ); - assert!(policy.policy.contains("com.apple.lsd.modifydb")); - assert!(policy.policy.contains("(allow lsopen)")); -} - -#[test] -fn accessibility_and_calendar_emit_mach_lookups() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_accessibility: true, - macos_calendar: true, - ..Default::default() - }); - assert!(policy.policy.contains("com.apple.axserver")); - assert!(policy.policy.contains("com.apple.CalendarAgent")); -} - -#[test] -fn reminders_emit_calendar_agent_and_remindd_lookups() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_reminders: true, - ..Default::default() - }); - assert!(policy.policy.contains("com.apple.CalendarAgent")); - assert!(policy.policy.contains("com.apple.remindd")); -} - -#[test] -fn contacts_read_only_emit_contacts_read_clauses() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_contacts: MacOsContactsPermission::ReadOnly, - ..Default::default() - }); - - assert!( - policy - .policy - .contains("(subpath \"/System/Library/Address Book Plug-Ins\")") - ); - assert!( - policy - .policy - .contains("(subpath (param \"ADDRESSBOOK_DIR\"))") - ); - assert!(policy.policy.contains("com.apple.contactsd.persistence")); - assert!(policy.policy.contains("com.apple.accountsd.accountmanager")); - assert!(!policy.policy.contains("com.apple.securityd.xpc")); - assert!( - policy - .dir_params - .iter() - .any(|(key, _)| key == "ADDRESSBOOK_DIR") - ); -} - -#[test] -fn contacts_read_write_emit_write_clauses() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions { - macos_contacts: MacOsContactsPermission::ReadWrite, - ..Default::default() - }); - - assert!(policy.policy.contains("(subpath \"/var/folders\")")); - assert!(policy.policy.contains("(subpath \"/private/var/folders\")")); - assert!(policy.policy.contains("com.apple.securityd.xpc")); -} - -#[test] -fn default_extensions_emit_preferences_read_only_policy() { - let policy = build_seatbelt_extensions(&MacOsSeatbeltProfileExtensions::default()); - assert!(policy.policy.contains("(allow user-preference-read)")); - assert!(!policy.policy.contains("(allow user-preference-write)")); -} diff --git a/codex-rs/sandboxing/src/seatbelt_tests.rs b/codex-rs/sandboxing/src/seatbelt_tests.rs index e508a028c2..cc6fce56a4 100644 --- a/codex-rs/sandboxing/src/seatbelt_tests.rs +++ b/codex-rs/sandboxing/src/seatbelt_tests.rs @@ -2,17 +2,13 @@ use super::MACOS_PATH_TO_SEATBELT_EXECUTABLE; use super::MACOS_SEATBELT_BASE_POLICY; use super::ProxyPolicyInputs; use super::UnixDomainSocketPolicy; -use super::create_seatbelt_command_args_for_policies_with_extensions; -use super::create_seatbelt_command_args_with_extensions; +use super::create_seatbelt_command_args; +use super::create_seatbelt_command_args_for_policies; use super::dynamic_network_policy; use super::macos_dir_params; use super::normalize_path_for_sandbox; use super::unix_socket_dir_params; use super::unix_socket_policy; -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; -use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::permissions::FileSystemAccessMode; use codex_protocol::permissions::FileSystemPath; use codex_protocol::permissions::FileSystemSandboxEntry; @@ -115,14 +111,13 @@ fn explicit_unreadable_paths_are_excluded_from_full_disk_read_and_write_access() }, ]); - let args = create_seatbelt_command_args_for_policies_with_extensions( + let args = create_seatbelt_command_args_for_policies( vec!["/bin/true".to_string()], &file_system_policy, NetworkSandboxPolicy::Restricted, Path::new("/"), false, None, - None, ); let policy = seatbelt_policy_arg(&args); @@ -181,14 +176,13 @@ fn explicit_unreadable_paths_are_excluded_from_readable_roots() { }, ]); - let args = create_seatbelt_command_args_for_policies_with_extensions( + let args = create_seatbelt_command_args_for_policies( vec!["/bin/true".to_string()], &file_system_policy, NetworkSandboxPolicy::Restricted, Path::new("/"), false, None, - None, ); let policy = seatbelt_policy_arg(&args); @@ -217,96 +211,15 @@ fn explicit_unreadable_paths_are_excluded_from_readable_roots() { ); } -#[test] -fn seatbelt_args_include_macos_permission_extensions() { - let cwd = std::env::temp_dir(); - let args = create_seatbelt_command_args_with_extensions( - vec!["echo".to_string(), "ok".to_string()], - &SandboxPolicy::new_read_only_policy(), - cwd.as_path(), - false, - None, - Some(&MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - macos_launch_services: true, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: false, - macos_contacts: MacOsContactsPermission::None, - }), - ); - let policy = &args[1]; - - assert!(policy.contains("(allow user-preference-write)")); - assert!(policy.contains("(appleevent-destination \"com.apple.Notes\")")); - assert!(policy.contains("com.apple.axserver")); - assert!(policy.contains("com.apple.CalendarAgent")); -} - -#[test] -fn bundle_id_automation_keeps_lsopen_denied() { - let tmp = TempDir::new().expect("tempdir"); - let cwd = tmp.path().join("cwd"); - fs::create_dir_all(&cwd).expect("create cwd"); - - let args = create_seatbelt_command_args_with_extensions( - vec![ - "/usr/bin/python3".to_string(), - "-c".to_string(), - r#"import ctypes -import os -import sys -lib = ctypes.CDLL("/usr/lib/libsandbox.1.dylib") -lib.sandbox_check.restype = ctypes.c_int -allowed = lib.sandbox_check(os.getpid(), b"lsopen", 0) == 0 -sys.exit(0 if allowed else 13) -"# - .to_string(), - ], - &SandboxPolicy::new_read_only_policy(), - cwd.as_path(), - false, - None, - Some(&MacOsSeatbeltProfileExtensions { - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Notes".to_string(), - ]), - ..Default::default() - }), - ); - - let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) - .args(&args) - .current_dir(&cwd) - .output() - .expect("execute seatbelt command"); - - let stderr = String::from_utf8_lossy(&output.stderr); - if stderr.contains("sandbox-exec: sandbox_apply: Operation not permitted") { - return; - } - - assert_eq!( - Some(13), - output.status.code(), - "lsopen should remain denied even with bundle-scoped automation\nstdout: {}\nstderr: {stderr}", - String::from_utf8_lossy(&output.stdout), - ); -} - #[test] fn seatbelt_args_without_extension_profile_keep_legacy_preferences_read_access() { let cwd = std::env::temp_dir(); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( vec!["echo".to_string(), "ok".to_string()], &SandboxPolicy::new_read_only_policy(), cwd.as_path(), /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let policy = &args[1]; assert!(policy.contains("(allow user-preference-read)")); @@ -319,7 +232,7 @@ fn seatbelt_legacy_workspace_write_nested_readable_root_stays_writable() { let cwd = tmp.path().join("workspace"); fs::create_dir_all(cwd.join("docs")).expect("create docs"); let docs = AbsolutePathBuf::from_absolute_path(cwd.join("docs")).expect("absolute docs"); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( vec!["/bin/true".to_string()], &SandboxPolicy::WorkspaceWrite { writable_roots: vec![], @@ -334,7 +247,6 @@ fn seatbelt_legacy_workspace_write_nested_readable_root_stays_writable() { cwd.as_path(), /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); assert!( @@ -351,25 +263,6 @@ fn seatbelt_legacy_workspace_write_nested_readable_root_stays_writable() { ); } -#[test] -fn seatbelt_args_default_extension_profile_keeps_preferences_read_access() { - let cwd = std::env::temp_dir(); - let args = create_seatbelt_command_args_with_extensions( - vec!["echo".to_string(), "ok".to_string()], - &SandboxPolicy::new_read_only_policy(), - cwd.as_path(), - false, - None, - Some(&MacOsSeatbeltProfileExtensions::default()), - ); - let policy = &args[1]; - assert!(!policy.contains("appleevent-send")); - assert!(!policy.contains("com.apple.axserver")); - assert!(!policy.contains("com.apple.CalendarAgent")); - assert!(policy.contains("(allow user-preference-read)")); - assert!(!policy.contains("user-preference-write")); -} - #[test] fn create_seatbelt_args_allows_local_binding_when_explicitly_enabled() { let policy = dynamic_network_policy( @@ -664,13 +557,12 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { .iter() .map(std::string::ToString::to_string) .collect(); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( shell_command.clone(), &policy, &cwd, /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let policy_text = seatbelt_policy_arg(&args); @@ -779,13 +671,12 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { .iter() .map(std::string::ToString::to_string) .collect(); - let write_hooks_file_args = create_seatbelt_command_args_with_extensions( + let write_hooks_file_args = create_seatbelt_command_args( shell_command_git, &policy, &cwd, /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) .args(&write_hooks_file_args) @@ -816,13 +707,12 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { .iter() .map(std::string::ToString::to_string) .collect(); - let write_allowed_file_args = create_seatbelt_command_args_with_extensions( + let write_allowed_file_args = create_seatbelt_command_args( shell_command_allowed, &policy, &cwd, /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) .args(&write_allowed_file_args) @@ -882,13 +772,12 @@ fn create_seatbelt_args_block_first_time_dot_codex_creation_with_exact_and_desce .iter() .map(std::string::ToString::to_string) .collect(); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( shell_command, &policy, repo_root.as_path(), /*enforce_managed_network*/ false, /*network*/ None, - /*extensions*/ None, ); let policy_text = seatbelt_policy_arg(&args); @@ -938,13 +827,12 @@ fn create_seatbelt_args_with_read_only_git_pointer_file() { .iter() .map(std::string::ToString::to_string) .collect(); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( shell_command, &policy, &cwd, /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) @@ -975,13 +863,12 @@ fn create_seatbelt_args_with_read_only_git_pointer_file() { .iter() .map(std::string::ToString::to_string) .collect(); - let gitdir_args = create_seatbelt_command_args_with_extensions( + let gitdir_args = create_seatbelt_command_args( shell_command_gitdir, &policy, &cwd, /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) .args(&gitdir_args) @@ -1039,13 +926,12 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { .iter() .map(std::string::ToString::to_string) .collect(); - let args = create_seatbelt_command_args_with_extensions( + let args = create_seatbelt_command_args( shell_command.clone(), &policy, vulnerable_root.as_path(), /*enforce_managed_network*/ false, None, - /*extensions*/ None, ); let tmpdir_env_var = std::env::var("TMPDIR") @@ -1067,19 +953,19 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { // - write access to WRITABLE_ROOT_0 (but not its .git or .codex), WRITABLE_ROOT_1, and cwd as WRITABLE_ROOT_2. let expected_policy = format!( r#"{MACOS_SEATBELT_BASE_POLICY} -; allow read-only file operations -(allow file-read*) -(allow file-write* -(require-all (subpath (param "WRITABLE_ROOT_0")) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_1"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_1"))) ) (subpath (param "WRITABLE_ROOT_1")){tempdir_policy_entry} -) - -; macOS permission profile extensions +; allow readonly user preferences (allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) (allow mach-lookup (global-name "com.apple.cfprefsd.daemon") (global-name "com.apple.cfprefsd.agent") (local-name "com.apple.cfprefsd.agent")) (allow user-preference-read) + +; allow read-only file operations +(allow file-read*) +(allow file-write* +(require-all (subpath (param "WRITABLE_ROOT_0")) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_0"))) (require-not (literal (param "WRITABLE_ROOT_0_EXCLUDED_1"))) (require-not (subpath (param "WRITABLE_ROOT_0_EXCLUDED_1"))) ) (subpath (param "WRITABLE_ROOT_1")){tempdir_policy_entry} +) "#, ); diff --git a/codex-rs/tui/src/bottom_pane/approval_overlay.rs b/codex-rs/tui/src/bottom_pane/approval_overlay.rs index 1b403c251f..0d6ef77c1f 100644 --- a/codex-rs/tui/src/bottom_pane/approval_overlay.rs +++ b/codex-rs/tui/src/bottom_pane/approval_overlay.rs @@ -19,9 +19,6 @@ use crate::render::renderable::Renderable; use codex_features::Features; use codex_protocol::ThreadId; use codex_protocol::mcp::RequestId; -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::ElicitationAction; use codex_protocol::protocol::FileChange; @@ -777,48 +774,6 @@ pub(crate) fn format_additional_permissions_rule( parts.push(format!("write {writes}")); } } - if let Some(macos) = additional_permissions.macos.as_ref() { - if !matches!( - macos.macos_preferences, - MacOsPreferencesPermission::ReadOnly - ) { - let value = match macos.macos_preferences { - MacOsPreferencesPermission::ReadOnly => "readonly", - MacOsPreferencesPermission::ReadWrite => "readwrite", - MacOsPreferencesPermission::None => "none", - }; - parts.push(format!("macOS preferences {value}")); - } - match &macos.macos_automation { - MacOsAutomationPermission::All => { - parts.push("macOS automation all".to_string()); - } - MacOsAutomationPermission::BundleIds(bundle_ids) => { - if !bundle_ids.is_empty() { - parts.push(format!("macOS automation {}", bundle_ids.join(", "))); - } - } - MacOsAutomationPermission::None => {} - } - if macos.macos_accessibility { - parts.push("macOS accessibility".to_string()); - } - if macos.macos_calendar { - parts.push("macOS calendar".to_string()); - } - if macos.macos_reminders { - parts.push("macOS reminders".to_string()); - } - if !matches!(macos.macos_contacts, MacOsContactsPermission::None) { - let value = match macos.macos_contacts { - MacOsContactsPermission::None => "none", - MacOsContactsPermission::ReadOnly => "readonly", - MacOsContactsPermission::ReadWrite => "readwrite", - }; - parts.push(format!("macOS contacts {value}")); - } - } - if parts.is_empty() { None } else { @@ -906,9 +861,6 @@ mod tests { use super::*; use crate::app_event::AppEvent; use codex_protocol::models::FileSystemPermissions; - use codex_protocol::models::MacOsAutomationPermission; - use codex_protocol::models::MacOsPreferencesPermission; - use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::NetworkPermissions; use codex_protocol::protocol::ExecPolicyAmendment; use codex_protocol::protocol::NetworkApprovalProtocol; @@ -1334,7 +1286,6 @@ mod tests { read: Some(vec![absolute_path("/tmp/readme.txt")]), write: Some(vec![absolute_path("/tmp/out.txt")]), }), - ..Default::default() }), }; @@ -1382,7 +1333,6 @@ mod tests { read: Some(vec![absolute_path("/tmp/readme.txt")]), write: Some(vec![absolute_path("/tmp/out.txt")]), }), - ..Default::default() }), }; @@ -1404,42 +1354,6 @@ mod tests { ); } - #[test] - fn additional_permissions_macos_prompt_snapshot() { - let (tx, _rx) = unbounded_channel::(); - let tx = AppEventSender::new(tx); - let exec_request = ApprovalRequest::Exec { - thread_id: ThreadId::new(), - thread_label: None, - id: "test".into(), - command: vec!["osascript".into(), "-e".into(), "tell application".into()], - reason: Some("need macOS automation".into()), - available_decisions: vec![ReviewDecision::Approved, ReviewDecision::Abort], - network_approval_context: None, - additional_permissions: Some(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - "com.apple.Notes".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::None, - }), - ..Default::default() - }), - }; - - let view = ApprovalOverlay::new(exec_request, tx, Features::with_defaults()); - assert_snapshot!( - "approval_overlay_additional_permissions_macos_prompt", - render_overlay_lines(&view, 120) - ); - } - #[test] fn network_exec_prompt_title_includes_host() { let (tx, _rx) = unbounded_channel::(); diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap deleted file mode 100644 index d9d8717fe9..0000000000 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap +++ /dev/null @@ -1,18 +0,0 @@ ---- -source: tui/src/bottom_pane/approval_overlay.rs -expression: "render_overlay_lines(&view, 120)" ---- - - Would you like to run the following command? - - Reason: need macOS automation - - Permission rule: macOS preferences readwrite; macOS automation com.apple.Calendar, com.apple.Notes; macOS - accessibility; macOS calendar; macOS reminders - - $ osascript -e 'tell application' - -› 1. Yes, proceed (y) - 2. No, and tell Codex what to do differently (esc) - - Press enter to confirm or esc to cancel diff --git a/codex-rs/tui_app_server/src/app.rs b/codex-rs/tui_app_server/src/app.rs index a86a8c9388..c36f793b75 100644 --- a/codex-rs/tui_app_server/src/app.rs +++ b/codex-rs/tui_app_server/src/app.rs @@ -7568,7 +7568,6 @@ guardian_approval = true enabled: Some(true), }), file_system: None, - macos: None, }); params.proposed_network_policy_amendments = Some(vec![AppServerNetworkPolicyAmendment { host: "example.com".to_string(), @@ -7601,7 +7600,6 @@ guardian_approval = true enabled: Some(true), }), file_system: None, - macos: None, }) ); assert_eq!( diff --git a/codex-rs/tui_app_server/src/bottom_pane/approval_overlay.rs b/codex-rs/tui_app_server/src/bottom_pane/approval_overlay.rs index f5d1cee621..84f6da6cb7 100644 --- a/codex-rs/tui_app_server/src/bottom_pane/approval_overlay.rs +++ b/codex-rs/tui_app_server/src/bottom_pane/approval_overlay.rs @@ -19,9 +19,6 @@ use crate::render::renderable::Renderable; use codex_features::Features; use codex_protocol::ThreadId; use codex_protocol::mcp::RequestId; -use codex_protocol::models::MacOsAutomationPermission; -use codex_protocol::models::MacOsContactsPermission; -use codex_protocol::models::MacOsPreferencesPermission; use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::ElicitationAction; use codex_protocol::protocol::FileChange; @@ -763,48 +760,6 @@ pub(crate) fn format_additional_permissions_rule( parts.push(format!("write {writes}")); } } - if let Some(macos) = additional_permissions.macos.as_ref() { - if !matches!( - macos.macos_preferences, - MacOsPreferencesPermission::ReadOnly - ) { - let value = match macos.macos_preferences { - MacOsPreferencesPermission::ReadOnly => "readonly", - MacOsPreferencesPermission::ReadWrite => "readwrite", - MacOsPreferencesPermission::None => "none", - }; - parts.push(format!("macOS preferences {value}")); - } - match &macos.macos_automation { - MacOsAutomationPermission::All => { - parts.push("macOS automation all".to_string()); - } - MacOsAutomationPermission::BundleIds(bundle_ids) => { - if !bundle_ids.is_empty() { - parts.push(format!("macOS automation {}", bundle_ids.join(", "))); - } - } - MacOsAutomationPermission::None => {} - } - if macos.macos_accessibility { - parts.push("macOS accessibility".to_string()); - } - if macos.macos_calendar { - parts.push("macOS calendar".to_string()); - } - if macos.macos_reminders { - parts.push("macOS reminders".to_string()); - } - if !matches!(macos.macos_contacts, MacOsContactsPermission::None) { - let value = match macos.macos_contacts { - MacOsContactsPermission::None => "none", - MacOsContactsPermission::ReadOnly => "readonly", - MacOsContactsPermission::ReadWrite => "readwrite", - }; - parts.push(format!("macOS contacts {value}")); - } - } - if parts.is_empty() { None } else { @@ -892,9 +847,6 @@ mod tests { use super::*; use crate::app_event::AppEvent; use codex_protocol::models::FileSystemPermissions; - use codex_protocol::models::MacOsAutomationPermission; - use codex_protocol::models::MacOsPreferencesPermission; - use codex_protocol::models::MacOsSeatbeltProfileExtensions; use codex_protocol::models::NetworkPermissions; use codex_protocol::protocol::ExecPolicyAmendment; use codex_protocol::protocol::NetworkApprovalProtocol; @@ -1320,7 +1272,6 @@ mod tests { read: Some(vec![absolute_path("/tmp/readme.txt")]), write: Some(vec![absolute_path("/tmp/out.txt")]), }), - ..Default::default() }), }; @@ -1368,7 +1319,6 @@ mod tests { read: Some(vec![absolute_path("/tmp/readme.txt")]), write: Some(vec![absolute_path("/tmp/out.txt")]), }), - ..Default::default() }), }; @@ -1390,42 +1340,6 @@ mod tests { ); } - #[test] - fn additional_permissions_macos_prompt_snapshot() { - let (tx, _rx) = unbounded_channel::(); - let tx = AppEventSender::new(tx); - let exec_request = ApprovalRequest::Exec { - thread_id: ThreadId::new(), - thread_label: None, - id: "test".into(), - command: vec!["osascript".into(), "-e".into(), "tell application".into()], - reason: Some("need macOS automation".into()), - available_decisions: vec![ReviewDecision::Approved, ReviewDecision::Abort], - network_approval_context: None, - additional_permissions: Some(PermissionProfile { - macos: Some(MacOsSeatbeltProfileExtensions { - macos_preferences: MacOsPreferencesPermission::ReadWrite, - macos_automation: MacOsAutomationPermission::BundleIds(vec![ - "com.apple.Calendar".to_string(), - "com.apple.Notes".to_string(), - ]), - macos_launch_services: false, - macos_accessibility: true, - macos_calendar: true, - macos_reminders: true, - macos_contacts: MacOsContactsPermission::None, - }), - ..Default::default() - }), - }; - - let view = ApprovalOverlay::new(exec_request, tx, Features::with_defaults()); - assert_snapshot!( - "approval_overlay_additional_permissions_macos_prompt", - render_overlay_lines(&view, 120) - ); - } - #[test] fn network_exec_prompt_title_includes_host() { let (tx, _rx) = unbounded_channel::(); diff --git a/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap b/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap deleted file mode 100644 index d9d8717fe9..0000000000 --- a/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap +++ /dev/null @@ -1,18 +0,0 @@ ---- -source: tui/src/bottom_pane/approval_overlay.rs -expression: "render_overlay_lines(&view, 120)" ---- - - Would you like to run the following command? - - Reason: need macOS automation - - Permission rule: macOS preferences readwrite; macOS automation com.apple.Calendar, com.apple.Notes; macOS - accessibility; macOS calendar; macOS reminders - - $ osascript -e 'tell application' - -› 1. Yes, proceed (y) - 2. No, and tell Codex what to do differently (esc) - - Press enter to confirm or esc to cancel diff --git a/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap b/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap deleted file mode 100644 index 7731a880be..0000000000 --- a/codex-rs/tui_app_server/src/bottom_pane/snapshots/codex_tui_app_server__bottom_pane__approval_overlay__tests__approval_overlay_additional_permissions_macos_prompt.snap +++ /dev/null @@ -1,18 +0,0 @@ ---- -source: tui_app_server/src/bottom_pane/approval_overlay.rs -expression: "render_overlay_lines(&view, 120)" ---- - - Would you like to run the following command? - - Reason: need macOS automation - - Permission rule: macOS preferences readwrite; macOS automation com.apple.Calendar, com.apple.Notes; macOS - accessibility; macOS calendar; macOS reminders - - $ osascript -e 'tell application' - -› 1. Yes, proceed (y) - 2. No, and tell Codex what to do differently (esc) - - Press enter to confirm or esc to cancel From 81fa04783a551d7e2fc5cfdfc91921aaec3e64bb Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Thu, 26 Mar 2026 17:27:38 -0700 Subject: [PATCH 024/191] feat(windows-sandbox): add network proxy support (#12220) ## Summary This PR makes Windows sandbox proxying enforceable by routing proxy-only runs through the existing `offline` sandbox user and reserving direct network access for the existing `online` sandbox user. In brief: - if a Windows sandbox run should be proxy-enforced, we run it as the `offline` user - the `offline` user gets firewall rules that block direct outbound traffic and only permit the configured localhost proxy path - if a Windows sandbox run should have true direct network access, we run it as the `online` user - no new sandbox identity is introduced This brings Windows in line with the intended model: proxy use is not just env-based, it is backed by OS-level egress controls. Windows already has two sandbox identities: - `offline`: intended to have no direct network egress - `online`: intended to have full network access This PR makes proxy-enforced runs use that model directly. ### Proxy-enforced runs When proxy enforcement is active: - the run is assigned to the `offline` identity - setup extracts the loopback proxy ports from the sandbox env - Windows setup programs firewall rules for the `offline` user that: - block all non-loopback outbound traffic - block loopback UDP - block loopback TCP except for the configured proxy ports - optionally allow broader localhost access when `allow_local_binding=1` So the sandboxed process can only talk to the local proxy. It cannot open direct outbound sockets or do local UDP-based DNS on its own.The proxy then performs the real outbound network access outside that restricted sandbox identity. ### Direct-network runs When proxy enforcement is not active and full network access is allowed: - the run is assigned to the `online` identity - no proxy-only firewall restrictions are applied - the process gets normal direct network access ### Unelevated vs elevated The restricted-token / unelevated path cannot enforce per-identity firewall policy by itself. So for Windows proxy-enforced runs, we transparently use the logon-user sandbox path under the hood, even if the caller started from the unelevated mode. That keeps enforcement real instead of best-effort. --------- Co-authored-by: Codex --- codex-rs/cli/src/debug_sandbox.rs | 19 +- codex-rs/core/src/exec.rs | 25 +- codex-rs/core/src/windows_sandbox.rs | 17 +- codex-rs/network-proxy/src/proxy.rs | 225 ++++++++-- .../windows-sandbox-rs/sandbox_smoketests.py | 133 +++++- .../windows-sandbox-rs/src/elevated_impl.rs | 59 ++- codex-rs/windows-sandbox-rs/src/firewall.rs | 254 +++++++++-- codex-rs/windows-sandbox-rs/src/identity.rs | 70 ++- codex-rs/windows-sandbox-rs/src/lib.rs | 6 + .../windows-sandbox-rs/src/sandbox_users.rs | 10 + .../windows-sandbox-rs/src/setup_main_win.rs | 21 + .../src/setup_orchestrator.rs | 409 +++++++++++++++--- 12 files changed, 1032 insertions(+), 216 deletions(-) diff --git a/codex-rs/cli/src/debug_sandbox.rs b/codex-rs/cli/src/debug_sandbox.rs index e5cccfc53a..f64c45c73f 100644 --- a/codex-rs/cli/src/debug_sandbox.rs +++ b/codex-rs/cli/src/debug_sandbox.rs @@ -164,14 +164,17 @@ async fn run_command_under_sandbox( let res = tokio::task::spawn_blocking(move || { if use_elevated { run_windows_sandbox_capture_elevated( - policy_str.as_str(), - &sandbox_cwd, - base_dir.as_path(), - command_vec, - &cwd_clone, - env_map, - /*timeout_ms*/ None, - config.permissions.windows_sandbox_private_desktop, + codex_windows_sandbox::ElevatedSandboxCaptureRequest { + policy_json_or_preset: policy_str.as_str(), + sandbox_policy_cwd: &sandbox_cwd, + codex_home: base_dir.as_path(), + command: command_vec, + cwd: &cwd_clone, + env_map, + timeout_ms: None, + use_private_desktop: config.permissions.windows_sandbox_private_desktop, + proxy_enforced: false, + }, ) } else { run_windows_sandbox_capture( diff --git a/codex-rs/core/src/exec.rs b/codex-rs/core/src/exec.rs index 11d8600ba1..fde8782f08 100644 --- a/codex-rs/core/src/exec.rs +++ b/codex-rs/core/src/exec.rs @@ -478,7 +478,11 @@ async fn exec_windows_sandbox( })?; let command_path = command.first().cloned(); let sandbox_level = windows_sandbox_level; - let use_elevated = matches!(sandbox_level, WindowsSandboxLevel::Elevated); + let proxy_enforced = network.is_some(); + // Windows firewall enforcement is tied to the logon-user sandbox identities, so + // proxy-enforced sessions must use that backend even when the configured mode is + // the default restricted-token sandbox. + let use_elevated = proxy_enforced || matches!(sandbox_level, WindowsSandboxLevel::Elevated); let additional_deny_write_paths = windows_restricted_token_filesystem_overlay .map(|overlay| { overlay @@ -491,14 +495,17 @@ async fn exec_windows_sandbox( let spawn_res = tokio::task::spawn_blocking(move || { if use_elevated { run_windows_sandbox_capture_elevated( - policy_str.as_str(), - &sandbox_cwd, - codex_home.as_ref(), - command, - &cwd, - env, - timeout_ms, - windows_sandbox_private_desktop, + codex_windows_sandbox::ElevatedSandboxCaptureRequest { + policy_json_or_preset: policy_str.as_str(), + sandbox_policy_cwd: &sandbox_cwd, + codex_home: codex_home.as_ref(), + command, + cwd: &cwd, + env_map: env, + timeout_ms, + use_private_desktop: windows_sandbox_private_desktop, + proxy_enforced, + }, ) } else { run_windows_sandbox_capture_with_extra_deny_write_paths( diff --git a/codex-rs/core/src/windows_sandbox.rs b/codex-rs/core/src/windows_sandbox.rs index 1fdf3ee338..ee3a040a69 100644 --- a/codex-rs/core/src/windows_sandbox.rs +++ b/codex-rs/core/src/windows_sandbox.rs @@ -180,13 +180,15 @@ pub fn run_elevated_setup( codex_home: &Path, ) -> anyhow::Result<()> { codex_windows_sandbox::run_elevated_setup( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - /*read_roots_override*/ None, - /*write_roots_override*/ None, + codex_windows_sandbox::SandboxSetupRequest { + policy, + policy_cwd, + command_cwd, + env_map, + codex_home, + proxy_enforced: false, + }, + codex_windows_sandbox::SetupRootOverrides::default(), ) } @@ -234,6 +236,7 @@ pub fn run_setup_refresh_with_extra_read_roots( env_map, codex_home, extra_read_roots, + /*proxy_enforced*/ false, ) } diff --git a/codex-rs/network-proxy/src/proxy.rs b/codex-rs/network-proxy/src/proxy.rs index 8f596f6842..3eecef3ee7 100644 --- a/codex-rs/network-proxy/src/proxy.rs +++ b/codex-rs/network-proxy/src/proxy.rs @@ -51,6 +51,43 @@ impl ReservedListeners { } } +struct ReservedListenerSet { + http_listener: StdTcpListener, + socks_listener: Option, +} + +impl ReservedListenerSet { + fn new(http_listener: StdTcpListener, socks_listener: Option) -> Self { + Self { + http_listener, + socks_listener, + } + } + + fn http_addr(&self) -> Result { + self.http_listener + .local_addr() + .context("failed to read reserved HTTP proxy address") + } + + fn socks_addr(&self, default_addr: SocketAddr) -> Result { + self.socks_listener + .as_ref() + .map_or(Ok(default_addr), |listener| { + listener + .local_addr() + .context("failed to read reserved SOCKS5 proxy address") + }) + } + + fn into_reserved_listeners(self) -> Arc { + Arc::new(ReservedListeners::new( + self.http_listener, + self.socks_listener, + )) + } +} + #[derive(Clone)] pub struct NetworkProxyBuilder { state: Option>, @@ -134,38 +171,41 @@ impl NetworkProxyBuilder { .set_blocked_request_observer(self.blocked_request_observer.clone()) .await; let current_cfg = state.current_cfg().await?; - let (requested_http_addr, requested_socks_addr, reserved_listeners) = - if self.managed_by_codex { - let runtime = config::resolve_runtime(¤t_cfg)?; - let (http_listener, socks_listener) = - reserve_loopback_ephemeral_listeners(current_cfg.network.enable_socks5) - .context("reserve managed loopback proxy listeners")?; - let http_addr = http_listener - .local_addr() - .context("failed to read reserved HTTP proxy address")?; - let socks_addr = if let Some(socks_listener) = socks_listener.as_ref() { - socks_listener - .local_addr() - .context("failed to read reserved SOCKS5 proxy address")? - } else { - runtime.socks_addr - }; - ( - http_addr, - socks_addr, - Some(Arc::new(ReservedListeners::new( - http_listener, - socks_listener, - ))), - ) - } else { - let runtime = config::resolve_runtime(¤t_cfg)?; - ( - self.http_addr.unwrap_or(runtime.http_addr), - self.socks_addr.unwrap_or(runtime.socks_addr), - None, - ) - }; + let (requested_http_addr, requested_socks_addr, reserved_listeners) = if self + .managed_by_codex + { + let runtime = config::resolve_runtime(¤t_cfg)?; + #[cfg(target_os = "windows")] + let (managed_http_addr, managed_socks_addr) = config::clamp_bind_addrs( + runtime.http_addr, + runtime.socks_addr, + ¤t_cfg.network, + ); + #[cfg(target_os = "windows")] + let reserved = reserve_windows_managed_listeners( + managed_http_addr, + managed_socks_addr, + current_cfg.network.enable_socks5, + ) + .context("reserve managed loopback proxy listeners")?; + #[cfg(not(target_os = "windows"))] + let reserved = reserve_loopback_ephemeral_listeners(current_cfg.network.enable_socks5) + .context("reserve managed loopback proxy listeners")?; + let http_addr = reserved.http_addr()?; + let socks_addr = reserved.socks_addr(runtime.socks_addr)?; + ( + http_addr, + socks_addr, + Some(reserved.into_reserved_listeners()), + ) + } else { + let runtime = config::resolve_runtime(¤t_cfg)?; + ( + self.http_addr.unwrap_or(runtime.http_addr), + self.socks_addr.unwrap_or(runtime.socks_addr), + None, + ) + }; // Reapply bind clamping for caller overrides so unix-socket proxying stays loopback-only. let (http_addr, socks_addr) = config::clamp_bind_addrs( @@ -192,7 +232,7 @@ impl NetworkProxyBuilder { fn reserve_loopback_ephemeral_listeners( reserve_socks_listener: bool, -) -> Result<(StdTcpListener, Option)> { +) -> Result { let http_listener = reserve_loopback_ephemeral_listener().context("reserve HTTP proxy listener")?; let socks_listener = if reserve_socks_listener { @@ -200,7 +240,53 @@ fn reserve_loopback_ephemeral_listeners( } else { None }; - Ok((http_listener, socks_listener)) + Ok(ReservedListenerSet::new(http_listener, socks_listener)) +} + +#[cfg(target_os = "windows")] +fn reserve_windows_managed_listeners( + http_addr: SocketAddr, + socks_addr: SocketAddr, + reserve_socks_listener: bool, +) -> Result { + let http_addr = windows_managed_loopback_addr(http_addr); + let socks_addr = windows_managed_loopback_addr(socks_addr); + + match try_reserve_windows_managed_listeners(http_addr, socks_addr, reserve_socks_listener) { + Ok(listeners) => Ok(listeners), + Err(err) if err.kind() == std::io::ErrorKind::AddrInUse => { + warn!("managed Windows proxy ports are busy; falling back to ephemeral loopback ports"); + reserve_loopback_ephemeral_listeners(reserve_socks_listener) + .context("reserve fallback loopback proxy listeners") + } + Err(err) => Err(err).context("reserve Windows managed proxy listeners"), + } +} + +#[cfg(target_os = "windows")] +fn try_reserve_windows_managed_listeners( + http_addr: SocketAddr, + socks_addr: SocketAddr, + reserve_socks_listener: bool, +) -> std::io::Result { + let http_listener = StdTcpListener::bind(http_addr)?; + let socks_listener = if reserve_socks_listener { + Some(StdTcpListener::bind(socks_addr)?) + } else { + None + }; + Ok(ReservedListenerSet::new(http_listener, socks_listener)) +} + +#[cfg(target_os = "windows")] +fn windows_managed_loopback_addr(addr: SocketAddr) -> SocketAddr { + if !addr.ip().is_loopback() { + warn!( + "managed Windows proxies must bind to loopback; clamping {addr} to 127.0.0.1:{}", + addr.port() + ); + } + SocketAddr::from(([127, 0, 0, 1], addr.port())) } fn reserve_loopback_ephemeral_listener() -> Result { @@ -570,10 +656,12 @@ mod tests { use std::net::Ipv4Addr; #[tokio::test] - async fn managed_proxy_builder_uses_loopback_ephemeral_ports() { - let state = Arc::new(network_proxy_state_for_policy( - NetworkProxySettings::default(), - )); + async fn managed_proxy_builder_uses_loopback_ports() { + let state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { + proxy_url: "http://127.0.0.1:43128".to_string(), + socks_url: "http://127.0.0.1:48081".to_string(), + ..NetworkProxySettings::default() + })); let proxy = match NetworkProxy::builder().state(state).build().await { Ok(proxy) => proxy, Err(err) => { @@ -589,8 +677,22 @@ mod tests { assert!(proxy.http_addr.ip().is_loopback()); assert!(proxy.socks_addr.ip().is_loopback()); - assert_ne!(proxy.http_addr.port(), 0); - assert_ne!(proxy.socks_addr.port(), 0); + #[cfg(target_os = "windows")] + { + assert_eq!( + proxy.http_addr, + "127.0.0.1:43128".parse::().unwrap() + ); + assert_eq!( + proxy.socks_addr, + "127.0.0.1:48081".parse::().unwrap() + ); + } + #[cfg(not(target_os = "windows"))] + { + assert_ne!(proxy.http_addr.port(), 0); + assert_ne!(proxy.socks_addr.port(), 0); + } } #[tokio::test] @@ -622,6 +724,7 @@ mod tests { async fn managed_proxy_builder_does_not_reserve_socks_listener_when_disabled() { let settings = NetworkProxySettings { enable_socks5: false, + proxy_url: "http://127.0.0.1:43128".to_string(), socks_url: "http://127.0.0.1:43129".to_string(), ..NetworkProxySettings::default() }; @@ -640,6 +743,7 @@ mod tests { }; assert!(proxy.http_addr.ip().is_loopback()); + assert_ne!(proxy.http_addr.port(), 0); assert_eq!( proxy.socks_addr, "127.0.0.1:43129".parse::().unwrap() @@ -654,6 +758,47 @@ mod tests { ); } + #[cfg(target_os = "windows")] + #[test] + fn windows_managed_loopback_addr_clamps_non_loopback_inputs() { + assert_eq!( + windows_managed_loopback_addr("0.0.0.0:3128".parse::().unwrap()), + "127.0.0.1:3128".parse::().unwrap() + ); + assert_eq!( + windows_managed_loopback_addr("[::]:8081".parse::().unwrap()), + "127.0.0.1:8081".parse::().unwrap() + ); + } + + #[cfg(target_os = "windows")] + #[test] + fn reserve_windows_managed_listeners_falls_back_when_http_port_is_busy() { + let occupied = StdTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0))).unwrap(); + let busy_port = occupied.local_addr().unwrap().port(); + + let reserved = reserve_windows_managed_listeners( + SocketAddr::from(([127, 0, 0, 1], busy_port)), + SocketAddr::from(([127, 0, 0, 1], 48081)), + false, + ) + .unwrap(); + + assert!(reserved.socks_listener.is_none()); + assert!( + reserved + .http_listener + .local_addr() + .unwrap() + .ip() + .is_loopback() + ); + assert_ne!( + reserved.http_listener.local_addr().unwrap().port(), + busy_port + ); + } + #[test] fn proxy_url_env_value_resolves_lowercase_aliases() { let mut env = HashMap::new(); diff --git a/codex-rs/windows-sandbox-rs/sandbox_smoketests.py b/codex-rs/windows-sandbox-rs/sandbox_smoketests.py index 0fc049d4ed..f629a4c6d9 100644 --- a/codex-rs/windows-sandbox-rs/sandbox_smoketests.py +++ b/codex-rs/windows-sandbox-rs/sandbox_smoketests.py @@ -6,8 +6,13 @@ import os import sys import shutil import subprocess +import contextlib +import http.client +import http.server +import threading from pathlib import Path from typing import List, Optional, Tuple +from urllib.parse import urlsplit def _resolve_codex_cmd() -> List[str]: """Resolve the Codex CLI to invoke `codex sandbox windows`. @@ -135,6 +140,68 @@ def make_symlink(link: Path, target: Path) -> bool: cp = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) return cp.returncode == 0 and link.exists() +class _QuietHandler(http.server.BaseHTTPRequestHandler): + def log_message(self, format, *args): + pass + +class _TargetHandler(_QuietHandler): + def do_GET(self): + body = b"proxy-ok" + self.send_response(200) + self.send_header("Content-Type", "text/plain") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + +class _ProxyHandler(_QuietHandler): + def do_GET(self): + parsed = urlsplit(self.path) + if not parsed.scheme or not parsed.hostname: + self.send_error(400, "absolute URL required") + return + if parsed.hostname not in ("127.0.0.1", "localhost"): + self.send_error(403, "only loopback hosts are allowed in smoke proxy") + return + path = parsed.path or "/" + if parsed.query: + path = f"{path}?{parsed.query}" + conn = None + try: + conn = http.client.HTTPConnection(parsed.hostname, parsed.port or 80, timeout=2) + conn.request("GET", path) + upstream = conn.getresponse() + body = upstream.read() + except Exception as err: + self.send_error(502, f"proxy upstream error: {err}") + return + finally: + if conn is not None: + with contextlib.suppress(Exception): + conn.close() + self.send_response(upstream.status, upstream.reason) + self.send_header("Content-Type", "text/plain") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + +@contextlib.contextmanager +def start_loopback_proxy_fixture(): + target = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _TargetHandler) + proxy = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _ProxyHandler) + target_port = target.server_address[1] + proxy_port = proxy.server_address[1] + target_thread = threading.Thread(target=target.serve_forever, daemon=True) + proxy_thread = threading.Thread(target=proxy.serve_forever, daemon=True) + target_thread.start() + proxy_thread.start() + try: + yield target_port, proxy_port + finally: + proxy.shutdown() + target.shutdown() + proxy.server_close() + target.server_close() + def summarize(results: List[CaseResult]) -> int: ok = sum(1 for r in results if r.ok) total = len(results) @@ -278,7 +345,65 @@ def main() -> int: "try { iwr http://neverssl.com -TimeoutSec 2 } catch { exit 1 }"], WS_ROOT) add("WS: iwr network blocked", rc != 0, f"rc={rc}") - # 17. RO: deny TEMP writes via PowerShell + # 17. WS: direct loopback blocked, proxy loopback allowed via env proxy + if have("curl"): + with start_loopback_proxy_fixture() as (target_port, proxy_port): + proxy_home = WS_ROOT / ".codex_proxy_smoke" + remove_if_exists(proxy_home) + proxy_home.mkdir(parents=True, exist_ok=True) + proxy_url = f"http://127.0.0.1:{proxy_port}" + proxy_env = { + "CODEX_HOME": str(proxy_home), + "HTTP_PROXY": proxy_url, + "http_proxy": proxy_url, + "ALL_PROXY": proxy_url, + "all_proxy": proxy_url, + "NO_PROXY": "", + "no_proxy": "", + } + proxied_cmd = [ + "curl", + "--noproxy", + "", + "--connect-timeout", + "2", + "--max-time", + "4", + f"http://127.0.0.1:{target_port}/proxied", + ] + rc_proxy, out_proxy, err_proxy = run_sbx( + "workspace-write", + proxied_cmd, + WS_ROOT, + env_extra=proxy_env, + ) + add( + "WS: loopback proxy allowed", + rc_proxy == 0 and "proxy-ok" in out_proxy, + f"rc={rc_proxy}, out={out_proxy}, err={err_proxy}", + ) + + direct_cmd = [ + "curl", + "--noproxy", + "*", + "--connect-timeout", + "1", + "--max-time", + "2", + f"http://127.0.0.1:{target_port}/direct", + ] + rc_direct, _out_direct, err_direct = run_sbx( + "workspace-write", + direct_cmd, + WS_ROOT, + env_extra={"CODEX_HOME": str(proxy_home)}, + ) + add("WS: direct loopback blocked", rc_direct != 0, f"rc={rc_direct}, err={err_direct}") + else: + add("WS: direct/proxy loopback tests (curl missing)", True, "curl not installed") + + # 18. RO: deny TEMP writes via PowerShell rc, out, err = run_sbx("read-only", ["powershell", "-NoLogo", "-NoProfile", "-Command", "Set-Content -LiteralPath $env:TEMP\\ro_tmpfail.txt -Value 'x'"], WS_ROOT) @@ -287,21 +412,21 @@ def main() -> int: else: add("RO: TEMP write denied (PS, skipped)", True) - # 18. WS: curl version check — don't rely on stub, just succeed + # 19. WS: curl version check — don't rely on stub, just succeed if have("curl"): rc, out, err = run_sbx("workspace-write", ["cmd", "/c", "curl --version"], WS_ROOT) add("WS: curl present (version prints)", rc == 0, f"rc={rc}, err={err}") else: add("WS: curl present (optional, skipped)", True) - # 19. Optional: ripgrep version + # 20. Optional: ripgrep version if have("rg"): rc, out, err = run_sbx("workspace-write", ["cmd", "/c", "rg --version"], WS_ROOT) add("WS: rg --version (optional)", rc == 0, f"rc={rc}, err={err}") else: add("WS: rg --version (optional, skipped)", True) - # 20. Optional: git --version + # 21. Optional: git --version if have("git"): rc, out, err = run_sbx("workspace-write", ["git", "--version"], WS_ROOT) add("WS: git --version (optional)", rc == 0, f"rc={rc}, err={err}") diff --git a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs index f6c4563e17..365ac37d6f 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs @@ -1,4 +1,20 @@ +use std::collections::HashMap; +use std::path::Path; + +pub struct ElevatedSandboxCaptureRequest<'a> { + pub policy_json_or_preset: &'a str, + pub sandbox_policy_cwd: &'a Path, + pub codex_home: &'a Path, + pub command: Vec, + pub cwd: &'a Path, + pub env_map: HashMap, + pub timeout_ms: Option, + pub use_private_desktop: bool, + pub proxy_enforced: bool, +} + mod windows_impl { + use super::ElevatedSandboxCaptureRequest; use crate::acl::allow_null_device; use crate::allow::compute_allow_paths; use crate::allow::AllowDenyPaths; @@ -203,15 +219,19 @@ mod windows_impl { /// Launches the command runner under the sandbox user and captures its output. #[allow(clippy::too_many_arguments)] pub fn run_windows_sandbox_capture( - policy_json_or_preset: &str, - sandbox_policy_cwd: &Path, - codex_home: &Path, - command: Vec, - cwd: &Path, - mut env_map: HashMap, - timeout_ms: Option, - use_private_desktop: bool, + request: ElevatedSandboxCaptureRequest<'_>, ) -> Result { + let ElevatedSandboxCaptureRequest { + policy_json_or_preset, + sandbox_policy_cwd, + codex_home, + command, + cwd, + mut env_map, + timeout_ms, + use_private_desktop, + proxy_enforced, + } = request; let policy = parse_policy(policy_json_or_preset)?; normalize_null_device_env(&mut env_map); ensure_non_interactive_pager(&mut env_map); @@ -224,8 +244,14 @@ mod windows_impl { let logs_base_dir: Option<&Path> = Some(sandbox_base.as_path()); log_start(&command, logs_base_dir); - let sandbox_creds = - require_logon_sandbox_creds(&policy, sandbox_policy_cwd, cwd, &env_map, codex_home)?; + let sandbox_creds = require_logon_sandbox_creds( + &policy, + sandbox_policy_cwd, + cwd, + &env_map, + codex_home, + proxy_enforced, + )?; let sandbox_sid = resolve_sid(&sandbox_creds.username).map_err(|err: anyhow::Error| { io::Error::new(io::ErrorKind::PermissionDenied, err.to_string()) })?; @@ -480,11 +506,9 @@ pub use windows_impl::run_windows_sandbox_capture; #[cfg(not(target_os = "windows"))] mod stub { + use super::ElevatedSandboxCaptureRequest; use anyhow::bail; use anyhow::Result; - use codex_protocol::protocol::SandboxPolicy; - use std::collections::HashMap; - use std::path::Path; #[derive(Debug, Default)] pub struct CaptureResult { @@ -497,14 +521,7 @@ mod stub { /// Stub implementation for non-Windows targets; sandboxing only works on Windows. #[allow(clippy::too_many_arguments)] pub fn run_windows_sandbox_capture( - _policy_json_or_preset: &str, - _sandbox_policy_cwd: &Path, - _codex_home: &Path, - _command: Vec, - _cwd: &Path, - _env_map: HashMap, - _timeout_ms: Option, - _use_private_desktop: bool, + _request: ElevatedSandboxCaptureRequest<'_>, ) -> Result { bail!("Windows sandbox is only available on Windows") } diff --git a/codex-rs/windows-sandbox-rs/src/firewall.rs b/codex-rs/windows-sandbox-rs/src/firewall.rs index b4939fd42b..a95d3b57e2 100644 --- a/codex-rs/windows-sandbox-rs/src/firewall.rs +++ b/codex-rs/windows-sandbox-rs/src/firewall.rs @@ -9,10 +9,13 @@ use windows::core::BSTR; use windows::Win32::Foundation::VARIANT_TRUE; use windows::Win32::NetworkManagement::WindowsFirewall::INetFwPolicy2; use windows::Win32::NetworkManagement::WindowsFirewall::INetFwRule3; +use windows::Win32::NetworkManagement::WindowsFirewall::INetFwRules; use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2; use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_ACTION_BLOCK; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_ANY; +use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_TCP; +use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_UDP; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_PROFILE2_ALL; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_OUT; use windows::Win32::System::Com::CoCreateInstance; @@ -27,9 +30,128 @@ use codex_windows_sandbox::SetupFailure; // This is the stable identifier we use to find/update the rule idempotently. // It intentionally does not change between installs. const OFFLINE_BLOCK_RULE_NAME: &str = "codex_sandbox_offline_block_outbound"; +const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_tcp"; +const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_udp"; // Friendly text shown in the firewall UI. -const OFFLINE_BLOCK_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Outbound"; +const OFFLINE_BLOCK_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Non-Loopback Outbound"; +const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY: &str = + "Codex Sandbox Offline - Block Loopback TCP (Except Proxy)"; +const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback UDP"; +const OFFLINE_PROXY_ALLOW_RULE_NAME: &str = "codex_sandbox_offline_allow_loopback_proxy"; +const LOOPBACK_REMOTE_ADDRESSES: &str = "127.0.0.0/8,::1"; +const NON_LOOPBACK_REMOTE_ADDRESSES: &str = + "0.0.0.0-126.255.255.255,128.0.0.0-255.255.255.255,::,::2-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; + +struct BlockRuleSpec<'a> { + internal_name: &'a str, + friendly_desc: &'a str, + protocol: i32, + local_user_spec: &'a str, + offline_sid: &'a str, + remote_addresses: Option<&'a str>, + remote_ports: Option<&'a str>, +} + +pub fn ensure_offline_proxy_allowlist( + offline_sid: &str, + proxy_ports: &[u16], + allow_local_binding: bool, + log: &mut File, +) -> Result<()> { + let local_user_spec = format!("O:LSD:(A;;CC;;;{offline_sid})"); + + let hr = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }; + if hr.is_err() { + return Err(anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallComInitFailed, + format!("CoInitializeEx failed: {hr:?}"), + ))); + } + + let result = unsafe { + (|| -> Result<()> { + let policy: INetFwPolicy2 = CoCreateInstance(&NetFwPolicy2, None, CLSCTX_INPROC_SERVER) + .map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallPolicyAccessFailed, + format!("CoCreateInstance NetFwPolicy2 failed: {err:?}"), + )) + })?; + let rules = policy.Rules().map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallPolicyAccessFailed, + format!("INetFwPolicy2::Rules failed: {err:?}"), + )) + })?; + + if allow_local_binding { + // Remove the legacy overlapping allow rule before returning to the local-binding + // mode so stale proxy exceptions do not linger. + remove_rule_if_present(&rules, OFFLINE_PROXY_ALLOW_RULE_NAME, log)?; + remove_rule_if_present(&rules, OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME, log)?; + remove_rule_if_present(&rules, OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, log)?; + return Ok(()); + } + + ensure_block_rule( + &rules, + &BlockRuleSpec { + internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME, + friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY, + protocol: NET_FW_IP_PROTOCOL_UDP.0, + local_user_spec: &local_user_spec, + offline_sid, + remote_addresses: Some(LOOPBACK_REMOTE_ADDRESSES), + remote_ports: None, + }, + log, + )?; + + // Install a broad TCP loopback block before narrowing it to the allowed proxy port + // complement. If the narrowing update fails, the sandbox remains fail-closed. + ensure_block_rule( + &rules, + &BlockRuleSpec { + internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, + friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY, + protocol: NET_FW_IP_PROTOCOL_TCP.0, + local_user_spec: &local_user_spec, + offline_sid, + remote_addresses: Some(LOOPBACK_REMOTE_ADDRESSES), + remote_ports: None, + }, + log, + )?; + + // Remove the legacy overlapping allow rule only after the explicit block rules are in + // place so transitions back to proxy-only mode do not fail open. + remove_rule_if_present(&rules, OFFLINE_PROXY_ALLOW_RULE_NAME, log)?; + + if let Some(blocked_remote_ports) = blocked_loopback_tcp_remote_ports(proxy_ports) { + ensure_block_rule( + &rules, + &BlockRuleSpec { + internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, + friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY, + protocol: NET_FW_IP_PROTOCOL_TCP.0, + local_user_spec: &local_user_spec, + offline_sid, + remote_addresses: Some(LOOPBACK_REMOTE_ADDRESSES), + remote_ports: Some(&blocked_remote_ports), + }, + log, + )?; + } + Ok(()) + })() + }; + + unsafe { + CoUninitialize(); + } + result +} pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut File) -> Result<()> { let local_user_spec = format!("O:LSD:(A;;CC;;;{offline_sid})"); @@ -61,11 +183,15 @@ pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut File) -> Resul // Block all outbound IP protocols for this user. ensure_block_rule( &rules, - OFFLINE_BLOCK_RULE_NAME, - OFFLINE_BLOCK_RULE_FRIENDLY, - NET_FW_IP_PROTOCOL_ANY.0, - &local_user_spec, - offline_sid, + &BlockRuleSpec { + internal_name: OFFLINE_BLOCK_RULE_NAME, + friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY, + protocol: NET_FW_IP_PROTOCOL_ANY.0, + local_user_spec: &local_user_spec, + offline_sid, + remote_addresses: Some(NON_LOOPBACK_REMOTE_ADDRESSES), + remote_ports: None, + }, log, )?; Ok(()) @@ -78,16 +204,22 @@ pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut File) -> Resul result } -fn ensure_block_rule( - rules: &windows::Win32::NetworkManagement::WindowsFirewall::INetFwRules, - internal_name: &str, - friendly_desc: &str, - protocol: i32, - local_user_spec: &str, - offline_sid: &str, - log: &mut File, -) -> Result<()> { +fn remove_rule_if_present(rules: &INetFwRules, internal_name: &str, log: &mut File) -> Result<()> { let name = BSTR::from(internal_name); + if unsafe { rules.Item(&name) }.is_ok() { + unsafe { rules.Remove(&name) }.map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, + format!("Rules::Remove failed for {internal_name}: {err:?}"), + )) + })?; + log_line(log, &format!("firewall rule removed name={internal_name}"))?; + } + Ok(()) +} + +fn ensure_block_rule(rules: &INetFwRules, spec: &BlockRuleSpec<'_>, log: &mut File) -> Result<()> { + let name = BSTR::from(spec.internal_name); let rule: INetFwRule3 = match unsafe { rules.Item(&name) } { Ok(existing) => existing.cast().map_err(|err| { anyhow::Error::new(SetupFailure::new( @@ -112,13 +244,7 @@ fn ensure_block_rule( )) })?; // Set all properties before adding the rule so we don't leave half-configured rules. - configure_rule( - &new_rule, - friendly_desc, - protocol, - local_user_spec, - offline_sid, - )?; + configure_rule(&new_rule, spec)?; unsafe { rules.Add(&new_rule) }.map_err(|err| { anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, @@ -130,26 +256,24 @@ fn ensure_block_rule( }; // Always re-apply fields to keep the setup idempotent. - configure_rule(&rule, friendly_desc, protocol, local_user_spec, offline_sid)?; + configure_rule(&rule, spec)?; + + let remote_addresses_log = spec.remote_addresses.unwrap_or("*"); + let remote_ports_log = spec.remote_ports.unwrap_or("*"); log_line( log, &format!( - "firewall rule configured name={internal_name} protocol={protocol} LocalUserAuthorizedList={local_user_spec}" + "firewall rule configured name={} protocol={} RemoteAddresses={remote_addresses_log} RemotePorts={remote_ports_log} LocalUserAuthorizedList={}", + spec.internal_name, spec.protocol, spec.local_user_spec ), )?; Ok(()) } -fn configure_rule( - rule: &INetFwRule3, - friendly_desc: &str, - protocol: i32, - local_user_spec: &str, - offline_sid: &str, -) -> Result<()> { +fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> { unsafe { - rule.SetDescription(&BSTR::from(friendly_desc)) + rule.SetDescription(&BSTR::from(spec.friendly_desc)) .map_err(|err| { anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, @@ -180,13 +304,29 @@ fn configure_rule( format!("SetProfiles failed: {err:?}"), )) })?; - rule.SetProtocol(protocol).map_err(|err| { + rule.SetProtocol(spec.protocol).map_err(|err| { anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, format!("SetProtocol failed: {err:?}"), )) })?; - rule.SetLocalUserAuthorizedList(&BSTR::from(local_user_spec)) + let remote_addresses = spec.remote_addresses.unwrap_or("*"); + rule.SetRemoteAddresses(&BSTR::from(remote_addresses)) + .map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, + format!("SetRemoteAddresses failed: {err:?}"), + )) + })?; + let remote_ports = spec.remote_ports.unwrap_or("*"); + rule.SetRemotePorts(&BSTR::from(remote_ports)) + .map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, + format!("SetRemotePorts failed: {err:?}"), + )) + })?; + rule.SetLocalUserAuthorizedList(&BSTR::from(spec.local_user_spec)) .map_err(|err| { anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, @@ -203,17 +343,59 @@ fn configure_rule( )) })?; let actual_str = actual.to_string(); - if !actual_str.contains(offline_sid) { + if !actual_str.contains(spec.offline_sid) { return Err(anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleVerifyFailed, format!( - "offline firewall rule user scope mismatch: expected SID {offline_sid}, got {actual_str}" + "offline firewall rule user scope mismatch: expected SID {}, got {actual_str}", + spec.offline_sid ), ))); } Ok(()) } +fn blocked_loopback_tcp_remote_ports(proxy_ports: &[u16]) -> Option { + let mut allowed_ports = proxy_ports + .iter() + .copied() + .filter(|port| *port != 0) + .collect::>(); + allowed_ports.sort_unstable(); + allowed_ports.dedup(); + + let mut blocked_ranges = Vec::new(); + let mut start = 1_u32; + for port in allowed_ports { + let port = u32::from(port); + if port < start { + continue; + } + if port > start { + blocked_ranges.push(port_range_string(start, port - 1)); + } + start = port + 1; + } + + if start <= u32::from(u16::MAX) { + blocked_ranges.push(port_range_string(start, u32::from(u16::MAX))); + } + + if blocked_ranges.is_empty() { + None + } else { + Some(blocked_ranges.join(",")) + } +} + +fn port_range_string(start: u32, end: u32) -> String { + if start == end { + start.to_string() + } else { + format!("{start}-{end}") + } +} + fn log_line(log: &mut File, msg: &str) -> Result<()> { let ts = chrono::Utc::now().to_rfc3339(); writeln!(log, "[{ts}] {msg}")?; diff --git a/codex-rs/windows-sandbox-rs/src/identity.rs b/codex-rs/windows-sandbox-rs/src/identity.rs index b9d5e29ed7..b7768c1a3e 100644 --- a/codex-rs/windows-sandbox-rs/src/identity.rs +++ b/codex-rs/windows-sandbox-rs/src/identity.rs @@ -3,9 +3,11 @@ use crate::logging::debug_log; use crate::policy::SandboxPolicy; use crate::setup::gather_read_roots; use crate::setup::gather_write_roots; +use crate::setup::offline_proxy_settings_from_env; use crate::setup::run_elevated_setup; use crate::setup::sandbox_users_path; use crate::setup::setup_marker_path; +use crate::setup::SandboxNetworkIdentity; use crate::setup::SandboxUserRecord; use crate::setup::SandboxUsersFile; use crate::setup::SetupMarker; @@ -33,7 +35,8 @@ pub struct SandboxCreds { /// Returns true when the on-disk setup artifacts exist and match the current /// setup version. /// -/// This reuses the same marker/users validation used by `require_logon_sandbox_creds`. +/// This is a coarse readiness check; `require_logon_sandbox_creds` performs the +/// additional runtime validation for offline firewall settings. pub fn sandbox_setup_is_complete(codex_home: &Path) -> bool { let marker_ok = matches!(load_marker(codex_home), Ok(Some(marker)) if marker.version_matches()); if !marker_ok { @@ -101,7 +104,10 @@ fn decode_password(record: &SandboxUserRecord) -> Result { Ok(pwd) } -fn select_identity(policy: &SandboxPolicy, codex_home: &Path) -> Result> { +fn select_identity( + network_identity: SandboxNetworkIdentity, + codex_home: &Path, +) -> Result> { let _marker = match load_marker(codex_home)? { Some(m) if m.version_matches() => m, _ => return Ok(None), @@ -110,10 +116,9 @@ fn select_identity(policy: &SandboxPolicy, codex_home: &Path) -> Result u, _ => return Ok(None), }; - let chosen = if !policy.has_full_network_access() { - users.offline - } else { - users.online + let chosen = match network_identity { + SandboxNetworkIdentity::Offline => users.offline, + SandboxNetworkIdentity::Online => users.online, }; let password = decode_password(&chosen)?; Ok(Some(SandboxIdentity { @@ -128,25 +133,34 @@ pub fn require_logon_sandbox_creds( command_cwd: &Path, env_map: &HashMap, codex_home: &Path, + proxy_enforced: bool, ) -> Result { let sandbox_dir = crate::setup::sandbox_dir(codex_home); let needed_read = gather_read_roots(command_cwd, policy, codex_home); let needed_write = gather_write_roots(policy, policy_cwd, command_cwd, env_map); + let network_identity = SandboxNetworkIdentity::from_policy(policy, proxy_enforced); + let desired_offline_proxy_settings = + offline_proxy_settings_from_env(env_map, network_identity); // NOTE: Do not add CODEX_HOME/.sandbox to `needed_write`; it must remain non-writable by the // restricted capability token. The setup helper's `lock_sandbox_dir` is responsible for // granting the sandbox group access to this directory without granting the capability SID. let mut setup_reason: Option = None; - let mut _existing_marker: Option = None; let mut identity = match load_marker(codex_home)? { Some(marker) if marker.version_matches() => { - _existing_marker = Some(marker.clone()); - let selected = select_identity(policy, codex_home)?; - if selected.is_none() { - setup_reason = - Some("sandbox users missing or incompatible with marker version".to_string()); + if let Some(reason) = + marker.request_mismatch_reason(network_identity, &desired_offline_proxy_settings) + { + setup_reason = Some(reason); + None + } else { + let selected = select_identity(network_identity, codex_home)?; + if selected.is_none() { + setup_reason = + Some("sandbox users missing or incompatible with marker version".to_string()); + } + selected } - selected } _ => { setup_reason = Some("sandbox setup marker missing or incompatible".to_string()); @@ -164,18 +178,30 @@ pub fn require_logon_sandbox_creds( crate::logging::log_note("sandbox setup required", Some(&sandbox_dir)); } run_elevated_setup( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - Some(needed_read.clone()), - Some(needed_write.clone()), + crate::setup::SandboxSetupRequest { + policy, + policy_cwd, + command_cwd, + env_map, + codex_home, + proxy_enforced, + }, + crate::setup::SetupRootOverrides { + read_roots: Some(needed_read.clone()), + write_roots: Some(needed_write.clone()), + }, )?; - identity = select_identity(policy, codex_home)?; + identity = select_identity(network_identity, codex_home)?; } // Always refresh ACLs (non-elevated) for current roots via the setup binary. - crate::setup::run_setup_refresh(policy, policy_cwd, command_cwd, env_map, codex_home)?; + crate::setup::run_setup_refresh( + policy, + policy_cwd, + command_cwd, + env_map, + codex_home, + proxy_enforced, + )?; let identity = identity.ok_or_else(|| { anyhow!( "Windows sandbox setup is missing or out of date; rerun the sandbox setup with elevation" diff --git a/codex-rs/windows-sandbox-rs/src/lib.rs b/codex-rs/windows-sandbox-rs/src/lib.rs index 73193a207d..11afda750d 100644 --- a/codex-rs/windows-sandbox-rs/src/lib.rs +++ b/codex-rs/windows-sandbox-rs/src/lib.rs @@ -72,6 +72,8 @@ pub use dpapi::unprotect as dpapi_unprotect; #[cfg(target_os = "windows")] pub use elevated_impl::run_windows_sandbox_capture as run_windows_sandbox_capture_elevated; #[cfg(target_os = "windows")] +pub use elevated_impl::ElevatedSandboxCaptureRequest; +#[cfg(target_os = "windows")] pub use helper_materialization::resolve_current_exe_for_launch; #[cfg(target_os = "windows")] pub use hide_users::hide_current_user_profile_dir; @@ -116,6 +118,10 @@ pub use setup::sandbox_dir; #[cfg(target_os = "windows")] pub use setup::sandbox_secrets_dir; #[cfg(target_os = "windows")] +pub use setup::SandboxSetupRequest; +#[cfg(target_os = "windows")] +pub use setup::SetupRootOverrides; +#[cfg(target_os = "windows")] pub use setup::SETUP_VERSION; #[cfg(target_os = "windows")] pub use setup_error::extract_failure as extract_setup_failure; diff --git a/codex-rs/windows-sandbox-rs/src/sandbox_users.rs b/codex-rs/windows-sandbox-rs/src/sandbox_users.rs index 5ab39c3cec..e006b696fb 100644 --- a/codex-rs/windows-sandbox-rs/src/sandbox_users.rs +++ b/codex-rs/windows-sandbox-rs/src/sandbox_users.rs @@ -63,6 +63,8 @@ pub fn provision_sandbox_users( codex_home: &Path, offline_username: &str, online_username: &str, + proxy_ports: &[u16], + allow_local_binding: bool, log: &mut File, ) -> Result<()> { ensure_sandbox_users_group(log)?; @@ -80,6 +82,8 @@ pub fn provision_sandbox_users( &offline_password, online_username, &online_password, + proxy_ports, + allow_local_binding, )?; Ok(()) } @@ -388,6 +392,8 @@ struct SetupMarker { offline_username: String, online_username: String, created_at: String, + proxy_ports: Vec, + allow_local_binding: bool, read_roots: Vec, write_roots: Vec, } @@ -398,6 +404,8 @@ fn write_secrets( offline_pwd: &str, online_user: &str, online_pwd: &str, + proxy_ports: &[u16], + allow_local_binding: bool, ) -> Result<()> { let sandbox_dir = sandbox_dir(codex_home); std::fs::create_dir_all(&sandbox_dir).map_err(|err| { @@ -447,6 +455,8 @@ fn write_secrets( offline_username: offline_user.to_string(), online_username: online_user.to_string(), created_at: chrono::Utc::now().to_rfc3339(), + proxy_ports: proxy_ports.to_vec(), + allow_local_binding, read_roots: Vec::new(), write_roots: Vec::new(), }; diff --git a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs index 86c1d104e0..8a72b22a91 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs @@ -84,6 +84,10 @@ struct Payload { command_cwd: PathBuf, read_roots: Vec, write_roots: Vec, + #[serde(default)] + proxy_ports: Vec, + #[serde(default)] + allow_local_binding: bool, real_user: String, #[serde(default)] mode: SetupMode, @@ -510,6 +514,8 @@ fn run_setup_full(payload: &Payload, log: &mut File, sbx_dir: &Path) -> Result<( &payload.codex_home, &payload.offline_username, &payload.online_username, + &payload.proxy_ports, + payload.allow_local_binding, log, ); if let Err(err) = provision_result { @@ -572,6 +578,21 @@ fn run_setup_full(payload: &Payload, log: &mut File, sbx_dir: &Path) -> Result<( }; let mut refresh_errors: Vec = Vec::new(); if !refresh_only { + let proxy_allowlist_result = firewall::ensure_offline_proxy_allowlist( + &offline_sid_str, + &payload.proxy_ports, + payload.allow_local_binding, + log, + ); + if let Err(err) = proxy_allowlist_result { + if extract_setup_failure(&err).is_some() { + return Err(err); + } + return Err(anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, + format!("ensure offline proxy allowlist failed: {err}"), + ))); + } let firewall_result = firewall::ensure_offline_outbound_block(&offline_sid_str, log); if let Err(err) = firewall_result { if extract_setup_failure(&err).is_some() { diff --git a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs index 3296d09c43..962a53d60c 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs @@ -1,5 +1,6 @@ use serde::Deserialize; use serde::Serialize; +use std::collections::BTreeSet; use std::collections::HashMap; use std::collections::HashSet; use std::ffi::c_void; @@ -78,21 +79,39 @@ pub fn sandbox_users_path(codex_home: &Path) -> PathBuf { sandbox_secrets_dir(codex_home).join("sandbox_users.json") } +pub struct SandboxSetupRequest<'a> { + pub policy: &'a SandboxPolicy, + pub policy_cwd: &'a Path, + pub command_cwd: &'a Path, + pub env_map: &'a HashMap, + pub codex_home: &'a Path, + pub proxy_enforced: bool, +} + +#[derive(Default)] +pub struct SetupRootOverrides { + pub read_roots: Option>, + pub write_roots: Option>, +} + pub fn run_setup_refresh( policy: &SandboxPolicy, policy_cwd: &Path, command_cwd: &Path, env_map: &HashMap, codex_home: &Path, + proxy_enforced: bool, ) -> Result<()> { run_setup_refresh_inner( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - /*read_roots_override*/ None, - /*write_roots_override*/ None, + SandboxSetupRequest { + policy, + policy_cwd, + command_cwd, + env_map, + codex_home, + proxy_enforced, + }, + SetupRootOverrides::default(), ) } @@ -103,53 +122,51 @@ pub fn run_setup_refresh_with_extra_read_roots( env_map: &HashMap, codex_home: &Path, extra_read_roots: Vec, + proxy_enforced: bool, ) -> Result<()> { let mut read_roots = gather_read_roots(command_cwd, policy, codex_home); read_roots.extend(extra_read_roots); run_setup_refresh_inner( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - Some(read_roots), - Some(Vec::new()), + SandboxSetupRequest { + policy, + policy_cwd, + command_cwd, + env_map, + codex_home, + proxy_enforced, + }, + SetupRootOverrides { + read_roots: Some(read_roots), + write_roots: Some(Vec::new()), + }, ) } fn run_setup_refresh_inner( - policy: &SandboxPolicy, - policy_cwd: &Path, - command_cwd: &Path, - env_map: &HashMap, - codex_home: &Path, - read_roots_override: Option>, - write_roots_override: Option>, + request: SandboxSetupRequest<'_>, + overrides: SetupRootOverrides, ) -> Result<()> { // Skip in danger-full-access. if matches!( - policy, + request.policy, SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. } ) { return Ok(()); } - let (read_roots, write_roots) = build_payload_roots( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - read_roots_override, - write_roots_override, - ); + let (read_roots, write_roots) = build_payload_roots(&request, overrides); + let network_identity = + SandboxNetworkIdentity::from_policy(request.policy, request.proxy_enforced); + let offline_proxy_settings = offline_proxy_settings_from_env(request.env_map, network_identity); let payload = ElevationPayload { version: SETUP_VERSION, offline_username: OFFLINE_USERNAME.to_string(), online_username: ONLINE_USERNAME.to_string(), - codex_home: codex_home.to_path_buf(), - command_cwd: command_cwd.to_path_buf(), + codex_home: request.codex_home.to_path_buf(), + command_cwd: request.command_cwd.to_path_buf(), read_roots, write_roots, + proxy_ports: offline_proxy_settings.proxy_ports, + allow_local_binding: offline_proxy_settings.allow_local_binding, real_user: std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string()), refresh_only: true, }; @@ -159,7 +176,7 @@ fn run_setup_refresh_inner( // Refresh should never request elevation; ensure verb isn't set and we don't trigger UAC. let mut cmd = Command::new(&exe); cmd.arg(&b64).stdout(Stdio::null()).stderr(Stdio::null()); - let cwd = std::env::current_dir().unwrap_or_else(|_| codex_home.to_path_buf()); + let cwd = std::env::current_dir().unwrap_or_else(|_| request.codex_home.to_path_buf()); log_note( &format!( "setup refresh: spawning {} (cwd={}, payload_len={})", @@ -167,14 +184,14 @@ fn run_setup_refresh_inner( cwd.display(), b64.len() ), - Some(&sandbox_dir(codex_home)), + Some(&sandbox_dir(request.codex_home)), ); let status = cmd .status() .map_err(|e| { log_note( &format!("setup refresh: failed to spawn {}: {e}", exe.display()), - Some(&sandbox_dir(codex_home)), + Some(&sandbox_dir(request.codex_home)), ); e }) @@ -182,7 +199,7 @@ fn run_setup_refresh_inner( if !status.success() { log_note( &format!("setup refresh: exited with status {status:?}"), - Some(&sandbox_dir(codex_home)), + Some(&sandbox_dir(request.codex_home)), ); return Err(anyhow!("setup refresh failed with status {}", status)); } @@ -196,12 +213,38 @@ pub struct SetupMarker { pub online_username: String, #[serde(default)] pub created_at: Option, + #[serde(default)] + pub proxy_ports: Vec, + #[serde(default)] + pub allow_local_binding: bool, } impl SetupMarker { pub fn version_matches(&self) -> bool { self.version == SETUP_VERSION } + + pub(crate) fn request_mismatch_reason( + &self, + network_identity: SandboxNetworkIdentity, + offline_proxy_settings: &OfflineProxySettings, + ) -> Option { + if !network_identity.uses_offline_identity() { + return None; + } + if self.proxy_ports == offline_proxy_settings.proxy_ports + && self.allow_local_binding == offline_proxy_settings.allow_local_binding + { + return None; + } + Some(format!( + "offline firewall settings changed (stored_ports={:?}, desired_ports={:?}, stored_allow_local_binding={}, desired_allow_local_binding={})", + self.proxy_ports, + offline_proxy_settings.proxy_ports, + self.allow_local_binding, + offline_proxy_settings.allow_local_binding + )) + } } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -390,11 +433,106 @@ struct ElevationPayload { command_cwd: PathBuf, read_roots: Vec, write_roots: Vec, + #[serde(default)] + proxy_ports: Vec, + #[serde(default)] + allow_local_binding: bool, real_user: String, #[serde(default)] refresh_only: bool, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct OfflineProxySettings { + pub proxy_ports: Vec, + pub allow_local_binding: bool, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SandboxNetworkIdentity { + Offline, + Online, +} + +impl SandboxNetworkIdentity { + pub(crate) fn from_policy(policy: &SandboxPolicy, proxy_enforced: bool) -> Self { + if proxy_enforced || !policy.has_full_network_access() { + Self::Offline + } else { + Self::Online + } + } + + pub(crate) fn uses_offline_identity(self) -> bool { + matches!(self, Self::Offline) + } +} + +const PROXY_ENV_KEYS: &[&str] = &[ + "HTTP_PROXY", + "HTTPS_PROXY", + "ALL_PROXY", + "WS_PROXY", + "WSS_PROXY", + "http_proxy", + "https_proxy", + "all_proxy", + "ws_proxy", + "wss_proxy", +]; +const ALLOW_LOCAL_BINDING_ENV_KEY: &str = "CODEX_NETWORK_ALLOW_LOCAL_BINDING"; + +pub(crate) fn offline_proxy_settings_from_env( + env_map: &HashMap, + network_identity: SandboxNetworkIdentity, +) -> OfflineProxySettings { + if !network_identity.uses_offline_identity() { + return OfflineProxySettings { + proxy_ports: vec![], + allow_local_binding: false, + }; + } + OfflineProxySettings { + proxy_ports: proxy_ports_from_env(env_map), + allow_local_binding: env_map + .get(ALLOW_LOCAL_BINDING_ENV_KEY) + .is_some_and(|value| value == "1"), + } +} + +pub(crate) fn proxy_ports_from_env(env_map: &HashMap) -> Vec { + let mut ports = BTreeSet::new(); + for key in PROXY_ENV_KEYS { + if let Some(value) = env_map.get(*key) { + if let Some(port) = loopback_proxy_port_from_url(value) { + ports.insert(port); + } + } + } + ports.into_iter().collect() +} + +fn loopback_proxy_port_from_url(url: &str) -> Option { + let authority = url.trim().split_once("://")?.1.split('/').next()?; + let host_port = authority.rsplit_once('@').map_or(authority, |(_, hp)| hp); + + if let Some(host) = host_port.strip_prefix('[') { + let (host, rest) = host.split_once(']')?; + if host != "::1" { + return None; + } + let port = rest.strip_prefix(':')?.parse::().ok()?; + return (port != 0).then_some(port); + } + + let (host, port) = host_port.rsplit_once(':')?; + if !(host.eq_ignore_ascii_case("localhost") || host == "127.0.0.1") { + return None; + } + let port = port.parse::().ok()?; + (port != 0).then_some(port) +} + fn quote_arg(arg: &str) -> String { let needs = arg.is_empty() || arg @@ -574,39 +712,31 @@ fn run_setup_exe( } pub fn run_elevated_setup( - policy: &SandboxPolicy, - policy_cwd: &Path, - command_cwd: &Path, - env_map: &HashMap, - codex_home: &Path, - read_roots_override: Option>, - write_roots_override: Option>, + request: SandboxSetupRequest<'_>, + overrides: SetupRootOverrides, ) -> Result<()> { // Ensure the shared sandbox directory exists before we send it to the elevated helper. - let sbx_dir = sandbox_dir(codex_home); + let sbx_dir = sandbox_dir(request.codex_home); std::fs::create_dir_all(&sbx_dir).map_err(|err| { failure( SetupErrorCode::OrchestratorSandboxDirCreateFailed, format!("failed to create sandbox dir {}: {err}", sbx_dir.display()), ) })?; - let (read_roots, write_roots) = build_payload_roots( - policy, - policy_cwd, - command_cwd, - env_map, - codex_home, - read_roots_override, - write_roots_override, - ); + let (read_roots, write_roots) = build_payload_roots(&request, overrides); + let network_identity = + SandboxNetworkIdentity::from_policy(request.policy, request.proxy_enforced); + let offline_proxy_settings = offline_proxy_settings_from_env(request.env_map, network_identity); let payload = ElevationPayload { version: SETUP_VERSION, offline_username: OFFLINE_USERNAME.to_string(), online_username: ONLINE_USERNAME.to_string(), - codex_home: codex_home.to_path_buf(), - command_cwd: command_cwd.to_path_buf(), + codex_home: request.codex_home.to_path_buf(), + command_cwd: request.command_cwd.to_path_buf(), read_roots, write_roots, + proxy_ports: offline_proxy_settings.proxy_ports, + allow_local_binding: offline_proxy_settings.allow_local_binding, real_user: std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string()), refresh_only: false, }; @@ -616,28 +746,28 @@ pub fn run_elevated_setup( format!("failed to determine elevation state: {err}"), ) })?; - run_setup_exe(&payload, needs_elevation, codex_home) + run_setup_exe(&payload, needs_elevation, request.codex_home) } fn build_payload_roots( - policy: &SandboxPolicy, - policy_cwd: &Path, - command_cwd: &Path, - env_map: &HashMap, - codex_home: &Path, - read_roots_override: Option>, - write_roots_override: Option>, + request: &SandboxSetupRequest<'_>, + overrides: SetupRootOverrides, ) -> (Vec, Vec) { - let write_roots = if let Some(roots) = write_roots_override { + let write_roots = if let Some(roots) = overrides.write_roots { canonical_existing(&roots) } else { - gather_write_roots(policy, policy_cwd, command_cwd, env_map) + gather_write_roots( + request.policy, + request.policy_cwd, + request.command_cwd, + request.env_map, + ) }; - let write_roots = filter_sensitive_write_roots(write_roots, codex_home); - let mut read_roots = if let Some(roots) = read_roots_override { + let write_roots = filter_sensitive_write_roots(write_roots, request.codex_home); + let mut read_roots = if let Some(roots) = overrides.read_roots { canonical_existing(&roots) } else { - gather_read_roots(command_cwd, policy, codex_home) + gather_read_roots(request.command_cwd, request.policy, request.codex_home) }; let write_root_set: HashSet = write_roots.iter().cloned().collect(); read_roots.retain(|root| !write_root_set.contains(root)); @@ -673,13 +803,17 @@ fn filter_sensitive_write_roots(mut roots: Vec, codex_home: &Path) -> V mod tests { use super::gather_legacy_full_read_roots; use super::gather_read_roots; + use super::loopback_proxy_port_from_url; + use super::offline_proxy_settings_from_env; use super::profile_read_roots; + use super::proxy_ports_from_env; use super::WINDOWS_PLATFORM_DEFAULT_READ_ROOTS; use crate::helper_materialization::helper_bin_dir; use crate::policy::SandboxPolicy; use codex_protocol::protocol::ReadOnlyAccess; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; + use std::collections::HashMap; use std::collections::HashSet; use std::fs; use std::path::PathBuf; @@ -692,6 +826,143 @@ mod tests { .collect() } + #[test] + fn loopback_proxy_url_parsing_supports_common_forms() { + assert_eq!( + loopback_proxy_port_from_url("http://localhost:3128"), + Some(3128) + ); + assert_eq!( + loopback_proxy_port_from_url("https://127.0.0.1:8080"), + Some(8080) + ); + assert_eq!( + loopback_proxy_port_from_url("socks5h://user:pass@[::1]:1080"), + Some(1080) + ); + } + + #[test] + fn loopback_proxy_url_parsing_rejects_non_loopback_and_zero_port() { + assert_eq!( + loopback_proxy_port_from_url("http://example.com:3128"), + None + ); + assert_eq!(loopback_proxy_port_from_url("http://127.0.0.1:0"), None); + assert_eq!(loopback_proxy_port_from_url("localhost:8080"), None); + } + + #[test] + fn proxy_ports_from_env_dedupes_and_sorts() { + let mut env = HashMap::new(); + env.insert( + "HTTP_PROXY".to_string(), + "http://127.0.0.1:8080".to_string(), + ); + env.insert( + "http_proxy".to_string(), + "http://localhost:8080".to_string(), + ); + env.insert("ALL_PROXY".to_string(), "socks5h://[::1]:1081".to_string()); + env.insert( + "HTTPS_PROXY".to_string(), + "https://example.com:9999".to_string(), + ); + + assert_eq!(proxy_ports_from_env(&env), vec![1081, 8080]); + } + + #[test] + fn offline_proxy_settings_ignore_proxy_env_when_online_identity_selected() { + let mut env = HashMap::new(); + env.insert( + "HTTP_PROXY".to_string(), + "http://127.0.0.1:8080".to_string(), + ); + env.insert( + "CODEX_NETWORK_ALLOW_LOCAL_BINDING".to_string(), + "1".to_string(), + ); + + assert_eq!( + offline_proxy_settings_from_env(&env, super::SandboxNetworkIdentity::Online), + super::OfflineProxySettings { + proxy_ports: vec![], + allow_local_binding: false, + } + ); + } + + #[test] + fn offline_proxy_settings_capture_proxy_ports_and_local_binding_for_offline_identity() { + let mut env = HashMap::new(); + env.insert( + "HTTP_PROXY".to_string(), + "http://127.0.0.1:8080".to_string(), + ); + env.insert( + "ALL_PROXY".to_string(), + "socks5h://127.0.0.1:1081".to_string(), + ); + env.insert( + "CODEX_NETWORK_ALLOW_LOCAL_BINDING".to_string(), + "1".to_string(), + ); + + assert_eq!( + offline_proxy_settings_from_env(&env, super::SandboxNetworkIdentity::Offline), + super::OfflineProxySettings { + proxy_ports: vec![1081, 8080], + allow_local_binding: true, + } + ); + } + + #[test] + fn setup_marker_request_mismatch_reason_ignores_proxy_drift_for_online_identity() { + let marker = super::SetupMarker { + version: super::SETUP_VERSION, + offline_username: "offline".to_string(), + online_username: "online".to_string(), + created_at: None, + proxy_ports: vec![3128], + allow_local_binding: false, + }; + let desired = super::OfflineProxySettings { + proxy_ports: vec![1081, 8080], + allow_local_binding: true, + }; + + assert_eq!( + marker.request_mismatch_reason(super::SandboxNetworkIdentity::Online, &desired), + None + ); + } + + #[test] + fn setup_marker_request_mismatch_reason_reports_offline_firewall_drift() { + let marker = super::SetupMarker { + version: super::SETUP_VERSION, + offline_username: "offline".to_string(), + online_username: "online".to_string(), + created_at: None, + proxy_ports: vec![3128], + allow_local_binding: false, + }; + let desired = super::OfflineProxySettings { + proxy_ports: vec![1081, 8080], + allow_local_binding: true, + }; + + assert_eq!( + marker.request_mismatch_reason(super::SandboxNetworkIdentity::Offline, &desired), + Some( + "offline firewall settings changed (stored_ports=[3128], desired_ports=[1081, 8080], stored_allow_local_binding=false, desired_allow_local_binding=true)" + .to_string() + ) + ); + } + #[test] fn profile_read_roots_excludes_configured_top_level_entries() { let tmp = TempDir::new().expect("tempdir"); From d76124d656424cd9a15c9ef9e901fc1a9f3679c1 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 18:23:14 -0700 Subject: [PATCH 025/191] fix: make MACOS_DEFAULT_PREFERENCES_POLICY part of MACOS_SEATBELT_BASE_POLICY (#15931) --- codex-rs/sandboxing/src/seatbelt.rs | 9 --------- codex-rs/sandboxing/src/seatbelt_base_policy.sbpl | 8 ++++++++ codex-rs/sandboxing/src/seatbelt_tests.rs | 8 -------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/codex-rs/sandboxing/src/seatbelt.rs b/codex-rs/sandboxing/src/seatbelt.rs index 5c11c4fc45..bb784bc0df 100644 --- a/codex-rs/sandboxing/src/seatbelt.rs +++ b/codex-rs/sandboxing/src/seatbelt.rs @@ -19,14 +19,6 @@ const MACOS_SEATBELT_BASE_POLICY: &str = include_str!("seatbelt_base_policy.sbpl const MACOS_SEATBELT_NETWORK_POLICY: &str = include_str!("seatbelt_network_policy.sbpl"); const MACOS_RESTRICTED_READ_ONLY_PLATFORM_DEFAULTS: &str = include_str!("restricted_read_only_platform_defaults.sbpl"); -const MACOS_DEFAULT_PREFERENCES_POLICY: &str = r#"; allow readonly user preferences -(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) -(allow mach-lookup - (global-name "com.apple.cfprefsd.daemon") - (global-name "com.apple.cfprefsd.agent") - (local-name "com.apple.cfprefsd.agent")) -(allow user-preference-read) -"#; /// When working with `sandbox-exec`, only consider `sandbox-exec` in `/usr/bin` /// to defend against an attacker trying to inject a malicious version on the @@ -476,7 +468,6 @@ pub fn create_seatbelt_command_args_for_policies( let include_platform_defaults = file_system_sandbox_policy.include_platform_defaults(); let mut policy_sections = vec![ MACOS_SEATBELT_BASE_POLICY.to_string(), - MACOS_DEFAULT_PREFERENCES_POLICY.to_string(), file_read_policy, file_write_policy, network_policy, diff --git a/codex-rs/sandboxing/src/seatbelt_base_policy.sbpl b/codex-rs/sandboxing/src/seatbelt_base_policy.sbpl index 72b13c96cd..8d80277f6c 100644 --- a/codex-rs/sandboxing/src/seatbelt_base_policy.sbpl +++ b/codex-rs/sandboxing/src/seatbelt_base_policy.sbpl @@ -106,3 +106,11 @@ ; PTYs created before entering seatbelt may lack the extension; allow ioctl ; on those slave ttys so interactive shells detect a TTY and remain functional. (allow file-ioctl (regex #"^/dev/ttys[0-9]+")) + +; allow readonly user preferences +(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) +(allow mach-lookup + (global-name "com.apple.cfprefsd.daemon") + (global-name "com.apple.cfprefsd.agent") + (local-name "com.apple.cfprefsd.agent")) +(allow user-preference-read) diff --git a/codex-rs/sandboxing/src/seatbelt_tests.rs b/codex-rs/sandboxing/src/seatbelt_tests.rs index cc6fce56a4..b83ac31184 100644 --- a/codex-rs/sandboxing/src/seatbelt_tests.rs +++ b/codex-rs/sandboxing/src/seatbelt_tests.rs @@ -953,14 +953,6 @@ fn create_seatbelt_args_for_cwd_as_git_repo() { // - write access to WRITABLE_ROOT_0 (but not its .git or .codex), WRITABLE_ROOT_1, and cwd as WRITABLE_ROOT_2. let expected_policy = format!( r#"{MACOS_SEATBELT_BASE_POLICY} -; allow readonly user preferences -(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs.")) -(allow mach-lookup - (global-name "com.apple.cfprefsd.daemon") - (global-name "com.apple.cfprefsd.agent") - (local-name "com.apple.cfprefsd.agent")) -(allow user-preference-read) - ; allow read-only file operations (allow file-read*) (allow file-write* From d838c23867f6fe2e3e43a0c82d25c17cb3730b1d Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 18:32:13 -0700 Subject: [PATCH 026/191] fix: use matrix.target instead of matrix.os for actions/cache build action (#15933) This seems like a more precise cache key. --- .github/workflows/bazel.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 1c12db52b1..79d963a537 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -86,9 +86,9 @@ jobs: with: path: | ~/.cache/bazel-repo-cache - key: bazel-cache-${{ matrix.os }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} + key: bazel-cache-${{ matrix.target }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} restore-keys: | - bazel-cache-${{ matrix.os }} + bazel-cache-${{ matrix.target }} - name: Configure Bazel startup args (Windows) if: runner.os == 'Windows' @@ -230,4 +230,4 @@ jobs: with: path: | ~/.cache/bazel-repo-cache - key: bazel-cache-${{ matrix.os }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} + key: bazel-cache-${{ matrix.target }}-${{ hashFiles('MODULE.bazel', 'codex-rs/Cargo.lock', 'codex-rs/Cargo.toml') }} From be5afc65d31728c11d67c589cebbc64a87979e74 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 19:57:26 -0700 Subject: [PATCH 027/191] codex-tools: extract MCP schema adapters (#15928) ## Why `codex-tools` already owns the shared tool input schema model and parser from the first extraction step, but `core/src/tools/spec.rs` still owned the MCP-specific adapter that normalizes `rmcp::model::Tool` schemas and wraps `structuredContent` into the call result output schema. Keeping that adapter in `codex-core` means the reusable MCP schema path is still split across crates, and the unit tests for that logic stay anchored in `codex-core` even though the runtime orchestration does not need to move yet. This change takes the next small step by moving the reusable MCP schema adapter into `codex-tools` while leaving `ResponsesApiTool` assembly in `codex-core`. ## What changed - added `tools/src/mcp_tool.rs` and sibling `tools/src/mcp_tool_tests.rs` - introduced `ParsedMcpTool`, `parse_mcp_tool()`, and `mcp_call_tool_result_output_schema()` in `codex-tools` - updated `core/src/tools/spec.rs` to consume parsed MCP tool parts from `codex-tools` - removed the now-redundant MCP schema unit tests from `core/src/tools/spec_tests.rs` - expanded `codex-rs/tools/README.md` to describe this second migration step ## Test plan - `cargo test -p codex-tools` - `cargo test -p codex-core --lib tools::spec::` --- codex-rs/Cargo.lock | 1 + codex-rs/core/src/tools/spec.rs | 73 +++----------- codex-rs/core/src/tools/spec_tests.rs | 133 -------------------------- codex-rs/tools/Cargo.toml | 6 ++ codex-rs/tools/README.md | 7 +- codex-rs/tools/src/lib.rs | 4 + codex-rs/tools/src/mcp_tool.rs | 65 +++++++++++++ codex-rs/tools/src/mcp_tool_tests.rs | 120 +++++++++++++++++++++++ 8 files changed, 212 insertions(+), 197 deletions(-) create mode 100644 codex-rs/tools/src/mcp_tool.rs create mode 100644 codex-rs/tools/src/mcp_tool_tests.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index f494c8528b..c5846fbe19 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2633,6 +2633,7 @@ name = "codex-tools" version = "0.0.0" dependencies = [ "pretty_assertions", + "rmcp", "serde", "serde_json", ] diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index a81bf5a3c3..ea85eaacdd 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -46,6 +46,7 @@ use codex_protocol::openai_models::WebSearchToolType; use codex_protocol::protocol::SandboxPolicy; use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SubAgentSource; +use codex_tools::parse_mcp_tool; pub use codex_tools::parse_tool_input_schema; use codex_utils_absolute_path::AbsolutePathBuf; use serde::Deserialize; @@ -58,6 +59,9 @@ use std::path::PathBuf; pub type JsonSchema = codex_tools::JsonSchema; +#[cfg(test)] +pub(crate) use codex_tools::mcp_call_tool_result_output_schema; + const TOOL_SEARCH_DESCRIPTION_TEMPLATE: &str = include_str!("../../templates/search_tool/tool_description.md"); const TOOL_SUGGEST_DESCRIPTION_TEMPLATE: &str = @@ -2362,15 +2366,15 @@ pub(crate) fn mcp_tool_to_openai_tool( fully_qualified_name: String, tool: rmcp::model::Tool, ) -> Result { - let (description, input_schema, output_schema) = mcp_tool_to_openai_tool_parts(tool)?; + let parsed_tool = parse_mcp_tool(&tool)?; Ok(ResponsesApiTool { name: fully_qualified_name, - description, + description: parsed_tool.description, strict: false, defer_loading: None, - parameters: input_schema, - output_schema, + parameters: parsed_tool.input_schema, + output_schema: Some(parsed_tool.output_schema), }) } @@ -2378,14 +2382,14 @@ pub(crate) fn mcp_tool_to_deferred_openai_tool( name: String, tool: rmcp::model::Tool, ) -> Result { - let (description, input_schema, _) = mcp_tool_to_openai_tool_parts(tool)?; + let parsed_tool = parse_mcp_tool(&tool)?; Ok(ResponsesApiTool { name, - description, + description: parsed_tool.description, strict: false, defer_loading: Some(true), - parameters: input_schema, + parameters: parsed_tool.input_schema, output_schema: None, }) } @@ -2405,61 +2409,6 @@ fn dynamic_tool_to_openai_tool( }) } -fn mcp_tool_to_openai_tool_parts( - tool: rmcp::model::Tool, -) -> Result<(String, JsonSchema, Option), serde_json::Error> { - let rmcp::model::Tool { - description, - input_schema, - output_schema, - .. - } = tool; - - let mut serialized_input_schema = serde_json::Value::Object(input_schema.as_ref().clone()); - - // OpenAI models mandate the "properties" field in the schema. Some MCP - // servers omit it (or set it to null), so we insert an empty object to - // match the behavior of the Agents SDK. - if let serde_json::Value::Object(obj) = &mut serialized_input_schema - && obj.get("properties").is_none_or(serde_json::Value::is_null) - { - obj.insert( - "properties".to_string(), - serde_json::Value::Object(serde_json::Map::new()), - ); - } - - let input_schema = parse_tool_input_schema(&serialized_input_schema)?; - let structured_content_schema = output_schema - .map(|output_schema| serde_json::Value::Object(output_schema.as_ref().clone())) - .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())); - let output_schema = Some(mcp_call_tool_result_output_schema( - structured_content_schema, - )); - let description = description.map(Into::into).unwrap_or_default(); - - Ok((description, input_schema, output_schema)) -} - -fn mcp_call_tool_result_output_schema(structured_content_schema: JsonValue) -> JsonValue { - json!({ - "type": "object", - "properties": { - "content": { - "type": "array", - "items": {} - }, - "structuredContent": structured_content_schema, - "isError": { - "type": "boolean" - }, - "_meta": {} - }, - "required": ["content"], - "additionalProperties": false - }) -} - /// Builds the tool registry builder while collecting tool specs for later serialization. #[cfg(test)] pub(crate) fn build_specs( diff --git a/codex-rs/core/src/tools/spec_tests.rs b/codex-rs/core/src/tools/spec_tests.rs index 82310e8726..5b9016ec6a 100644 --- a/codex-rs/core/src/tools/spec_tests.rs +++ b/codex-rs/core/src/tools/spec_tests.rs @@ -63,139 +63,6 @@ fn search_capable_model_info() -> ModelInfo { model_info } -#[test] -fn mcp_tool_to_openai_tool_inserts_empty_properties() { - let mut schema = rmcp::model::JsonObject::new(); - schema.insert("type".to_string(), serde_json::json!("object")); - - let tool = rmcp::model::Tool { - name: "no_props".to_string().into(), - title: None, - description: Some("No properties".to_string().into()), - input_schema: std::sync::Arc::new(schema), - output_schema: None, - annotations: None, - execution: None, - icons: None, - meta: None, - }; - - let openai_tool = - mcp_tool_to_openai_tool("server/no_props".to_string(), tool).expect("convert tool"); - let parameters = serde_json::to_value(openai_tool.parameters).expect("serialize schema"); - - assert_eq!(parameters.get("properties"), Some(&serde_json::json!({}))); -} - -#[test] -fn mcp_tool_to_openai_tool_preserves_top_level_output_schema() { - let mut input_schema = rmcp::model::JsonObject::new(); - input_schema.insert("type".to_string(), serde_json::json!("object")); - - let mut output_schema = rmcp::model::JsonObject::new(); - output_schema.insert( - "properties".to_string(), - serde_json::json!({ - "result": { - "properties": { - "nested": {} - } - } - }), - ); - output_schema.insert("required".to_string(), serde_json::json!(["result"])); - - let tool = rmcp::model::Tool { - name: "with_output".to_string().into(), - title: None, - description: Some("Has output schema".to_string().into()), - input_schema: std::sync::Arc::new(input_schema), - output_schema: Some(std::sync::Arc::new(output_schema)), - annotations: None, - execution: None, - icons: None, - meta: None, - }; - - let openai_tool = mcp_tool_to_openai_tool("mcp__server__with_output".to_string(), tool) - .expect("convert tool"); - - assert_eq!( - openai_tool.output_schema, - Some(serde_json::json!({ - "type": "object", - "properties": { - "content": { - "type": "array", - "items": {} - }, - "structuredContent": { - "properties": { - "result": { - "properties": { - "nested": {} - } - } - }, - "required": ["result"] - }, - "isError": { - "type": "boolean" - }, - "_meta": {} - }, - "required": ["content"], - "additionalProperties": false - })) - ); -} - -#[test] -fn mcp_tool_to_openai_tool_preserves_output_schema_without_inferred_type() { - let mut input_schema = rmcp::model::JsonObject::new(); - input_schema.insert("type".to_string(), serde_json::json!("object")); - - let mut output_schema = rmcp::model::JsonObject::new(); - output_schema.insert("enum".to_string(), serde_json::json!(["ok", "error"])); - - let tool = rmcp::model::Tool { - name: "with_enum_output".to_string().into(), - title: None, - description: Some("Has enum output schema".to_string().into()), - input_schema: std::sync::Arc::new(input_schema), - output_schema: Some(std::sync::Arc::new(output_schema)), - annotations: None, - execution: None, - icons: None, - meta: None, - }; - - let openai_tool = mcp_tool_to_openai_tool("mcp__server__with_enum_output".to_string(), tool) - .expect("convert tool"); - - assert_eq!( - openai_tool.output_schema, - Some(serde_json::json!({ - "type": "object", - "properties": { - "content": { - "type": "array", - "items": {} - }, - "structuredContent": { - "enum": ["ok", "error"] - }, - "isError": { - "type": "boolean" - }, - "_meta": {} - }, - "required": ["content"], - "additionalProperties": false - })) - ); -} - #[test] fn search_tool_deferred_tools_always_set_defer_loading_true() { let tool = mcp_tool( diff --git a/codex-rs/tools/Cargo.toml b/codex-rs/tools/Cargo.toml index c596520c57..d695336fae 100644 --- a/codex-rs/tools/Cargo.toml +++ b/codex-rs/tools/Cargo.toml @@ -8,6 +8,12 @@ version.workspace = true workspace = true [dependencies] +rmcp = { workspace = true, default-features = false, features = [ + "base64", + "macros", + "schemars", + "server", +] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } diff --git a/codex-rs/tools/README.md b/codex-rs/tools/README.md index d4c2f28e91..b8780fe7b1 100644 --- a/codex-rs/tools/README.md +++ b/codex-rs/tools/README.md @@ -4,12 +4,15 @@ shared across multiple crates and does not need to stay coupled to `codex-core`. -Today this crate is intentionally small. It only owns the shared tool input -schema model and parser that were previously defined in `core/src/tools/spec.rs`: +Today this crate is intentionally small. It currently owns the shared tool +schema primitives that no longer need to live in `core/src/tools/spec.rs`: - `JsonSchema` - `AdditionalProperties` - `parse_tool_input_schema()` +- `ParsedMcpTool` +- `parse_mcp_tool()` +- `mcp_call_tool_result_output_schema()` That extraction is the first step in a longer migration. The goal is not to move all of `core/src/tools` into this crate in one shot. Instead, the plan is diff --git a/codex-rs/tools/src/lib.rs b/codex-rs/tools/src/lib.rs index 90d3af10c8..e719d3e23f 100644 --- a/codex-rs/tools/src/lib.rs +++ b/codex-rs/tools/src/lib.rs @@ -1,7 +1,11 @@ //! Shared tool-schema parsing primitives that can live outside `codex-core`. mod json_schema; +mod mcp_tool; pub use json_schema::AdditionalProperties; pub use json_schema::JsonSchema; pub use json_schema::parse_tool_input_schema; +pub use mcp_tool::ParsedMcpTool; +pub use mcp_tool::mcp_call_tool_result_output_schema; +pub use mcp_tool::parse_mcp_tool; diff --git a/codex-rs/tools/src/mcp_tool.rs b/codex-rs/tools/src/mcp_tool.rs new file mode 100644 index 0000000000..99294a0e67 --- /dev/null +++ b/codex-rs/tools/src/mcp_tool.rs @@ -0,0 +1,65 @@ +use crate::JsonSchema; +use crate::parse_tool_input_schema; +use serde_json::Value as JsonValue; +use serde_json::json; + +/// Parsed MCP tool metadata and schemas that can be adapted into a higher-level +/// tool spec by downstream crates. +#[derive(Debug, PartialEq)] +pub struct ParsedMcpTool { + pub description: String, + pub input_schema: JsonSchema, + pub output_schema: JsonValue, +} + +pub fn parse_mcp_tool(tool: &rmcp::model::Tool) -> Result { + let mut serialized_input_schema = serde_json::Value::Object(tool.input_schema.as_ref().clone()); + + // OpenAI models mandate the "properties" field in the schema. Some MCP + // servers omit it (or set it to null), so we insert an empty object to + // match the behavior of the Agents SDK. + if let serde_json::Value::Object(obj) = &mut serialized_input_schema + && obj.get("properties").is_none_or(serde_json::Value::is_null) + { + obj.insert( + "properties".to_string(), + serde_json::Value::Object(serde_json::Map::new()), + ); + } + + let input_schema = parse_tool_input_schema(&serialized_input_schema)?; + let structured_content_schema = tool + .output_schema + .as_ref() + .map(|output_schema| serde_json::Value::Object(output_schema.as_ref().clone())) + .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())); + + Ok(ParsedMcpTool { + description: tool.description.clone().map(Into::into).unwrap_or_default(), + input_schema, + output_schema: mcp_call_tool_result_output_schema(structured_content_schema), + }) +} + +pub fn mcp_call_tool_result_output_schema(structured_content_schema: JsonValue) -> JsonValue { + json!({ + "type": "object", + "properties": { + "content": { + "type": "array", + "items": {} + }, + "structuredContent": structured_content_schema, + "isError": { + "type": "boolean" + }, + "_meta": {} + }, + "required": ["content"], + "additionalProperties": false + }) +} + +#[cfg(test)] +#[path = "mcp_tool_tests.rs"] +mod tests; diff --git a/codex-rs/tools/src/mcp_tool_tests.rs b/codex-rs/tools/src/mcp_tool_tests.rs new file mode 100644 index 0000000000..2d4df6f362 --- /dev/null +++ b/codex-rs/tools/src/mcp_tool_tests.rs @@ -0,0 +1,120 @@ +use super::ParsedMcpTool; +use super::mcp_call_tool_result_output_schema; +use super::parse_mcp_tool; +use crate::JsonSchema; +use pretty_assertions::assert_eq; +use std::collections::BTreeMap; + +fn mcp_tool(name: &str, description: &str, input_schema: serde_json::Value) -> rmcp::model::Tool { + rmcp::model::Tool { + name: name.to_string().into(), + title: None, + description: Some(description.to_string().into()), + input_schema: std::sync::Arc::new(rmcp::model::object(input_schema)), + output_schema: None, + annotations: None, + execution: None, + icons: None, + meta: None, + } +} + +#[test] +fn parse_mcp_tool_inserts_empty_properties() { + let tool = mcp_tool( + "no_props", + "No properties", + serde_json::json!({ + "type": "object" + }), + ); + + assert_eq!( + parse_mcp_tool(&tool).expect("parse MCP tool"), + ParsedMcpTool { + description: "No properties".to_string(), + input_schema: JsonSchema::Object { + properties: BTreeMap::new(), + required: None, + additional_properties: None, + }, + output_schema: mcp_call_tool_result_output_schema(serde_json::json!({})), + } + ); +} + +#[test] +fn parse_mcp_tool_preserves_top_level_output_schema() { + let mut tool = mcp_tool( + "with_output", + "Has output schema", + serde_json::json!({ + "type": "object" + }), + ); + tool.output_schema = Some(std::sync::Arc::new(rmcp::model::object( + serde_json::json!({ + "properties": { + "result": { + "properties": { + "nested": {} + } + } + }, + "required": ["result"] + }), + ))); + + assert_eq!( + parse_mcp_tool(&tool).expect("parse MCP tool"), + ParsedMcpTool { + description: "Has output schema".to_string(), + input_schema: JsonSchema::Object { + properties: BTreeMap::new(), + required: None, + additional_properties: None, + }, + output_schema: mcp_call_tool_result_output_schema(serde_json::json!({ + "properties": { + "result": { + "properties": { + "nested": {} + } + } + }, + "required": ["result"] + })), + } + ); +} + +#[test] +fn parse_mcp_tool_preserves_output_schema_without_inferred_type() { + let mut tool = mcp_tool( + "with_enum_output", + "Has enum output schema", + serde_json::json!({ + "type": "object" + }), + ); + tool.output_schema = Some(std::sync::Arc::new(rmcp::model::object( + serde_json::json!({ + "enum": ["ok", "error"] + }), + ))); + + assert_eq!( + parse_mcp_tool(&tool).expect("parse MCP tool"), + ParsedMcpTool { + description: "Has enum output schema".to_string(), + input_schema: JsonSchema::Object { + properties: BTreeMap::new(), + required: None, + additional_properties: None, + }, + output_schema: mcp_call_tool_result_output_schema(serde_json::json!({ + "enum": ["ok", "error"] + })), + } + ); +} From 41fe98b18538c60feeb779f8fad685a1b048da7b Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 26 Mar 2026 20:54:55 -0700 Subject: [PATCH 028/191] fix: increase timeout for rust-ci to 45 minutes for now (#15948) https://github.com/openai/codex/pull/15478 raised the timeout to 35 minutes for `windows-arm64` only, though I just hit 35 minutes on https://github.com/openai/codex/actions/runs/23628986591/job/68826740108?pr=15944, so let's just increase it to 45 minutes. As noted, I'm hoping that we can bring it back down once we no longer have two copies of the `tui` crate. --- .github/workflows/rust-ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index c203e2b742..a7e6aa4431 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -547,7 +547,10 @@ jobs: tests: name: Tests — ${{ matrix.runner }} - ${{ matrix.target }}${{ matrix.remote_env == 'true' && ' (remote)' || '' }} runs-on: ${{ matrix.runs_on || matrix.runner }} - timeout-minutes: ${{ matrix.runner == 'windows-arm64' && 35 || 30 }} + # Perhaps we can bring this back down to 30m once we finish the cutover + # from tui_app_server/ to tui/. Incidentally, windows-arm64 was the main + # offender for exceeding the timeout. + timeout-minutes: 45 needs: changed if: ${{ needs.changed.outputs.codex == 'true' || needs.changed.outputs.workflows == 'true' || github.event_name == 'push' }} defaults: From 21a03f16718dae1ca2f810c33b50c99b5634c931 Mon Sep 17 00:00:00 2001 From: rhan-oai Date: Thu, 26 Mar 2026 21:33:25 -0700 Subject: [PATCH 029/191] [app-server-protocol] introduce generic ClientResponse for app-server-protocol (#15921) - introduces `ClientResponse` as the symmetrical typed response union to `ClientRequest` for app-server-protocol - enables scalable event stream ingestion for use cases such as analytics - no runtime behavior changes, protocol/schema plumbing only --- .../src/protocol/common.rs | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index f3e56f4ffe..6bd44562e0 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -120,6 +120,41 @@ macro_rules! client_request_definitions { } } + /// Typed response from the server to the client. + #[derive(Serialize, Deserialize, Debug, Clone)] + #[serde(tag = "method", rename_all = "camelCase")] + pub enum ClientResponse { + $( + $(#[doc = $variant_doc])* + $(#[serde(rename = $wire)])? + $variant { + #[serde(rename = "id")] + request_id: RequestId, + response: $response, + }, + )* + } + + impl ClientResponse { + pub fn id(&self) -> &RequestId { + match self { + $(Self::$variant { request_id, .. } => request_id,)* + } + } + + pub fn method(&self) -> String { + serde_json::to_value(self) + .ok() + .and_then(|value| { + value + .get("method") + .and_then(serde_json::Value::as_str) + .map(str::to_owned) + }) + .unwrap_or_else(|| "".to_string()) + } + } + impl crate::experimental_api::ExperimentalApi for ClientRequest { fn experimental_reason(&self) -> Option<&'static str> { match self { @@ -1265,6 +1300,84 @@ mod tests { Ok(()) } + #[test] + fn serialize_client_response() -> Result<()> { + let response = ClientResponse::ThreadStart { + request_id: RequestId::Integer(7), + response: v2::ThreadStartResponse { + thread: v2::Thread { + id: "67e55044-10b1-426f-9247-bb680e5fe0c8".to_string(), + preview: "first prompt".to_string(), + ephemeral: true, + model_provider: "openai".to_string(), + created_at: 1, + updated_at: 2, + status: v2::ThreadStatus::Idle, + path: None, + cwd: PathBuf::from("/tmp"), + cli_version: "0.0.0".to_string(), + source: v2::SessionSource::Exec, + agent_nickname: None, + agent_role: None, + git_info: None, + name: None, + turns: Vec::new(), + }, + model: "gpt-5".to_string(), + model_provider: "openai".to_string(), + service_tier: None, + cwd: PathBuf::from("/tmp"), + approval_policy: v2::AskForApproval::OnFailure, + approvals_reviewer: v2::ApprovalsReviewer::User, + sandbox: v2::SandboxPolicy::DangerFullAccess, + reasoning_effort: None, + }, + }; + + assert_eq!(response.id(), &RequestId::Integer(7)); + assert_eq!(response.method(), "thread/start"); + assert_eq!( + json!({ + "method": "thread/start", + "id": 7, + "response": { + "thread": { + "id": "67e55044-10b1-426f-9247-bb680e5fe0c8", + "preview": "first prompt", + "ephemeral": true, + "modelProvider": "openai", + "createdAt": 1, + "updatedAt": 2, + "status": { + "type": "idle" + }, + "path": null, + "cwd": "/tmp", + "cliVersion": "0.0.0", + "source": "exec", + "agentNickname": null, + "agentRole": null, + "gitInfo": null, + "name": null, + "turns": [] + }, + "model": "gpt-5", + "modelProvider": "openai", + "serviceTier": null, + "cwd": "/tmp", + "approvalPolicy": "on-failure", + "approvalsReviewer": "user", + "sandbox": { + "type": "dangerFullAccess" + }, + "reasoningEffort": null + } + }), + serde_json::to_value(&response)?, + ); + Ok(()) + } + #[test] fn serialize_config_requirements_read() -> Result<()> { let request = ClientRequest::ConfigRequirementsRead { From dd30c8eedd171d2dda71c43fac27dc42f457da5f Mon Sep 17 00:00:00 2001 From: Celia Chen Date: Thu, 26 Mar 2026 23:17:59 -0700 Subject: [PATCH 030/191] chore: refactor network permissions to use explicit domain and unix socket rule maps (#15120) ## Summary This PR replaces the legacy network allow/deny list model with explicit rule maps for domains and unix sockets across managed requirements, permissions profiles, the network proxy config, and the app server protocol. Concretely, it: - introduces typed domain (`allow` / `deny`) and unix socket permission (`allow` / `none`) entries instead of separate `allowed_domains`, `denied_domains`, and `allow_unix_sockets` lists - updates config loading, managed requirements merging, and exec-policy overlays to read and upsert rule entries consistently - exposes the new shape through protocol/schema outputs, debug surfaces, and app-server config APIs - rejects the legacy list-based keys and updates docs/tests to reflect the new config format ## Why The previous representation split related network policy across multiple parallel lists, which made merging and overriding rules harder to reason about. Moving to explicit keyed permission maps gives us a single source of truth per host/socket entry, makes allow/deny precedence clearer, and gives protocol consumers access to the full rule state instead of derived projections only. ## Backward Compatibility ### Backward compatible - Managed requirements still accept the legacy `experimental_network.allowed_domains`, `experimental_network.denied_domains`, and `experimental_network.allow_unix_sockets` fields. They are normalized into the new canonical `domains` and `unix_sockets` maps internally. - App-server v2 still deserializes legacy `allowedDomains`, `deniedDomains`, and `allowUnixSockets` payloads, so older clients can continue reading managed network requirements. - App-server v2 responses still populate `allowedDomains`, `deniedDomains`, and `allowUnixSockets` as legacy compatibility views derived from the canonical maps. - `managed_allowed_domains_only` keeps the same behavior after normalization. Legacy managed allowlists still participate in the same enforcement path as canonical `domains` entries. ### Not backward compatible - Permissions profiles under `[permissions..network]` no longer accept the legacy list-based keys. Those configs must use the canonical `[domains]` and `[unix_sockets]` tables instead of `allowed_domains`, `denied_domains`, or `allow_unix_sockets`. - Managed `experimental_network` config cannot mix canonical and legacy forms in the same block. For example, `domains` cannot be combined with `allowed_domains` or `denied_domains`, and `unix_sockets` cannot be combined with `allow_unix_sockets`. - The canonical format can express explicit `"none"` entries for unix sockets, but those entries do not round-trip through the legacy compatibility fields because the legacy fields only represent allow/deny lists. ## Testing `/target/debug/codex sandbox macos --log-denials /bin/zsh -c 'curl https://www.example.com' ` gives 200 with config ``` [permissions.workspace.network.domains] "www.example.com" = "allow" ``` and fails when set to deny: `curl: (56) CONNECT tunnel failed, response 403`. Also tested backward compatibility path by verifying that adding the following to `/etc/codex/requirements.toml` works: ``` [experimental_network] allowed_domains = ["www.example.com"] ``` --- .../codex_app_server_protocol.schemas.json | 44 ++ .../codex_app_server_protocol.v2.schemas.json | 44 ++ .../v2/ConfigRequirementsReadResponse.json | 44 ++ .../typescript/v2/NetworkDomainPermission.ts | 5 + .../typescript/v2/NetworkRequirements.ts | 29 +- .../v2/NetworkUnixSocketPermission.ts | 5 + .../schema/typescript/v2/index.ts | 2 + .../app-server-protocol/src/protocol/v2.rs | 114 +++++ codex-rs/app-server/README.md | 2 +- codex-rs/app-server/src/config_api.rs | 157 ++++++- codex-rs/config/src/config_requirements.rs | 428 +++++++++++++++++- codex-rs/config/src/lib.rs | 4 + codex-rs/core/config.schema.json | 42 +- codex-rs/core/src/codex.rs | 12 +- codex-rs/core/src/codex_tests.rs | 31 +- codex-rs/core/src/config/config_tests.rs | 21 +- codex-rs/core/src/config/mod.rs | 5 + .../core/src/config/network_proxy_spec.rs | 94 ++-- .../src/config/network_proxy_spec_tests.rs | 287 ++++++++++-- codex-rs/core/src/config/permissions.rs | 137 +++++- codex-rs/core/src/config/permissions_tests.rs | 129 ++++++ codex-rs/core/src/config_loader/mod.rs | 4 + codex-rs/core/src/environment_context.rs | 12 +- codex-rs/core/src/guardian/tests.rs | 17 +- codex-rs/core/src/network_proxy_loader.rs | 40 +- .../core/src/network_proxy_loader_tests.rs | 168 ++++++- codex-rs/network-proxy/README.md | 33 +- codex-rs/network-proxy/src/config.rs | 301 +++++++++++- codex-rs/network-proxy/src/http_proxy.rs | 39 +- codex-rs/network-proxy/src/lib.rs | 5 + codex-rs/network-proxy/src/mitm_tests.rs | 23 +- codex-rs/network-proxy/src/network_policy.rs | 18 +- codex-rs/network-proxy/src/proxy.rs | 2 +- codex-rs/network-proxy/src/runtime.rs | 352 +++++++------- codex-rs/network-proxy/src/state.rs | 90 ++-- codex-rs/tui/src/debug_config.rs | 64 ++- codex-rs/tui_app_server/src/debug_config.rs | 101 ++++- 37 files changed, 2413 insertions(+), 492 deletions(-) create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/NetworkDomainPermission.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/NetworkUnixSocketPermission.ts diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index f52faf5b15..8531542d55 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -9203,6 +9203,13 @@ ], "type": "string" }, + "NetworkDomainPermission": { + "enum": [ + "allow", + "deny" + ], + "type": "string" + }, "NetworkRequirements": { "properties": { "allowLocalBinding": { @@ -9212,6 +9219,7 @@ ] }, "allowUnixSockets": { + "description": "Legacy compatibility view derived from `unix_sockets`.", "items": { "type": "string" }, @@ -9227,6 +9235,7 @@ ] }, "allowedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -9248,6 +9257,7 @@ ] }, "deniedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -9256,6 +9266,16 @@ "null" ] }, + "domains": { + "additionalProperties": { + "$ref": "#/definitions/v2/NetworkDomainPermission" + }, + "description": "Canonical network permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] + }, "enabled": { "type": [ "boolean", @@ -9270,6 +9290,13 @@ "null" ] }, + "managedAllowedDomainsOnly": { + "description": "When true, only managed allowlist entries are respected while managed network enforcement is active.", + "type": [ + "boolean", + "null" + ] + }, "socksPort": { "format": "uint16", "minimum": 0.0, @@ -9277,10 +9304,27 @@ "integer", "null" ] + }, + "unixSockets": { + "additionalProperties": { + "$ref": "#/definitions/v2/NetworkUnixSocketPermission" + }, + "description": "Canonical unix socket permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] } }, "type": "object" }, + "NetworkUnixSocketPermission": { + "enum": [ + "allow", + "none" + ], + "type": "string" + }, "NonSteerableTurnKind": { "enum": [ "review", diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index 5d053604f5..9d28f531af 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -6017,6 +6017,13 @@ ], "type": "string" }, + "NetworkDomainPermission": { + "enum": [ + "allow", + "deny" + ], + "type": "string" + }, "NetworkRequirements": { "properties": { "allowLocalBinding": { @@ -6026,6 +6033,7 @@ ] }, "allowUnixSockets": { + "description": "Legacy compatibility view derived from `unix_sockets`.", "items": { "type": "string" }, @@ -6041,6 +6049,7 @@ ] }, "allowedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -6062,6 +6071,7 @@ ] }, "deniedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -6070,6 +6080,16 @@ "null" ] }, + "domains": { + "additionalProperties": { + "$ref": "#/definitions/NetworkDomainPermission" + }, + "description": "Canonical network permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] + }, "enabled": { "type": [ "boolean", @@ -6084,6 +6104,13 @@ "null" ] }, + "managedAllowedDomainsOnly": { + "description": "When true, only managed allowlist entries are respected while managed network enforcement is active.", + "type": [ + "boolean", + "null" + ] + }, "socksPort": { "format": "uint16", "minimum": 0.0, @@ -6091,10 +6118,27 @@ "integer", "null" ] + }, + "unixSockets": { + "additionalProperties": { + "$ref": "#/definitions/NetworkUnixSocketPermission" + }, + "description": "Canonical unix socket permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] } }, "type": "object" }, + "NetworkUnixSocketPermission": { + "enum": [ + "allow", + "none" + ], + "type": "string" + }, "NonSteerableTurnKind": { "enum": [ "review", diff --git a/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json index e0c8304c1f..de5eca1d30 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json @@ -102,6 +102,13 @@ }, "type": "object" }, + "NetworkDomainPermission": { + "enum": [ + "allow", + "deny" + ], + "type": "string" + }, "NetworkRequirements": { "properties": { "allowLocalBinding": { @@ -111,6 +118,7 @@ ] }, "allowUnixSockets": { + "description": "Legacy compatibility view derived from `unix_sockets`.", "items": { "type": "string" }, @@ -126,6 +134,7 @@ ] }, "allowedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -147,6 +156,7 @@ ] }, "deniedDomains": { + "description": "Legacy compatibility view derived from `domains`.", "items": { "type": "string" }, @@ -155,6 +165,16 @@ "null" ] }, + "domains": { + "additionalProperties": { + "$ref": "#/definitions/NetworkDomainPermission" + }, + "description": "Canonical network permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] + }, "enabled": { "type": [ "boolean", @@ -169,6 +189,13 @@ "null" ] }, + "managedAllowedDomainsOnly": { + "description": "When true, only managed allowlist entries are respected while managed network enforcement is active.", + "type": [ + "boolean", + "null" + ] + }, "socksPort": { "format": "uint16", "minimum": 0.0, @@ -176,10 +203,27 @@ "integer", "null" ] + }, + "unixSockets": { + "additionalProperties": { + "$ref": "#/definitions/NetworkUnixSocketPermission" + }, + "description": "Canonical unix socket permission map for `experimental_network`.", + "type": [ + "object", + "null" + ] } }, "type": "object" }, + "NetworkUnixSocketPermission": { + "enum": [ + "allow", + "none" + ], + "type": "string" + }, "ResidencyRequirement": { "enum": [ "us" diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkDomainPermission.ts b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkDomainPermission.ts new file mode 100644 index 0000000000..2ea44392de --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkDomainPermission.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type NetworkDomainPermission = "allow" | "deny"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts index 1f1653c277..5fc942bef5 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts @@ -1,5 +1,32 @@ // GENERATED CODE! DO NOT MODIFY BY HAND! // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { NetworkDomainPermission } from "./NetworkDomainPermission"; +import type { NetworkUnixSocketPermission } from "./NetworkUnixSocketPermission"; -export type NetworkRequirements = { enabled: boolean | null, httpPort: number | null, socksPort: number | null, allowUpstreamProxy: boolean | null, dangerouslyAllowNonLoopbackProxy: boolean | null, dangerouslyAllowAllUnixSockets: boolean | null, allowedDomains: Array | null, deniedDomains: Array | null, allowUnixSockets: Array | null, allowLocalBinding: boolean | null, }; +export type NetworkRequirements = { enabled: boolean | null, httpPort: number | null, socksPort: number | null, allowUpstreamProxy: boolean | null, dangerouslyAllowNonLoopbackProxy: boolean | null, dangerouslyAllowAllUnixSockets: boolean | null, +/** + * Canonical network permission map for `experimental_network`. + */ +domains: { [key in string]?: NetworkDomainPermission } | null, +/** + * When true, only managed allowlist entries are respected while managed + * network enforcement is active. + */ +managedAllowedDomainsOnly: boolean | null, +/** + * Legacy compatibility view derived from `domains`. + */ +allowedDomains: Array | null, +/** + * Legacy compatibility view derived from `domains`. + */ +deniedDomains: Array | null, +/** + * Canonical unix socket permission map for `experimental_network`. + */ +unixSockets: { [key in string]?: NetworkUnixSocketPermission } | null, +/** + * Legacy compatibility view derived from `unix_sockets`. + */ +allowUnixSockets: Array | null, allowLocalBinding: boolean | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkUnixSocketPermission.ts b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkUnixSocketPermission.ts new file mode 100644 index 0000000000..466c6e5f8f --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkUnixSocketPermission.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type NetworkUnixSocketPermission = "allow" | "none"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index 73fe05eaeb..d0687e5f1d 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -197,9 +197,11 @@ export type { ModelUpgradeInfo } from "./ModelUpgradeInfo"; export type { NetworkAccess } from "./NetworkAccess"; export type { NetworkApprovalContext } from "./NetworkApprovalContext"; export type { NetworkApprovalProtocol } from "./NetworkApprovalProtocol"; +export type { NetworkDomainPermission } from "./NetworkDomainPermission"; export type { NetworkPolicyAmendment } from "./NetworkPolicyAmendment"; export type { NetworkPolicyRuleAction } from "./NetworkPolicyRuleAction"; export type { NetworkRequirements } from "./NetworkRequirements"; +export type { NetworkUnixSocketPermission } from "./NetworkUnixSocketPermission"; export type { NonSteerableTurnKind } from "./NonSteerableTurnKind"; export type { OverriddenMetadata } from "./OverriddenMetadata"; export type { PatchApplyStatus } from "./PatchApplyStatus"; diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 2211e7aa15..930112eed9 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -866,12 +866,38 @@ pub struct NetworkRequirements { pub allow_upstream_proxy: Option, pub dangerously_allow_non_loopback_proxy: Option, pub dangerously_allow_all_unix_sockets: Option, + /// Canonical network permission map for `experimental_network`. + pub domains: Option>, + /// When true, only managed allowlist entries are respected while managed + /// network enforcement is active. + pub managed_allowed_domains_only: Option, + /// Legacy compatibility view derived from `domains`. pub allowed_domains: Option>, + /// Legacy compatibility view derived from `domains`. pub denied_domains: Option>, + /// Canonical unix socket permission map for `experimental_network`. + pub unix_sockets: Option>, + /// Legacy compatibility view derived from `unix_sockets`. pub allow_unix_sockets: Option>, pub allow_local_binding: Option, } +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "lowercase")] +#[ts(export_to = "v2/")] +pub enum NetworkDomainPermission { + Allow, + Deny, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "lowercase")] +#[ts(export_to = "v2/")] +pub enum NetworkUnixSocketPermission { + Allow, + None, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] @@ -7487,6 +7513,94 @@ mod tests { ); } + #[test] + fn network_requirements_deserializes_legacy_fields() { + let requirements: NetworkRequirements = serde_json::from_value(json!({ + "allowedDomains": ["api.openai.com"], + "deniedDomains": ["blocked.example.com"], + "allowUnixSockets": ["/tmp/proxy.sock"] + })) + .expect("legacy network requirements should deserialize"); + + assert_eq!( + requirements, + NetworkRequirements { + enabled: None, + http_port: None, + socks_port: None, + allow_upstream_proxy: None, + dangerously_allow_non_loopback_proxy: None, + dangerously_allow_all_unix_sockets: None, + domains: None, + managed_allowed_domains_only: None, + allowed_domains: Some(vec!["api.openai.com".to_string()]), + denied_domains: Some(vec!["blocked.example.com".to_string()]), + unix_sockets: None, + allow_unix_sockets: Some(vec!["/tmp/proxy.sock".to_string()]), + allow_local_binding: None, + } + ); + } + + #[test] + fn network_requirements_serializes_canonical_and_legacy_fields() { + let requirements = NetworkRequirements { + enabled: Some(true), + http_port: Some(8080), + socks_port: Some(1080), + allow_upstream_proxy: Some(false), + dangerously_allow_non_loopback_proxy: Some(false), + dangerously_allow_all_unix_sockets: Some(true), + domains: Some(BTreeMap::from([ + ("api.openai.com".to_string(), NetworkDomainPermission::Allow), + ( + "blocked.example.com".to_string(), + NetworkDomainPermission::Deny, + ), + ])), + managed_allowed_domains_only: Some(true), + allowed_domains: Some(vec!["api.openai.com".to_string()]), + denied_domains: Some(vec!["blocked.example.com".to_string()]), + unix_sockets: Some(BTreeMap::from([ + ( + "/tmp/proxy.sock".to_string(), + NetworkUnixSocketPermission::Allow, + ), + ( + "/tmp/ignored.sock".to_string(), + NetworkUnixSocketPermission::None, + ), + ])), + allow_unix_sockets: Some(vec!["/tmp/proxy.sock".to_string()]), + allow_local_binding: Some(true), + }; + + assert_eq!( + serde_json::to_value(requirements).expect("network requirements should serialize"), + json!({ + "enabled": true, + "httpPort": 8080, + "socksPort": 1080, + "allowUpstreamProxy": false, + "dangerouslyAllowNonLoopbackProxy": false, + "dangerouslyAllowAllUnixSockets": true, + "domains": { + "api.openai.com": "allow", + "blocked.example.com": "deny" + }, + "managedAllowedDomainsOnly": true, + "allowedDomains": ["api.openai.com"], + "deniedDomains": ["blocked.example.com"], + "unixSockets": { + "/tmp/ignored.sock": "none", + "/tmp/proxy.sock": "allow" + }, + "allowUnixSockets": ["/tmp/proxy.sock"], + "allowLocalBinding": true + }) + ); + } + #[test] fn core_turn_item_into_thread_item_converts_supported_variants() { let user_item = TurnItem::UserMessage(UserMessageItem { diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index cabea022a1..38cdacac79 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -194,7 +194,7 @@ Example with notification opt-out: - `externalAgentConfig/import` — apply selected external-agent migration items by passing explicit `migrationItems` with `cwd` (`null` for home). - `config/value/write` — write a single config key/value to the user's config.toml on disk. - `config/batchWrite` — apply multiple config edits atomically to the user's config.toml on disk, with optional `reloadUserConfig: true` to hot-reload loaded threads. -- `configRequirements/read` — fetch loaded requirements constraints from `requirements.toml` and/or MDM (or `null` if none are configured), including allow-lists (`allowedApprovalPolicies`, `allowedSandboxModes`, `allowedWebSearchModes`), pinned feature values (`featureRequirements`), `enforceResidency`, and `network` constraints. +- `configRequirements/read` — fetch loaded requirements constraints from `requirements.toml` and/or MDM (or `null` if none are configured), including allow-lists (`allowedApprovalPolicies`, `allowedSandboxModes`, `allowedWebSearchModes`), pinned feature values (`featureRequirements`), `enforceResidency`, and `network` constraints such as canonical domain/socket permissions plus `managedAllowedDomainsOnly`. ### Example: Start or resume a thread diff --git a/codex-rs/app-server/src/config_api.rs b/codex-rs/app-server/src/config_api.rs index 7f6acc54d1..d138d2f5eb 100644 --- a/codex-rs/app-server/src/config_api.rs +++ b/codex-rs/app-server/src/config_api.rs @@ -12,7 +12,9 @@ use codex_app_server_protocol::ConfigWriteResponse; use codex_app_server_protocol::ExperimentalFeatureEnablementSetParams; use codex_app_server_protocol::ExperimentalFeatureEnablementSetResponse; use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::NetworkDomainPermission; use codex_app_server_protocol::NetworkRequirements; +use codex_app_server_protocol::NetworkUnixSocketPermission; use codex_app_server_protocol::SandboxMode; use codex_core::AnalyticsEventsClient; use codex_core::ThreadManager; @@ -410,6 +412,20 @@ fn map_residency_requirement_to_api( fn map_network_requirements_to_api( network: codex_core::config_loader::NetworkRequirementsToml, ) -> NetworkRequirements { + let allowed_domains = network + .domains + .as_ref() + .and_then(codex_core::config_loader::NetworkDomainPermissionsToml::allowed_domains); + let denied_domains = network + .domains + .as_ref() + .and_then(codex_core::config_loader::NetworkDomainPermissionsToml::denied_domains); + let allow_unix_sockets = network + .unix_sockets + .as_ref() + .map(codex_core::config_loader::NetworkUnixSocketPermissionsToml::allow_unix_sockets) + .filter(|entries| !entries.is_empty()); + NetworkRequirements { enabled: network.enabled, http_port: network.http_port, @@ -417,13 +433,58 @@ fn map_network_requirements_to_api( allow_upstream_proxy: network.allow_upstream_proxy, dangerously_allow_non_loopback_proxy: network.dangerously_allow_non_loopback_proxy, dangerously_allow_all_unix_sockets: network.dangerously_allow_all_unix_sockets, - allowed_domains: network.allowed_domains, - denied_domains: network.denied_domains, - allow_unix_sockets: network.allow_unix_sockets, + domains: network.domains.map(|domains| { + domains + .entries + .into_iter() + .map(|(pattern, permission)| { + (pattern, map_network_domain_permission_to_api(permission)) + }) + .collect() + }), + managed_allowed_domains_only: network.managed_allowed_domains_only, + allowed_domains, + denied_domains, + unix_sockets: network.unix_sockets.map(|unix_sockets| { + unix_sockets + .entries + .into_iter() + .map(|(path, permission)| { + (path, map_network_unix_socket_permission_to_api(permission)) + }) + .collect() + }), + allow_unix_sockets, allow_local_binding: network.allow_local_binding, } } +fn map_network_domain_permission_to_api( + permission: codex_core::config_loader::NetworkDomainPermissionToml, +) -> NetworkDomainPermission { + match permission { + codex_core::config_loader::NetworkDomainPermissionToml::Allow => { + NetworkDomainPermission::Allow + } + codex_core::config_loader::NetworkDomainPermissionToml::Deny => { + NetworkDomainPermission::Deny + } + } +} + +fn map_network_unix_socket_permission_to_api( + permission: codex_core::config_loader::NetworkUnixSocketPermissionToml, +) -> NetworkUnixSocketPermission { + match permission { + codex_core::config_loader::NetworkUnixSocketPermissionToml::Allow => { + NetworkUnixSocketPermission::Allow + } + codex_core::config_loader::NetworkUnixSocketPermissionToml::None => { + NetworkUnixSocketPermission::None + } + } +} + fn map_error(err: ConfigServiceError) -> JSONRPCErrorError { if let Some(code) = err.write_error_code() { return config_write_error(code, err.to_string()); @@ -452,7 +513,11 @@ mod tests { use codex_core::AnalyticsEventsClient; use codex_core::AuthManager; use codex_core::CodexAuth; + use codex_core::config_loader::NetworkDomainPermissionToml as CoreNetworkDomainPermissionToml; + use codex_core::config_loader::NetworkDomainPermissionsToml as CoreNetworkDomainPermissionsToml; use codex_core::config_loader::NetworkRequirementsToml as CoreNetworkRequirementsToml; + use codex_core::config_loader::NetworkUnixSocketPermissionToml as CoreNetworkUnixSocketPermissionToml; + use codex_core::config_loader::NetworkUnixSocketPermissionsToml as CoreNetworkUnixSocketPermissionsToml; use codex_features::Feature; use codex_protocol::protocol::AskForApproval as CoreAskForApproval; use pretty_assertions::assert_eq; @@ -505,10 +570,25 @@ mod tests { allow_upstream_proxy: Some(false), dangerously_allow_non_loopback_proxy: Some(false), dangerously_allow_all_unix_sockets: Some(true), - allowed_domains: Some(vec!["api.openai.com".to_string()]), + domains: Some(CoreNetworkDomainPermissionsToml { + entries: std::collections::BTreeMap::from([ + ( + "api.openai.com".to_string(), + CoreNetworkDomainPermissionToml::Allow, + ), + ( + "example.com".to_string(), + CoreNetworkDomainPermissionToml::Deny, + ), + ]), + }), managed_allowed_domains_only: Some(false), - denied_domains: Some(vec!["example.com".to_string()]), - allow_unix_sockets: Some(vec!["/tmp/proxy.sock".to_string()]), + unix_sockets: Some(CoreNetworkUnixSocketPermissionsToml { + entries: std::collections::BTreeMap::from([( + "/tmp/proxy.sock".to_string(), + CoreNetworkUnixSocketPermissionToml::Allow, + )]), + }), allow_local_binding: Some(true), }), }; @@ -550,14 +630,79 @@ mod tests { allow_upstream_proxy: Some(false), dangerously_allow_non_loopback_proxy: Some(false), dangerously_allow_all_unix_sockets: Some(true), + domains: Some(std::collections::BTreeMap::from([ + ("api.openai.com".to_string(), NetworkDomainPermission::Allow,), + ("example.com".to_string(), NetworkDomainPermission::Deny), + ])), + managed_allowed_domains_only: Some(false), allowed_domains: Some(vec!["api.openai.com".to_string()]), denied_domains: Some(vec!["example.com".to_string()]), + unix_sockets: Some(std::collections::BTreeMap::from([( + "/tmp/proxy.sock".to_string(), + NetworkUnixSocketPermission::Allow, + )])), allow_unix_sockets: Some(vec!["/tmp/proxy.sock".to_string()]), allow_local_binding: Some(true), }), ); } + #[test] + fn map_requirements_toml_to_api_omits_unix_socket_none_entries_from_legacy_network_fields() { + let requirements = ConfigRequirementsToml { + allowed_approval_policies: None, + allowed_sandbox_modes: None, + allowed_web_search_modes: None, + guardian_developer_instructions: None, + feature_requirements: None, + mcp_servers: None, + apps: None, + rules: None, + enforce_residency: None, + network: Some(CoreNetworkRequirementsToml { + enabled: None, + http_port: None, + socks_port: None, + allow_upstream_proxy: None, + dangerously_allow_non_loopback_proxy: None, + dangerously_allow_all_unix_sockets: None, + domains: None, + managed_allowed_domains_only: None, + unix_sockets: Some(CoreNetworkUnixSocketPermissionsToml { + entries: std::collections::BTreeMap::from([( + "/tmp/ignored.sock".to_string(), + CoreNetworkUnixSocketPermissionToml::None, + )]), + }), + allow_local_binding: None, + }), + }; + + let mapped = map_requirements_toml_to_api(requirements); + + assert_eq!( + mapped.network, + Some(NetworkRequirements { + enabled: None, + http_port: None, + socks_port: None, + allow_upstream_proxy: None, + dangerously_allow_non_loopback_proxy: None, + dangerously_allow_all_unix_sockets: None, + domains: None, + managed_allowed_domains_only: None, + allowed_domains: None, + denied_domains: None, + unix_sockets: Some(std::collections::BTreeMap::from([( + "/tmp/ignored.sock".to_string(), + NetworkUnixSocketPermission::None, + )])), + allow_unix_sockets: None, + allow_local_binding: None, + }), + ); + } + #[test] fn map_requirements_toml_to_api_normalizes_allowed_web_search_modes() { let requirements = ConfigRequirementsToml { diff --git a/codex-rs/config/src/config_requirements.rs b/codex-rs/config/src/config_requirements.rs index 57d762c0f1..d63fa1e8b2 100644 --- a/codex-rs/config/src/config_requirements.rs +++ b/codex-rs/config/src/config_requirements.rs @@ -5,6 +5,7 @@ use codex_protocol::protocol::SandboxPolicy; use codex_utils_absolute_path::AbsolutePathBuf; use serde::Deserialize; use serde::Serialize; +use serde::de::Error as _; use std::collections::BTreeMap; use std::fmt; @@ -132,7 +133,93 @@ pub struct McpServerRequirement { pub identity: McpServerIdentity, } -#[derive(Deserialize, Debug, Clone, Default, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] +pub struct NetworkDomainPermissionsToml { + #[serde(flatten)] + pub entries: BTreeMap, +} + +impl NetworkDomainPermissionsToml { + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + pub fn allowed_domains(&self) -> Option> { + let allowed_domains: Vec = self + .entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkDomainPermissionToml::Allow)) + .map(|(pattern, _)| pattern.clone()) + .collect(); + (!allowed_domains.is_empty()).then_some(allowed_domains) + } + + pub fn denied_domains(&self) -> Option> { + let denied_domains: Vec = self + .entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkDomainPermissionToml::Deny)) + .map(|(pattern, _)| pattern.clone()) + .collect(); + (!denied_domains.is_empty()).then_some(denied_domains) + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[serde(rename_all = "lowercase")] +pub enum NetworkDomainPermissionToml { + Allow, + Deny, +} + +impl std::fmt::Display for NetworkDomainPermissionToml { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let permission = match self { + Self::Allow => "allow", + Self::Deny => "deny", + }; + f.write_str(permission) + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] +pub struct NetworkUnixSocketPermissionsToml { + #[serde(flatten)] + pub entries: BTreeMap, +} + +impl NetworkUnixSocketPermissionsToml { + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + pub fn allow_unix_sockets(&self) -> Vec { + self.entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkUnixSocketPermissionToml::Allow)) + .map(|(path, _)| path.clone()) + .collect() + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[serde(rename_all = "lowercase")] +pub enum NetworkUnixSocketPermissionToml { + Allow, + None, +} + +impl std::fmt::Display for NetworkUnixSocketPermissionToml { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let permission = match self { + Self::Allow => "allow", + Self::None => "none", + }; + f.write_str(permission) + } +} + +#[derive(Serialize, Debug, Clone, Default, PartialEq, Eq)] pub struct NetworkRequirementsToml { pub enabled: Option, pub http_port: Option, @@ -140,17 +227,121 @@ pub struct NetworkRequirementsToml { pub allow_upstream_proxy: Option, pub dangerously_allow_non_loopback_proxy: Option, pub dangerously_allow_all_unix_sockets: Option, - pub allowed_domains: Option>, + pub domains: Option, /// When true, only managed `allowed_domains` are respected while managed /// network enforcement is active. User allowlist entries are ignored. pub managed_allowed_domains_only: Option, - pub denied_domains: Option>, - pub allow_unix_sockets: Option>, + pub unix_sockets: Option, pub allow_local_binding: Option, } +#[derive(Deserialize)] +struct RawNetworkRequirementsToml { + enabled: Option, + http_port: Option, + socks_port: Option, + allow_upstream_proxy: Option, + dangerously_allow_non_loopback_proxy: Option, + dangerously_allow_all_unix_sockets: Option, + domains: Option, + #[serde(default)] + allowed_domains: Option>, + /// When true, only managed `allowed_domains` are respected while managed + /// network enforcement is active. User allowlist entries are ignored. + managed_allowed_domains_only: Option, + #[serde(default)] + denied_domains: Option>, + unix_sockets: Option, + #[serde(default)] + allow_unix_sockets: Option>, + allow_local_binding: Option, +} + +impl<'de> Deserialize<'de> for NetworkRequirementsToml { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let raw = RawNetworkRequirementsToml::deserialize(deserializer)?; + let RawNetworkRequirementsToml { + enabled, + http_port, + socks_port, + allow_upstream_proxy, + dangerously_allow_non_loopback_proxy, + dangerously_allow_all_unix_sockets, + domains, + allowed_domains, + managed_allowed_domains_only, + denied_domains, + unix_sockets, + allow_unix_sockets, + allow_local_binding, + } = raw; + + if domains.is_some() && (allowed_domains.is_some() || denied_domains.is_some()) { + return Err(D::Error::custom( + "`experimental_network.domains` cannot be combined with legacy `allowed_domains` or `denied_domains`", + )); + } + + if unix_sockets.is_some() && allow_unix_sockets.is_some() { + return Err(D::Error::custom( + "`experimental_network.unix_sockets` cannot be combined with legacy `allow_unix_sockets`", + )); + } + + Ok(Self { + enabled, + http_port, + socks_port, + allow_upstream_proxy, + dangerously_allow_non_loopback_proxy, + dangerously_allow_all_unix_sockets, + domains: domains + .or_else(|| legacy_domain_permissions_from_lists(allowed_domains, denied_domains)), + managed_allowed_domains_only, + unix_sockets: unix_sockets + .or_else(|| legacy_unix_socket_permissions_from_list(allow_unix_sockets)), + allow_local_binding, + }) + } +} + +/// Legacy list normalization is intentionally lossy: explicit empty legacy +/// lists are treated as unset when converted to the canonical network +/// permission shape. +fn legacy_domain_permissions_from_lists( + allowed_domains: Option>, + denied_domains: Option>, +) -> Option { + let mut entries = BTreeMap::new(); + + for pattern in allowed_domains.unwrap_or_default() { + entries.insert(pattern, NetworkDomainPermissionToml::Allow); + } + + for pattern in denied_domains.unwrap_or_default() { + entries.insert(pattern, NetworkDomainPermissionToml::Deny); + } + + (!entries.is_empty()).then_some(NetworkDomainPermissionsToml { entries }) +} + +fn legacy_unix_socket_permissions_from_list( + allow_unix_sockets: Option>, +) -> Option { + let entries = allow_unix_sockets + .unwrap_or_default() + .into_iter() + .map(|path| (path, NetworkUnixSocketPermissionToml::Allow)) + .collect::>(); + + (!entries.is_empty()).then_some(NetworkUnixSocketPermissionsToml { entries }) +} + /// Normalized network constraints derived from requirements TOML. -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] pub struct NetworkConstraints { pub enabled: Option, pub http_port: Option, @@ -158,15 +349,24 @@ pub struct NetworkConstraints { pub allow_upstream_proxy: Option, pub dangerously_allow_non_loopback_proxy: Option, pub dangerously_allow_all_unix_sockets: Option, - pub allowed_domains: Option>, + pub domains: Option, /// When true, only managed `allowed_domains` are respected while managed /// network enforcement is active. User allowlist entries are ignored. pub managed_allowed_domains_only: Option, - pub denied_domains: Option>, - pub allow_unix_sockets: Option>, + pub unix_sockets: Option, pub allow_local_binding: Option, } +impl<'de> Deserialize<'de> for NetworkConstraints { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let requirements = NetworkRequirementsToml::deserialize(deserializer)?; + Ok(requirements.into()) + } +} + impl From for NetworkConstraints { fn from(value: NetworkRequirementsToml) -> Self { let NetworkRequirementsToml { @@ -176,10 +376,9 @@ impl From for NetworkConstraints { allow_upstream_proxy, dangerously_allow_non_loopback_proxy, dangerously_allow_all_unix_sockets, - allowed_domains, + domains, managed_allowed_domains_only, - denied_domains, - allow_unix_sockets, + unix_sockets, allow_local_binding, } = value; Self { @@ -189,10 +388,9 @@ impl From for NetworkConstraints { allow_upstream_proxy, dangerously_allow_non_loopback_proxy, dangerously_allow_all_unix_sockets, - allowed_domains, + domains, managed_allowed_domains_only, - denied_domains, - allow_unix_sockets, + unix_sockets, allow_local_binding, } } @@ -1470,6 +1668,78 @@ guardian_developer_instructions = """ #[test] fn network_requirements_are_preserved_as_constraints_with_source() -> Result<()> { + let toml_str = r#" + [experimental_network] + enabled = true + allow_upstream_proxy = false + dangerously_allow_all_unix_sockets = true + managed_allowed_domains_only = true + allow_local_binding = false + + [experimental_network.domains] + "api.example.com" = "allow" + "*.openai.com" = "allow" + "blocked.example.com" = "deny" + + [experimental_network.unix_sockets] + "/tmp/example.sock" = "allow" + "#; + + let source = RequirementSource::CloudRequirements; + let mut requirements_with_sources = ConfigRequirementsWithSources::default(); + requirements_with_sources.merge_unset_fields(source.clone(), from_str(toml_str)?); + + let requirements = ConfigRequirements::try_from(requirements_with_sources)?; + let sourced_network = requirements + .network + .expect("network requirements should be preserved as constraints"); + + assert_eq!(sourced_network.source, source); + assert_eq!(sourced_network.value.enabled, Some(true)); + assert_eq!(sourced_network.value.allow_upstream_proxy, Some(false)); + assert_eq!( + sourced_network.value.dangerously_allow_all_unix_sockets, + Some(true) + ); + assert_eq!( + sourced_network.value.domains.as_ref(), + Some(&NetworkDomainPermissionsToml { + entries: BTreeMap::from([ + ( + "*.openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "blocked.example.com".to_string(), + NetworkDomainPermissionToml::Deny, + ), + ]), + }) + ); + assert_eq!( + sourced_network.value.managed_allowed_domains_only, + Some(true) + ); + assert_eq!( + sourced_network.value.unix_sockets.as_ref(), + Some(&NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([( + "/tmp/example.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + )]), + }) + ); + assert_eq!(sourced_network.value.allow_local_binding, Some(false)); + + Ok(()) + } + + #[test] + fn legacy_network_requirements_are_preserved_as_constraints_with_source() -> Result<()> { let toml_str = r#" [experimental_network] enabled = true @@ -1499,29 +1769,137 @@ guardian_developer_instructions = """ Some(true) ); assert_eq!( - sourced_network.value.allowed_domains.as_ref(), - Some(&vec![ - "api.example.com".to_string(), - "*.openai.com".to_string() - ]) + sourced_network.value.domains.as_ref(), + Some(&NetworkDomainPermissionsToml { + entries: BTreeMap::from([ + ( + "*.openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "blocked.example.com".to_string(), + NetworkDomainPermissionToml::Deny, + ), + ]), + }) ); assert_eq!( sourced_network.value.managed_allowed_domains_only, Some(true) ); assert_eq!( - sourced_network.value.denied_domains.as_ref(), - Some(&vec!["blocked.example.com".to_string()]) - ); - assert_eq!( - sourced_network.value.allow_unix_sockets.as_ref(), - Some(&vec!["/tmp/example.sock".to_string()]) + sourced_network.value.unix_sockets.as_ref(), + Some(&NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([( + "/tmp/example.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + )]), + }) ); assert_eq!(sourced_network.value.allow_local_binding, Some(false)); Ok(()) } + #[test] + fn mixed_legacy_and_canonical_network_requirements_are_rejected() { + let err = from_str::( + r#" + [experimental_network] + allowed_domains = ["api.example.com"] + + [experimental_network.domains] + "*.openai.com" = "allow" + "#, + ) + .expect_err("mixed network domain shapes should fail"); + + assert!( + err.to_string() + .contains("`experimental_network.domains` cannot be combined"), + "unexpected error: {err:#}" + ); + + let err = from_str::( + r#" + [experimental_network] + allow_unix_sockets = ["/tmp/example.sock"] + + [experimental_network.unix_sockets] + "/tmp/another.sock" = "allow" + "#, + ) + .expect_err("mixed network unix socket shapes should fail"); + + assert!( + err.to_string() + .contains("`experimental_network.unix_sockets` cannot be combined"), + "unexpected error: {err:#}" + ); + } + + #[test] + fn network_permission_containers_project_allowed_and_denied_entries() { + let domains = NetworkDomainPermissionsToml { + entries: BTreeMap::from([ + ( + "*.openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "blocked.example.com".to_string(), + NetworkDomainPermissionToml::Deny, + ), + ]), + }; + let unix_sockets = NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([ + ( + "/tmp/example.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ( + "/tmp/ignored.sock".to_string(), + NetworkUnixSocketPermissionToml::None, + ), + ]), + }; + + assert_eq!( + domains.allowed_domains(), + Some(vec![ + "*.openai.com".to_string(), + "api.example.com".to_string() + ]) + ); + assert_eq!( + domains.denied_domains(), + Some(vec!["blocked.example.com".to_string()]) + ); + assert_eq!( + NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + } + .denied_domains(), + None + ); + assert_eq!( + unix_sockets.allow_unix_sockets(), + vec!["/tmp/example.sock".to_string()] + ); + } + #[test] fn deserialize_mcp_server_requirements() -> Result<()> { let toml_str = r#" diff --git a/codex-rs/config/src/lib.rs b/codex-rs/config/src/lib.rs index 7bd572fea3..9492d56b4d 100644 --- a/codex-rs/config/src/lib.rs +++ b/codex-rs/config/src/lib.rs @@ -25,7 +25,11 @@ pub use config_requirements::FeatureRequirementsToml; pub use config_requirements::McpServerIdentity; pub use config_requirements::McpServerRequirement; pub use config_requirements::NetworkConstraints; +pub use config_requirements::NetworkDomainPermissionToml; +pub use config_requirements::NetworkDomainPermissionsToml; pub use config_requirements::NetworkRequirementsToml; +pub use config_requirements::NetworkUnixSocketPermissionToml; +pub use config_requirements::NetworkUnixSocketPermissionsToml; pub use config_requirements::RequirementSource; pub use config_requirements::ResidencyRequirement; pub use config_requirements::SandboxModeRequirement; diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 3d091006c9..78113b126d 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -913,6 +913,16 @@ ], "type": "object" }, + "NetworkDomainPermissionToml": { + "enum": [ + "allow", + "deny" + ], + "type": "string" + }, + "NetworkDomainPermissionsToml": { + "type": "object" + }, "NetworkModeSchema": { "enum": [ "limited", @@ -926,32 +936,17 @@ "allow_local_binding": { "type": "boolean" }, - "allow_unix_sockets": { - "items": { - "type": "string" - }, - "type": "array" - }, "allow_upstream_proxy": { "type": "boolean" }, - "allowed_domains": { - "items": { - "type": "string" - }, - "type": "array" - }, "dangerously_allow_all_unix_sockets": { "type": "boolean" }, "dangerously_allow_non_loopback_proxy": { "type": "boolean" }, - "denied_domains": { - "items": { - "type": "string" - }, - "type": "array" + "domains": { + "$ref": "#/definitions/NetworkDomainPermissionsToml" }, "enable_socks5": { "type": "boolean" @@ -970,10 +965,23 @@ }, "socks_url": { "type": "string" + }, + "unix_sockets": { + "$ref": "#/definitions/NetworkUnixSocketPermissionsToml" } }, "type": "object" }, + "NetworkUnixSocketPermissionToml": { + "enum": [ + "allow", + "none" + ], + "type": "string" + }, + "NetworkUnixSocketPermissionsToml": { + "type": "object" + }, "Notice": { "description": "Settings for notices we display to users via the tui and app-server clients (primarily the Codex IDE extension). NOTE: these are different from notifications - notices are warnings, NUX screens, acknowledgements, etc.", "properties": { diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index ed24d28466..54f5c93f2d 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1029,8 +1029,16 @@ impl TurnContext { .network .as_ref()?; Some(TurnContextNetworkItem { - allowed_domains: network.allowed_domains.clone().unwrap_or_default(), - denied_domains: network.denied_domains.clone().unwrap_or_default(), + allowed_domains: network + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) + .unwrap_or_default(), + denied_domains: network + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::denied_domains) + .unwrap_or_default(), }) } } diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index 5a556f2071..ff0e08e211 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -5,6 +5,8 @@ use crate::config::test_config; use crate::config_loader::ConfigLayerStack; use crate::config_loader::ConfigLayerStackOrdering; use crate::config_loader::NetworkConstraints; +use crate::config_loader::NetworkDomainPermissionToml; +use crate::config_loader::NetworkDomainPermissionsToml; use crate::config_loader::RequirementSource; use crate::config_loader::Sourced; use crate::exec::ExecCapturePolicy; @@ -471,8 +473,8 @@ async fn start_managed_network_proxy_applies_execpolicy_network_rules() -> anyho let current_cfg = started_proxy.proxy().current_cfg().await?; assert_eq!( - current_cfg.network.allowed_domains, - vec!["example.com".to_string()] + current_cfg.network.allowed_domains(), + Some(vec!["example.com".to_string()]) ); Ok(()) } @@ -483,7 +485,12 @@ async fn start_managed_network_proxy_ignores_invalid_execpolicy_network_rules() let spec = crate::config::NetworkProxySpec::from_config_and_constraints( NetworkProxyConfig::default(), Some(NetworkConstraints { - allowed_domains: Some(vec!["managed.example.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: std::collections::BTreeMap::from([( + "managed.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), managed_allowed_domains_only: Some(true), ..Default::default() }), @@ -510,8 +517,8 @@ async fn start_managed_network_proxy_ignores_invalid_execpolicy_network_rules() let current_cfg = started_proxy.proxy().current_cfg().await?; assert_eq!( - current_cfg.network.allowed_domains, - vec!["managed.example.com".to_string()] + current_cfg.network.allowed_domains(), + Some(vec!["managed.example.com".to_string()]) ); Ok(()) } @@ -3687,8 +3694,18 @@ async fn build_settings_update_items_emits_environment_item_for_network_changes( let mut requirements = config.config_layer_stack.requirements().clone(); requirements.network = Some(Sourced::new( NetworkConstraints { - allowed_domains: Some(vec!["api.example.com".to_string()]), - denied_domains: Some(vec!["blocked.example.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: std::collections::BTreeMap::from([ + ( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "blocked.example.com".to_string(), + NetworkDomainPermissionToml::Deny, + ), + ]), + }), ..Default::default() }, RequirementSource::CloudRequirements, diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 4d6527ab80..60f0e06f7d 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -306,7 +306,9 @@ enabled = true proxy_url = "http://127.0.0.1:43128" enable_socks5 = false allow_upstream_proxy = false -allowed_domains = ["openai.com"] + +[permissions.workspace.network.domains] +"openai.com" = "allow" "#; let cfg: ConfigToml = toml::from_str(toml).expect("TOML deserialization should succeed for permissions profiles"); @@ -343,9 +345,13 @@ allowed_domains = ["openai.com"] dangerously_allow_non_loopback_proxy: None, dangerously_allow_all_unix_sockets: None, mode: None, - allowed_domains: Some(vec!["openai.com".to_string()]), - denied_domains: None, - allow_unix_sockets: None, + domains: Some(NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), + unix_sockets: None, allow_local_binding: None, }), }, @@ -421,7 +427,12 @@ fn permissions_profiles_network_disabled_by_default_does_not_start_proxy() -> st )]), }), network: Some(NetworkToml { - allowed_domains: Some(vec!["openai.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), ..Default::default() }), }, diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index fa21cbad84..1a0722119b 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -123,9 +123,14 @@ pub use network_proxy_spec::NetworkProxySpec; pub use network_proxy_spec::StartedNetworkProxy; pub use permissions::FilesystemPermissionToml; pub use permissions::FilesystemPermissionsToml; +pub use permissions::NetworkDomainPermissionToml; +pub use permissions::NetworkDomainPermissionsToml; pub use permissions::NetworkToml; +pub use permissions::NetworkUnixSocketPermissionToml; +pub use permissions::NetworkUnixSocketPermissionsToml; pub use permissions::PermissionProfileToml; pub use permissions::PermissionsToml; +pub(crate) use permissions::overlay_network_domain_permissions; pub(crate) use permissions::resolve_permission_profile; pub use service::ConfigService; pub use service::ConfigServiceError; diff --git a/codex-rs/core/src/config/network_proxy_spec.rs b/codex-rs/core/src/config/network_proxy_spec.rs index 386100d34b..93b59cf5f5 100644 --- a/codex-rs/core/src/config/network_proxy_spec.rs +++ b/codex-rs/core/src/config/network_proxy_spec.rs @@ -226,33 +226,63 @@ impl NetworkProxySpec { Some(dangerously_allow_all_unix_sockets); } let managed_allowed_domains = if hard_deny_allowlist_misses { - Some(requirements.allowed_domains.clone().unwrap_or_default()) + Some( + requirements + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) + .unwrap_or_default(), + ) } else { - requirements.allowed_domains.clone() + requirements + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) }; - if let Some(allowed_domains) = managed_allowed_domains { + if let Some(managed_allowed_domains) = managed_allowed_domains { // Managed requirements seed the baseline allowlist. User additions // can extend that baseline unless managed-only mode pins the // effective allowlist to the managed set. - config.network.allowed_domains = if allowlist_expansion_enabled { - Self::merge_domain_lists(allowed_domains.clone(), &config.network.allowed_domains) + let effective_allowed_domains = if allowlist_expansion_enabled { + Self::merge_domain_lists( + managed_allowed_domains.clone(), + config.network.allowed_domains().as_deref().unwrap_or(&[]), + ) } else { - allowed_domains.clone() + managed_allowed_domains.clone() }; - constraints.allowed_domains = Some(allowed_domains); + config + .network + .set_allowed_domains(effective_allowed_domains); + constraints.allowed_domains = Some(managed_allowed_domains); constraints.allowlist_expansion_enabled = Some(allowlist_expansion_enabled); } - if let Some(denied_domains) = requirements.denied_domains.clone() { - config.network.denied_domains = if denylist_expansion_enabled { - Self::merge_domain_lists(denied_domains.clone(), &config.network.denied_domains) + let managed_denied_domains = requirements + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::denied_domains); + if let Some(managed_denied_domains) = managed_denied_domains { + let effective_denied_domains = if denylist_expansion_enabled { + Self::merge_domain_lists( + managed_denied_domains.clone(), + config.network.denied_domains().as_deref().unwrap_or(&[]), + ) } else { - denied_domains.clone() + managed_denied_domains.clone() }; - constraints.denied_domains = Some(denied_domains); + config.network.set_denied_domains(effective_denied_domains); + constraints.denied_domains = Some(managed_denied_domains); constraints.denylist_expansion_enabled = Some(denylist_expansion_enabled); } - if let Some(allow_unix_sockets) = requirements.allow_unix_sockets.clone() { - config.network.allow_unix_sockets = allow_unix_sockets.clone(); + if requirements.unix_sockets.is_some() { + let allow_unix_sockets = requirements + .unix_sockets + .as_ref() + .map(codex_config::NetworkUnixSocketPermissionsToml::allow_unix_sockets) + .unwrap_or_default(); + config + .network + .set_allow_unix_sockets(allow_unix_sockets.clone()); constraints.allow_unix_sockets = Some(allow_unix_sockets); } if let Some(allow_local_binding) = requirements.allow_local_binding { @@ -299,37 +329,25 @@ impl NetworkProxySpec { fn apply_exec_policy_network_rules(config: &mut NetworkProxyConfig, exec_policy: &Policy) { let (allowed_domains, denied_domains) = exec_policy.compiled_network_domains(); - upsert_network_domains( - &mut config.network.allowed_domains, - &mut config.network.denied_domains, - allowed_domains, - ); - upsert_network_domains( - &mut config.network.denied_domains, - &mut config.network.allowed_domains, - denied_domains, - ); + upsert_network_domains(config, allowed_domains, /*allow*/ true); + upsert_network_domains(config, denied_domains, /*allow*/ false); } -fn upsert_network_domains( - target: &mut Vec, - opposite: &mut Vec, - hosts: Vec, -) { +fn upsert_network_domains(config: &mut NetworkProxyConfig, hosts: Vec, allow: bool) { let mut incoming = HashSet::new(); - let mut deduped_hosts = Vec::new(); for host in hosts { if incoming.insert(host.clone()) { - deduped_hosts.push(host); + config.network.upsert_domain_permission( + host, + if allow { + codex_network_proxy::NetworkDomainPermission::Allow + } else { + codex_network_proxy::NetworkDomainPermission::Deny + }, + normalize_host, + ); } } - if incoming.is_empty() { - return; - } - - opposite.retain(|entry| !incoming.contains(&normalize_host(entry))); - target.retain(|entry| !incoming.contains(&normalize_host(entry))); - target.extend(deduped_hosts); } #[cfg(test)] diff --git a/codex-rs/core/src/config/network_proxy_spec_tests.rs b/codex-rs/core/src/config/network_proxy_spec_tests.rs index 4c6e82358e..77007885a4 100644 --- a/codex-rs/core/src/config/network_proxy_spec_tests.rs +++ b/codex-rs/core/src/config/network_proxy_spec_tests.rs @@ -1,6 +1,20 @@ use super::*; +use crate::config_loader::NetworkDomainPermissionToml; +use crate::config_loader::NetworkDomainPermissionsToml; +use codex_network_proxy::NetworkDomainPermission; use pretty_assertions::assert_eq; +fn domain_permissions( + entries: impl IntoIterator, +) -> NetworkDomainPermissionsToml { + NetworkDomainPermissionsToml { + entries: entries + .into_iter() + .map(|(pattern, permission)| (pattern.to_string(), permission)) + .collect(), + } +} + #[test] fn build_state_with_audit_metadata_threads_metadata_to_state() { let spec = NetworkProxySpec { @@ -24,9 +38,14 @@ fn build_state_with_audit_metadata_threads_metadata_to_state() { #[test] fn requirements_allowed_domains_are_a_baseline_for_user_allowlist() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["api.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); let requirements = NetworkConstraints { - allowed_domains: Some(vec!["*.example.com".to_string()]), + domains: Some(domain_permissions([( + "*.example.com", + NetworkDomainPermissionToml::Allow, + )])), ..Default::default() }; @@ -38,8 +57,11 @@ fn requirements_allowed_domains_are_a_baseline_for_user_allowlist() { .expect("config should stay within the managed allowlist"); assert_eq!( - spec.config.network.allowed_domains, - vec!["*.example.com".to_string(), "api.example.com".to_string()] + spec.config.network.allowed_domains(), + Some(vec![ + "*.example.com".to_string(), + "api.example.com".to_string() + ]) ); assert_eq!( spec.constraints.allowed_domains, @@ -48,14 +70,92 @@ fn requirements_allowed_domains_are_a_baseline_for_user_allowlist() { assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(true)); } +#[test] +fn requirements_allowed_domains_do_not_override_user_denies_for_same_pattern() { + let mut config = NetworkProxyConfig::default(); + config + .network + .set_denied_domains(vec!["api.example.com".to_string()]); + let requirements = NetworkConstraints { + domains: Some(domain_permissions([( + "api.example.com", + NetworkDomainPermissionToml::Allow, + )])), + ..Default::default() + }; + + let spec = NetworkProxySpec::from_config_and_constraints( + config, + Some(requirements), + &SandboxPolicy::new_workspace_write_policy(), + ) + .expect("managed allowlist should not erase a user deny"); + + assert_eq!(spec.config.network.allowed_domains(), None); + assert_eq!( + spec.config.network.denied_domains(), + Some(vec!["api.example.com".to_string()]) + ); + assert_eq!( + spec.constraints.allowed_domains, + Some(vec!["api.example.com".to_string()]) + ); +} + +#[test] +fn requirements_allowlist_expansion_keeps_user_entries_mutable() { + let mut config = NetworkProxyConfig::default(); + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); + let requirements = NetworkConstraints { + domains: Some(domain_permissions([( + "*.example.com", + NetworkDomainPermissionToml::Allow, + )])), + ..Default::default() + }; + + let spec = NetworkProxySpec::from_config_and_constraints( + config, + Some(requirements), + &SandboxPolicy::new_workspace_write_policy(), + ) + .expect("managed baseline should still allow user edits"); + + let mut candidate = spec.config.clone(); + candidate.network.upsert_domain_permission( + "api.example.com".to_string(), + NetworkDomainPermission::Deny, + normalize_host, + ); + + assert_eq!( + candidate.network.allowed_domains(), + Some(vec!["*.example.com".to_string()]) + ); + assert_eq!( + candidate.network.denied_domains(), + Some(vec!["api.example.com".to_string()]) + ); + validate_policy_against_constraints(&candidate, &spec.constraints) + .expect("user allowlist entries should not become managed constraints"); +} + #[test] fn danger_full_access_keeps_managed_allowlist_and_denylist_fixed() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["evil.com".to_string()]; - config.network.denied_domains = vec!["more-blocked.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["evil.com".to_string()]); + config + .network + .set_denied_domains(vec!["more-blocked.example.com".to_string()]); let requirements = NetworkConstraints { - allowed_domains: Some(vec!["*.example.com".to_string()]), - denied_domains: Some(vec!["blocked.example.com".to_string()]), + domains: Some(domain_permissions([ + ("*.example.com", NetworkDomainPermissionToml::Allow), + ("blocked.example.com", NetworkDomainPermissionToml::Deny), + ])), ..Default::default() }; @@ -67,12 +167,12 @@ fn danger_full_access_keeps_managed_allowlist_and_denylist_fixed() { .expect("yolo mode should pin the effective policy to the managed baseline"); assert_eq!( - spec.config.network.allowed_domains, - vec!["*.example.com".to_string()] + spec.config.network.allowed_domains(), + Some(vec!["*.example.com".to_string()]) ); assert_eq!( - spec.config.network.denied_domains, - vec!["blocked.example.com".to_string()] + spec.config.network.denied_domains(), + Some(vec!["blocked.example.com".to_string()]) ); assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(false)); assert_eq!(spec.constraints.denylist_expansion_enabled, Some(false)); @@ -81,9 +181,14 @@ fn danger_full_access_keeps_managed_allowlist_and_denylist_fixed() { #[test] fn managed_allowed_domains_only_disables_default_mode_allowlist_expansion() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["api.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); let requirements = NetworkConstraints { - allowed_domains: Some(vec!["*.example.com".to_string()]), + domains: Some(domain_permissions([( + "*.example.com", + NetworkDomainPermissionToml::Allow, + )])), managed_allowed_domains_only: Some(true), ..Default::default() }; @@ -96,8 +201,8 @@ fn managed_allowed_domains_only_disables_default_mode_allowlist_expansion() { .expect("managed baseline should still load"); assert_eq!( - spec.config.network.allowed_domains, - vec!["*.example.com".to_string()] + spec.config.network.allowed_domains(), + Some(vec!["*.example.com".to_string()]) ); assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(false)); } @@ -105,9 +210,14 @@ fn managed_allowed_domains_only_disables_default_mode_allowlist_expansion() { #[test] fn managed_allowed_domains_only_ignores_user_allowlist_and_hard_denies_misses() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["api.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); let requirements = NetworkConstraints { - allowed_domains: Some(vec!["managed.example.com".to_string()]), + domains: Some(domain_permissions([( + "managed.example.com", + NetworkDomainPermissionToml::Allow, + )])), managed_allowed_domains_only: Some(true), ..Default::default() }; @@ -120,8 +230,8 @@ fn managed_allowed_domains_only_ignores_user_allowlist_and_hard_denies_misses() .expect("managed-only allowlist should still load"); assert_eq!( - spec.config.network.allowed_domains, - vec!["managed.example.com".to_string()] + spec.config.network.allowed_domains(), + Some(vec!["managed.example.com".to_string()]) ); assert_eq!( spec.constraints.allowed_domains, @@ -134,7 +244,9 @@ fn managed_allowed_domains_only_ignores_user_allowlist_and_hard_denies_misses() #[test] fn managed_allowed_domains_only_without_managed_allowlist_blocks_all_user_domains() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["api.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); let requirements = NetworkConstraints { managed_allowed_domains_only: Some(true), ..Default::default() @@ -147,7 +259,7 @@ fn managed_allowed_domains_only_without_managed_allowlist_blocks_all_user_domain ) .expect("managed-only mode should treat missing managed allowlist as empty"); - assert!(spec.config.network.allowed_domains.is_empty()); + assert_eq!(spec.config.network.allowed_domains(), None); assert_eq!(spec.constraints.allowed_domains, Some(Vec::new())); assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(false)); assert!(spec.hard_deny_allowlist_misses); @@ -156,7 +268,9 @@ fn managed_allowed_domains_only_without_managed_allowlist_blocks_all_user_domain #[test] fn managed_allowed_domains_only_blocks_all_user_domains_in_full_access_without_managed_list() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["api.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); let requirements = NetworkConstraints { managed_allowed_domains_only: Some(true), ..Default::default() @@ -169,18 +283,89 @@ fn managed_allowed_domains_only_blocks_all_user_domains_in_full_access_without_m ) .expect("managed-only mode should treat missing managed allowlist as empty"); - assert!(spec.config.network.allowed_domains.is_empty()); + assert_eq!(spec.config.network.allowed_domains(), None); assert_eq!(spec.constraints.allowed_domains, Some(Vec::new())); assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(false)); assert!(spec.hard_deny_allowlist_misses); } +#[test] +fn deny_only_requirements_do_not_create_allow_constraints_in_full_access() { + let mut config = NetworkProxyConfig::default(); + config + .network + .set_allowed_domains(vec!["api.example.com".to_string()]); + let requirements = NetworkConstraints { + domains: Some(domain_permissions([( + "managed-blocked.example.com", + NetworkDomainPermissionToml::Deny, + )])), + ..Default::default() + }; + + let spec = NetworkProxySpec::from_config_and_constraints( + config, + Some(requirements), + &SandboxPolicy::DangerFullAccess, + ) + .expect("deny-only requirements should not constrain the allowlist"); + + assert_eq!( + spec.config.network.allowed_domains(), + Some(vec!["api.example.com".to_string()]) + ); + assert_eq!(spec.constraints.allowed_domains, None); + assert_eq!(spec.constraints.allowlist_expansion_enabled, None); + assert_eq!( + spec.config.network.denied_domains(), + Some(vec!["managed-blocked.example.com".to_string()]) + ); +} + +#[test] +fn allow_only_requirements_do_not_create_deny_constraints_in_full_access() { + let mut config = NetworkProxyConfig::default(); + config + .network + .set_denied_domains(vec!["blocked.example.com".to_string()]); + let requirements = NetworkConstraints { + domains: Some(domain_permissions([( + "managed.example.com", + NetworkDomainPermissionToml::Allow, + )])), + ..Default::default() + }; + + let spec = NetworkProxySpec::from_config_and_constraints( + config, + Some(requirements), + &SandboxPolicy::DangerFullAccess, + ) + .expect("allow-only requirements should not constrain the denylist"); + + assert_eq!( + spec.config.network.allowed_domains(), + Some(vec!["managed.example.com".to_string()]) + ); + assert_eq!( + spec.config.network.denied_domains(), + Some(vec!["blocked.example.com".to_string()]) + ); + assert_eq!(spec.constraints.denied_domains, None); + assert_eq!(spec.constraints.denylist_expansion_enabled, None); +} + #[test] fn requirements_denied_domains_are_a_baseline_for_default_mode() { let mut config = NetworkProxyConfig::default(); - config.network.denied_domains = vec!["blocked.example.com".to_string()]; + config + .network + .set_denied_domains(vec!["blocked.example.com".to_string()]); let requirements = NetworkConstraints { - denied_domains: Some(vec!["managed-blocked.example.com".to_string()]), + domains: Some(domain_permissions([( + "managed-blocked.example.com", + NetworkDomainPermissionToml::Deny, + )])), ..Default::default() }; @@ -192,11 +377,55 @@ fn requirements_denied_domains_are_a_baseline_for_default_mode() { .expect("default mode should merge managed and user deny entries"); assert_eq!( - spec.config.network.denied_domains, - vec![ + spec.config.network.denied_domains(), + Some(vec![ "managed-blocked.example.com".to_string(), "blocked.example.com".to_string() - ] + ]) + ); + assert_eq!( + spec.constraints.denied_domains, + Some(vec!["managed-blocked.example.com".to_string()]) ); assert_eq!(spec.constraints.denylist_expansion_enabled, Some(true)); } + +#[test] +fn requirements_denylist_expansion_keeps_user_entries_mutable() { + let mut config = NetworkProxyConfig::default(); + config + .network + .set_denied_domains(vec!["blocked.example.com".to_string()]); + let requirements = NetworkConstraints { + domains: Some(domain_permissions([( + "managed-blocked.example.com", + NetworkDomainPermissionToml::Deny, + )])), + ..Default::default() + }; + + let spec = NetworkProxySpec::from_config_and_constraints( + config, + Some(requirements), + &SandboxPolicy::new_workspace_write_policy(), + ) + .expect("managed baseline should still allow user edits"); + + let mut candidate = spec.config.clone(); + candidate.network.upsert_domain_permission( + "blocked.example.com".to_string(), + NetworkDomainPermission::Allow, + normalize_host, + ); + + assert_eq!( + candidate.network.allowed_domains(), + Some(vec!["blocked.example.com".to_string()]) + ); + assert_eq!( + candidate.network.denied_domains(), + Some(vec!["managed-blocked.example.com".to_string()]) + ); + validate_policy_against_constraints(&candidate, &spec.constraints) + .expect("user denylist entries should not become managed constraints"); +} diff --git a/codex-rs/core/src/config/permissions.rs b/codex-rs/core/src/config/permissions.rs index 759c269b76..73dad1c73c 100644 --- a/codex-rs/core/src/config/permissions.rs +++ b/codex-rs/core/src/config/permissions.rs @@ -5,8 +5,11 @@ use std::path::Component; use std::path::Path; use std::path::PathBuf; +use codex_network_proxy::NetworkDomainPermission as ProxyNetworkDomainPermission; use codex_network_proxy::NetworkMode; use codex_network_proxy::NetworkProxyConfig; +use codex_network_proxy::NetworkUnixSocketPermission as ProxyNetworkUnixSocketPermission; +use codex_network_proxy::normalize_host; use codex_protocol::permissions::FileSystemAccessMode; use codex_protocol::permissions::FileSystemPath; use codex_protocol::permissions::FileSystemSandboxEntry; @@ -56,6 +59,98 @@ pub enum FilesystemPermissionToml { Scoped(BTreeMap), } +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)] +pub struct NetworkDomainPermissionsToml { + #[serde(flatten)] + pub entries: BTreeMap, +} + +impl NetworkDomainPermissionsToml { + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + #[cfg(test)] + pub(crate) fn allowed_domains(&self) -> Option> { + let allowed_domains: Vec = self + .entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkDomainPermissionToml::Allow)) + .map(|(pattern, _)| pattern.clone()) + .collect(); + (!allowed_domains.is_empty()).then_some(allowed_domains) + } + + #[cfg(test)] + pub(crate) fn denied_domains(&self) -> Option> { + let denied_domains: Vec = self + .entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkDomainPermissionToml::Deny)) + .map(|(pattern, _)| pattern.clone()) + .collect(); + (!denied_domains.is_empty()).then_some(denied_domains) + } +} + +#[derive( + Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, JsonSchema, +)] +#[serde(rename_all = "lowercase")] +pub enum NetworkDomainPermissionToml { + Allow, + Deny, +} + +impl std::fmt::Display for NetworkDomainPermissionToml { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let permission = match self { + Self::Allow => "allow", + Self::Deny => "deny", + }; + f.write_str(permission) + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)] +pub struct NetworkUnixSocketPermissionsToml { + #[serde(flatten)] + pub entries: BTreeMap, +} + +impl NetworkUnixSocketPermissionsToml { + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + pub(crate) fn allow_unix_sockets(&self) -> Vec { + self.entries + .iter() + .filter(|(_, permission)| matches!(permission, NetworkUnixSocketPermissionToml::Allow)) + .map(|(path, _)| path.clone()) + .collect() + } +} + +#[derive( + Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, JsonSchema, +)] +#[serde(rename_all = "lowercase")] +pub enum NetworkUnixSocketPermissionToml { + Allow, + None, +} + +impl std::fmt::Display for NetworkUnixSocketPermissionToml { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let permission = match self { + Self::Allow => "allow", + Self::None => "none", + }; + f.write_str(permission) + } +} + #[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)] #[schemars(deny_unknown_fields)] pub struct NetworkToml { @@ -69,9 +164,8 @@ pub struct NetworkToml { pub dangerously_allow_all_unix_sockets: Option, #[schemars(with = "Option")] pub mode: Option, - pub allowed_domains: Option>, - pub denied_domains: Option>, - pub allow_unix_sockets: Option>, + pub domains: Option, + pub unix_sockets: Option, pub allow_local_binding: Option, } @@ -114,14 +208,22 @@ impl NetworkToml { if let Some(mode) = self.mode { config.network.mode = mode; } - if let Some(allowed_domains) = self.allowed_domains.as_ref() { - config.network.allowed_domains = allowed_domains.clone(); + if let Some(domains) = self.domains.as_ref() { + overlay_network_domain_permissions(config, domains); } - if let Some(denied_domains) = self.denied_domains.as_ref() { - config.network.denied_domains = denied_domains.clone(); - } - if let Some(allow_unix_sockets) = self.allow_unix_sockets.as_ref() { - config.network.allow_unix_sockets = allow_unix_sockets.clone(); + if let Some(unix_sockets) = self.unix_sockets.as_ref() { + let mut proxy_unix_sockets = config.network.unix_sockets.take().unwrap_or_default(); + for (path, permission) in &unix_sockets.entries { + let permission = match permission { + NetworkUnixSocketPermissionToml::Allow => { + ProxyNetworkUnixSocketPermission::Allow + } + NetworkUnixSocketPermissionToml::None => ProxyNetworkUnixSocketPermission::None, + }; + proxy_unix_sockets.entries.insert(path.clone(), permission); + } + config.network.unix_sockets = + (!proxy_unix_sockets.entries.is_empty()).then_some(proxy_unix_sockets); } if let Some(allow_local_binding) = self.allow_local_binding { config.network.allow_local_binding = allow_local_binding; @@ -135,6 +237,21 @@ impl NetworkToml { } } +pub(crate) fn overlay_network_domain_permissions( + config: &mut NetworkProxyConfig, + domains: &NetworkDomainPermissionsToml, +) { + for (pattern, permission) in &domains.entries { + let permission = match permission { + NetworkDomainPermissionToml::Allow => ProxyNetworkDomainPermission::Allow, + NetworkDomainPermissionToml::Deny => ProxyNetworkDomainPermission::Deny, + }; + config + .network + .upsert_domain_permission(pattern.clone(), permission, normalize_host); + } +} + pub(crate) fn network_proxy_config_from_profile_network( network: Option<&NetworkToml>, ) -> NetworkProxyConfig { diff --git a/codex-rs/core/src/config/permissions_tests.rs b/codex-rs/core/src/config/permissions_tests.rs index b90b903e88..b1313d1b0e 100644 --- a/codex-rs/core/src/config/permissions_tests.rs +++ b/codex-rs/core/src/config/permissions_tests.rs @@ -76,3 +76,132 @@ fn restricted_read_implicitly_allows_helper_executables() -> std::io::Result<()> Ok(()) } + +#[test] +fn network_toml_ignores_legacy_network_list_keys() { + let parsed = toml::from_str::( + r#" +allowed_domains = ["openai.com"] +"#, + ) + .expect("legacy network list keys should be ignored"); + + assert_eq!(parsed, NetworkToml::default()); +} + +#[test] +fn network_permission_containers_project_allowed_and_denied_entries() { + let domains = NetworkDomainPermissionsToml { + entries: BTreeMap::from([ + ( + "*.openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + ), + ( + "blocked.example.com".to_string(), + NetworkDomainPermissionToml::Deny, + ), + ]), + }; + let unix_sockets = NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([ + ( + "/tmp/example.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ( + "/tmp/ignored.sock".to_string(), + NetworkUnixSocketPermissionToml::None, + ), + ]), + }; + + assert_eq!( + domains.allowed_domains(), + Some(vec![ + "*.openai.com".to_string(), + "api.example.com".to_string() + ]) + ); + assert_eq!( + domains.denied_domains(), + Some(vec!["blocked.example.com".to_string()]) + ); + assert_eq!( + NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "api.example.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + } + .denied_domains(), + None + ); + assert_eq!( + unix_sockets.allow_unix_sockets(), + vec!["/tmp/example.sock".to_string()] + ); +} + +#[test] +fn network_toml_overlays_unix_socket_permissions_by_path() { + let mut config = NetworkProxyConfig::default(); + + NetworkToml { + unix_sockets: Some(NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([ + ( + "/tmp/base.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ( + "/tmp/override.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ]), + }), + ..Default::default() + } + .apply_to_network_proxy_config(&mut config); + + NetworkToml { + unix_sockets: Some(NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([ + ( + "/tmp/extra.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ( + "/tmp/override.sock".to_string(), + NetworkUnixSocketPermissionToml::None, + ), + ]), + }), + ..Default::default() + } + .apply_to_network_proxy_config(&mut config); + + assert_eq!( + config.network.unix_sockets, + Some(codex_network_proxy::NetworkUnixSocketPermissions { + entries: BTreeMap::from([ + ( + "/tmp/base.sock".to_string(), + ProxyNetworkUnixSocketPermission::Allow, + ), + ( + "/tmp/extra.sock".to_string(), + ProxyNetworkUnixSocketPermission::Allow, + ), + ( + "/tmp/override.sock".to_string(), + ProxyNetworkUnixSocketPermission::None, + ), + ]), + }) + ); +} diff --git a/codex-rs/core/src/config_loader/mod.rs b/codex-rs/core/src/config_loader/mod.rs index e4c5039588..df33665956 100644 --- a/codex-rs/core/src/config_loader/mod.rs +++ b/codex-rs/core/src/config_loader/mod.rs @@ -42,7 +42,11 @@ pub use codex_config::LoaderOverrides; pub use codex_config::McpServerIdentity; pub use codex_config::McpServerRequirement; pub use codex_config::NetworkConstraints; +pub use codex_config::NetworkDomainPermissionToml; +pub use codex_config::NetworkDomainPermissionsToml; pub use codex_config::NetworkRequirementsToml; +pub use codex_config::NetworkUnixSocketPermissionToml; +pub use codex_config::NetworkUnixSocketPermissionsToml; pub use codex_config::RequirementSource; pub use codex_config::ResidencyRequirement; pub use codex_config::SandboxModeRequirement; diff --git a/codex-rs/core/src/environment_context.rs b/codex-rs/core/src/environment_context.rs index bbb39da113..df4e49cf4e 100644 --- a/codex-rs/core/src/environment_context.rs +++ b/codex-rs/core/src/environment_context.rs @@ -130,8 +130,16 @@ impl EnvironmentContext { .as_ref()?; Some(NetworkContext { - allowed_domains: network.allowed_domains.clone().unwrap_or_default(), - denied_domains: network.denied_domains.clone().unwrap_or_default(), + allowed_domains: network + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) + .unwrap_or_default(), + denied_domains: network + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::denied_domains) + .unwrap_or_default(), }) } diff --git a/codex-rs/core/src/guardian/tests.rs b/codex-rs/core/src/guardian/tests.rs index 3e6d409e76..b71b48bd62 100644 --- a/codex-rs/core/src/guardian/tests.rs +++ b/codex-rs/core/src/guardian/tests.rs @@ -11,6 +11,8 @@ use crate::config::test_config; use crate::config_loader::ConfigLayerStack; use crate::config_loader::FeatureRequirementsToml; use crate::config_loader::NetworkConstraints; +use crate::config_loader::NetworkDomainPermissionToml; +use crate::config_loader::NetworkDomainPermissionsToml; use crate::config_loader::RequirementSource; use crate::config_loader::Sourced; use crate::protocol::SandboxPolicy; @@ -971,7 +973,12 @@ fn guardian_review_session_config_preserves_parent_network_proxy() { NetworkProxyConfig::default(), Some(NetworkConstraints { enabled: Some(true), - allowed_domains: Some(vec!["github.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: std::collections::BTreeMap::from([( + "github.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), ..Default::default() }), parent_config.permissions.sandbox_policy.get(), @@ -1027,7 +1034,9 @@ fn guardian_review_session_config_uses_live_network_proxy_state() { let mut parent_config = test_config(); let mut parent_network = NetworkProxyConfig::default(); parent_network.network.enabled = true; - parent_network.network.allowed_domains = vec!["parent.example".to_string()]; + parent_network + .network + .set_allowed_domains(vec!["parent.example".to_string()]); parent_config.permissions.network = Some( NetworkProxySpec::from_config_and_constraints( parent_network, @@ -1039,7 +1048,9 @@ fn guardian_review_session_config_uses_live_network_proxy_state() { let mut live_network = NetworkProxyConfig::default(); live_network.network.enabled = true; - live_network.network.allowed_domains = vec!["github.com".to_string()]; + live_network + .network + .set_allowed_domains(vec!["github.com".to_string()]); let guardian_config = build_guardian_review_session_config_for_test( &parent_config, diff --git a/codex-rs/core/src/network_proxy_loader.rs b/codex-rs/core/src/network_proxy_loader.rs index 1db111ee63..a6740b05fb 100644 --- a/codex-rs/core/src/network_proxy_loader.rs +++ b/codex-rs/core/src/network_proxy_loader.rs @@ -1,6 +1,7 @@ use crate::config::NetworkToml; use crate::config::PermissionsToml; use crate::config::find_codex_home; +use crate::config::overlay_network_domain_permissions; use crate::config::resolve_permission_profile; use crate::config_loader::CloudRequirementsLoader; use crate::config_loader::ConfigLayerStack; @@ -150,13 +151,20 @@ fn apply_network_constraints(network: NetworkToml, constraints: &mut NetworkProx if let Some(dangerously_allow_all_unix_sockets) = network.dangerously_allow_all_unix_sockets { constraints.dangerously_allow_all_unix_sockets = Some(dangerously_allow_all_unix_sockets); } - if let Some(allowed_domains) = network.allowed_domains { - constraints.allowed_domains = Some(allowed_domains); + if let Some(domains) = network.domains.as_ref() { + let mut config = NetworkProxyConfig::default(); + if let Some(allowed_domains) = constraints.allowed_domains.take() { + config.network.set_allowed_domains(allowed_domains); + } + if let Some(denied_domains) = constraints.denied_domains.take() { + config.network.set_denied_domains(denied_domains); + } + overlay_network_domain_permissions(&mut config, domains); + constraints.allowed_domains = config.network.allowed_domains(); + constraints.denied_domains = config.network.denied_domains(); } - if let Some(denied_domains) = network.denied_domains { - constraints.denied_domains = Some(denied_domains); - } - if let Some(allow_unix_sockets) = network.allow_unix_sockets { + if let Some(unix_sockets) = network.unix_sockets.as_ref() { + let allow_unix_sockets = unix_sockets.allow_unix_sockets(); constraints.allow_unix_sockets = Some(allow_unix_sockets); } if let Some(allow_local_binding) = network.allow_local_binding { @@ -220,24 +228,28 @@ fn apply_exec_policy_network_rules( let (allowed_domains, denied_domains) = exec_policy.compiled_network_domains(); for host in allowed_domains { upsert_network_domain( - &mut config.network.allowed_domains, - &mut config.network.denied_domains, + config, host, + codex_network_proxy::NetworkDomainPermission::Allow, ); } for host in denied_domains { upsert_network_domain( - &mut config.network.denied_domains, - &mut config.network.allowed_domains, + config, host, + codex_network_proxy::NetworkDomainPermission::Deny, ); } } -fn upsert_network_domain(target: &mut Vec, opposite: &mut Vec, host: String) { - opposite.retain(|entry| normalize_host(entry) != host); - target.retain(|entry| normalize_host(entry) != host); - target.push(host); +fn upsert_network_domain( + config: &mut NetworkProxyConfig, + host: String, + permission: codex_network_proxy::NetworkDomainPermission, +) { + config + .network + .upsert_domain_permission(host, permission, normalize_host); } fn is_user_controlled_layer(layer: &ConfigLayerSource) -> bool { diff --git a/codex-rs/core/src/network_proxy_loader_tests.rs b/codex-rs/core/src/network_proxy_loader_tests.rs index 018061463b..395b761dd5 100644 --- a/codex-rs/core/src/network_proxy_loader_tests.rs +++ b/codex-rs/core/src/network_proxy_loader_tests.rs @@ -6,13 +6,16 @@ use codex_execpolicy::Policy; use pretty_assertions::assert_eq; #[test] -fn higher_precedence_profile_network_beats_lower_profile_network() { +fn higher_precedence_profile_network_overlays_domain_entries() { let lower_network: toml::Value = toml::from_str( r#" default_permissions = "workspace" [permissions.workspace.network] -allowed_domains = ["lower.example.com"] + +[permissions.workspace.network.domains] +"lower.example.com" = "allow" +"blocked.example.com" = "deny" "#, ) .expect("lower layer should parse"); @@ -21,7 +24,9 @@ allowed_domains = ["lower.example.com"] default_permissions = "workspace" [permissions.workspace.network] -allowed_domains = ["higher.example.com"] + +[permissions.workspace.network.domains] +"higher.example.com" = "allow" "#, ) .expect("higher layer should parse"); @@ -38,14 +43,76 @@ allowed_domains = ["higher.example.com"] ) .expect("higher layer should apply"); - assert_eq!(config.network.allowed_domains, vec!["higher.example.com"]); + assert_eq!( + config.network.allowed_domains(), + Some(vec![ + "lower.example.com".to_string(), + "higher.example.com".to_string() + ]) + ); + assert_eq!( + config.network.denied_domains(), + Some(vec!["blocked.example.com".to_string()]) + ); +} + +#[test] +fn higher_precedence_profile_network_overrides_matching_domain_entries() { + let lower_network: toml::Value = toml::from_str( + r#" +default_permissions = "workspace" + +[permissions.workspace.network] + +[permissions.workspace.network.domains] +"shared.example.com" = "deny" +"other.example.com" = "allow" +"#, + ) + .expect("lower layer should parse"); + let higher_network: toml::Value = toml::from_str( + r#" +default_permissions = "workspace" + +[permissions.workspace.network] + +[permissions.workspace.network.domains] +"shared.example.com" = "allow" +"#, + ) + .expect("higher layer should parse"); + + let mut config = NetworkProxyConfig::default(); + apply_network_tables( + &mut config, + network_tables_from_toml(&lower_network).expect("lower layer should deserialize"), + ) + .expect("lower layer should apply"); + apply_network_tables( + &mut config, + network_tables_from_toml(&higher_network).expect("higher layer should deserialize"), + ) + .expect("higher layer should apply"); + + assert_eq!( + config.network.allowed_domains(), + Some(vec![ + "other.example.com".to_string(), + "shared.example.com".to_string() + ]) + ); + assert_eq!(config.network.denied_domains(), None); } #[test] fn execpolicy_network_rules_overlay_network_lists() { let mut config = NetworkProxyConfig::default(); - config.network.allowed_domains = vec!["config.example.com".to_string()]; - config.network.denied_domains = vec!["blocked.example.com".to_string()]; + config + .network + .set_allowed_domains(vec!["config.example.com".to_string()]); + config + .network + .set_denied_domains(vec!["blocked.example.com".to_string()]); let mut exec_policy = Policy::empty(); exec_policy @@ -68,15 +135,15 @@ fn execpolicy_network_rules_overlay_network_lists() { apply_exec_policy_network_rules(&mut config, &exec_policy); assert_eq!( - config.network.allowed_domains, - vec![ + config.network.allowed_domains(), + Some(vec![ "config.example.com".to_string(), "blocked.example.com".to_string() - ] + ]) ); assert_eq!( - config.network.denied_domains, - vec!["api.example.com".to_string()] + config.network.denied_domains(), + Some(vec!["api.example.com".to_string()]) ); } @@ -102,3 +169,82 @@ dangerously_allow_all_unix_sockets = true assert_eq!(constraints.dangerously_allow_all_unix_sockets, Some(true)); } + +#[test] +fn apply_network_constraints_skips_empty_domain_sides() { + let config: toml::Value = toml::from_str( + r#" +default_permissions = "workspace" + +[permissions.workspace.network] + +[permissions.workspace.network.domains] +"managed.example.com" = "allow" +"#, + ) + .expect("permissions profile should parse"); + let network = selected_network_from_tables( + network_tables_from_toml(&config).expect("permissions profile should deserialize"), + ) + .expect("permissions profile should select a network table") + .expect("network table should be present"); + + let mut constraints = NetworkProxyConstraints::default(); + apply_network_constraints(network, &mut constraints); + + assert_eq!( + constraints.allowed_domains, + Some(vec!["managed.example.com".to_string()]) + ); + assert_eq!(constraints.denied_domains, None); +} + +#[test] +fn apply_network_constraints_overlay_domain_entries() { + let lower_network: toml::Value = toml::from_str( + r#" +default_permissions = "workspace" + +[permissions.workspace.network] + +[permissions.workspace.network.domains] +"blocked.example.com" = "deny" +"#, + ) + .expect("lower layer should parse"); + let higher_network: toml::Value = toml::from_str( + r#" +default_permissions = "workspace" + +[permissions.workspace.network] + +[permissions.workspace.network.domains] +"api.example.com" = "allow" +"#, + ) + .expect("higher layer should parse"); + + let lower_network = selected_network_from_tables( + network_tables_from_toml(&lower_network).expect("lower layer should deserialize"), + ) + .expect("lower layer should select a network table") + .expect("lower network table should be present"); + let higher_network = selected_network_from_tables( + network_tables_from_toml(&higher_network).expect("higher layer should deserialize"), + ) + .expect("higher layer should select a network table") + .expect("higher network table should be present"); + + let mut constraints = NetworkProxyConstraints::default(); + apply_network_constraints(lower_network, &mut constraints); + apply_network_constraints(higher_network, &mut constraints); + + assert_eq!( + constraints.allowed_domains, + Some(vec!["api.example.com".to_string()]) + ); + assert_eq!( + constraints.denied_domains, + Some(vec!["blocked.example.com".to_string()]) + ); +} diff --git a/codex-rs/network-proxy/README.md b/codex-rs/network-proxy/README.md index 0347e6ddf2..d9eecf4f6a 100644 --- a/codex-rs/network-proxy/README.md +++ b/codex-rs/network-proxy/README.md @@ -37,24 +37,29 @@ mode = "full" # default when unset; use "limited" for read-only mode mitm = false # CA cert/key are managed internally under $CODEX_HOME/proxy/ (ca.pem + ca.key). -# Hosts must match the allowlist (unless denied). -# Use exact hosts or scoped wildcards like `*.openai.com` or `**.openai.com`. -# The global `*` wildcard is allowed in `allowed_domains` to delegate public-host filtering to -# `denied_domains`. -# If `allowed_domains` is empty, the proxy blocks requests until an allowlist is configured. -allowed_domains = ["*.openai.com", "localhost", "127.0.0.1", "::1"] -denied_domains = ["evil.example"] - # If false, local/private networking is rejected. Explicit allowlisting of local IP literals # (or `localhost`) is required to permit them. # Hostnames that resolve to local/private IPs are still blocked even if allowlisted. allow_local_binding = false -# macOS-only: allows proxying to a unix socket when request includes `x-unix-socket: /path`. -allow_unix_sockets = ["/tmp/example.sock"] # DANGEROUS (macOS-only): bypasses unix socket allowlisting and permits any # absolute socket path from `x-unix-socket`. dangerously_allow_all_unix_sockets = false + +# Hosts must match the allowlist (unless denied). +# Use exact hosts or scoped wildcards like `*.openai.com` or `**.openai.com`. +# The global `*` wildcard is rejected. +# If no domain entries are marked `allow`, the proxy blocks requests until an allowlist is configured. +[permissions.workspace.network.domains] +"*.openai.com" = "allow" +"localhost" = "allow" +"127.0.0.1" = "allow" +"::1" = "allow" +"evil.example" = "deny" + +# macOS-only: allows proxying to a unix socket when request includes `x-unix-socket: /path`. +[permissions.workspace.network.unix_sockets] +"/tmp/example.sock" = "allow" ``` ### 2) Run the proxy @@ -124,7 +129,7 @@ let handle = proxy.run().await?; handle.shutdown().await?; ``` -When unix socket proxying is enabled (`allow_unix_sockets` or +When unix socket proxying is enabled (`unix_sockets` or `dangerously_allow_all_unix_sockets`), proxy bind overrides are still clamped to loopback to avoid turning the proxy into a remote bridge to local daemons. @@ -189,9 +194,9 @@ Audit events intentionally avoid logging full URL/path/query data. This section documents the protections implemented by `codex-network-proxy`, and the boundaries of what it can reasonably guarantee. -- Allowlist-first policy: if `allowed_domains` is empty, requests are blocked until an allowlist is configured. -- Domain patterns: exact hosts plus scoped wildcards (`*.example.com`, `**.example.com`) are supported. A global `*` wildcard is allowed in `allowed_domains` to permit all public hosts by default, while `denied_domains` remains field-specific and still rejects global `*`. -- Deny wins: entries in `denied_domains` always override the allowlist. +- Allowlist-first policy: if `domains` has no `allow` entries, requests are blocked until an allowlist is configured. +- Domain patterns: exact hosts are supported, `*.example.com` matches subdomains only, and `**.example.com` matches the apex plus subdomains; the global `*` wildcard is only accepted when explicitly enabled for allowlist compilation and is otherwise rejected. +- Deny wins: `domains` entries marked `deny` always override the allowlist. - Local/private network protection: when `allow_local_binding = false`, the proxy blocks loopback and common private/link-local ranges. Explicit allowlisting of local IP literals (or `localhost`) is required to permit them; hostnames that resolve to local/private IPs are still blocked even if diff --git a/codex-rs/network-proxy/src/config.rs b/codex-rs/network-proxy/src/config.rs index f3f0e06c34..b081abb15f 100644 --- a/codex-rs/network-proxy/src/config.rs +++ b/codex-rs/network-proxy/src/config.rs @@ -3,7 +3,10 @@ use anyhow::Result; use anyhow::bail; use codex_utils_absolute_path::AbsolutePathBuf; use serde::Deserialize; +use serde::Deserializer; use serde::Serialize; +use serde::Serializer; +use std::collections::BTreeMap; use std::net::IpAddr; use std::net::SocketAddr; use std::path::Path; @@ -16,6 +19,101 @@ pub struct NetworkProxyConfig { pub network: NetworkProxySettings, } +/// Variant order encodes effective precedence for duplicate patterns: +/// `None < Allow < Deny`, so deny wins over allow when entries conflict. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +#[serde(rename_all = "lowercase")] +pub enum NetworkDomainPermission { + None, + Allow, + Deny, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NetworkDomainPermissionEntry { + pub pattern: String, + pub permission: NetworkDomainPermission, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct NetworkDomainPermissions { + pub entries: Vec, +} + +impl Serialize for NetworkDomainPermissions { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + self.effective_entries() + .into_iter() + .map(|entry| (entry.pattern, entry.permission)) + .collect::>() + .serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for NetworkDomainPermissions { + fn deserialize(deserializer: D) -> std::result::Result + where + D: Deserializer<'de>, + { + let entries = BTreeMap::::deserialize(deserializer)? + .into_iter() + .map(|(pattern, permission)| NetworkDomainPermissionEntry { + pattern, + permission, + }) + .collect(); + Ok(Self { entries }) + } +} + +impl NetworkDomainPermissions { + fn effective_entries(&self) -> Vec { + let mut order = Vec::new(); + let mut effective_permissions = BTreeMap::new(); + + for entry in &self.entries { + if !effective_permissions.contains_key(&entry.pattern) { + order.push(entry.pattern.clone()); + } + + let permission = effective_permissions + .entry(entry.pattern.clone()) + .or_insert(entry.permission); + if entry.permission > *permission { + *permission = entry.permission; + } + } + + order + .into_iter() + .filter_map(|pattern| { + effective_permissions.remove(&pattern).map(|permission| { + NetworkDomainPermissionEntry { + pattern, + permission, + } + }) + }) + .collect() + } +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum NetworkUnixSocketPermission { + Allow, + None, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] +pub struct NetworkUnixSocketPermissions { + #[serde(flatten)] + pub entries: BTreeMap, +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(default)] pub struct NetworkProxySettings { @@ -35,11 +133,9 @@ pub struct NetworkProxySettings { #[serde(default)] pub mode: NetworkMode, #[serde(default)] - pub allowed_domains: Vec, + pub domains: Option, #[serde(default)] - pub denied_domains: Vec, - #[serde(default)] - pub allow_unix_sockets: Vec, + pub unix_sockets: Option, pub allow_local_binding: bool, #[serde(default)] pub mitm: bool, @@ -57,15 +153,119 @@ impl Default for NetworkProxySettings { dangerously_allow_non_loopback_proxy: false, dangerously_allow_all_unix_sockets: false, mode: NetworkMode::default(), - allowed_domains: Vec::new(), - denied_domains: Vec::new(), - allow_unix_sockets: Vec::new(), + domains: None, + unix_sockets: None, allow_local_binding: false, mitm: false, } } } +impl NetworkProxySettings { + pub fn allowed_domains(&self) -> Option> { + self.domain_entries(NetworkDomainPermission::Allow) + } + + pub fn denied_domains(&self) -> Option> { + self.domain_entries(NetworkDomainPermission::Deny) + } + + fn domain_entries(&self, permission: NetworkDomainPermission) -> Option> { + self.domains + .as_ref() + .map(|domains| { + domains + .effective_entries() + .iter() + .filter(|entry| entry.permission == permission) + .map(|entry| entry.pattern.clone()) + .collect() + }) + .filter(|entries: &Vec| !entries.is_empty()) + } + + pub fn allow_unix_sockets(&self) -> Vec { + self.unix_sockets + .as_ref() + .map(|unix_sockets| { + unix_sockets + .entries + .iter() + .filter(|(_, permission)| { + matches!(permission, NetworkUnixSocketPermission::Allow) + }) + .map(|(path, _)| path.clone()) + .collect() + }) + .unwrap_or_default() + } + + pub fn set_allowed_domains(&mut self, allowed_domains: Vec) { + self.set_domain_entries(allowed_domains, NetworkDomainPermission::Allow); + } + + pub fn set_denied_domains(&mut self, denied_domains: Vec) { + self.set_domain_entries(denied_domains, NetworkDomainPermission::Deny); + } + + pub fn upsert_domain_permission( + &mut self, + host: String, + permission: NetworkDomainPermission, + normalize: impl Fn(&str) -> String, + ) { + let mut domains = self.domains.take().unwrap_or_default(); + let normalized_host = normalize(&host); + domains + .entries + .retain(|entry| normalize(&entry.pattern) != normalized_host); + domains.entries.push(NetworkDomainPermissionEntry { + pattern: host, + permission, + }); + self.domains = (!domains.entries.is_empty()).then_some(domains); + } + + pub fn set_allow_unix_sockets(&mut self, allow_unix_sockets: Vec) { + self.set_unix_socket_entries(allow_unix_sockets, NetworkUnixSocketPermission::Allow); + } + + fn set_domain_entries(&mut self, entries: Vec, permission: NetworkDomainPermission) { + let mut domains = self.domains.take().unwrap_or_default(); + domains + .entries + .retain(|entry| entry.permission != permission); + for entry in entries { + if !domains + .entries + .iter() + .any(|existing| existing.pattern == entry && existing.permission == permission) + { + domains.entries.push(NetworkDomainPermissionEntry { + pattern: entry, + permission, + }); + } + } + self.domains = (!domains.entries.is_empty()).then_some(domains); + } + + fn set_unix_socket_entries( + &mut self, + entries: Vec, + permission: NetworkUnixSocketPermission, + ) { + let mut unix_sockets = self.unix_sockets.take().unwrap_or_default(); + unix_sockets + .entries + .retain(|_, existing| *existing != permission); + for entry in entries { + unix_sockets.entries.insert(entry, permission); + } + self.unix_sockets = (!unix_sockets.entries.is_empty()).then_some(unix_sockets); + } +} + #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)] #[serde(rename_all = "lowercase")] pub enum NetworkMode { @@ -136,7 +336,7 @@ pub(crate) fn clamp_bind_addrs( "SOCKS5 proxy", "dangerously_allow_non_loopback_proxy", ); - if cfg.allow_unix_sockets.is_empty() && !cfg.dangerously_allow_all_unix_sockets { + if cfg.allow_unix_sockets().is_empty() && !cfg.dangerously_allow_all_unix_sockets { return (http_addr, socks_addr); } @@ -198,7 +398,7 @@ impl ValidatedUnixSocketPath { } pub(crate) fn validate_unix_socket_allowlist_paths(cfg: &NetworkProxyConfig) -> Result<()> { - for (index, socket_path) in cfg.network.allow_unix_sockets.iter().enumerate() { + for (index, socket_path) in cfg.network.allow_unix_sockets().iter().enumerate() { ValidatedUnixSocketPath::parse(socket_path) .with_context(|| format!("invalid network.allow_unix_sockets[{index}]"))?; } @@ -357,6 +557,19 @@ mod tests { use pretty_assertions::assert_eq; + fn settings_with_unix_sockets(unix_sockets: &[&str]) -> NetworkProxySettings { + let mut settings = NetworkProxySettings::default(); + if !unix_sockets.is_empty() { + settings.set_allow_unix_sockets( + unix_sockets + .iter() + .map(|path| (*path).to_string()) + .collect(), + ); + } + settings + } + #[test] fn network_proxy_settings_default_matches_local_use_baseline() { assert_eq!( @@ -371,9 +584,8 @@ mod tests { dangerously_allow_non_loopback_proxy: false, dangerously_allow_all_unix_sockets: false, mode: NetworkMode::Full, - allowed_domains: Vec::new(), - denied_domains: Vec::new(), - allow_unix_sockets: Vec::new(), + domains: None, + unix_sockets: None, allow_local_binding: false, mitm: false, } @@ -398,6 +610,53 @@ mod tests { assert_eq!(config.network, expected); } + #[test] + fn set_allowed_domains_preserves_existing_deny_for_same_pattern() { + let mut settings = NetworkProxySettings::default(); + settings.set_denied_domains(vec!["example.com".to_string()]); + + settings.set_allowed_domains(vec!["example.com".to_string()]); + + assert_eq!(settings.allowed_domains(), None); + assert_eq!( + settings.denied_domains(), + Some(vec!["example.com".to_string()]) + ); + } + + #[test] + fn network_domain_permissions_serialize_to_effective_map_shape() { + let mut settings = NetworkProxySettings::default(); + settings.set_denied_domains(vec!["example.com".to_string()]); + settings.set_allowed_domains(vec!["example.com".to_string()]); + let config = NetworkProxyConfig { network: settings }; + + let value = serde_json::to_value(&config).unwrap(); + + assert_eq!( + value, + serde_json::json!({ + "network": { + "enabled": false, + "proxy_url": "http://127.0.0.1:3128", + "enable_socks5": true, + "socks_url": "http://127.0.0.1:8081", + "enable_socks5_udp": true, + "allow_upstream_proxy": true, + "dangerously_allow_non_loopback_proxy": false, + "dangerously_allow_all_unix_sockets": false, + "mode": "full", + "domains": { + "example.com": "deny", + }, + "unix_sockets": null, + "allow_local_binding": false, + "mitm": false, + } + }) + ); + } + #[test] fn parse_host_port_defaults_for_empty_string() { assert!(parse_host_port("", 1234).is_err()); @@ -536,10 +795,10 @@ mod tests { #[test] fn clamp_bind_addrs_forces_loopback_when_unix_sockets_enabled() { - let cfg = NetworkProxySettings { - dangerously_allow_non_loopback_proxy: true, - allow_unix_sockets: vec!["/tmp/docker.sock".to_string()], - ..Default::default() + let cfg = { + let mut settings = settings_with_unix_sockets(&["/tmp/docker.sock"]); + settings.dangerously_allow_non_loopback_proxy = true; + settings }; let http_addr = "0.0.0.0:3128".parse::().unwrap(); let socks_addr = "0.0.0.0:8081".parse::().unwrap(); @@ -569,10 +828,7 @@ mod tests { #[test] fn resolve_runtime_rejects_relative_allow_unix_sockets_entries() { let cfg = NetworkProxyConfig { - network: NetworkProxySettings { - allow_unix_sockets: vec!["relative.sock".to_string()], - ..NetworkProxySettings::default() - }, + network: settings_with_unix_sockets(&["relative.sock"]), }; let err = match resolve_runtime(&cfg) { @@ -591,10 +847,7 @@ mod tests { #[test] fn resolve_runtime_accepts_unix_style_absolute_allow_unix_sockets_entries() { let cfg = NetworkProxyConfig { - network: NetworkProxySettings { - allow_unix_sockets: vec!["/private/tmp/example.sock".to_string()], - ..NetworkProxySettings::default() - }, + network: settings_with_unix_sockets(&["/private/tmp/example.sock"]), }; assert!( diff --git a/codex-rs/network-proxy/src/http_proxy.rs b/codex-rs/network-proxy/src/http_proxy.rs index a3da7e2ddd..1be610feba 100644 --- a/codex-rs/network-proxy/src/http_proxy.rs +++ b/codex-rs/network-proxy/src/http_proxy.rs @@ -1002,9 +1002,10 @@ mod tests { #[tokio::test] async fn http_connect_accept_blocks_in_limited_mode() { - let policy = NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..Default::default() + let policy = { + let mut policy = NetworkProxySettings::default(); + policy.set_allowed_domains(vec!["example.com".to_string()]); + policy }; let state = Arc::new(network_proxy_state_for_policy(policy)); state.set_network_mode(NetworkMode::Limited).await.unwrap(); @@ -1027,9 +1028,10 @@ mod tests { #[tokio::test] async fn http_connect_accept_allows_allowlisted_host_in_full_mode() { - let policy = NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..Default::default() + let policy = { + let mut policy = NetworkProxySettings::default(); + policy.set_allowed_domains(vec!["example.com".to_string()]); + policy }; let state = Arc::new(network_proxy_state_for_policy(policy)); @@ -1062,10 +1064,11 @@ mod tests { let _ = timeout(Duration::from_secs(1), stream.read(&mut buf)).await; }); - let state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["127.0.0.1".to_string()], - allow_local_binding: true, - ..NetworkProxySettings::default() + let state = Arc::new(network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["127.0.0.1".to_string()]); + network.allow_local_binding = true; + network })); let listener = StdTcpListener::bind((Ipv4Addr::LOCALHOST, 0)).expect("proxy listener should bind"); @@ -1161,9 +1164,10 @@ mod tests { #[cfg(target_os = "macos")] #[tokio::test(flavor = "current_thread")] async fn http_plain_proxy_attempts_allowed_unix_socket_proxy() { - let state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { - allow_unix_sockets: vec!["/tmp/test.sock".to_string()], - ..NetworkProxySettings::default() + let state = Arc::new(network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allow_unix_sockets(vec!["/tmp/test.sock".to_string()]); + network })); let mut req = Request::builder() @@ -1180,10 +1184,11 @@ mod tests { #[tokio::test] async fn http_connect_accept_denies_denylisted_host() { - let policy = NetworkProxySettings { - allowed_domains: vec!["**.openai.com".to_string()], - denied_domains: vec!["api.openai.com".to_string()], - ..Default::default() + let policy = { + let mut policy = NetworkProxySettings::default(); + policy.set_allowed_domains(vec!["**.openai.com".to_string()]); + policy.set_denied_domains(vec!["api.openai.com".to_string()]); + policy }; let state = Arc::new(network_proxy_state_for_policy(policy)); diff --git a/codex-rs/network-proxy/src/lib.rs b/codex-rs/network-proxy/src/lib.rs index 1093a14aa4..4fb313805c 100644 --- a/codex-rs/network-proxy/src/lib.rs +++ b/codex-rs/network-proxy/src/lib.rs @@ -14,8 +14,13 @@ mod socks5; mod state; mod upstream; +pub use config::NetworkDomainPermission; +pub use config::NetworkDomainPermissionEntry; +pub use config::NetworkDomainPermissions; pub use config::NetworkMode; pub use config::NetworkProxyConfig; +pub use config::NetworkUnixSocketPermission; +pub use config::NetworkUnixSocketPermissions; pub use config::host_and_port_from_network_addr; pub use network_policy::NetworkDecision; pub use network_policy::NetworkDecisionSource; diff --git a/codex-rs/network-proxy/src/mitm_tests.rs b/codex-rs/network-proxy/src/mitm_tests.rs index d2f7984a70..28eb5c5f79 100644 --- a/codex-rs/network-proxy/src/mitm_tests.rs +++ b/codex-rs/network-proxy/src/mitm_tests.rs @@ -26,9 +26,10 @@ fn policy_ctx( #[tokio::test] async fn mitm_policy_blocks_disallowed_method_and_records_telemetry() { - let app_state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() + let app_state = Arc::new(network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["example.com".to_string()]); + network })); let ctx = policy_ctx(app_state.clone(), NetworkMode::Limited, "example.com", 443); let req = Request::builder() @@ -59,9 +60,10 @@ async fn mitm_policy_blocks_disallowed_method_and_records_telemetry() { #[tokio::test] async fn mitm_policy_rejects_host_mismatch() { - let app_state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() + let app_state = Arc::new(network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["example.com".to_string()]); + network })); let ctx = policy_ctx(app_state.clone(), NetworkMode::Full, "example.com", 443); let req = Request::builder() @@ -82,10 +84,11 @@ async fn mitm_policy_rejects_host_mismatch() { #[tokio::test] async fn mitm_policy_rechecks_local_private_target_after_connect() { - let app_state = Arc::new(network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() + let app_state = Arc::new(network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["example.com".to_string()]); + network.allow_local_binding = false; + network })); let ctx = policy_ctx(app_state.clone(), NetworkMode::Full, "10.0.0.1", 443); let req = Request::builder() diff --git a/codex-rs/network-proxy/src/network_policy.rs b/codex-rs/network-proxy/src/network_policy.rs index e759e75c0d..f9670c0550 100644 --- a/codex-rs/network-proxy/src/network_policy.rs +++ b/codex-rs/network-proxy/src/network_policy.rs @@ -676,10 +676,11 @@ mod tests { #[tokio::test(flavor = "current_thread")] async fn evaluate_host_policy_emits_domain_event_for_baseline_deny() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - denied_domains: vec!["blocked.com".to_string()], - ..NetworkProxySettings::default() + let state = network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["example.com".to_string()]); + network.set_denied_domains(vec!["blocked.com".to_string()]); + network }); let request = NetworkPolicyRequest::new(NetworkPolicyRequestArgs { protocol: NetworkProtocol::Http, @@ -850,10 +851,11 @@ mod tests { #[tokio::test(flavor = "current_thread")] async fn evaluate_host_policy_still_denies_not_allowed_local_without_decider_override() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() + let state = network_proxy_state_for_policy({ + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["example.com".to_string()]); + network.allow_local_binding = false; + network }); let request = NetworkPolicyRequest::new(NetworkPolicyRequestArgs { protocol: NetworkProtocol::Http, diff --git a/codex-rs/network-proxy/src/proxy.rs b/codex-rs/network-proxy/src/proxy.rs index 3eecef3ee7..3dc6f6bad8 100644 --- a/codex-rs/network-proxy/src/proxy.rs +++ b/codex-rs/network-proxy/src/proxy.rs @@ -220,7 +220,7 @@ impl NetworkProxyBuilder { socks_addr, socks_enabled: current_cfg.network.enable_socks5, allow_local_binding: current_cfg.network.allow_local_binding, - allow_unix_sockets: current_cfg.network.allow_unix_sockets.clone(), + allow_unix_sockets: current_cfg.network.allow_unix_sockets(), dangerously_allow_all_unix_sockets: current_cfg .network .dangerously_allow_all_unix_sockets, diff --git a/codex-rs/network-proxy/src/runtime.rs b/codex-rs/network-proxy/src/runtime.rs index ec43a54792..c03c3da4a5 100644 --- a/codex-rs/network-proxy/src/runtime.rs +++ b/codex-rs/network-proxy/src/runtime.rs @@ -1,3 +1,4 @@ +use crate::config::NetworkDomainPermission; use crate::config::NetworkMode; use crate::config::NetworkProxyConfig; use crate::config::ValidatedUnixSocketPath; @@ -295,8 +296,8 @@ impl NetworkProxyState { self.reload_if_needed().await?; let guard = self.state.read().await; Ok(( - guard.config.network.allowed_domains.clone(), - guard.config.network.denied_domains.clone(), + guard.config.network.allowed_domains().unwrap_or_default(), + guard.config.network.denied_domains().unwrap_or_default(), )) } @@ -340,16 +341,18 @@ impl NetworkProxyState { Ok(host) => host, Err(_) => return Ok(HostBlockDecision::Blocked(HostBlockReason::NotAllowed)), }; - let (deny_set, allow_set, allow_local_binding, allowed_domains_empty, allowed_domains) = { + let (deny_set, allow_set, allow_local_binding, allowed_domains) = { let guard = self.state.read().await; + let allowed_domains = guard.config.network.allowed_domains(); ( guard.deny_set.clone(), guard.allow_set.clone(), guard.config.network.allow_local_binding, - guard.config.network.allowed_domains.is_empty(), - guard.config.network.allowed_domains.clone(), + allowed_domains, ) }; + let allowed_domains_empty = allowed_domains.is_none(); + let allowed_domains = allowed_domains.unwrap_or_default(); let host_str = host.as_str(); @@ -481,7 +484,7 @@ impl NetworkProxyState { Err(_) => return Ok(false), }; let requested_canonical = std::fs::canonicalize(requested_abs.as_path()).ok(); - for allowed in &guard.config.network.allow_unix_sockets { + for allowed in &guard.config.network.allow_unix_sockets() { let allowed_path = match ValidatedUnixSocketPath::parse(allowed) { Ok(ValidatedUnixSocketPath::Native(path)) => path, Ok(ValidatedUnixSocketPath::UnixStyleAbsolute(_)) => continue, @@ -585,7 +588,8 @@ impl NetworkProxyState { }; let mut candidate = previous_cfg.clone(); - let (target_entries, opposite_entries) = candidate.split_domain_lists_mut(target); + let target_entries = target.entries(&candidate.network); + let opposite_entries = target.opposite_entries(&candidate.network); let target_contains = target_entries .iter() .any(|entry| normalize_host(entry) == normalized_host); @@ -596,9 +600,11 @@ impl NetworkProxyState { return Ok(()); } - target_entries.retain(|entry| normalize_host(entry) != normalized_host); - target_entries.push(normalized_host.clone()); - opposite_entries.retain(|entry| normalize_host(entry) != normalized_host); + candidate.network.upsert_domain_permission( + normalized_host.clone(), + target.permission(), + normalize_host, + ); validate_policy_against_constraints(&candidate, &constraints) .map_err(NetworkProxyConstraintError::into_anyhow) @@ -669,22 +675,25 @@ impl DomainListKind { Self::Deny => "network.denied_domains", } } -} -impl NetworkProxyConfig { - fn split_domain_lists_mut( - &mut self, - target: DomainListKind, - ) -> (&mut Vec, &mut Vec) { - match target { - DomainListKind::Allow => ( - &mut self.network.allowed_domains, - &mut self.network.denied_domains, - ), - DomainListKind::Deny => ( - &mut self.network.denied_domains, - &mut self.network.allowed_domains, - ), + fn permission(self) -> NetworkDomainPermission { + match self { + Self::Allow => NetworkDomainPermission::Allow, + Self::Deny => NetworkDomainPermission::Deny, + } + } + + fn entries(self, network: &crate::config::NetworkProxySettings) -> Vec { + match self { + Self::Allow => network.allowed_domains().unwrap_or_default(), + Self::Deny => network.denied_domains().unwrap_or_default(), + } + } + + fn opposite_entries(self, network: &crate::config::NetworkProxySettings) -> Vec { + match self { + Self::Allow => network.denied_domains().unwrap_or_default(), + Self::Deny => network.allowed_domains().unwrap_or_default(), } } } @@ -726,16 +735,16 @@ async fn host_resolves_to_non_public_ip(host: &str, port: u16) -> bool { } fn log_policy_changes(previous: &NetworkProxyConfig, next: &NetworkProxyConfig) { + let previous_allowed_domains = previous.network.allowed_domains().unwrap_or_default(); + let next_allowed_domains = next.network.allowed_domains().unwrap_or_default(); log_domain_list_changes( "allowlist", - &previous.network.allowed_domains, - &next.network.allowed_domains, - ); - log_domain_list_changes( - "denylist", - &previous.network.denied_domains, - &next.network.denied_domains, + &previous_allowed_domains, + &next_allowed_domains, ); + let previous_denied_domains = previous.network.denied_domains().unwrap_or_default(); + let next_denied_domains = next.network.denied_domains().unwrap_or_default(); + log_domain_list_changes("denylist", &previous_denied_domains, &next_denied_domains); } fn log_domain_list_changes(list_name: &str, previous: &[String], next: &[String]) { @@ -836,13 +845,37 @@ mod tests { use crate::state::validate_policy_against_constraints; use pretty_assertions::assert_eq; + fn strings(entries: &[&str]) -> Vec { + entries.iter().map(|entry| (*entry).to_string()).collect() + } + + fn network_settings(allowed_domains: &[&str], denied_domains: &[&str]) -> NetworkProxySettings { + let mut network = NetworkProxySettings::default(); + if !allowed_domains.is_empty() { + network.set_allowed_domains(strings(allowed_domains)); + } + if !denied_domains.is_empty() { + network.set_denied_domains(strings(denied_domains)); + } + network + } + + fn network_settings_with_unix_sockets( + allowed_domains: &[&str], + denied_domains: &[&str], + unix_sockets: &[String], + ) -> NetworkProxySettings { + let mut network = network_settings(allowed_domains, denied_domains); + if !unix_sockets.is_empty() { + network.set_allow_unix_sockets(unix_sockets.to_vec()); + } + network + } + #[tokio::test] async fn host_blocked_denied_wins_over_allowed() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - denied_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() - }); + let state = + network_proxy_state_for_policy(network_settings(&["example.com"], &["example.com"])); assert_eq!( state.host_blocked("example.com", 80).await.unwrap(), @@ -852,10 +885,7 @@ mod tests { #[tokio::test] async fn host_blocked_requires_allowlist_match() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["example.com"], &[])); assert_eq!( state.host_blocked("example.com", 80).await.unwrap(), @@ -871,10 +901,7 @@ mod tests { #[tokio::test] async fn add_allowed_domain_removes_matching_deny_entry() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - denied_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&[], &["example.com"])); state.add_allowed_domain("ExAmPlE.CoM").await.unwrap(); @@ -889,10 +916,7 @@ mod tests { #[tokio::test] async fn add_denied_domain_removes_matching_allow_entry() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["example.com"], &[])); state.add_denied_domain("EXAMPLE.COM").await.unwrap(); @@ -907,10 +931,7 @@ mod tests { #[tokio::test] async fn add_denied_domain_forces_block_with_global_wildcard_allowlist() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["*".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["*"], &[])); assert_eq!( // Use a public IP literal to avoid relying on ambient DNS behavior. @@ -932,10 +953,10 @@ mod tests { #[tokio::test] async fn add_allowed_domain_succeeds_when_managed_baseline_allows_expansion() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["managed.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["managed.example.com"], &[]); + network.enabled = true; + network }, }; let constraints = NetworkProxyConstraints { @@ -964,10 +985,10 @@ mod tests { #[tokio::test] async fn add_allowed_domain_rejects_expansion_when_managed_baseline_is_fixed() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["managed.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["managed.example.com"], &[]); + network.enabled = true; + network }, }; let constraints = NetworkProxyConstraints { @@ -994,10 +1015,10 @@ mod tests { #[tokio::test] async fn add_denied_domain_rejects_expansion_when_managed_baseline_is_fixed() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - denied_domains: vec!["managed.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&[], &["managed.example.com"]); + network.enabled = true; + network }, }; let constraints = NetworkProxyConstraints { @@ -1109,10 +1130,7 @@ mod tests { #[tokio::test] async fn host_blocked_subdomain_wildcards_exclude_apex() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["*.openai.com".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["*.openai.com"], &[])); assert_eq!( state.host_blocked("api.openai.com", 80).await.unwrap(), @@ -1126,11 +1144,7 @@ mod tests { #[tokio::test] async fn host_blocked_global_wildcard_allowlist_allows_public_hosts_except_denylist() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["*".to_string()], - denied_domains: vec!["evil.example".to_string()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["*"], &["evil.example"])); assert_eq!( state.host_blocked("example.com", 80).await.unwrap(), @@ -1148,11 +1162,7 @@ mod tests { #[tokio::test] async fn host_blocked_rejects_loopback_when_local_binding_disabled() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["example.com"], &[])); assert_eq!( state.host_blocked("127.0.0.1", 80).await.unwrap(), @@ -1166,11 +1176,7 @@ mod tests { #[tokio::test] async fn host_blocked_allows_loopback_when_explicitly_allowlisted_and_local_binding_disabled() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["localhost".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["localhost"], &[])); assert_eq!( state.host_blocked("localhost", 80).await.unwrap(), @@ -1180,11 +1186,7 @@ mod tests { #[tokio::test] async fn host_blocked_allows_private_ip_literal_when_explicitly_allowlisted() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["10.0.0.1".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["10.0.0.1"], &[])); assert_eq!( state.host_blocked("10.0.0.1", 80).await.unwrap(), @@ -1194,11 +1196,7 @@ mod tests { #[tokio::test] async fn host_blocked_rejects_scoped_ipv6_literal_when_not_allowlisted() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["example.com"], &[])); assert_eq!( state.host_blocked("fe80::1%lo0", 80).await.unwrap(), @@ -1208,11 +1206,7 @@ mod tests { #[tokio::test] async fn host_blocked_allows_scoped_ipv6_literal_when_explicitly_allowlisted() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["fe80::1%lo0".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["fe80::1%lo0"], &[])); assert_eq!( state.host_blocked("fe80::1%lo0", 80).await.unwrap(), @@ -1222,11 +1216,7 @@ mod tests { #[tokio::test] async fn host_blocked_rejects_private_ip_literals_when_local_binding_disabled() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings(&["example.com"], &[])); assert_eq!( state.host_blocked("10.0.0.1", 80).await.unwrap(), @@ -1236,11 +1226,7 @@ mod tests { #[tokio::test] async fn host_blocked_rejects_loopback_when_allowlist_empty() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec![], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(NetworkProxySettings::default()); assert_eq!( state.host_blocked("127.0.0.1", 80).await.unwrap(), @@ -1250,11 +1236,9 @@ mod tests { #[tokio::test] async fn host_blocked_rejects_allowlisted_hostname_when_dns_lookup_fails() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["does-not-resolve.invalid".to_string()], - allow_local_binding: false, - ..NetworkProxySettings::default() - }); + let mut network = NetworkProxySettings::default(); + network.set_allowed_domains(vec!["does-not-resolve.invalid".to_string()]); + let state = network_proxy_state_for_policy(network); assert_eq!( state @@ -1273,10 +1257,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["example.com".to_string(), "evil.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["example.com", "evil.com"], &[]); + network.enabled = true; + network }, }; @@ -1292,10 +1276,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["example.com".to_string(), "api.openai.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["example.com", "api.openai.com"], &[]); + network.enabled = true; + network }, }; @@ -1328,10 +1312,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["api.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["api.example.com"], &[]); + network.enabled = true; + network }, }; @@ -1346,10 +1330,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["**.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["**.example.com"], &[]); + network.enabled = true; + network }, }; @@ -1364,10 +1348,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["api.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["api.example.com"], &[]); + network.enabled = true; + network }, }; @@ -1383,10 +1367,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["api.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["api.example.com"], &[]); + network.enabled = true; + network }, }; @@ -1402,10 +1386,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["api.example.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["api.example.com"], &[]); + network.enabled = true; + network }, }; @@ -1422,7 +1406,6 @@ mod tests { let config = NetworkProxyConfig { network: NetworkProxySettings { enabled: true, - denied_domains: vec![], ..NetworkProxySettings::default() }, }; @@ -1439,10 +1422,10 @@ mod tests { }; let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - denied_domains: vec!["evil.com".to_string(), "more-evil.com".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&[], &["evil.com", "more-evil.com"]); + network.enabled = true; + network }, }; @@ -1626,10 +1609,10 @@ mod tests { #[test] fn build_config_state_allows_global_wildcard_allowed_domains() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["*".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["*"], &[]); + network.enabled = true; + network }, }; @@ -1639,10 +1622,10 @@ mod tests { #[test] fn build_config_state_allows_bracketed_global_wildcard_allowed_domains() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["[*]".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["[*]"], &[]); + network.enabled = true; + network }, }; @@ -1652,11 +1635,10 @@ mod tests { #[test] fn build_config_state_rejects_global_wildcard_denied_domains() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["example.com".to_string()], - denied_domains: vec!["*".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["example.com"], &["*"]); + network.enabled = true; + network }, }; @@ -1666,11 +1648,10 @@ mod tests { #[test] fn build_config_state_rejects_bracketed_global_wildcard_denied_domains() { let config = NetworkProxyConfig { - network: NetworkProxySettings { - enabled: true, - allowed_domains: vec!["example.com".to_string()], - denied_domains: vec!["[*]".to_string()], - ..NetworkProxySettings::default() + network: { + let mut network = network_settings(&["example.com"], &["[*]"]); + network.enabled = true; + network }, }; @@ -1681,11 +1662,11 @@ mod tests { #[tokio::test] async fn unix_socket_allowlist_is_respected_on_macos() { let socket_path = "/tmp/example.sock".to_string(); - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_unix_sockets: vec![socket_path.clone()], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings_with_unix_sockets( + &["example.com"], + &[], + std::slice::from_ref(&socket_path), + )); assert!(state.is_unix_socket_allowed(&socket_path).await.unwrap()); assert!( @@ -1716,11 +1697,11 @@ mod tests { let real_s = real.to_str().unwrap().to_string(); let link_s = link.to_str().unwrap().to_string(); - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_unix_sockets: vec![real_s], - ..NetworkProxySettings::default() - }); + let state = network_proxy_state_for_policy(network_settings_with_unix_sockets( + &["example.com"], + &[], + std::slice::from_ref(&real_s), + )); assert!(state.is_unix_socket_allowed(&link_s).await.unwrap()); } @@ -1728,10 +1709,10 @@ mod tests { #[cfg(target_os = "macos")] #[tokio::test] async fn unix_socket_allow_all_flag_bypasses_allowlist() { - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - dangerously_allow_all_unix_sockets: true, - ..NetworkProxySettings::default() + let state = network_proxy_state_for_policy({ + let mut network = network_settings(&["example.com"], &[]); + network.dangerously_allow_all_unix_sockets = true; + network }); assert!(state.is_unix_socket_allowed("/tmp/any.sock").await.unwrap()); @@ -1742,11 +1723,14 @@ mod tests { #[tokio::test] async fn unix_socket_allowlist_is_rejected_on_non_macos() { let socket_path = "/tmp/example.sock".to_string(); - let state = network_proxy_state_for_policy(NetworkProxySettings { - allowed_domains: vec!["example.com".to_string()], - allow_unix_sockets: vec![socket_path.clone()], - dangerously_allow_all_unix_sockets: true, - ..NetworkProxySettings::default() + let state = network_proxy_state_for_policy({ + let mut network = network_settings_with_unix_sockets( + &["example.com"], + &[], + std::slice::from_ref(&socket_path), + ); + network.dangerously_allow_all_unix_sockets = true; + network }); assert!(!state.is_unix_socket_allowed(&socket_path).await.unwrap()); diff --git a/codex-rs/network-proxy/src/state.rs b/codex-rs/network-proxy/src/state.rs index 435e5beab4..01e4966b5a 100644 --- a/codex-rs/network-proxy/src/state.rs +++ b/codex-rs/network-proxy/src/state.rs @@ -1,5 +1,7 @@ +use crate::config::NetworkDomainPermissions; use crate::config::NetworkMode; use crate::config::NetworkProxyConfig; +use crate::config::NetworkUnixSocketPermissions; use crate::mitm::MitmState; use crate::policy::DomainPattern; use crate::policy::compile_allowlist_globset; @@ -46,12 +48,9 @@ pub struct PartialNetworkConfig { pub dangerously_allow_non_loopback_proxy: Option, pub dangerously_allow_all_unix_sockets: Option, #[serde(default)] - pub allowed_domains: Option>, - #[serde(default)] - pub denied_domains: Option>, - #[serde(default)] - pub allow_unix_sockets: Option>, + pub domains: Option, #[serde(default)] + pub unix_sockets: Option, pub allow_local_binding: Option, } @@ -60,10 +59,12 @@ pub fn build_config_state( constraints: NetworkProxyConstraints, ) -> anyhow::Result { crate::config::validate_unix_socket_allowlist_paths(&config)?; - validate_denylist_domain_patterns("network.denied_domains", &config.network.denied_domains) + let allowed_domains = config.network.allowed_domains().unwrap_or_default(); + let denied_domains = config.network.denied_domains().unwrap_or_default(); + validate_non_global_wildcard_domain_patterns("network.denied_domains", &denied_domains) .map_err(NetworkProxyConstraintError::into_anyhow)?; - let deny_set = compile_denylist_globset(&config.network.denied_domains)?; - let allow_set = compile_allowlist_globset(&config.network.allowed_domains)?; + let deny_set = compile_denylist_globset(&denied_domains)?; + let allow_set = compile_allowlist_globset(&allowed_domains)?; let mitm = if config.network.mitm { Some(Arc::new(MitmState::new( config.network.allow_upstream_proxy, @@ -106,7 +107,14 @@ pub fn validate_policy_against_constraints( } let enabled = config.network.enabled; - validate_denylist_domain_patterns("network.denied_domains", &config.network.denied_domains)?; + let config_allowed_domains = config.network.allowed_domains().unwrap_or_default(); + let config_denied_domains = config.network.denied_domains().unwrap_or_default(); + let denied_domain_overrides: HashSet = config_denied_domains + .iter() + .map(|entry| entry.to_ascii_lowercase()) + .collect(); + let config_allow_unix_sockets = config.network.allow_unix_sockets(); + validate_non_global_wildcard_domain_patterns("network.denied_domains", &config_denied_domains)?; if let Some(max_enabled) = constraints.enabled { validate(enabled, move |candidate| { if *candidate && !max_enabled { @@ -206,20 +214,24 @@ pub fn validate_policy_against_constraints( } if let Some(allowed_domains) = &constraints.allowed_domains { + validate_non_global_wildcard_domain_patterns("network.allowed_domains", allowed_domains)?; match constraints.allowlist_expansion_enabled { Some(true) => { let required_set: HashSet = allowed_domains .iter() .map(|entry| entry.to_ascii_lowercase()) .collect(); - validate(config.network.allowed_domains.clone(), move |candidate| { + validate(config_allowed_domains, |candidate| { let candidate_set: HashSet = candidate .iter() .map(|entry| entry.to_ascii_lowercase()) .collect(); let missing: Vec = required_set .iter() - .filter(|entry| !candidate_set.contains(*entry)) + .filter(|entry| { + !candidate_set.contains(*entry) + && !denied_domain_overrides.contains(*entry) + }) .cloned() .collect(); if missing.is_empty() { @@ -238,12 +250,16 @@ pub fn validate_policy_against_constraints( .iter() .map(|entry| entry.to_ascii_lowercase()) .collect(); - validate(config.network.allowed_domains.clone(), move |candidate| { + validate(config_allowed_domains, |candidate| { let candidate_set: HashSet = candidate .iter() .map(|entry| entry.to_ascii_lowercase()) .collect(); - if candidate_set == required_set { + let expected_set: HashSet = required_set + .difference(&denied_domain_overrides) + .cloned() + .collect(); + if candidate_set == expected_set { Ok(()) } else { Err(invalid_value( @@ -259,7 +275,7 @@ pub fn validate_policy_against_constraints( .iter() .map(|entry| DomainPattern::parse_for_constraints(entry)) .collect(); - validate(config.network.allowed_domains.clone(), move |candidate| { + validate(config_allowed_domains, move |candidate| { let mut invalid = Vec::new(); for entry in candidate { let candidate_pattern = DomainPattern::parse_for_constraints(entry); @@ -285,14 +301,14 @@ pub fn validate_policy_against_constraints( } if let Some(denied_domains) = &constraints.denied_domains { - validate_denylist_domain_patterns("network.denied_domains", denied_domains)?; + validate_non_global_wildcard_domain_patterns("network.denied_domains", denied_domains)?; let required_set: HashSet = denied_domains .iter() .map(|s| s.to_ascii_lowercase()) .collect(); match constraints.denylist_expansion_enabled { Some(false) => { - validate(config.network.denied_domains.clone(), move |candidate| { + validate(config_denied_domains, move |candidate| { let candidate_set: HashSet = candidate .iter() .map(|entry| entry.to_ascii_lowercase()) @@ -309,7 +325,7 @@ pub fn validate_policy_against_constraints( })?; } Some(true) | None => { - validate(config.network.denied_domains.clone(), move |candidate| { + validate(config_denied_domains, move |candidate| { let candidate_set: HashSet = candidate.iter().map(|s| s.to_ascii_lowercase()).collect(); let missing: Vec = required_set @@ -336,32 +352,29 @@ pub fn validate_policy_against_constraints( .iter() .map(|s| s.to_ascii_lowercase()) .collect(); - validate( - config.network.allow_unix_sockets.clone(), - move |candidate| { - let mut invalid = Vec::new(); - for entry in candidate { - if !allowed_set.contains(&entry.to_ascii_lowercase()) { - invalid.push(entry.clone()); - } + validate(config_allow_unix_sockets, move |candidate| { + let mut invalid = Vec::new(); + for entry in candidate { + if !allowed_set.contains(&entry.to_ascii_lowercase()) { + invalid.push(entry.clone()); } - if invalid.is_empty() { - Ok(()) - } else { - Err(invalid_value( - "network.allow_unix_sockets", - format!("{invalid:?}"), - "subset of managed allow_unix_sockets", - )) - } - }, - )?; + } + if invalid.is_empty() { + Ok(()) + } else { + Err(invalid_value( + "network.allow_unix_sockets", + format!("{invalid:?}"), + "subset of managed allow_unix_sockets", + )) + } + })?; } Ok(()) } -fn validate_denylist_domain_patterns( +fn validate_non_global_wildcard_domain_patterns( field_name: &'static str, patterns: &[String], ) -> Result<(), NetworkProxyConstraintError> { @@ -401,3 +414,6 @@ fn network_mode_rank(mode: NetworkMode) -> u8 { NetworkMode::Full => 1, } } + +#[cfg(test)] +mod tests {} diff --git a/codex-rs/tui/src/debug_config.rs b/codex-rs/tui/src/debug_config.rs index 29a5cb7cdf..42cfa19586 100644 --- a/codex-rs/tui/src/debug_config.rs +++ b/codex-rs/tui/src/debug_config.rs @@ -5,6 +5,8 @@ use codex_core::config_loader::ConfigLayerEntry; use codex_core::config_loader::ConfigLayerStack; use codex_core::config_loader::ConfigLayerStackOrdering; use codex_core::config_loader::NetworkConstraints; +use codex_core::config_loader::NetworkDomainPermissionToml; +use codex_core::config_loader::NetworkUnixSocketPermissionToml; use codex_core::config_loader::RequirementSource; use codex_core::config_loader::ResidencyRequirement; use codex_core::config_loader::SandboxModeRequirement; @@ -333,10 +335,9 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { allow_upstream_proxy, dangerously_allow_non_loopback_proxy, dangerously_allow_all_unix_sockets, - allowed_domains, + domains, managed_allowed_domains_only, - denied_domains, - allow_unix_sockets, + unix_sockets, allow_local_binding, } = network; @@ -362,21 +363,24 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { "dangerously_allow_all_unix_sockets={dangerously_allow_all_unix_sockets}" )); } - if let Some(allowed_domains) = allowed_domains { - parts.push(format!("allowed_domains=[{}]", allowed_domains.join(", "))); + if let Some(domains) = domains { + parts.push(format!( + "domains={}", + format_network_permission_entries(&domains.entries, format_network_domain_permission) + )); } if let Some(managed_allowed_domains_only) = managed_allowed_domains_only { parts.push(format!( "managed_allowed_domains_only={managed_allowed_domains_only}" )); } - if let Some(denied_domains) = denied_domains { - parts.push(format!("denied_domains=[{}]", denied_domains.join(", "))); - } - if let Some(allow_unix_sockets) = allow_unix_sockets { + if let Some(unix_sockets) = unix_sockets { parts.push(format!( - "allow_unix_sockets=[{}]", - allow_unix_sockets.join(", ") + "unix_sockets={}", + format_network_permission_entries( + &unix_sockets.entries, + format_network_unix_socket_permission, + ) )); } if let Some(allow_local_binding) = allow_local_binding { @@ -386,6 +390,33 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { join_or_empty(parts) } +fn format_network_permission_entries( + entries: &std::collections::BTreeMap, + format_value: impl Fn(T) -> &'static str, +) -> String { + let parts = entries + .iter() + .map(|(key, value)| format!("{key}={}", format_value(*value))) + .collect::>(); + format!("{{{}}}", parts.join(", ")) +} + +fn format_network_domain_permission(permission: NetworkDomainPermissionToml) -> &'static str { + match permission { + NetworkDomainPermissionToml::Allow => "allow", + NetworkDomainPermissionToml::Deny => "deny", + } +} + +fn format_network_unix_socket_permission( + permission: NetworkUnixSocketPermissionToml, +) -> &'static str { + match permission { + NetworkUnixSocketPermissionToml::Allow => "allow", + NetworkUnixSocketPermissionToml::None => "none", + } +} + #[cfg(test)] mod tests { use super::render_debug_config_lines; @@ -400,6 +431,8 @@ mod tests { use codex_core::config_loader::McpServerIdentity; use codex_core::config_loader::McpServerRequirement; use codex_core::config_loader::NetworkConstraints; + use codex_core::config_loader::NetworkDomainPermissionToml; + use codex_core::config_loader::NetworkDomainPermissionsToml; use codex_core::config_loader::RequirementSource; use codex_core::config_loader::ResidencyRequirement; use codex_core::config_loader::SandboxModeRequirement; @@ -516,7 +549,12 @@ mod tests { network: Some(Sourced::new( NetworkConstraints { enabled: Some(true), - allowed_domains: Some(vec!["example.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "example.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), ..Default::default() }, RequirementSource::CloudRequirements, @@ -580,7 +618,7 @@ mod tests { assert!(rendered.contains("mcp_servers: docs (source: MDM managed_config.toml (legacy))")); assert!(rendered.contains("enforce_residency: us (source: cloud requirements)")); assert!(rendered.contains( - "experimental_network: enabled=true, allowed_domains=[example.com] (source: cloud requirements)" + "experimental_network: enabled=true, domains={example.com=allow} (source: cloud requirements)" )); assert!(!rendered.contains(" - rules:")); } diff --git a/codex-rs/tui_app_server/src/debug_config.rs b/codex-rs/tui_app_server/src/debug_config.rs index 29a5cb7cdf..76f4a91a52 100644 --- a/codex-rs/tui_app_server/src/debug_config.rs +++ b/codex-rs/tui_app_server/src/debug_config.rs @@ -5,6 +5,8 @@ use codex_core::config_loader::ConfigLayerEntry; use codex_core::config_loader::ConfigLayerStack; use codex_core::config_loader::ConfigLayerStackOrdering; use codex_core::config_loader::NetworkConstraints; +use codex_core::config_loader::NetworkDomainPermissionToml; +use codex_core::config_loader::NetworkUnixSocketPermissionToml; use codex_core::config_loader::RequirementSource; use codex_core::config_loader::ResidencyRequirement; use codex_core::config_loader::SandboxModeRequirement; @@ -333,10 +335,9 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { allow_upstream_proxy, dangerously_allow_non_loopback_proxy, dangerously_allow_all_unix_sockets, - allowed_domains, + domains, managed_allowed_domains_only, - denied_domains, - allow_unix_sockets, + unix_sockets, allow_local_binding, } = network; @@ -362,21 +363,24 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { "dangerously_allow_all_unix_sockets={dangerously_allow_all_unix_sockets}" )); } - if let Some(allowed_domains) = allowed_domains { - parts.push(format!("allowed_domains=[{}]", allowed_domains.join(", "))); + if let Some(domains) = domains { + parts.push(format!( + "domains={}", + format_network_permission_entries(&domains.entries, format_network_domain_permission) + )); } if let Some(managed_allowed_domains_only) = managed_allowed_domains_only { parts.push(format!( "managed_allowed_domains_only={managed_allowed_domains_only}" )); } - if let Some(denied_domains) = denied_domains { - parts.push(format!("denied_domains=[{}]", denied_domains.join(", "))); - } - if let Some(allow_unix_sockets) = allow_unix_sockets { + if let Some(unix_sockets) = unix_sockets { parts.push(format!( - "allow_unix_sockets=[{}]", - allow_unix_sockets.join(", ") + "unix_sockets={}", + format_network_permission_entries( + &unix_sockets.entries, + format_network_unix_socket_permission, + ) )); } if let Some(allow_local_binding) = allow_local_binding { @@ -386,6 +390,33 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { join_or_empty(parts) } +fn format_network_permission_entries( + entries: &std::collections::BTreeMap, + format_value: impl Fn(T) -> &'static str, +) -> String { + let parts = entries + .iter() + .map(|(key, value)| format!("{key}={}", format_value(*value))) + .collect::>(); + format!("{{{}}}", parts.join(", ")) +} + +fn format_network_domain_permission(permission: NetworkDomainPermissionToml) -> &'static str { + match permission { + NetworkDomainPermissionToml::Allow => "allow", + NetworkDomainPermissionToml::Deny => "deny", + } +} + +fn format_network_unix_socket_permission( + permission: NetworkUnixSocketPermissionToml, +) -> &'static str { + match permission { + NetworkUnixSocketPermissionToml::Allow => "allow", + NetworkUnixSocketPermissionToml::None => "none", + } +} + #[cfg(test)] mod tests { use super::render_debug_config_lines; @@ -400,6 +431,10 @@ mod tests { use codex_core::config_loader::McpServerIdentity; use codex_core::config_loader::McpServerRequirement; use codex_core::config_loader::NetworkConstraints; + use codex_core::config_loader::NetworkDomainPermissionToml; + use codex_core::config_loader::NetworkDomainPermissionsToml; + use codex_core::config_loader::NetworkUnixSocketPermissionToml; + use codex_core::config_loader::NetworkUnixSocketPermissionsToml; use codex_core::config_loader::RequirementSource; use codex_core::config_loader::ResidencyRequirement; use codex_core::config_loader::SandboxModeRequirement; @@ -516,7 +551,12 @@ mod tests { network: Some(Sourced::new( NetworkConstraints { enabled: Some(true), - allowed_domains: Some(vec!["example.com".to_string()]), + domains: Some(NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "example.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), ..Default::default() }, RequirementSource::CloudRequirements, @@ -580,10 +620,45 @@ mod tests { assert!(rendered.contains("mcp_servers: docs (source: MDM managed_config.toml (legacy))")); assert!(rendered.contains("enforce_residency: us (source: cloud requirements)")); assert!(rendered.contains( - "experimental_network: enabled=true, allowed_domains=[example.com] (source: cloud requirements)" + "experimental_network: enabled=true, domains={example.com=allow} (source: cloud requirements)" )); assert!(!rendered.contains(" - rules:")); } + + #[test] + fn debug_config_output_formats_unix_socket_permissions() { + let requirements = ConfigRequirements { + network: Some(Sourced::new( + NetworkConstraints { + unix_sockets: Some(NetworkUnixSocketPermissionsToml { + entries: BTreeMap::from([ + ( + "/tmp/codex.sock".to_string(), + NetworkUnixSocketPermissionToml::Allow, + ), + ( + "/tmp/blocked.sock".to_string(), + NetworkUnixSocketPermissionToml::None, + ), + ]), + }), + ..Default::default() + }, + RequirementSource::CloudRequirements, + )), + ..ConfigRequirements::default() + }; + + let stack = + ConfigLayerStack::new(Vec::new(), requirements, ConfigRequirementsToml::default()) + .expect("config layer stack"); + + let rendered = render_to_text(&render_debug_config_lines(&stack)); + assert!(rendered.contains( + "experimental_network: unix_sockets={/tmp/blocked.sock=none, /tmp/codex.sock=allow} (source: cloud requirements)" + )); + } + #[test] fn debug_config_output_lists_session_flag_key_value_pairs() { let session_flags = toml::from_str::( From 47a9e2e084e21542821ab65aae91f2bd6bf17c07 Mon Sep 17 00:00:00 2001 From: daniel-oai Date: Fri, 27 Mar 2026 00:27:15 -0700 Subject: [PATCH 031/191] Add ChatGPT device-code login to app server (#15525) ## Problem App-server clients could only initiate ChatGPT login through the browser callback flow, even though the shared login crate already supports device-code auth. That left VS Code, Codex App, and other app-server clients without a first-class way to use the existing device-code backend when browser redirects are brittle or when the client UX wants to own the login ceremony. ## Mental model This change adds a second ChatGPT login start path to app-server: clients can now call `account/login/start` with `type: "chatgptDeviceCode"`. App-server immediately returns a `loginId` plus the device-code UX payload (`verificationUrl` and `userCode`), then completes the login asynchronously in the background using the existing `codex_login` polling flow. Successful device-code login still resolves to ordinary `chatgpt` auth, and completion continues to flow through the existing `account/login/completed` and `account/updated` notifications. ## Non-goals This does not introduce a new auth mode, a new account shape, or a device-code eligibility discovery API. It also does not add automatic fallback to browser login in core; clients remain responsible for choosing when to request device code and whether to retry with a different UX if the backend/admin policy rejects it. ## Tradeoffs We intentionally keep `login_chatgpt_common` as a local validation helper instead of turning it into a capability probe. Device-code eligibility is checked by actually calling `request_device_code`, which means policy-disabled cases surface as an immediate request error rather than an async completion event. We also keep the active-login state machine minimal: browser and device-code logins share the same public cancel contract, but device-code cancellation is implemented with a local cancel token rather than a larger cross-crate refactor. ## Architecture The protocol grows a new `chatgptDeviceCode` request/response variant in app-server v2. On the server side, the new handler reuses the existing ChatGPT login precondition checks, calls `request_device_code`, returns the device-code payload, and then spawns a background task that waits on either cancellation or `complete_device_code_login`. On success, it reuses the existing auth reload and cloud-requirements refresh path before emitting `account/login/completed` success and `account/updated`. On failure or cancellation, it emits only `account/login/completed` failure. The existing `account/login/cancel { loginId }` contract remains unchanged and now works for both browser and device-code attempts. ## Tests Added protocol serialization coverage for the new request/response variant, plus app-server tests for device-code success, failure, cancel, and start-time rejection behavior. Existing browser ChatGPT login coverage remains in place to show that the callback-based flow is unchanged. --- .../schema/json/ClientRequest.json | 16 + .../codex_app_server_protocol.schemas.json | 46 +++ .../codex_app_server_protocol.v2.schemas.json | 46 +++ .../schema/json/v2/LoginAccountParams.json | 16 + .../schema/json/v2/LoginAccountResponse.json | 30 ++ .../typescript/v2/LoginAccountParams.ts | 2 +- .../typescript/v2/LoginAccountResponse.ts | 10 +- .../src/protocol/common.rs | 27 +- .../app-server-protocol/src/protocol/v2.rs | 14 + codex-rs/app-server-test-client/src/lib.rs | 53 ++- codex-rs/app-server/README.md | 36 +- .../app-server/src/codex_message_processor.rs | 179 ++++++++- .../app-server/tests/common/mcp_process.rs | 8 + codex-rs/app-server/tests/suite/v2/account.rs | 355 ++++++++++++++++++ 14 files changed, 802 insertions(+), 36 deletions(-) diff --git a/codex-rs/app-server-protocol/schema/json/ClientRequest.json b/codex-rs/app-server-protocol/schema/json/ClientRequest.json index 9227a97d55..7c94419844 100644 --- a/codex-rs/app-server-protocol/schema/json/ClientRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ClientRequest.json @@ -1156,6 +1156,22 @@ "title": "ChatgptLoginAccountParams", "type": "object" }, + { + "properties": { + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodeLoginAccountParamsType", + "type": "string" + } + }, + "required": [ + "type" + ], + "title": "ChatgptDeviceCodeLoginAccountParams", + "type": "object" + }, { "description": "[UNSTABLE] FOR OPENAI INTERNAL USE ONLY - DO NOT USE. The access token must contain the same scopes that Codex-managed ChatGPT auth tokens have.", "properties": { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 8531542d55..91490ce26f 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -8569,6 +8569,22 @@ "title": "Chatgptv2::LoginAccountParams", "type": "object" }, + { + "properties": { + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParamsType", + "type": "string" + } + }, + "required": [ + "type" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParams", + "type": "object" + }, { "description": "[UNSTABLE] FOR OPENAI INTERNAL USE ONLY - DO NOT USE. The access token must contain the same scopes that Codex-managed ChatGPT auth tokens have.", "properties": { @@ -8650,6 +8666,36 @@ "title": "Chatgptv2::LoginAccountResponse", "type": "object" }, + { + "properties": { + "loginId": { + "type": "string" + }, + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponseType", + "type": "string" + }, + "userCode": { + "description": "One-time code the user must enter after signing in.", + "type": "string" + }, + "verificationUrl": { + "description": "URL the client should open in a browser to complete device code authorization.", + "type": "string" + } + }, + "required": [ + "loginId", + "type", + "userCode", + "verificationUrl" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponse", + "type": "object" + }, { "properties": { "type": { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index 9d28f531af..0f31f5b3ec 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -5383,6 +5383,22 @@ "title": "Chatgptv2::LoginAccountParams", "type": "object" }, + { + "properties": { + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParamsType", + "type": "string" + } + }, + "required": [ + "type" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParams", + "type": "object" + }, { "description": "[UNSTABLE] FOR OPENAI INTERNAL USE ONLY - DO NOT USE. The access token must contain the same scopes that Codex-managed ChatGPT auth tokens have.", "properties": { @@ -5464,6 +5480,36 @@ "title": "Chatgptv2::LoginAccountResponse", "type": "object" }, + { + "properties": { + "loginId": { + "type": "string" + }, + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponseType", + "type": "string" + }, + "userCode": { + "description": "One-time code the user must enter after signing in.", + "type": "string" + }, + "verificationUrl": { + "description": "URL the client should open in a browser to complete device code authorization.", + "type": "string" + } + }, + "required": [ + "loginId", + "type", + "userCode", + "verificationUrl" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponse", + "type": "object" + }, { "properties": { "type": { diff --git a/codex-rs/app-server-protocol/schema/json/v2/LoginAccountParams.json b/codex-rs/app-server-protocol/schema/json/v2/LoginAccountParams.json index ce6bdd4a3f..a933b71a83 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/LoginAccountParams.json +++ b/codex-rs/app-server-protocol/schema/json/v2/LoginAccountParams.json @@ -37,6 +37,22 @@ "title": "Chatgptv2::LoginAccountParams", "type": "object" }, + { + "properties": { + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParamsType", + "type": "string" + } + }, + "required": [ + "type" + ], + "title": "ChatgptDeviceCodev2::LoginAccountParams", + "type": "object" + }, { "description": "[UNSTABLE] FOR OPENAI INTERNAL USE ONLY - DO NOT USE. The access token must contain the same scopes that Codex-managed ChatGPT auth tokens have.", "properties": { diff --git a/codex-rs/app-server-protocol/schema/json/v2/LoginAccountResponse.json b/codex-rs/app-server-protocol/schema/json/v2/LoginAccountResponse.json index e2697ea44e..a800bffccd 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/LoginAccountResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/LoginAccountResponse.json @@ -42,6 +42,36 @@ "title": "Chatgptv2::LoginAccountResponse", "type": "object" }, + { + "properties": { + "loginId": { + "type": "string" + }, + "type": { + "enum": [ + "chatgptDeviceCode" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponseType", + "type": "string" + }, + "userCode": { + "description": "One-time code the user must enter after signing in.", + "type": "string" + }, + "verificationUrl": { + "description": "URL the client should open in a browser to complete device code authorization.", + "type": "string" + } + }, + "required": [ + "loginId", + "type", + "userCode", + "verificationUrl" + ], + "title": "ChatgptDeviceCodev2::LoginAccountResponse", + "type": "object" + }, { "properties": { "type": { diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountParams.ts index ef668f9c1a..9fcb01f97c 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountParams.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountParams.ts @@ -2,7 +2,7 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type LoginAccountParams = { "type": "apiKey", apiKey: string, } | { "type": "chatgpt" } | { "type": "chatgptAuthTokens", +export type LoginAccountParams = { "type": "apiKey", apiKey: string, } | { "type": "chatgpt" } | { "type": "chatgptDeviceCode" } | { "type": "chatgptAuthTokens", /** * Access token (JWT) supplied by the client. * This token is used for backend API requests and email extraction. diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountResponse.ts index cd79f6c83f..651f171e31 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountResponse.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/LoginAccountResponse.ts @@ -6,4 +6,12 @@ export type LoginAccountResponse = { "type": "apiKey", } | { "type": "chatgpt", /** * URL the client should open in a browser to initiate the OAuth flow. */ -authUrl: string, } | { "type": "chatgptAuthTokens", }; +authUrl: string, } | { "type": "chatgptDeviceCode", loginId: string, +/** + * URL the client should open in a browser to complete device code authorization. + */ +verificationUrl: string, +/** + * One-time code the user must enter after signing in. + */ +userCode: string, } | { "type": "chatgptAuthTokens", }; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index 6bd44562e0..30061c716e 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -1435,16 +1435,35 @@ mod tests { Ok(()) } + #[test] + fn serialize_account_login_chatgpt_device_code() -> Result<()> { + let request = ClientRequest::LoginAccount { + request_id: RequestId::Integer(4), + params: v2::LoginAccountParams::ChatgptDeviceCode, + }; + assert_eq!( + json!({ + "method": "account/login/start", + "id": 4, + "params": { + "type": "chatgptDeviceCode" + } + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } + #[test] fn serialize_account_logout() -> Result<()> { let request = ClientRequest::LogoutAccount { - request_id: RequestId::Integer(4), + request_id: RequestId::Integer(5), params: None, }; assert_eq!( json!({ "method": "account/logout", - "id": 4, + "id": 5, }), serde_json::to_value(&request)?, ); @@ -1454,7 +1473,7 @@ mod tests { #[test] fn serialize_account_login_chatgpt_auth_tokens() -> Result<()> { let request = ClientRequest::LoginAccount { - request_id: RequestId::Integer(5), + request_id: RequestId::Integer(6), params: v2::LoginAccountParams::ChatgptAuthTokens { access_token: "access-token".to_string(), chatgpt_account_id: "org-123".to_string(), @@ -1464,7 +1483,7 @@ mod tests { assert_eq!( json!({ "method": "account/login/start", - "id": 5, + "id": 6, "params": { "type": "chatgptAuthTokens", "accessToken": "access-token", diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 930112eed9..9373c852d8 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -1589,6 +1589,9 @@ pub enum LoginAccountParams { #[serde(rename = "chatgpt")] #[ts(rename = "chatgpt")] Chatgpt, + #[serde(rename = "chatgptDeviceCode")] + #[ts(rename = "chatgptDeviceCode")] + ChatgptDeviceCode, /// [UNSTABLE] FOR OPENAI INTERNAL USE ONLY - DO NOT USE. /// The access token must contain the same scopes that Codex-managed ChatGPT auth tokens have. #[experimental("account/login/start.chatgptAuthTokens")] @@ -1626,6 +1629,17 @@ pub enum LoginAccountResponse { /// URL the client should open in a browser to initiate the OAuth flow. auth_url: String, }, + #[serde(rename = "chatgptDeviceCode", rename_all = "camelCase")] + #[ts(rename = "chatgptDeviceCode", rename_all = "camelCase")] + ChatgptDeviceCode { + // Use plain String for identifiers to avoid TS/JSON Schema quirks around uuid-specific types. + // Convert to/from UUIDs at the application layer as needed. + login_id: String, + /// URL the client should open in a browser to complete device code authorization. + verification_url: String, + /// One-time code the user must enter after signing in. + user_code: String, + }, #[serde(rename = "chatgptAuthTokens", rename_all = "camelCase")] #[ts(rename = "chatgptAuthTokens", rename_all = "camelCase")] ChatgptAuthTokens {}, diff --git a/codex-rs/app-server-test-client/src/lib.rs b/codex-rs/app-server-test-client/src/lib.rs index 82954d3cb6..dd548012a0 100644 --- a/codex-rs/app-server-test-client/src/lib.rs +++ b/codex-rs/app-server-test-client/src/lib.rs @@ -225,7 +225,11 @@ enum CliCommand { abort_on: Option, }, /// Trigger the ChatGPT login flow and wait for completion. - TestLogin, + TestLogin { + /// Use the device-code login flow instead of the browser callback flow. + #[arg(long, default_value_t = false)] + device_code: bool, + }, /// Fetch the current account rate limits from the Codex app-server. GetAccountRateLimits, /// List the available models from the Codex app-server. @@ -372,10 +376,10 @@ pub async fn run() -> Result<()> { ) .await } - CliCommand::TestLogin => { + CliCommand::TestLogin { device_code } => { ensure_dynamic_tools_unused(&dynamic_tools, "test-login")?; let endpoint = resolve_endpoint(codex_bin, url)?; - test_login(&endpoint, &config_overrides).await + test_login(&endpoint, &config_overrides, device_code).await } CliCommand::GetAccountRateLimits => { ensure_dynamic_tools_unused(&dynamic_tools, "get-account-rate-limits")?; @@ -1028,17 +1032,38 @@ async fn send_follow_up_v2( .await } -async fn test_login(endpoint: &Endpoint, config_overrides: &[String]) -> Result<()> { +async fn test_login( + endpoint: &Endpoint, + config_overrides: &[String], + device_code: bool, +) -> Result<()> { with_client("test-login", endpoint, config_overrides, |client| { let initialize = client.initialize()?; println!("< initialize response: {initialize:?}"); - let login_response = client.login_account_chatgpt()?; - println!("< account/login/start response: {login_response:?}"); - let LoginAccountResponse::Chatgpt { login_id, auth_url } = login_response else { - bail!("expected chatgpt login response"); + let login_response = if device_code { + client.login_account_chatgpt_device_code()? + } else { + client.login_account_chatgpt()? + }; + println!("< account/login/start response: {login_response:?}"); + let login_id = match login_response { + LoginAccountResponse::Chatgpt { login_id, auth_url } => { + println!("Open the following URL in your browser to continue:\n{auth_url}"); + login_id + } + LoginAccountResponse::ChatgptDeviceCode { + login_id, + verification_url, + user_code, + } => { + println!( + "Open the following URL and enter the code to continue:\n{verification_url}\n\nCode: {user_code}" + ); + login_id + } + _ => bail!("expected chatgpt login response"), }; - println!("Open the following URL in your browser to continue:\n{auth_url}"); let completion = client.wait_for_account_login_completion(&login_id)?; println!("< account/login/completed notification: {completion:?}"); @@ -1590,6 +1615,16 @@ impl CodexClient { self.send_request(request, request_id, "account/login/start") } + fn login_account_chatgpt_device_code(&mut self) -> Result { + let request_id = self.request_id(); + let request = ClientRequest::LoginAccount { + request_id: request_id.clone(), + params: codex_app_server_protocol::LoginAccountParams::ChatgptDeviceCode, + }; + + self.send_request(request, request_id, "account/login/start") + } + fn get_account_rate_limits(&mut self) -> Result { let request_id = self.request_id(); let request = ClientRequest::GetAccountRateLimits { diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 38cdacac79..4365cb8e29 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -1309,14 +1309,14 @@ The JSON-RPC auth/account surface exposes request/response methods plus server-i Codex supports these authentication modes. The current mode is surfaced in `account/updated` (`authMode`), which also includes the current ChatGPT `planType` when available, and can be inferred from `account/read`. - **API key (`apiKey`)**: Caller supplies an OpenAI API key via `account/login/start` with `type: "apiKey"`. The API key is saved and used for API requests. -- **ChatGPT managed (`chatgpt`)** (recommended): Codex owns the ChatGPT OAuth flow and refresh tokens. Start via `account/login/start` with `type: "chatgpt"`; Codex persists tokens to disk and refreshes them automatically. +- **ChatGPT managed (`chatgpt`)** (recommended): Codex owns the ChatGPT OAuth flow and refresh tokens. Start via `account/login/start` with `type: "chatgpt"` for the browser flow or `type: "chatgptDeviceCode"` for device code; Codex persists tokens to disk and refreshes them automatically. ### API Overview - `account/read` — fetch current account info; optionally refresh tokens. -- `account/login/start` — begin login (`apiKey`, `chatgpt`). +- `account/login/start` — begin login (`apiKey`, `chatgpt`, `chatgptDeviceCode`). - `account/login/completed` (notify) — emitted when a login attempt finishes (success or error). -- `account/login/cancel` — cancel a pending ChatGPT login by `loginId`. +- `account/login/cancel` — cancel a pending managed ChatGPT login by `loginId`. - `account/logout` — sign out; triggers `account/updated`. - `account/updated` (notify) — emitted whenever auth mode changes (`authMode`: `apikey`, `chatgpt`, or `null`) and includes the current ChatGPT `planType` when available. - `account/rateLimits/read` — fetch ChatGPT rate limits; updates arrive via `account/rateLimits/updated` (notify). @@ -1380,26 +1380,40 @@ Field notes: { "method": "account/updated", "params": { "authMode": "chatgpt", "planType": "plus" } } ``` -### 4) Cancel a ChatGPT login +### 4) Log in with ChatGPT (device code flow) + +1. Start: + ```json + { "method": "account/login/start", "id": 4, "params": { "type": "chatgptDeviceCode" } } + { "id": 4, "result": { "type": "chatgptDeviceCode", "loginId": "", "verificationUrl": "https://auth.openai.com/codex/device", "userCode": "ABCD-1234" } } + ``` +2. Show `verificationUrl` and `userCode` to the user; the frontend owns the UX. +3. Wait for notifications: + ```json + { "method": "account/login/completed", "params": { "loginId": "", "success": true, "error": null } } + { "method": "account/updated", "params": { "authMode": "chatgpt", "planType": "plus" } } + ``` + +### 5) Cancel a ChatGPT login ```json -{ "method": "account/login/cancel", "id": 4, "params": { "loginId": "" } } +{ "method": "account/login/cancel", "id": 5, "params": { "loginId": "" } } { "method": "account/login/completed", "params": { "loginId": "", "success": false, "error": "…" } } ``` -### 5) Logout +### 6) Logout ```json -{ "method": "account/logout", "id": 5 } -{ "id": 5, "result": {} } +{ "method": "account/logout", "id": 6 } +{ "id": 6, "result": {} } { "method": "account/updated", "params": { "authMode": null, "planType": null } } ``` -### 6) Rate limits (ChatGPT) +### 7) Rate limits (ChatGPT) ```json -{ "method": "account/rateLimits/read", "id": 6 } -{ "id": 6, "result": { "rateLimits": { "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null } } } +{ "method": "account/rateLimits/read", "id": 7 } +{ "id": 7, "result": { "rateLimits": { "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null } } } { "method": "account/rateLimits/updated", "params": { "rateLimits": { … } } } ``` diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 875eb2b87f..f7e2916747 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -249,6 +249,8 @@ use codex_git_utils::git_diff_to_remote; use codex_login::ServerOptions as LoginServerOptions; use codex_login::ShutdownHandle; use codex_login::auth::login_with_chatgpt_auth_tokens; +use codex_login::complete_device_code_login; +use codex_login::request_device_code; use codex_login::run_login_server; use codex_protocol::ThreadId; use codex_protocol::config_types::CollaborationMode; @@ -339,12 +341,39 @@ struct ThreadListFilters { search_term: Option, } -// Duration before a ChatGPT login attempt is abandoned. +// Duration before a browser ChatGPT login attempt is abandoned. const LOGIN_CHATGPT_TIMEOUT: Duration = Duration::from_secs(10 * 60); +const LOGIN_ISSUER_OVERRIDE_ENV_VAR: &str = "CODEX_APP_SERVER_LOGIN_ISSUER"; const APP_LIST_LOAD_TIMEOUT: Duration = Duration::from_secs(90); -struct ActiveLogin { - shutdown_handle: ShutdownHandle, - login_id: Uuid, + +enum ActiveLogin { + Browser { + shutdown_handle: ShutdownHandle, + login_id: Uuid, + }, + DeviceCode { + cancel: CancellationToken, + login_id: Uuid, + }, +} + +impl ActiveLogin { + fn login_id(&self) -> Uuid { + match self { + ActiveLogin::Browser { login_id, .. } | ActiveLogin::DeviceCode { login_id, .. } => { + *login_id + } + } + } + + fn cancel(&self) { + match self { + ActiveLogin::Browser { + shutdown_handle, .. + } => shutdown_handle.shutdown(), + ActiveLogin::DeviceCode { cancel, .. } => cancel.cancel(), + } + } } #[derive(Clone, Copy, Debug)] @@ -365,7 +394,7 @@ enum ThreadShutdownResult { impl Drop for ActiveLogin { fn drop(&mut self) { - self.shutdown_handle.shutdown(); + self.cancel(); } } @@ -954,6 +983,9 @@ impl CodexMessageProcessor { LoginAccountParams::Chatgpt => { self.login_chatgpt_v2(request_id).await; } + LoginAccountParams::ChatgptDeviceCode => { + self.login_chatgpt_device_code_v2(request_id).await; + } LoginAccountParams::ChatgptAuthTokens { access_token, chatgpt_account_id, @@ -1074,7 +1106,7 @@ impl CodexMessageProcessor { }); } - Ok(LoginServerOptions { + let mut opts = LoginServerOptions { open_browser: false, ..LoginServerOptions::new( config.codex_home.clone(), @@ -1082,7 +1114,32 @@ impl CodexMessageProcessor { config.forced_chatgpt_workspace_id.clone(), config.cli_auth_credentials_store_mode, ) - }) + }; + #[cfg(debug_assertions)] + if let Ok(issuer) = std::env::var(LOGIN_ISSUER_OVERRIDE_ENV_VAR) + && !issuer.trim().is_empty() + { + opts.issuer = issuer; + } + + Ok(opts) + } + + fn login_chatgpt_device_code_start_error(err: IoError) -> JSONRPCErrorError { + let is_not_found = err.kind() == std::io::ErrorKind::NotFound; + JSONRPCErrorError { + code: if is_not_found { + INVALID_REQUEST_ERROR_CODE + } else { + INTERNAL_ERROR_CODE + }, + message: if is_not_found { + err.to_string() + } else { + format!("failed to request device code: {err}") + }, + data: None, + } } async fn login_chatgpt_v2(&mut self, request_id: ConnectionRequestId) { @@ -1098,7 +1155,7 @@ impl CodexMessageProcessor { if let Some(existing) = guard.take() { drop(existing); } - *guard = Some(ActiveLogin { + *guard = Some(ActiveLogin::Browser { shutdown_handle: shutdown_handle.clone(), login_id, }); @@ -1168,7 +1225,7 @@ impl CodexMessageProcessor { // Clear the active login if it matches this attempt. It may have been replaced or cancelled. let mut guard = active_login.lock().await; - if guard.as_ref().map(|l| l.login_id) == Some(login_id) { + if guard.as_ref().map(ActiveLogin::login_id) == Some(login_id) { *guard = None; } }); @@ -1194,12 +1251,114 @@ impl CodexMessageProcessor { } } + async fn login_chatgpt_device_code_v2(&mut self, request_id: ConnectionRequestId) { + match self.login_chatgpt_common().await { + Ok(opts) => match request_device_code(&opts).await { + Ok(device_code) => { + let login_id = Uuid::new_v4(); + let cancel = CancellationToken::new(); + + { + let mut guard = self.active_login.lock().await; + if let Some(existing) = guard.take() { + drop(existing); + } + *guard = Some(ActiveLogin::DeviceCode { + cancel: cancel.clone(), + login_id, + }); + } + + let verification_url = device_code.verification_url.clone(); + let user_code = device_code.user_code.clone(); + let response = + codex_app_server_protocol::LoginAccountResponse::ChatgptDeviceCode { + login_id: login_id.to_string(), + verification_url, + user_code, + }; + self.outgoing.send_response(request_id, response).await; + + let outgoing_clone = self.outgoing.clone(); + let active_login = self.active_login.clone(); + let auth_manager = self.auth_manager.clone(); + let cloud_requirements = self.cloud_requirements.clone(); + let chatgpt_base_url = self.config.chatgpt_base_url.clone(); + let codex_home = self.config.codex_home.clone(); + let cli_overrides = self.current_cli_overrides(); + tokio::spawn(async move { + let (success, error_msg) = tokio::select! { + _ = cancel.cancelled() => { + (false, Some("Login was not completed".to_string())) + } + r = complete_device_code_login(opts, device_code) => { + match r { + Ok(()) => (true, None), + Err(err) => (false, Some(err.to_string())), + } + } + }; + + let payload_v2 = AccountLoginCompletedNotification { + login_id: Some(login_id.to_string()), + success, + error: error_msg, + }; + outgoing_clone + .send_server_notification(ServerNotification::AccountLoginCompleted( + payload_v2, + )) + .await; + + if success { + auth_manager.reload(); + replace_cloud_requirements_loader( + cloud_requirements.as_ref(), + auth_manager.clone(), + chatgpt_base_url, + codex_home, + ); + sync_default_client_residency_requirement( + &cli_overrides, + cloud_requirements.as_ref(), + ) + .await; + + let auth = auth_manager.auth_cached(); + let payload_v2 = AccountUpdatedNotification { + auth_mode: auth.as_ref().map(CodexAuth::api_auth_mode), + plan_type: auth.as_ref().and_then(CodexAuth::account_plan_type), + }; + outgoing_clone + .send_server_notification(ServerNotification::AccountUpdated( + payload_v2, + )) + .await; + } + + let mut guard = active_login.lock().await; + if guard.as_ref().map(ActiveLogin::login_id) == Some(login_id) { + *guard = None; + } + }); + } + Err(err) => { + let error = Self::login_chatgpt_device_code_start_error(err); + self.outgoing.send_error(request_id, error).await; + } + }, + Err(err) => { + self.outgoing.send_error(request_id, err).await; + } + } + } + async fn cancel_login_chatgpt_common( &mut self, login_id: Uuid, ) -> std::result::Result<(), CancelLoginError> { let mut guard = self.active_login.lock().await; - if guard.as_ref().map(|l| l.login_id) == Some(login_id) { + if guard.as_ref().map(ActiveLogin::login_id) == Some(login_id) { if let Some(active) = guard.take() { drop(active); } diff --git a/codex-rs/app-server/tests/common/mcp_process.rs b/codex-rs/app-server/tests/common/mcp_process.rs index fe21cdafff..0345028d88 100644 --- a/codex-rs/app-server/tests/common/mcp_process.rs +++ b/codex-rs/app-server/tests/common/mcp_process.rs @@ -840,6 +840,14 @@ impl McpProcess { self.send_request("account/login/start", Some(params)).await } + /// Send an `account/login/start` JSON-RPC request for ChatGPT device code login. + pub async fn send_login_account_chatgpt_device_code_request(&mut self) -> anyhow::Result { + let params = serde_json::json!({ + "type": "chatgptDeviceCode" + }); + self.send_request("account/login/start", Some(params)).await + } + /// Send an `account/login/cancel` JSON-RPC request. pub async fn send_cancel_login_account_request( &mut self, diff --git a/codex-rs/app-server/tests/suite/v2/account.rs b/codex-rs/app-server/tests/suite/v2/account.rs index 514a6542a0..9458ae7f21 100644 --- a/codex-rs/app-server/tests/suite/v2/account.rs +++ b/codex-rs/app-server/tests/suite/v2/account.rs @@ -39,10 +39,14 @@ use std::path::Path; use std::time::Duration; use tempfile::TempDir; use tokio::time::timeout; +use wiremock::Mock; use wiremock::MockServer; use wiremock::ResponseTemplate; +use wiremock::matchers::method; +use wiremock::matchers::path; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); +const LOGIN_ISSUER_ENV_VAR: &str = "CODEX_APP_SERVER_LOGIN_ISSUER"; // Helper to create a minimal config.toml for the app server #[derive(Default)] @@ -98,6 +102,58 @@ stream_max_retries = 0 std::fs::write(config_toml, contents) } +async fn mock_device_code_usercode(server: &MockServer, interval_seconds: u64) { + Mock::given(method("POST")) + .and(path("/api/accounts/deviceauth/usercode")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "device_auth_id": "device-auth-123", + "user_code": "CODE-12345", + "interval": interval_seconds.to_string(), + }))) + .mount(server) + .await; +} + +async fn mock_device_code_usercode_failure(server: &MockServer, status: u16) { + Mock::given(method("POST")) + .and(path("/api/accounts/deviceauth/usercode")) + .respond_with(ResponseTemplate::new(status)) + .mount(server) + .await; +} + +async fn mock_device_code_token_success(server: &MockServer) { + Mock::given(method("POST")) + .and(path("/api/accounts/deviceauth/token")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "authorization_code": "poll-code-321", + "code_challenge": "code-challenge-321", + "code_verifier": "code-verifier-321", + }))) + .mount(server) + .await; +} + +async fn mock_device_code_token_failure(server: &MockServer, status: u16) { + Mock::given(method("POST")) + .and(path("/api/accounts/deviceauth/token")) + .respond_with(ResponseTemplate::new(status)) + .mount(server) + .await; +} + +async fn mock_device_code_oauth_token(server: &MockServer, id_token: &str) { + Mock::given(method("POST")) + .and(path("/oauth/token")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "id_token": id_token, + "access_token": "access-token-123", + "refresh_token": "refresh-token-123", + }))) + .mount(server) + .await; +} + #[tokio::test] async fn logout_account_removes_auth_and_notifies() -> Result<()> { let codex_home = TempDir::new()?; @@ -912,6 +968,305 @@ async fn login_account_chatgpt_rejected_when_forced_api() -> Result<()> { Ok(()) } +#[tokio::test] +async fn login_account_chatgpt_device_code_returns_error_when_disabled() -> Result<()> { + let codex_home = TempDir::new()?; + let mock_server = MockServer::start().await; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + base_url: Some(format!("{}/v1", mock_server.uri())), + ..Default::default() + }, + )?; + write_models_cache(codex_home.path())?; + mock_device_code_usercode_failure(&mock_server, 404).await; + + let issuer = mock_server.uri(); + let mut mcp = McpProcess::new_with_env( + codex_home.path(), + &[ + ("OPENAI_API_KEY", None), + (LOGIN_ISSUER_ENV_VAR, Some(issuer.as_str())), + ], + ) + .await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_device_code_request().await?; + let err: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + assert!( + err.error + .message + .contains("device code login is not enabled"), + "unexpected error: {:?}", + err.error.message + ); + + let maybe_completed = timeout( + Duration::from_millis(500), + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await; + assert!( + maybe_completed.is_err(), + "account/login/completed should not be emitted when device code start fails" + ); + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should not be created when device code start fails" + ); + Ok(()) +} + +#[tokio::test] +async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> { + let codex_home = TempDir::new()?; + let mock_server = MockServer::start().await; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + base_url: Some(format!("{}/v1", mock_server.uri())), + ..Default::default() + }, + )?; + write_models_cache(codex_home.path())?; + + mock_device_code_usercode(&mock_server, 0).await; + mock_device_code_token_success(&mock_server).await; + let id_token = encode_id_token( + &ChatGptIdTokenClaims::new() + .email("device@example.com") + .plan_type("pro") + .chatgpt_account_id("org-device"), + )?; + mock_device_code_oauth_token(&mock_server, &id_token).await; + + let issuer = mock_server.uri(); + let mut mcp = McpProcess::new_with_env( + codex_home.path(), + &[ + ("OPENAI_API_KEY", None), + (LOGIN_ISSUER_ENV_VAR, Some(issuer.as_str())), + ], + ) + .await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_device_code_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let login: LoginAccountResponse = to_response(resp)?; + let LoginAccountResponse::ChatgptDeviceCode { + login_id, + verification_url, + user_code, + } = login + else { + bail!("unexpected login response: {login:?}"); + }; + assert_eq!(verification_url, format!("{issuer}/codex/device")); + assert_eq!(user_code, "CODE-12345"); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountLoginCompleted(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + assert_eq!(payload.login_id, Some(login_id)); + assert_eq!(payload.success, true); + assert_eq!(payload.error, None); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/updated"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountUpdated(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + assert_eq!(payload.auth_mode, Some(AuthMode::Chatgpt)); + assert_eq!(payload.plan_type, Some(AccountPlanType::Pro)); + assert!( + codex_home.path().join("auth.json").exists(), + "auth.json should be created when device code login succeeds" + ); + Ok(()) +} + +#[tokio::test] +async fn login_account_chatgpt_device_code_failure_notifies_without_account_update() -> Result<()> { + let codex_home = TempDir::new()?; + let mock_server = MockServer::start().await; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + base_url: Some(format!("{}/v1", mock_server.uri())), + ..Default::default() + }, + )?; + write_models_cache(codex_home.path())?; + + mock_device_code_usercode(&mock_server, 0).await; + mock_device_code_token_failure(&mock_server, 500).await; + + let issuer = mock_server.uri(); + let mut mcp = McpProcess::new_with_env( + codex_home.path(), + &[ + ("OPENAI_API_KEY", None), + (LOGIN_ISSUER_ENV_VAR, Some(issuer.as_str())), + ], + ) + .await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_device_code_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let login: LoginAccountResponse = to_response(resp)?; + let LoginAccountResponse::ChatgptDeviceCode { login_id, .. } = login else { + bail!("unexpected login response: {login:?}"); + }; + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountLoginCompleted(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + assert_eq!(payload.login_id, Some(login_id)); + assert_eq!(payload.success, false); + assert!( + payload + .error + .as_deref() + .is_some_and(|error| error.contains("device auth failed with status")), + "unexpected error: {:?}", + payload.error + ); + + let maybe_updated = timeout( + Duration::from_millis(500), + mcp.read_stream_until_notification_message("account/updated"), + ) + .await; + assert!( + maybe_updated.is_err(), + "account/updated should not be emitted when device code login fails" + ); + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should not be created when device code login fails" + ); + Ok(()) +} + +#[tokio::test] +async fn login_account_chatgpt_device_code_can_be_cancelled() -> Result<()> { + let codex_home = TempDir::new()?; + let mock_server = MockServer::start().await; + create_config_toml( + codex_home.path(), + CreateConfigTomlParams { + requires_openai_auth: Some(true), + base_url: Some(format!("{}/v1", mock_server.uri())), + ..Default::default() + }, + )?; + write_models_cache(codex_home.path())?; + + mock_device_code_usercode(&mock_server, 1).await; + mock_device_code_token_failure(&mock_server, 404).await; + + let issuer = mock_server.uri(); + let mut mcp = McpProcess::new_with_env( + codex_home.path(), + &[ + ("OPENAI_API_KEY", None), + (LOGIN_ISSUER_ENV_VAR, Some(issuer.as_str())), + ], + ) + .await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp.send_login_account_chatgpt_device_code_request().await?; + let resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + let login: LoginAccountResponse = to_response(resp)?; + let LoginAccountResponse::ChatgptDeviceCode { login_id, .. } = login else { + bail!("unexpected login response: {login:?}"); + }; + + let cancel_id = mcp + .send_cancel_login_account_request(CancelLoginAccountParams { + login_id: login_id.clone(), + }) + .await?; + let cancel_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(cancel_id)), + ) + .await??; + let cancel: CancelLoginAccountResponse = to_response(cancel_resp)?; + assert_eq!(cancel.status, CancelLoginAccountStatus::Canceled); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("account/login/completed"), + ) + .await??; + let parsed: ServerNotification = note.try_into()?; + let ServerNotification::AccountLoginCompleted(payload) = parsed else { + bail!("unexpected notification: {parsed:?}"); + }; + assert_eq!(payload.login_id, Some(login_id)); + assert_eq!(payload.success, false); + assert!( + payload.error.is_some(), + "expected a non-empty error on device code cancel" + ); + + let maybe_updated = timeout( + Duration::from_millis(500), + mcp.read_stream_until_notification_message("account/updated"), + ) + .await; + assert!( + maybe_updated.is_err(), + "account/updated should not be emitted when device code login is cancelled" + ); + assert!( + !codex_home.path().join("auth.json").exists(), + "auth.json should not be created when device code login is cancelled" + ); + Ok(()) +} + #[tokio::test] // Serialize tests that launch the login server since it binds to a fixed port. #[serial(login_port)] From 2e849703cd8a2eaebff094ef2a5108a1393d02ed Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 27 Mar 2026 09:41:47 +0100 Subject: [PATCH 032/191] chore: drop useless stuff (#15876) --- MODULE.bazel.lock | 3 - codex-rs/Cargo.lock | 52 -- codex-rs/Cargo.toml | 6 - codex-rs/package-manager/BUILD.bazel | 6 - codex-rs/package-manager/Cargo.toml | 27 - codex-rs/package-manager/README.md | 64 --- codex-rs/package-manager/src/archive.rs | 270 --------- codex-rs/package-manager/src/config.rs | 40 -- codex-rs/package-manager/src/error.rs | 54 -- codex-rs/package-manager/src/lib.rs | 17 - codex-rs/package-manager/src/manager.rs | 464 --------------- codex-rs/package-manager/src/package.rs | 69 --- codex-rs/package-manager/src/platform.rs | 48 -- codex-rs/package-manager/src/tests.rs | 700 ----------------------- 14 files changed, 1820 deletions(-) delete mode 100644 codex-rs/package-manager/BUILD.bazel delete mode 100644 codex-rs/package-manager/Cargo.toml delete mode 100644 codex-rs/package-manager/README.md delete mode 100644 codex-rs/package-manager/src/archive.rs delete mode 100644 codex-rs/package-manager/src/config.rs delete mode 100644 codex-rs/package-manager/src/error.rs delete mode 100644 codex-rs/package-manager/src/lib.rs delete mode 100644 codex-rs/package-manager/src/manager.rs delete mode 100644 codex-rs/package-manager/src/package.rs delete mode 100644 codex-rs/package-manager/src/platform.rs delete mode 100644 codex-rs/package-manager/src/tests.rs diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 7b3454a508..2333f48504 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -824,7 +824,6 @@ "fdeflate_0.3.7": "{\"dependencies\":[{\"kind\":\"dev\",\"name\":\"miniz_oxide\",\"req\":\"^0.7.1\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8.5\"},{\"name\":\"simd-adler32\",\"req\":\"^0.3.4\"}],\"features\":{}}", "fiat-crypto_0.2.9": "{\"dependencies\":[],\"features\":{\"default\":[\"std\"],\"std\":[]}}", "filedescriptor_0.8.3": "{\"dependencies\":[{\"name\":\"libc\",\"req\":\"^0.2\"},{\"name\":\"thiserror\",\"req\":\"^1.0\"},{\"features\":[\"winuser\",\"handleapi\",\"fileapi\",\"namedpipeapi\",\"processthreadsapi\",\"winsock2\",\"processenv\"],\"name\":\"winapi\",\"req\":\"^0.3\",\"target\":\"cfg(windows)\"}],\"features\":{}}", - "filetime_0.2.27": "{\"dependencies\":[{\"name\":\"cfg-if\",\"req\":\"^1.0.0\"},{\"name\":\"libc\",\"req\":\"^0.2.27\",\"target\":\"cfg(unix)\"},{\"name\":\"libredox\",\"req\":\"^0.1.0\",\"target\":\"cfg(target_os = \\\"redox\\\")\"},{\"kind\":\"dev\",\"name\":\"tempfile\",\"req\":\"^3\"}],\"features\":{}}", "find-crate_0.6.3": "{\"dependencies\":[{\"kind\":\"dev\",\"name\":\"proc-macro2\",\"req\":\"^1\"},{\"kind\":\"dev\",\"name\":\"quote\",\"req\":\"^1\"},{\"kind\":\"dev\",\"name\":\"semver\",\"req\":\"^0.11\"},{\"name\":\"toml\",\"req\":\"^0.5.2\"}],\"features\":{}}", "find-msvc-tools_0.1.9": "{\"dependencies\":[],\"features\":{}}", "findshlibs_0.10.2": "{\"dependencies\":[{\"kind\":\"build\",\"name\":\"cc\",\"req\":\"^1.0.67\"},{\"name\":\"lazy_static\",\"req\":\"^1.4\",\"target\":\"cfg(any(target_os = \\\"macos\\\", target_os = \\\"ios\\\"))\"},{\"name\":\"libc\",\"req\":\"^0.2.104\"},{\"features\":[\"psapi\",\"memoryapi\",\"libloaderapi\",\"processthreadsapi\"],\"name\":\"winapi\",\"req\":\"^0.3.9\",\"target\":\"cfg(target_os = \\\"windows\\\")\"}],\"features\":{}}", @@ -1326,7 +1325,6 @@ "system-configuration-sys_0.6.0": "{\"dependencies\":[{\"name\":\"core-foundation-sys\",\"req\":\"^0.8\"},{\"name\":\"libc\",\"req\":\"^0.2.149\"}],\"features\":{}}", "system-configuration_0.6.1": "{\"dependencies\":[{\"name\":\"bitflags\",\"req\":\"^2\"},{\"name\":\"core-foundation\",\"req\":\"^0.9\"},{\"name\":\"system-configuration-sys\",\"req\":\"^0.6\"}],\"features\":{}}", "tagptr_0.2.0": "{\"dependencies\":[],\"features\":{}}", - "tar_0.4.45": "{\"dependencies\":[{\"kind\":\"dev\",\"name\":\"astral-tokio-tar\",\"req\":\"^0.5\"},{\"name\":\"filetime\",\"req\":\"^0.2.8\"},{\"name\":\"libc\",\"req\":\"^0.2\",\"target\":\"cfg(unix)\"},{\"features\":[\"small_rng\"],\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"},{\"kind\":\"dev\",\"name\":\"tempfile\",\"req\":\"^3\"},{\"features\":[\"macros\",\"rt\"],\"kind\":\"dev\",\"name\":\"tokio\",\"req\":\"^1\"},{\"kind\":\"dev\",\"name\":\"tokio-stream\",\"req\":\"^0.1\"},{\"name\":\"xattr\",\"optional\":true,\"req\":\"^1.1.3\",\"target\":\"cfg(unix)\"}],\"features\":{\"default\":[\"xattr\"]}}", "tempfile_3.24.0": "{\"dependencies\":[{\"kind\":\"dev\",\"name\":\"doc-comment\",\"req\":\"^0.3\"},{\"name\":\"fastrand\",\"req\":\"^2.1.1\"},{\"default_features\":false,\"name\":\"getrandom\",\"optional\":true,\"req\":\"^0.3.0\",\"target\":\"cfg(any(unix, windows, target_os = \\\"wasi\\\"))\"},{\"default_features\":false,\"features\":[\"std\"],\"name\":\"once_cell\",\"req\":\"^1.19.0\"},{\"features\":[\"fs\"],\"name\":\"rustix\",\"req\":\"^1.1.3\",\"target\":\"cfg(any(unix, target_os = \\\"wasi\\\"))\"},{\"features\":[\"Win32_Storage_FileSystem\",\"Win32_Foundation\"],\"name\":\"windows-sys\",\"req\":\">=0.52, <0.62\",\"target\":\"cfg(windows)\"}],\"features\":{\"default\":[\"getrandom\"],\"nightly\":[]}}", "temporal_capi_0.1.2": "{\"dependencies\":[{\"default_features\":false,\"name\":\"diplomat\",\"req\":\"^0.14.0\"},{\"default_features\":false,\"name\":\"diplomat-runtime\",\"req\":\"^0.14.0\"},{\"default_features\":false,\"features\":[\"unstable\"],\"name\":\"icu_calendar\",\"req\":\"^2.1.0\"},{\"name\":\"icu_locale\",\"req\":\"^2.1.0\"},{\"default_features\":false,\"name\":\"num-traits\",\"req\":\"^0.2.19\"},{\"default_features\":false,\"name\":\"temporal_rs\",\"req\":\"^0.1.2\"},{\"name\":\"timezone_provider\",\"req\":\"^0.1.2\"},{\"name\":\"writeable\",\"req\":\"^0.6.0\"},{\"name\":\"zoneinfo64\",\"optional\":true,\"req\":\"^0.2.0\"}],\"features\":{\"compiled_data\":[\"temporal_rs/compiled_data\"],\"zoneinfo64\":[\"dep:zoneinfo64\",\"timezone_provider/zoneinfo64\"]}}", "temporal_rs_0.1.2": "{\"dependencies\":[{\"name\":\"core_maths\",\"req\":\"^0.1.1\"},{\"name\":\"iana-time-zone\",\"optional\":true,\"req\":\"^0.1.64\"},{\"default_features\":false,\"features\":[\"unstable\",\"compiled_data\"],\"name\":\"icu_calendar\",\"req\":\"^2.1.0\"},{\"name\":\"icu_locale\",\"req\":\"^2.1.0\"},{\"features\":[\"duration\"],\"name\":\"ixdtf\",\"req\":\"^0.6.4\"},{\"name\":\"log\",\"optional\":true,\"req\":\"^0.4.28\"},{\"default_features\":false,\"name\":\"num-traits\",\"req\":\"^0.2.19\"},{\"name\":\"timezone_provider\",\"req\":\"^0.1.2\"},{\"name\":\"tinystr\",\"req\":\"^0.8.0\"},{\"name\":\"web-time\",\"optional\":true,\"req\":\"^1.1.0\"},{\"name\":\"writeable\",\"req\":\"^0.6.0\"}],\"features\":{\"compiled_data\":[\"tzdb\"],\"default\":[\"sys\"],\"float64_representable_durations\":[],\"log\":[\"dep:log\"],\"std\":[],\"sys\":[\"std\",\"compiled_data\",\"dep:web-time\",\"dep:iana-time-zone\"],\"tzdb\":[\"std\",\"timezone_provider/tzif\"]}}", @@ -1542,7 +1540,6 @@ "x11rb_0.13.2": "{\"dependencies\":[{\"name\":\"as-raw-xcb-connection\",\"optional\":true,\"req\":\"^1.0\"},{\"name\":\"gethostname\",\"req\":\"^1.0\"},{\"name\":\"libc\",\"optional\":true,\"req\":\"^0.2\"},{\"name\":\"libloading\",\"optional\":true,\"req\":\"^0.8.0\"},{\"name\":\"once_cell\",\"optional\":true,\"req\":\"^1.19\"},{\"kind\":\"dev\",\"name\":\"polling\",\"req\":\"^3.4\"},{\"name\":\"raw-window-handle\",\"optional\":true,\"req\":\"^0.5.0\"},{\"default_features\":false,\"features\":[\"std\",\"event\",\"fs\",\"net\",\"system\"],\"name\":\"rustix\",\"req\":\"^1.0\"},{\"default_features\":false,\"name\":\"tracing\",\"optional\":true,\"req\":\"^0.1\"},{\"kind\":\"dev\",\"name\":\"tracing-subscriber\",\"req\":\"^0.3\"},{\"default_features\":false,\"features\":[\"std\"],\"name\":\"x11rb-protocol\",\"req\":\"^0.13.2\"},{\"name\":\"xcursor\",\"optional\":true,\"req\":\"^0.3.7\"}],\"features\":{\"all-extensions\":[\"x11rb-protocol/all-extensions\",\"composite\",\"damage\",\"dbe\",\"dpms\",\"dri2\",\"dri3\",\"glx\",\"present\",\"randr\",\"record\",\"render\",\"res\",\"screensaver\",\"shape\",\"shm\",\"sync\",\"xevie\",\"xf86dri\",\"xf86vidmode\",\"xfixes\",\"xinerama\",\"xinput\",\"xkb\",\"xprint\",\"xselinux\",\"xtest\",\"xv\",\"xvmc\"],\"allow-unsafe-code\":[\"libc\",\"as-raw-xcb-connection\"],\"composite\":[\"x11rb-protocol/composite\",\"xfixes\"],\"cursor\":[\"render\",\"resource_manager\",\"xcursor\"],\"damage\":[\"x11rb-protocol/damage\",\"xfixes\"],\"dbe\":[\"x11rb-protocol/dbe\"],\"dl-libxcb\":[\"allow-unsafe-code\",\"libloading\",\"once_cell\"],\"dpms\":[\"x11rb-protocol/dpms\"],\"dri2\":[\"x11rb-protocol/dri2\"],\"dri3\":[\"x11rb-protocol/dri3\"],\"extra-traits\":[\"x11rb-protocol/extra-traits\"],\"glx\":[\"x11rb-protocol/glx\"],\"image\":[],\"present\":[\"x11rb-protocol/present\",\"randr\",\"xfixes\",\"sync\"],\"randr\":[\"x11rb-protocol/randr\",\"render\"],\"record\":[\"x11rb-protocol/record\"],\"render\":[\"x11rb-protocol/render\"],\"request-parsing\":[\"x11rb-protocol/request-parsing\"],\"res\":[\"x11rb-protocol/res\"],\"resource_manager\":[\"x11rb-protocol/resource_manager\"],\"screensaver\":[\"x11rb-protocol/screensaver\"],\"shape\":[\"x11rb-protocol/shape\"],\"shm\":[\"x11rb-protocol/shm\"],\"sync\":[\"x11rb-protocol/sync\"],\"xevie\":[\"x11rb-protocol/xevie\"],\"xf86dri\":[\"x11rb-protocol/xf86dri\"],\"xf86vidmode\":[\"x11rb-protocol/xf86vidmode\"],\"xfixes\":[\"x11rb-protocol/xfixes\",\"render\",\"shape\"],\"xinerama\":[\"x11rb-protocol/xinerama\"],\"xinput\":[\"x11rb-protocol/xinput\",\"xfixes\"],\"xkb\":[\"x11rb-protocol/xkb\"],\"xprint\":[\"x11rb-protocol/xprint\"],\"xselinux\":[\"x11rb-protocol/xselinux\"],\"xtest\":[\"x11rb-protocol/xtest\"],\"xv\":[\"x11rb-protocol/xv\",\"shm\"],\"xvmc\":[\"x11rb-protocol/xvmc\",\"xv\"]}}", "x25519-dalek_2.0.1": "{\"dependencies\":[{\"kind\":\"dev\",\"name\":\"bincode\",\"req\":\"^1\"},{\"kind\":\"dev\",\"name\":\"criterion\",\"req\":\"^0.5\"},{\"default_features\":false,\"name\":\"curve25519-dalek\",\"req\":\"^4\"},{\"default_features\":false,\"name\":\"rand_core\",\"req\":\"^0.6\"},{\"default_features\":false,\"features\":[\"getrandom\"],\"kind\":\"dev\",\"name\":\"rand_core\",\"req\":\"^0.6\"},{\"default_features\":false,\"features\":[\"derive\"],\"name\":\"serde\",\"optional\":true,\"req\":\"^1\"},{\"default_features\":false,\"features\":[\"zeroize_derive\"],\"name\":\"zeroize\",\"optional\":true,\"req\":\"^1\"}],\"features\":{\"alloc\":[\"curve25519-dalek/alloc\",\"serde?/alloc\",\"zeroize?/alloc\"],\"default\":[\"alloc\",\"precomputed-tables\",\"zeroize\"],\"getrandom\":[\"rand_core/getrandom\"],\"precomputed-tables\":[\"curve25519-dalek/precomputed-tables\"],\"reusable_secrets\":[],\"serde\":[\"dep:serde\",\"curve25519-dalek/serde\"],\"static_secrets\":[],\"zeroize\":[\"dep:zeroize\",\"curve25519-dalek/zeroize\"]}}", "x509-parser_0.18.1": "{\"dependencies\":[{\"features\":[\"datetime\"],\"name\":\"asn1-rs\",\"req\":\"^0.7.0\"},{\"name\":\"aws-lc-rs\",\"optional\":true,\"req\":\"^1.0\"},{\"name\":\"data-encoding\",\"req\":\"^2.2.1\"},{\"features\":[\"bigint\"],\"name\":\"der-parser\",\"req\":\"^10.0\"},{\"name\":\"lazy_static\",\"req\":\"^1.4\"},{\"name\":\"nom\",\"req\":\"^7.0\"},{\"features\":[\"crypto\",\"x509\",\"x962\"],\"name\":\"oid-registry\",\"req\":\"^0.8.1\"},{\"name\":\"ring\",\"optional\":true,\"req\":\"^0.17.12\"},{\"name\":\"rusticata-macros\",\"req\":\"^4.0\"},{\"name\":\"thiserror\",\"req\":\"^2.0\"},{\"features\":[\"formatting\"],\"name\":\"time\",\"req\":\"^0.3.35\"}],\"features\":{\"default\":[],\"validate\":[],\"verify\":[\"ring\"],\"verify-aws\":[\"aws-lc-rs\"]}}", - "xattr_1.6.1": "{\"dependencies\":[{\"name\":\"libc\",\"req\":\"^0.2.150\",\"target\":\"cfg(any(target_os = \\\"freebsd\\\", target_os = \\\"netbsd\\\"))\"},{\"default_features\":false,\"features\":[\"fs\",\"std\"],\"name\":\"rustix\",\"req\":\"^1.0.0\",\"target\":\"cfg(any(target_os = \\\"android\\\", target_os = \\\"linux\\\", target_os = \\\"macos\\\", target_os = \\\"hurd\\\"))\"},{\"kind\":\"dev\",\"name\":\"tempfile\",\"req\":\"^3\"}],\"features\":{\"default\":[\"unsupported\"],\"unsupported\":[]}}", "xdg-home_1.3.0": "{\"dependencies\":[{\"name\":\"libc\",\"req\":\"^0.2\",\"target\":\"cfg(unix)\"},{\"features\":[\"Win32_Foundation\",\"Win32_UI_Shell\",\"Win32_System_Com\"],\"name\":\"windows-sys\",\"req\":\"^0.59\",\"target\":\"cfg(windows)\"}],\"features\":{}}", "xz2_0.1.7": "{\"dependencies\":[{\"name\":\"futures\",\"optional\":true,\"req\":\"^0.1.26\"},{\"name\":\"lzma-sys\",\"req\":\"^0.1.18\"},{\"kind\":\"dev\",\"name\":\"quickcheck\",\"req\":\"^1.0.1\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8.0\"},{\"kind\":\"dev\",\"name\":\"tokio-core\",\"req\":\"^0.1.17\"},{\"name\":\"tokio-io\",\"optional\":true,\"req\":\"^0.1.12\"}],\"features\":{\"static\":[\"lzma-sys/static\"],\"tokio\":[\"tokio-io\",\"futures\"]}}", "yaml-rust_0.4.5": "{\"dependencies\":[{\"name\":\"linked-hash-map\",\"req\":\"^0.5.3\"},{\"kind\":\"dev\",\"name\":\"quickcheck\",\"req\":\"^0.9\"}],\"features\":{}}", diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index c5846fbe19..17d5c35e83 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2358,26 +2358,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "codex-package-manager" -version = "0.0.0" -dependencies = [ - "fd-lock", - "flate2", - "pretty_assertions", - "reqwest", - "serde", - "serde_json", - "sha2", - "tar", - "tempfile", - "thiserror 2.0.18", - "tokio", - "url", - "wiremock", - "zip", -] - [[package]] name = "codex-plugin" version = "0.0.0" @@ -4288,17 +4268,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "filetime" -version = "0.2.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98844151eee8917efc50bd9e8318cb963ae8b297431495d3f758616ea5c57db" -dependencies = [ - "cfg-if", - "libc", - "libredox", -] - [[package]] name = "find-crate" version = "0.6.3" @@ -9873,17 +9842,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" -[[package]] -name = "tar" -version = "0.4.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22692a6476a21fa75fdfc11d452fda482af402c008cdbaf3476414e122040973" -dependencies = [ - "filetime", - "libc", - "xattr", -] - [[package]] name = "tempfile" version = "3.24.0" @@ -12010,16 +11968,6 @@ dependencies = [ "time", ] -[[package]] -name = "xattr" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156" -dependencies = [ - "libc", - "rustix 1.1.3", -] - [[package]] name = "xdg-home" version = "1.3.0" diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index f05355e393..d844290146 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -81,7 +81,6 @@ members = [ "state", "terminal-detection", "codex-experimental-api-macros", - "package-manager", "plugin", ] resolver = "2" @@ -102,7 +101,6 @@ codex-ansi-escape = { path = "ansi-escape" } codex-analytics = { path = "analytics" } codex-api = { path = "codex-api" } codex-code-mode = { path = "code-mode" } -codex-package-manager = { path = "package-manager" } codex-app-server = { path = "app-server" } codex-app-server-client = { path = "app-server-client" } codex-app-server-protocol = { path = "app-server-protocol" } @@ -213,11 +211,9 @@ dirs = "6" dotenvy = "0.15.7" dunce = "1.0.4" encoding_rs = "0.8.35" -fd-lock = "4.0.4" env-flags = "0.1.1" env_logger = "0.11.9" eventsource-stream = "0.2.3" -flate2 = "1.1.4" futures = { version = "0.3", default-features = false } gethostname = "1.1.0" globset = "0.4" @@ -309,7 +305,6 @@ supports-color = "3.0.2" syntect = "5" sys-locale = "0.3.2" tempfile = "3.23.0" -tar = "0.4.45" test-log = "0.2.19" textwrap = "0.16.2" thiserror = "2.0.17" @@ -395,7 +390,6 @@ unwrap_used = "deny" ignored = [ "icu_provider", "openssl-sys", - "codex-package-manager", "codex-utils-readiness", "codex-utils-template", "codex-v8-poc", diff --git a/codex-rs/package-manager/BUILD.bazel b/codex-rs/package-manager/BUILD.bazel deleted file mode 100644 index bbbc725cbf..0000000000 --- a/codex-rs/package-manager/BUILD.bazel +++ /dev/null @@ -1,6 +0,0 @@ -load("//:defs.bzl", "codex_rust_crate") - -codex_rust_crate( - name = "package-manager", - crate_name = "codex_package_manager", -) diff --git a/codex-rs/package-manager/Cargo.toml b/codex-rs/package-manager/Cargo.toml deleted file mode 100644 index 3a138a80be..0000000000 --- a/codex-rs/package-manager/Cargo.toml +++ /dev/null @@ -1,27 +0,0 @@ -[package] -name = "codex-package-manager" -version.workspace = true -edition.workspace = true -license.workspace = true - -[dependencies] -fd-lock = { workspace = true } -flate2 = { workspace = true } -reqwest = { workspace = true, features = ["json", "stream"] } -serde = { workspace = true, features = ["derive"] } -sha2 = { workspace = true } -tar = { workspace = true } -tempfile = { workspace = true } -thiserror = { workspace = true } -tokio = { workspace = true, features = ["fs", "rt", "sync", "time"] } -url = { workspace = true } -zip = { workspace = true } - -[lints] -workspace = true - -[dev-dependencies] -pretty_assertions = { workspace = true } -serde_json = { workspace = true } -tokio = { workspace = true, features = ["fs", "macros", "rt", "rt-multi-thread"] } -wiremock = { workspace = true } diff --git a/codex-rs/package-manager/README.md b/codex-rs/package-manager/README.md deleted file mode 100644 index c761bcdd65..0000000000 --- a/codex-rs/package-manager/README.md +++ /dev/null @@ -1,64 +0,0 @@ -# codex-package-manager - -`codex-package-manager` is the shared installer used for versioned runtime bundles and other cached artifacts in `codex-rs`. - -It owns the generic parts of package installation: - -- current-platform detection -- manifest and archive fetches -- checksum and archive-size validation -- archive extraction for `.zip` and `.tar.gz` -- staging and promotion into a versioned cache directory -- cross-process install locking - -Package-specific code stays behind the `ManagedPackage` trait. - -## Model - -The package manager is intentionally small: - -1. A `ManagedPackage` implementation describes how to fetch a manifest, choose an archive for a `PackagePlatform`, and load a validated installed package from disk. -2. `PackageManager::resolve_cached()` returns a cached install for the current platform if `load_installed()` succeeds and the version matches. -3. `PackageManager::ensure_installed()` acquires a per-install lock, downloads the archive into a staging directory, extracts it, validates the staged package, and promotes it into the cache. - -The default cache root is: - -```text -/ -``` - -Callers can override that root with `PackageManagerConfig::with_cache_root(...)`. - -## ManagedPackage Contract - -The trait is small, but the invariants matter: - -- `install_dir()` should be unique per package version and platform. If two versions or two platforms share a directory, promotion and cleanup become unsafe. -- `load_installed()` must fully validate the installed package, not just deserialize a manifest. `resolve_cached()` trusts a successful load as a valid cache hit. -- The default `detect_extracted_root()` looks for `manifest.json` at the extraction root or inside a single top-level directory. Override it if your package layout differs. -- `archive_url()` should be derived from manifest data, not recomputed from unrelated caller state, so manifest selection and download stay aligned. - -## Consumer Guidance - -- If your feature can install on demand, do not gate feature registration on a preinstalled-cache check alone. `resolve_cached()` only answers "is it already present?" while `ensure_installed()` is the bootstrap path. -- Keep cache-root overrides inside your manager/config surface. Separate helpers that reconstruct install paths can drift from `PackageManagerConfig`. -- Prefer surfacing package-specific validation failures from `load_installed()` when debugging. The generic manager treats failed cache loads as cache misses today. - -## Security and Extraction Rules - -- `.zip` extraction rejects entries that escape the extraction root and preserves Unix executable bits when the archive carries them. -- `.tar.gz` extraction rejects symlinks, hard links, sparse files, device files, and FIFOs. Only regular files and directories are promoted. -- The archive SHA-256 is always verified, and `size_bytes` is enforced when present in the manifest. - -## Extending It - -Typical usage looks like this: - -```rust,ignore -let config = PackageManagerConfig::new(codex_home, MyPackage::new(...)); -let manager = PackageManager::new(config); - -let package = manager.ensure_installed().await?; -``` - -In practice, most packages should expose their own small wrapper config/manager types over the generic crate so the rest of the codebase does not depend on `ManagedPackage` details directly. diff --git a/codex-rs/package-manager/src/archive.rs b/codex-rs/package-manager/src/archive.rs deleted file mode 100644 index 80de96c84d..0000000000 --- a/codex-rs/package-manager/src/archive.rs +++ /dev/null @@ -1,270 +0,0 @@ -use crate::PackageManagerError; -use flate2::read::GzDecoder; -use sha2::Digest; -use sha2::Sha256; -use std::fs::File; -#[cfg(unix)] -use std::os::unix::fs::PermissionsExt; -use std::path::Component; -use std::path::Path; -use std::path::PathBuf; -use tar::Archive; -use zip::ZipArchive; - -/// Archive metadata for a platform entry in a release manifest. -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)] -pub struct PackageReleaseArchive { - /// Archive file name relative to the package release location. - pub archive: String, - /// Expected SHA-256 of the downloaded archive body. - pub sha256: String, - /// Archive format used by the download. - pub format: ArchiveFormat, - /// Expected archive length in bytes, when the manifest provides it. - pub size_bytes: Option, -} - -/// Archive formats supported by the generic extractor. -#[derive(Clone, Copy, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)] -pub enum ArchiveFormat { - /// A `.zip` archive. - #[serde(rename = "zip")] - Zip, - /// A `.tar.gz` archive. - #[serde(rename = "tar.gz")] - TarGz, -} - -/// Detects a package root with a `manifest.json` in an extraction directory. -pub(crate) fn detect_single_package_root( - extraction_root: &Path, -) -> Result { - let direct_manifest = extraction_root.join("manifest.json"); - if direct_manifest.exists() { - return Ok(extraction_root.to_path_buf()); - } - - let mut directory_candidates = Vec::new(); - for entry in std::fs::read_dir(extraction_root).map_err(|source| PackageManagerError::Io { - context: format!("failed to read {}", extraction_root.display()), - source, - })? { - let entry = entry.map_err(|source| PackageManagerError::Io { - context: format!("failed to read entry in {}", extraction_root.display()), - source, - })?; - let path = entry.path(); - if path.is_dir() { - directory_candidates.push(path); - } - } - - if directory_candidates.len() == 1 { - let candidate = &directory_candidates[0]; - if candidate.join("manifest.json").exists() { - return Ok(candidate.clone()); - } - } - - Err(PackageManagerError::MissingPackageRoot( - extraction_root.to_path_buf(), - )) -} - -pub(crate) fn verify_archive_size( - bytes: &[u8], - expected: Option, -) -> Result<(), PackageManagerError> { - let Some(expected) = expected else { - return Ok(()); - }; - let actual = bytes.len() as u64; - if actual == expected { - return Ok(()); - } - Err(PackageManagerError::UnexpectedArchiveSize { expected, actual }) -} - -pub(crate) fn verify_sha256(bytes: &[u8], expected: &str) -> Result<(), PackageManagerError> { - let actual = format!("{:x}", Sha256::digest(bytes)); - if actual == expected.to_ascii_lowercase() { - return Ok(()); - } - Err(PackageManagerError::ChecksumMismatch { - expected: expected.to_string(), - actual, - }) -} - -pub(crate) fn extract_archive( - archive_path: &Path, - destination: &Path, - format: ArchiveFormat, -) -> Result<(), PackageManagerError> { - match format { - ArchiveFormat::Zip => extract_zip_archive(archive_path, destination), - ArchiveFormat::TarGz => extract_tar_gz_archive(archive_path, destination), - } -} - -fn extract_zip_archive(archive_path: &Path, destination: &Path) -> Result<(), PackageManagerError> { - let file = File::open(archive_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to open {}", archive_path.display()), - source, - })?; - let mut archive = ZipArchive::new(file) - .map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))?; - for index in 0..archive.len() { - let mut entry = archive - .by_index(index) - .map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))?; - let Some(relative_path) = entry.enclosed_name() else { - return Err(PackageManagerError::ArchiveExtraction(format!( - "zip entry `{}` escapes extraction root", - entry.name() - ))); - }; - let output_path = destination.join(relative_path); - if entry.is_dir() { - std::fs::create_dir_all(&output_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", output_path.display()), - source, - })?; - continue; - } - if let Some(parent) = output_path.parent() { - std::fs::create_dir_all(parent).map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", parent.display()), - source, - })?; - } - let mut output = File::create(&output_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", output_path.display()), - source, - })?; - std::io::copy(&mut entry, &mut output).map_err(|source| PackageManagerError::Io { - context: format!("failed to write {}", output_path.display()), - source, - })?; - apply_zip_permissions(&entry, &output_path)?; - } - Ok(()) -} - -#[cfg(unix)] -fn apply_zip_permissions( - entry: &zip::read::ZipFile<'_>, - output_path: &Path, -) -> Result<(), PackageManagerError> { - let Some(mode) = entry.unix_mode() else { - return Ok(()); - }; - std::fs::set_permissions(output_path, std::fs::Permissions::from_mode(mode)).map_err(|source| { - PackageManagerError::Io { - context: format!("failed to set permissions on {}", output_path.display()), - source, - } - }) -} - -#[cfg(not(unix))] -fn apply_zip_permissions( - _entry: &zip::read::ZipFile<'_>, - _output_path: &Path, -) -> Result<(), PackageManagerError> { - Ok(()) -} - -fn extract_tar_gz_archive( - archive_path: &Path, - destination: &Path, -) -> Result<(), PackageManagerError> { - let file = File::open(archive_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to open {}", archive_path.display()), - source, - })?; - let decoder = GzDecoder::new(file); - let mut archive = Archive::new(decoder); - for entry in archive - .entries() - .map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))? - { - let mut entry = - entry.map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))?; - let path = entry - .path() - .map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))?; - let output_path = safe_extract_path(destination, path.as_ref())?; - let entry_type = entry.header().entry_type(); - - if entry_type.is_symlink() - || entry_type.is_hard_link() - || entry_type.is_block_special() - || entry_type.is_character_special() - || entry_type.is_fifo() - || entry_type.is_gnu_sparse() - { - return Err(PackageManagerError::ArchiveExtraction(format!( - "tar entry `{}` has unsupported type", - path.display() - ))); - } - - if entry_type.is_pax_global_extensions() - || entry_type.is_pax_local_extensions() - || entry_type.is_gnu_longname() - || entry_type.is_gnu_longlink() - { - continue; - } - - if entry_type.is_dir() { - std::fs::create_dir_all(&output_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", output_path.display()), - source, - })?; - continue; - } - - if !entry_type.is_file() && !entry_type.is_contiguous() { - return Err(PackageManagerError::ArchiveExtraction(format!( - "tar entry `{}` has unsupported type", - path.display() - ))); - } - - if let Some(parent) = output_path.parent() { - std::fs::create_dir_all(parent).map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", parent.display()), - source, - })?; - } - entry - .unpack(&output_path) - .map_err(|error| PackageManagerError::ArchiveExtraction(error.to_string()))?; - } - Ok(()) -} - -fn safe_extract_path(root: &Path, relative_path: &Path) -> Result { - let mut clean_relative = PathBuf::new(); - for component in relative_path.components() { - match component { - Component::Normal(segment) => clean_relative.push(segment), - Component::CurDir => {} - Component::ParentDir | Component::RootDir | Component::Prefix(_) => { - return Err(PackageManagerError::ArchiveExtraction(format!( - "entry `{}` escapes extraction root", - relative_path.display() - ))); - } - } - } - - if clean_relative.as_os_str().is_empty() { - return Err(PackageManagerError::ArchiveExtraction( - "archive entry had an empty path".to_string(), - )); - } - Ok(root.join(clean_relative)) -} diff --git a/codex-rs/package-manager/src/config.rs b/codex-rs/package-manager/src/config.rs deleted file mode 100644 index 819c5f6ddf..0000000000 --- a/codex-rs/package-manager/src/config.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::ManagedPackage; -use std::path::PathBuf; - -/// Immutable configuration for a [`crate::PackageManager`] instance. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct PackageManagerConfig

{ - pub(crate) codex_home: PathBuf, - pub(crate) package: P, - cache_root: Option, -} - -impl

PackageManagerConfig

{ - /// Creates a config rooted at the provided Codex home directory. - pub fn new(codex_home: PathBuf, package: P) -> Self { - Self { - codex_home, - package, - cache_root: None, - } - } - - /// Overrides the package cache root instead of deriving it from `codex_home`. - pub fn with_cache_root(mut self, cache_root: PathBuf) -> Self { - self.cache_root = Some(cache_root); - self - } -} - -impl PackageManagerConfig

{ - /// Returns the effective cache root for the package. - pub fn cache_root(&self) -> PathBuf { - self.cache_root.clone().unwrap_or_else(|| { - self.codex_home.join( - self.package - .default_cache_root_relative() - .replace('/', std::path::MAIN_SEPARATOR_STR), - ) - }) - } -} diff --git a/codex-rs/package-manager/src/error.rs b/codex-rs/package-manager/src/error.rs deleted file mode 100644 index 13883ff715..0000000000 --- a/codex-rs/package-manager/src/error.rs +++ /dev/null @@ -1,54 +0,0 @@ -use std::path::PathBuf; -use thiserror::Error; - -/// Errors returned by the generic package manager. -#[derive(Debug, Error)] -pub enum PackageManagerError { - /// The current machine OS/architecture pair is not supported by the package. - #[error("unsupported platform: {os}-{arch}")] - UnsupportedPlatform { os: String, arch: String }, - - /// The configured release base URL could not be joined with a package-specific path. - #[error("invalid release base url")] - InvalidBaseUrl(#[source] url::ParseError), - - /// An HTTP request failed while fetching the manifest or archive. - #[error("{context}")] - Http { - context: String, - #[source] - source: reqwest::Error, - }, - - /// A filesystem operation failed while reading, staging, or promoting a package. - #[error("{context}")] - Io { - context: String, - #[source] - source: std::io::Error, - }, - - /// The release manifest did not contain an archive for the current platform. - #[error("missing platform entry `{0}` in release manifest")] - MissingPlatform(String), - - /// The release manifest or installed package reported a different version than requested. - #[error("unexpected package version: expected `{expected}`, got `{actual}`")] - UnexpectedPackageVersion { expected: String, actual: String }, - - /// The downloaded archive length did not match the manifest metadata. - #[error("unexpected archive size: expected `{expected}`, got `{actual}`")] - UnexpectedArchiveSize { expected: u64, actual: u64 }, - - /// The downloaded archive checksum did not match the manifest metadata. - #[error("checksum mismatch: expected `{expected}`, got `{actual}`")] - ChecksumMismatch { expected: String, actual: String }, - - /// Archive extraction failed or the archive contents violated extraction rules. - #[error("archive extraction failed: {0}")] - ArchiveExtraction(String), - - /// The extracted archive layout did not contain a detectable package root. - #[error("archive did not contain a package root with manifest.json under {0}")] - MissingPackageRoot(PathBuf), -} diff --git a/codex-rs/package-manager/src/lib.rs b/codex-rs/package-manager/src/lib.rs deleted file mode 100644 index 5491d28af8..0000000000 --- a/codex-rs/package-manager/src/lib.rs +++ /dev/null @@ -1,17 +0,0 @@ -mod archive; -mod config; -mod error; -mod manager; -mod package; -mod platform; - -#[cfg(test)] -mod tests; - -pub use archive::ArchiveFormat; -pub use archive::PackageReleaseArchive; -pub use config::PackageManagerConfig; -pub use error::PackageManagerError; -pub use manager::PackageManager; -pub use package::ManagedPackage; -pub use platform::PackagePlatform; diff --git a/codex-rs/package-manager/src/manager.rs b/codex-rs/package-manager/src/manager.rs deleted file mode 100644 index 7b9aa193e4..0000000000 --- a/codex-rs/package-manager/src/manager.rs +++ /dev/null @@ -1,464 +0,0 @@ -use crate::ManagedPackage; -use crate::PackageManagerConfig; -use crate::PackageManagerError; -use crate::PackagePlatform; -use crate::archive::extract_archive; -use crate::archive::verify_archive_size; -use crate::archive::verify_sha256; -use fd_lock::RwLock as FileRwLock; -use reqwest::Client; -use std::fs::OpenOptions; -use std::path::Path; -use std::path::PathBuf; -use std::time::Duration; -use tempfile::tempdir_in; -use tokio::fs; -use tokio::time::sleep; -use url::Url; - -const INSTALL_LOCK_POLL_INTERVAL: Duration = Duration::from_millis(50); - -/// Fetches and installs a versioned package into a shared cache directory. -#[derive(Clone, Debug)] -pub struct PackageManager

{ - client: Client, - config: PackageManagerConfig

, -} - -impl

PackageManager

{ - /// Creates a manager with a default `reqwest` client. - pub fn new(config: PackageManagerConfig

) -> Self { - Self { - client: Client::new(), - config, - } - } - - /// Creates a manager with a caller-provided HTTP client. - pub fn with_client(config: PackageManagerConfig

, client: Client) -> Self { - Self { client, config } - } -} - -impl PackageManager

{ - /// Resolves a valid cached install for the current platform, if one exists. - pub async fn resolve_cached(&self) -> Result, P::Error> { - let platform = PackagePlatform::detect_current().map_err(P::Error::from)?; - let install_dir = self - .config - .package - .install_dir(&self.config.cache_root(), platform); - self.resolve_cached_at(platform, install_dir).await - } - - /// Ensures the requested package is installed for the current platform. - pub async fn ensure_installed(&self) -> Result { - // Fast path: most calls should resolve an already validated cache entry - // without touching the network or the install lock. - if let Some(package) = self.resolve_cached().await? { - return Ok(package); - } - - let platform = PackagePlatform::detect_current().map_err(P::Error::from)?; - let cache_root = self.config.cache_root(); - let install_dir = self.config.package.install_dir(&cache_root, platform); - if let Some(package) = self - .resolve_cached_at(platform, install_dir.clone()) - .await? - { - return Ok(package); - } - - if let Some(parent) = install_dir.parent() { - fs::create_dir_all(parent) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", parent.display()), - source, - }) - .map_err(P::Error::from)?; - } - - let lock_path = install_dir.with_extension("lock"); - let lock_file = OpenOptions::new() - .create(true) - .read(true) - .write(true) - .truncate(false) - .open(&lock_path) - .map_err(|source| PackageManagerError::Io { - context: format!("failed to open {}", lock_path.display()), - source, - }) - .map_err(P::Error::from)?; - let mut install_lock = FileRwLock::new(lock_file); - let _install_guard = loop { - match install_lock.try_write() { - Ok(guard) => break guard, - Err(source) if source.kind() == std::io::ErrorKind::WouldBlock => { - sleep(INSTALL_LOCK_POLL_INTERVAL).await; - } - Err(source) => { - return Err(PackageManagerError::Io { - context: format!("failed to lock {}", lock_path.display()), - source, - } - .into()); - } - } - }; - - // Another process may have finished the install while we were waiting - // on the lock, so re-check before doing any download or extraction work. - if let Some(package) = self - .resolve_cached_at(platform, install_dir.clone()) - .await? - { - return Ok(package); - } - - let manifest = self.fetch_release_manifest().await?; - if self.config.package.release_version(&manifest) != self.config.package.version() { - return Err(PackageManagerError::UnexpectedPackageVersion { - expected: self.config.package.version().to_string(), - actual: self.config.package.release_version(&manifest).to_string(), - } - .into()); - } - - fs::create_dir_all(&cache_root) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", cache_root.display()), - source, - }) - .map_err(P::Error::from)?; - let staging_root = cache_root.join(".staging"); - fs::create_dir_all(&staging_root) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", staging_root.display()), - source, - }) - .map_err(P::Error::from)?; - - // Everything below happens in a disposable staging area until the - // extracted package has passed package-specific validation. - let platform_archive = self.config.package.platform_archive(&manifest, platform)?; - let archive_url = self - .config - .package - .archive_url(&platform_archive) - .map_err(P::Error::from)?; - let archive_bytes = self.download_bytes(&archive_url).await?; - verify_archive_size(&archive_bytes, platform_archive.size_bytes).map_err(P::Error::from)?; - verify_sha256(&archive_bytes, &platform_archive.sha256).map_err(P::Error::from)?; - - let staging_dir = tempdir_in(&staging_root) - .map_err(|source| PackageManagerError::Io { - context: format!( - "failed to create staging directory in {}", - staging_root.display() - ), - source, - }) - .map_err(P::Error::from)?; - let archive_path = staging_dir.path().join(&platform_archive.archive); - fs::write(&archive_path, &archive_bytes) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to write {}", archive_path.display()), - source, - }) - .map_err(P::Error::from)?; - let extraction_root = staging_dir.path().join("extract"); - fs::create_dir_all(&extraction_root) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", extraction_root.display()), - source, - }) - .map_err(P::Error::from)?; - - extract_archive(&archive_path, &extraction_root, platform_archive.format) - .map_err(P::Error::from)?; - let extracted_root = self - .config - .package - .detect_extracted_root(&extraction_root)?; - let package = self - .config - .package - .load_installed(extracted_root.clone(), platform)?; - if self.config.package.installed_version(&package) != self.config.package.version() { - return Err(PackageManagerError::UnexpectedPackageVersion { - expected: self.config.package.version().to_string(), - actual: self.config.package.installed_version(&package).to_string(), - } - .into()); - } - - if let Some(parent) = install_dir.parent() { - fs::create_dir_all(parent) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to create {}", parent.display()), - source, - }) - .map_err(P::Error::from)?; - } - - // Promotion is intentionally two-phase: move the old install aside, - // then attempt an atomic rename of the staged tree into place. - // If promotion fails, restore the previous install before returning. - let replaced_install_dir = quarantine_existing_install(&install_dir) - .await - .map_err(P::Error::from)?; - let promotion = promote_staged_install(&extracted_root, &install_dir).await; - if let Err(error) = promotion { - // If another process won the race after we staged our copy, prefer - // the now-installed cache entry and clean up our quarantined copy. - if matches!( - &error, - PackageManagerError::Io { source, .. } - if matches!( - source.kind(), - std::io::ErrorKind::AlreadyExists - | std::io::ErrorKind::DirectoryNotEmpty - ) - ) && let Some(package) = self - .resolve_cached_at(platform, install_dir.clone()) - .await? - { - if let Some(replaced_install_dir) = replaced_install_dir { - let _ = fs::remove_dir_all(replaced_install_dir).await; - } - return Ok(package); - } - - restore_quarantined_install(&install_dir, replaced_install_dir.as_deref(), &error) - .await - .map_err(P::Error::from)?; - return Err(error.into()); - } - - // Validate from the final install path before deleting the quarantined - // previous install. Some packages may only fully validate once the - // promoted tree is in place at its real cache location. - let package = match self - .config - .package - .load_installed(install_dir.clone(), platform) - { - Ok(package) => package, - Err(error) => { - if let Some(replaced_install_dir) = replaced_install_dir.as_deref() { - // Final validation failed after promotion, so discard the - // broken install and restore the last known-good copy. - if fs::try_exists(&install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to read {}", install_dir.display()), - source, - }) - .map_err(P::Error::from)? - { - fs::remove_dir_all(&install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!( - "failed to remove invalid install {} after final validation failed", - install_dir.display() - ), - source, - }) - .map_err(P::Error::from)?; - } - fs::rename(replaced_install_dir, &install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!( - "failed to restore {} from {} after final validation failed", - install_dir.display(), - replaced_install_dir.display() - ), - source, - }) - .map_err(P::Error::from)?; - } - return Err(error); - } - }; - - if let Some(replaced_install_dir) = replaced_install_dir { - let _ = fs::remove_dir_all(replaced_install_dir).await; - } - - Ok(package) - } - - async fn resolve_cached_at( - &self, - platform: PackagePlatform, - install_dir: PathBuf, - ) -> Result, P::Error> { - if !fs::try_exists(&install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to read {}", install_dir.display()), - source, - }) - .map_err(P::Error::from)? - { - return Ok(None); - } - - let package = match self.config.package.load_installed(install_dir, platform) { - Ok(package) => package, - Err(_) => return Ok(None), - }; - if self.config.package.installed_version(&package) != self.config.package.version() { - return Ok(None); - } - Ok(Some(package)) - } - - async fn fetch_release_manifest(&self) -> Result { - let manifest_url = self.config.package.manifest_url().map_err(P::Error::from)?; - let response = self - .client - .get(manifest_url.clone()) - .send() - .await - .map_err(|source| PackageManagerError::Http { - context: format!("failed to fetch {manifest_url}"), - source, - }) - .map_err(P::Error::from)? - .error_for_status() - .map_err(|source| PackageManagerError::Http { - context: format!("manifest request failed for {manifest_url}"), - source, - }) - .map_err(P::Error::from)?; - - response - .json::() - .await - .map_err(|source| PackageManagerError::Http { - context: format!("failed to decode manifest from {manifest_url}"), - source, - }) - .map_err(P::Error::from) - } - - async fn download_bytes(&self, url: &Url) -> Result, P::Error> { - let response = self - .client - .get(url.clone()) - .send() - .await - .map_err(|source| PackageManagerError::Http { - context: format!("failed to download {url}"), - source, - }) - .map_err(P::Error::from)? - .error_for_status() - .map_err(|source| PackageManagerError::Http { - context: format!("archive request failed for {url}"), - source, - }) - .map_err(P::Error::from)?; - let bytes = response - .bytes() - .await - .map_err(|source| PackageManagerError::Http { - context: format!("failed to read response body for {url}"), - source, - }) - .map_err(P::Error::from)?; - Ok(bytes.to_vec()) - } -} - -pub(crate) async fn quarantine_existing_install( - install_dir: &Path, -) -> Result, PackageManagerError> { - if !fs::try_exists(install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!("failed to read {}", install_dir.display()), - source, - })? - { - return Ok(None); - } - - let install_name = install_dir.file_name().ok_or_else(|| { - PackageManagerError::ArchiveExtraction(format!( - "install path `{}` has no terminal component", - install_dir.display() - )) - })?; - let install_name = install_name.to_string_lossy(); - let mut suffix = 0u32; - loop { - let quarantined_path = install_dir.with_file_name(format!( - ".{install_name}.replaced-{}-{suffix}", - std::process::id() - )); - match fs::rename(install_dir, &quarantined_path).await { - Ok(()) => return Ok(Some(quarantined_path)), - Err(source) if source.kind() == std::io::ErrorKind::AlreadyExists => { - suffix += 1; - } - Err(source) => { - return Err(PackageManagerError::Io { - context: format!( - "failed to quarantine {} to {}", - install_dir.display(), - quarantined_path.display() - ), - source, - }); - } - } - } -} - -pub(crate) async fn promote_staged_install( - extracted_root: &Path, - install_dir: &Path, -) -> Result<(), PackageManagerError> { - fs::rename(extracted_root, install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!( - "failed to move {} to {}", - extracted_root.display(), - install_dir.display() - ), - source, - }) -} - -pub(crate) async fn restore_quarantined_install( - install_dir: &Path, - quarantined_install_dir: Option<&Path>, - promotion_error: &PackageManagerError, -) -> Result<(), PackageManagerError> { - let Some(quarantined_install_dir) = quarantined_install_dir else { - return Ok(()); - }; - - fs::rename(quarantined_install_dir, install_dir) - .await - .map_err(|source| PackageManagerError::Io { - context: format!( - "{promotion_error}; failed to restore {} from {}", - install_dir.display(), - quarantined_install_dir.display() - ), - source, - }) -} diff --git a/codex-rs/package-manager/src/package.rs b/codex-rs/package-manager/src/package.rs deleted file mode 100644 index 51be146084..0000000000 --- a/codex-rs/package-manager/src/package.rs +++ /dev/null @@ -1,69 +0,0 @@ -use crate::PackageManagerError; -use crate::PackagePlatform; -use crate::PackageReleaseArchive; -use crate::archive::detect_single_package_root; -use serde::de::DeserializeOwned; -use std::path::Path; -use std::path::PathBuf; -use url::Url; - -/// Describes how a specific package is located, validated, and loaded. -/// -/// Implementations should treat this trait as the package manager contract: -/// -/// - [`Self::install_dir`] should resolve to a directory unique to the package version and -/// platform so concurrent versions never overwrite each other. -/// - [`Self::load_installed`] should fully validate whatever "installed" means for the package, -/// because cache resolution trusts a successful load as a valid install. -/// - The default [`Self::detect_extracted_root`] implementation expects the extracted archive to -/// contain a `manifest.json` at the package root or a single top-level directory that does. -pub trait ManagedPackage: Clone { - /// Error type surfaced by package-specific loading and validation. - type Error: From; - - /// The fully loaded package instance returned to callers. - type Installed: Clone; - - /// The decoded release manifest fetched from the remote source. - type ReleaseManifest: DeserializeOwned; - - /// Returns the default cache root relative to Codex home. - fn default_cache_root_relative(&self) -> &str; - - /// Returns the requested package version. - fn version(&self) -> &str; - - /// Returns the manifest URL for the requested version. - fn manifest_url(&self) -> Result; - - /// Returns the archive download URL for a platform-specific manifest entry. - fn archive_url(&self, archive: &PackageReleaseArchive) -> Result; - - /// Returns the version string stored in the fetched release manifest. - fn release_version<'a>(&self, manifest: &'a Self::ReleaseManifest) -> &'a str; - - /// Selects the archive to download for the current platform. - fn platform_archive( - &self, - manifest: &Self::ReleaseManifest, - platform: PackagePlatform, - ) -> Result; - - /// Returns the final install directory for the package version and platform. - fn install_dir(&self, cache_root: &Path, platform: PackagePlatform) -> PathBuf; - - /// Returns the version string encoded in a fully loaded installed package. - fn installed_version<'a>(&self, package: &'a Self::Installed) -> &'a str; - - /// Loads and validates an installed package from disk. - fn load_installed( - &self, - root_dir: PathBuf, - platform: PackagePlatform, - ) -> Result; - - /// Resolves the extracted package root before the staged install is promoted. - fn detect_extracted_root(&self, extraction_root: &Path) -> Result { - detect_single_package_root(extraction_root).map_err(Self::Error::from) - } -} diff --git a/codex-rs/package-manager/src/platform.rs b/codex-rs/package-manager/src/platform.rs deleted file mode 100644 index c179b803e3..0000000000 --- a/codex-rs/package-manager/src/platform.rs +++ /dev/null @@ -1,48 +0,0 @@ -use crate::PackageManagerError; - -/// Supported OS and CPU combinations for managed packages. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum PackagePlatform { - /// macOS on Apple Silicon. - DarwinArm64, - /// macOS on x86_64. - DarwinX64, - /// Linux on AArch64. - LinuxArm64, - /// Linux on x86_64. - LinuxX64, - /// Windows on AArch64. - WindowsArm64, - /// Windows on x86_64. - WindowsX64, -} - -impl PackagePlatform { - /// Detects the current process platform. - pub fn detect_current() -> Result { - match (std::env::consts::OS, std::env::consts::ARCH) { - ("macos", "aarch64") | ("macos", "arm64") => Ok(Self::DarwinArm64), - ("macos", "x86_64") => Ok(Self::DarwinX64), - ("linux", "aarch64") | ("linux", "arm64") => Ok(Self::LinuxArm64), - ("linux", "x86_64") => Ok(Self::LinuxX64), - ("windows", "aarch64") | ("windows", "arm64") => Ok(Self::WindowsArm64), - ("windows", "x86_64") => Ok(Self::WindowsX64), - (os, arch) => Err(PackageManagerError::UnsupportedPlatform { - os: os.to_string(), - arch: arch.to_string(), - }), - } - } - - /// Returns the manifest/cache string for this platform. - pub fn as_str(self) -> &'static str { - match self { - Self::DarwinArm64 => "darwin-arm64", - Self::DarwinX64 => "darwin-x64", - Self::LinuxArm64 => "linux-arm64", - Self::LinuxX64 => "linux-x64", - Self::WindowsArm64 => "windows-arm64", - Self::WindowsX64 => "windows-x64", - } - } -} diff --git a/codex-rs/package-manager/src/tests.rs b/codex-rs/package-manager/src/tests.rs deleted file mode 100644 index a80e98ae74..0000000000 --- a/codex-rs/package-manager/src/tests.rs +++ /dev/null @@ -1,700 +0,0 @@ -use crate::ArchiveFormat; -use crate::ManagedPackage; -use crate::PackageManager; -use crate::PackageManagerConfig; -use crate::PackageManagerError; -use crate::PackagePlatform; -use crate::PackageReleaseArchive; -use crate::archive::detect_single_package_root; -use crate::archive::extract_archive; -use crate::manager::promote_staged_install; -use crate::manager::quarantine_existing_install; -use pretty_assertions::assert_eq; -use serde::Deserialize; -use sha2::Digest; -use sha2::Sha256; -use std::collections::BTreeMap; -use std::fs::File; -use std::io::Cursor; -use std::io::Write; -#[cfg(unix)] -use std::os::unix::fs::PermissionsExt; -use std::path::Path; -use std::path::PathBuf; -use std::sync::Arc; -use tar::Builder; -use tar::EntryType; -use tempfile::TempDir; -use tokio::sync::Barrier; -use url::Url; -use wiremock::Mock; -use wiremock::MockServer; -use wiremock::ResponseTemplate; -use wiremock::matchers::method; -use wiremock::matchers::path; -use zip::ZipWriter; -use zip::write::SimpleFileOptions; - -#[derive(Clone, Debug)] -struct TestPackage { - base_url: Url, - version: String, - fail_on_final_install_dir: bool, -} - -#[derive(Clone, Debug, Deserialize)] -struct TestReleaseManifest { - package_version: String, - platforms: BTreeMap, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct TestInstalledPackage { - version: String, - platform: PackagePlatform, - root_dir: PathBuf, -} - -impl ManagedPackage for TestPackage { - type Error = PackageManagerError; - type Installed = TestInstalledPackage; - type ReleaseManifest = TestReleaseManifest; - - fn default_cache_root_relative(&self) -> &str { - "packages/test-package" - } - - fn version(&self) -> &str { - &self.version - } - - fn manifest_url(&self) -> Result { - self.base_url - .join(&format!("test-package-v{}-manifest.json", self.version)) - .map_err(PackageManagerError::InvalidBaseUrl) - } - - fn archive_url(&self, archive: &PackageReleaseArchive) -> Result { - self.base_url - .join(&archive.archive) - .map_err(PackageManagerError::InvalidBaseUrl) - } - - fn release_version<'a>(&self, manifest: &'a Self::ReleaseManifest) -> &'a str { - &manifest.package_version - } - - fn platform_archive( - &self, - manifest: &Self::ReleaseManifest, - platform: PackagePlatform, - ) -> Result { - manifest - .platforms - .get(platform.as_str()) - .cloned() - .ok_or_else(|| PackageManagerError::MissingPlatform(platform.as_str().to_string())) - } - - fn install_dir(&self, cache_root: &Path, platform: PackagePlatform) -> PathBuf { - cache_root.join(self.version()).join(platform.as_str()) - } - - fn installed_version<'a>(&self, package: &'a Self::Installed) -> &'a str { - &package.version - } - - fn load_installed( - &self, - root_dir: PathBuf, - platform: PackagePlatform, - ) -> Result { - if self.fail_on_final_install_dir - && root_dir - .file_name() - .is_some_and(|name| name == platform.as_str()) - { - return Err(PackageManagerError::ArchiveExtraction(format!( - "refusing final install dir {}", - root_dir.display() - ))); - } - let manifest_path = root_dir.join("manifest.json"); - let version = - std::fs::read_to_string(&manifest_path).map_err(|source| PackageManagerError::Io { - context: format!("failed to read {}", manifest_path.display()), - source, - })?; - Ok(TestInstalledPackage { - version: version.trim().to_string(), - platform, - root_dir, - }) - } -} - -#[tokio::test] -async fn ensure_installed_downloads_and_extracts_zip_package() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let archive_bytes = build_zip_archive(version); - let archive_sha = format!("{:x}", Sha256::digest(&archive_bytes)); - let manifest = serde_json::json!({ - "package_version": version, - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": archive_sha, - "format": "zip", - "size_bytes": archive_bytes.len(), - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .mount(&server) - .await; - Mock::given(method("GET")) - .and(path(format!("/{archive_name}"))) - .respond_with(ResponseTemplate::new(200).set_body_bytes(archive_bytes)) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let package = TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: false, - }; - let manager = PackageManager::new(PackageManagerConfig::new( - codex_home.path().to_path_buf(), - package, - )); - - let installed = manager - .ensure_installed() - .await - .unwrap_or_else(|error| panic!("{error}")); - - assert_eq!( - installed, - TestInstalledPackage { - version: version.to_string(), - platform, - root_dir: codex_home - .path() - .join("packages") - .join("test-package") - .join(version) - .join(platform.as_str()), - } - ); - - #[cfg(unix)] - { - let executable_mode = std::fs::metadata(installed.root_dir.join("bin/tool")) - .unwrap_or_else(|error| panic!("{error}")) - .permissions() - .mode(); - assert_eq!(executable_mode & 0o111, 0o111); - } -} - -#[tokio::test] -async fn resolve_cached_uses_custom_cache_root() { - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let cache_root = codex_home.path().join("custom-cache"); - let install_dir = cache_root.join("0.1.0").join(platform.as_str()); - std::fs::create_dir_all(&install_dir).unwrap_or_else(|error| panic!("{error}")); - std::fs::write(install_dir.join("manifest.json"), "0.1.0") - .unwrap_or_else(|error| panic!("{error}")); - - let manager = PackageManager::new( - PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse("https://example.test/") - .unwrap_or_else(|error| panic!("{error}")), - version: "0.1.0".to_string(), - fail_on_final_install_dir: false, - }, - ) - .with_cache_root(cache_root.clone()), - ); - - let installed = manager - .resolve_cached() - .await - .unwrap_or_else(|error| panic!("{error}")); - - assert_eq!( - installed, - Some(TestInstalledPackage { - version: "0.1.0".to_string(), - platform, - root_dir: cache_root.join("0.1.0").join(platform.as_str()), - }) - ); -} - -#[tokio::test] -async fn ensure_installed_replaces_invalid_cached_install() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let archive_bytes = build_zip_archive(version); - let archive_sha = format!("{:x}", Sha256::digest(&archive_bytes)); - let manifest = serde_json::json!({ - "package_version": version, - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": archive_sha, - "format": "zip", - "size_bytes": archive_bytes.len(), - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .mount(&server) - .await; - Mock::given(method("GET")) - .and(path(format!("/{archive_name}"))) - .respond_with(ResponseTemplate::new(200).set_body_bytes(archive_bytes)) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let install_dir = codex_home - .path() - .join("packages") - .join("test-package") - .join(version) - .join(platform.as_str()); - std::fs::create_dir_all(&install_dir).unwrap_or_else(|error| panic!("{error}")); - std::fs::write(install_dir.join("broken.txt"), "stale") - .unwrap_or_else(|error| panic!("{error}")); - - let manager = PackageManager::new(PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: false, - }, - )); - - let installed = manager - .ensure_installed() - .await - .unwrap_or_else(|error| panic!("{error}")); - - assert_eq!(installed.version, version); - assert!(installed.root_dir.join("manifest.json").exists()); - assert!(!installed.root_dir.join("broken.txt").exists()); -} - -#[tokio::test] -async fn ensure_installed_rejects_manifest_version_mismatch() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let manifest = serde_json::json!({ - "package_version": "0.2.0", - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": "deadbeef", - "format": "zip", - "size_bytes": 1, - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let manager = PackageManager::new(PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: false, - }, - )); - - let error = manager - .ensure_installed() - .await - .expect_err("manifest version mismatch should fail"); - assert!(matches!( - error, - PackageManagerError::UnexpectedPackageVersion { expected, actual } - if expected == "0.1.0" && actual == "0.2.0" - )); -} - -#[tokio::test] -async fn ensure_installed_serializes_concurrent_installs() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let archive_bytes = build_zip_archive(version); - let archive_sha = format!("{:x}", Sha256::digest(&archive_bytes)); - let manifest = serde_json::json!({ - "package_version": version, - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": archive_sha, - "format": "zip", - "size_bytes": archive_bytes.len(), - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .expect(1) - .mount(&server) - .await; - Mock::given(method("GET")) - .and(path(format!("/{archive_name}"))) - .respond_with(ResponseTemplate::new(200).set_body_bytes(archive_bytes)) - .expect(1) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let config = PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: false, - }, - ); - let manager_one = PackageManager::new(config.clone()); - let manager_two = PackageManager::new(config); - let barrier = Arc::new(Barrier::new(2)); - let barrier_one = Arc::clone(&barrier); - let barrier_two = Arc::clone(&barrier); - - let (first, second) = tokio::join!( - async { - barrier_one.wait().await; - manager_one.ensure_installed().await - }, - async { - barrier_two.wait().await; - manager_two.ensure_installed().await - } - ); - - let first = first.unwrap_or_else(|error| panic!("{error}")); - let second = second.unwrap_or_else(|error| panic!("{error}")); - assert_eq!(first, second); -} - -#[tokio::test] -async fn ensure_installed_rejects_unexpected_archive_size() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let archive_bytes = build_zip_archive(version); - let actual_size = archive_bytes.len() as u64; - let expected_size = (archive_bytes.len() + 1) as u64; - let archive_sha = format!("{:x}", Sha256::digest(&archive_bytes)); - let manifest = serde_json::json!({ - "package_version": version, - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": archive_sha, - "format": "zip", - "size_bytes": expected_size, - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .mount(&server) - .await; - Mock::given(method("GET")) - .and(path(format!("/{archive_name}"))) - .respond_with(ResponseTemplate::new(200).set_body_bytes(archive_bytes)) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let manager = PackageManager::new(PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: false, - }, - )); - - let error = manager - .ensure_installed() - .await - .expect_err("archive size mismatch should fail"); - assert!(matches!( - error, - PackageManagerError::UnexpectedArchiveSize { expected, actual } - if expected == expected_size && actual == actual_size - )); -} - -#[tokio::test] -async fn staged_install_restore_keeps_previous_install_on_failed_promotion() { - let temp = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let install_dir = temp.path().join("install"); - let staged_dir = temp.path().join("missing-staged"); - std::fs::create_dir_all(&install_dir).unwrap_or_else(|error| panic!("{error}")); - std::fs::write(install_dir.join("manifest.json"), "0.1.0") - .unwrap_or_else(|error| panic!("{error}")); - - let quarantined = quarantine_existing_install(&install_dir) - .await - .unwrap_or_else(|error| panic!("{error}")); - let promotion_error = promote_staged_install(&staged_dir, &install_dir) - .await - .expect_err("promotion should fail"); - crate::manager::restore_quarantined_install( - &install_dir, - quarantined.as_deref(), - &promotion_error, - ) - .await - .unwrap_or_else(|error| panic!("{error}")); - - assert!(install_dir.join("manifest.json").exists()); - assert_eq!( - std::fs::read_to_string(install_dir.join("manifest.json")) - .unwrap_or_else(|error| panic!("{error}")), - "0.1.0" - ); -} - -#[tokio::test] -async fn ensure_installed_restores_previous_install_when_final_validation_fails() { - let server = MockServer::start().await; - let version = "0.1.0"; - let platform = PackagePlatform::detect_current().unwrap_or_else(|error| panic!("{error}")); - let archive_name = format!("test-package-v{version}-{}.zip", platform.as_str()); - let archive_bytes = build_zip_archive(version); - let archive_sha = format!("{:x}", Sha256::digest(&archive_bytes)); - let manifest = serde_json::json!({ - "package_version": version, - "platforms": { - platform.as_str(): { - "archive": archive_name, - "sha256": archive_sha, - "format": "zip", - "size_bytes": archive_bytes.len(), - } - } - }); - Mock::given(method("GET")) - .and(path(format!("/test-package-v{version}-manifest.json"))) - .respond_with(ResponseTemplate::new(200).set_body_json(&manifest)) - .mount(&server) - .await; - Mock::given(method("GET")) - .and(path(format!("/{archive_name}"))) - .respond_with(ResponseTemplate::new(200).set_body_bytes(archive_bytes)) - .mount(&server) - .await; - - let codex_home = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let install_dir = codex_home - .path() - .join("packages") - .join("test-package") - .join(version) - .join(platform.as_str()); - std::fs::create_dir_all(&install_dir).unwrap_or_else(|error| panic!("{error}")); - std::fs::write(install_dir.join("manifest.json"), "0.0.9") - .unwrap_or_else(|error| panic!("{error}")); - - let error = PackageManager::new(PackageManagerConfig::new( - codex_home.path().to_path_buf(), - TestPackage { - base_url: Url::parse(&format!("{}/", server.uri())) - .unwrap_or_else(|error| panic!("{error}")), - version: version.to_string(), - fail_on_final_install_dir: true, - }, - )) - .ensure_installed() - .await - .expect_err("final validation should fail"); - - assert!( - matches!(error, PackageManagerError::ArchiveExtraction(message) if message.contains("refusing final install dir")) - ); - assert_eq!( - std::fs::read_to_string(install_dir.join("manifest.json")) - .unwrap_or_else(|error| panic!("{error}")), - "0.0.9" - ); - assert!( - !install_dir - .parent() - .unwrap_or_else(|| panic!("install dir should have a parent")) - .read_dir() - .unwrap_or_else(|error| panic!("{error}")) - .any(|entry| { - entry - .unwrap_or_else(|error| panic!("{error}")) - .file_name() - .to_string_lossy() - .contains(".replaced-") - }) - ); -} - -#[test] -fn tar_gz_extraction_supports_default_package_root_detection() { - let temp = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let archive_path = temp.path().join("package.tar.gz"); - let extraction_root = temp.path().join("extract"); - std::fs::create_dir_all(&extraction_root).unwrap_or_else(|error| panic!("{error}")); - write_tar_gz_archive(&archive_path, "0.2.0"); - - extract_archive(&archive_path, &extraction_root, ArchiveFormat::TarGz) - .unwrap_or_else(|error| panic!("{error}")); - let package_root = - detect_single_package_root(&extraction_root).unwrap_or_else(|error| panic!("{error}")); - - assert!(package_root.join("manifest.json").exists()); -} - -#[test] -fn tar_gz_extraction_rejects_symlinks() { - let temp = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let archive_path = temp.path().join("package.tar.gz"); - let extraction_root = temp.path().join("extract"); - std::fs::create_dir_all(&extraction_root).unwrap_or_else(|error| panic!("{error}")); - write_tar_gz_archive_with_symlink(&archive_path); - - let error = extract_archive(&archive_path, &extraction_root, ArchiveFormat::TarGz) - .expect_err("symlink entry should fail"); - assert!( - matches!(error, PackageManagerError::ArchiveExtraction(message) if message.contains("unsupported type")) - ); -} - -#[test] -fn zip_extraction_rejects_parent_paths() { - let temp = TempDir::new().unwrap_or_else(|error| panic!("{error}")); - let archive_path = temp.path().join("package.zip"); - let extraction_root = temp.path().join("extract"); - std::fs::create_dir_all(&extraction_root).unwrap_or_else(|error| panic!("{error}")); - write_zip_archive_with_parent_path(&archive_path); - - let error = extract_archive(&archive_path, &extraction_root, ArchiveFormat::Zip) - .expect_err("parent path entry should fail"); - assert!( - matches!(error, PackageManagerError::ArchiveExtraction(message) if message.contains("escapes extraction root")) - ); -} - -fn build_zip_archive(version: &str) -> Vec { - let mut bytes = Cursor::new(Vec::new()); - { - let mut zip = ZipWriter::new(&mut bytes); - let options = SimpleFileOptions::default(); - zip.start_file("test-package/manifest.json", options) - .unwrap_or_else(|error| panic!("{error}")); - zip.write_all(version.as_bytes()) - .unwrap_or_else(|error| panic!("{error}")); - zip.start_file("test-package/bin/tool", options.unix_permissions(0o755)) - .unwrap_or_else(|error| panic!("{error}")); - zip.write_all(b"#!/bin/sh\n") - .unwrap_or_else(|error| panic!("{error}")); - zip.finish().unwrap_or_else(|error| panic!("{error}")); - } - bytes.into_inner() -} - -fn write_zip_archive_with_parent_path(archive_path: &Path) { - let file = File::create(archive_path).unwrap_or_else(|error| panic!("{error}")); - let mut zip = ZipWriter::new(file); - let options = SimpleFileOptions::default(); - zip.start_file("../escape.txt", options) - .unwrap_or_else(|error| panic!("{error}")); - zip.write_all(b"escape") - .unwrap_or_else(|error| panic!("{error}")); - zip.finish().unwrap_or_else(|error| panic!("{error}")); -} - -fn write_tar_gz_archive(archive_path: &Path, version: &str) { - let file = File::create(archive_path).unwrap_or_else(|error| panic!("{error}")); - let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); - let mut builder = Builder::new(encoder); - - append_tar_file( - &mut builder, - "test-package/manifest.json", - version.as_bytes(), - ); - builder.finish().unwrap_or_else(|error| panic!("{error}")); -} - -fn write_tar_gz_archive_with_symlink(archive_path: &Path) { - let file = File::create(archive_path).unwrap_or_else(|error| panic!("{error}")); - let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); - let mut builder = Builder::new(encoder); - - append_tar_file(&mut builder, "test-package/manifest.json", b"0.2.0"); - - let mut header = tar::Header::new_gnu(); - header.set_entry_type(EntryType::Symlink); - header.set_size(0); - header.set_mode(0o777); - header - .set_link_name("/tmp/escape") - .unwrap_or_else(|error| panic!("{error}")); - header.set_cksum(); - builder - .append_data(&mut header, "test-package/link", std::io::empty()) - .unwrap_or_else(|error| panic!("{error}")); - - builder.finish().unwrap_or_else(|error| panic!("{error}")); -} - -fn append_tar_file( - builder: &mut Builder>, - path: &str, - contents: &[u8], -) { - let mut header = tar::Header::new_gnu(); - header.set_size(contents.len() as u64); - header.set_mode(0o755); - header.set_cksum(); - builder - .append_data(&mut header, path, contents) - .unwrap_or_else(|error| panic!("{error}")); -} From 2ef91b71406da6c88cad959d647915b4e7909f1f Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Fri, 27 Mar 2026 02:31:08 -0700 Subject: [PATCH 033/191] chore: move pty and windows sandbox to Rust 2024 (#15954) ## Why `codex-utils-pty` and `codex-windows-sandbox` were the remaining crates in `codex-rs` that still overrode the workspace's Rust 2024 edition. Moving them forward in a separate PR keeps the baseline edition update isolated from the follow-on Bazel clippy workflow in #15955, while making linting and formatting behavior consistent with the rest of the workspace. This PR also needs Cargo and Bazel to agree on the edition for `codex-windows-sandbox`. Without the Bazel-side sync, the experimental Bazel app-server builds fail once they compile `windows-sandbox-rs`. ## What changed - switch `codex-rs/utils/pty` and `codex-rs/windows-sandbox-rs` to `edition = "2024"` - update `codex-utils-pty` callsites and tests to use the collapsed `if let` form that Clippy expects under the new edition - fix the Rust 2024 fallout in `windows-sandbox-rs`, including the reserved `gen` identifier, `unsafe extern` requirements, and new Clippy findings that surfaced under the edition bump - keep the edition bump separate from a larger unsafe cleanup by temporarily allowing `unsafe_op_in_unsafe_fn` in the Windows entrypoint modules that now report it under Rust 2024 - update `codex-rs/windows-sandbox-rs/BUILD.bazel` to `crate_edition = "2024"` so Bazel compiles the crate with the same edition as Cargo --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/15954). * #15976 * #15955 * __->__ #15954 --- codex-rs/utils/pty/Cargo.toml | 2 +- codex-rs/utils/pty/src/lib.rs | 4 +- codex-rs/utils/pty/src/pipe.rs | 8 +- codex-rs/utils/pty/src/process.rs | 42 ++++---- codex-rs/utils/pty/src/pty.rs | 4 +- codex-rs/utils/pty/src/tests.rs | 12 +-- codex-rs/utils/pty/src/win/conpty.rs | 2 +- codex-rs/utils/pty/src/win/mod.rs | 6 +- codex-rs/utils/pty/src/win/procthreadattr.rs | 2 +- codex-rs/utils/pty/src/win/psuedocon.rs | 4 +- codex-rs/windows-sandbox-rs/BUILD.bazel | 1 - codex-rs/windows-sandbox-rs/Cargo.toml | 2 +- codex-rs/windows-sandbox-rs/src/audit.rs | 8 +- codex-rs/windows-sandbox-rs/src/conpty/mod.rs | 2 +- .../src/conpty/proc_thread_attr.rs | 2 +- codex-rs/windows-sandbox-rs/src/desktop.rs | 2 +- .../src/elevated/command_runner_win.rs | 59 +++++------ .../src/elevated/ipc_framed.rs | 2 +- .../windows-sandbox-rs/src/elevated_impl.rs | 42 ++++---- codex-rs/windows-sandbox-rs/src/env.rs | 16 +-- codex-rs/windows-sandbox-rs/src/firewall.rs | 15 ++- .../src/helper_materialization.rs | 13 ++- codex-rs/windows-sandbox-rs/src/lib.rs | 97 ++++++++++--------- codex-rs/windows-sandbox-rs/src/logging.rs | 11 +-- .../windows-sandbox-rs/src/read_acl_mutex.rs | 4 +- .../windows-sandbox-rs/src/sandbox_users.rs | 18 ++-- .../windows-sandbox-rs/src/setup_error.rs | 17 ++-- .../windows-sandbox-rs/src/setup_main_win.rs | 22 ++--- .../src/setup_orchestrator.rs | 80 ++++++++------- codex-rs/windows-sandbox-rs/src/token.rs | 4 +- 30 files changed, 255 insertions(+), 248 deletions(-) diff --git a/codex-rs/utils/pty/Cargo.toml b/codex-rs/utils/pty/Cargo.toml index ec221bfc94..cf98ac4904 100644 --- a/codex-rs/utils/pty/Cargo.toml +++ b/codex-rs/utils/pty/Cargo.toml @@ -1,5 +1,5 @@ [package] -edition = "2021" +edition = "2024" license.workspace = true name = "codex-utils-pty" version.workspace = true diff --git a/codex-rs/utils/pty/src/lib.rs b/codex-rs/utils/pty/src/lib.rs index 48fcd53b50..87898ddf71 100644 --- a/codex-rs/utils/pty/src/lib.rs +++ b/codex-rs/utils/pty/src/lib.rs @@ -13,14 +13,14 @@ pub const DEFAULT_OUTPUT_BYTES_CAP: usize = 1024 * 1024; pub use pipe::spawn_process as spawn_pipe_process; /// Spawn a non-interactive process using regular pipes, but close stdin immediately. pub use pipe::spawn_process_no_stdin as spawn_pipe_process_no_stdin; -/// Combine stdout/stderr receivers into a single broadcast receiver. -pub use process::combine_output_receivers; /// Handle for interacting with a spawned process (PTY or pipe). pub use process::ProcessHandle; /// Bundle of process handles plus split output and exit receivers returned by spawn helpers. pub use process::SpawnedProcess; /// Terminal size in character cells used for PTY spawn and resize operations. pub use process::TerminalSize; +/// Combine stdout/stderr receivers into a single broadcast receiver. +pub use process::combine_output_receivers; /// Backwards-compatible alias for ProcessHandle. pub type ExecCommandSession = ProcessHandle; /// Backwards-compatible alias for SpawnedProcess. diff --git a/codex-rs/utils/pty/src/pipe.rs b/codex-rs/utils/pty/src/pipe.rs index 2cebdef49f..903d1cf55e 100644 --- a/codex-rs/utils/pty/src/pipe.rs +++ b/codex-rs/utils/pty/src/pipe.rs @@ -3,9 +3,9 @@ use std::io; use std::io::ErrorKind; use std::path::Path; use std::process::Stdio; -use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::sync::Mutex as StdMutex; +use std::sync::atomic::AtomicBool; use anyhow::Result; use tokio::io::AsyncRead; @@ -64,11 +64,7 @@ fn kill_process(pid: u32) -> io::Result<()> { let success = winapi::um::processthreadsapi::TerminateProcess(handle, 1); let err = io::Error::last_os_error(); winapi::um::handleapi::CloseHandle(handle); - if success == 0 { - Err(err) - } else { - Ok(()) - } + if success == 0 { Err(err) } else { Ok(()) } } } diff --git a/codex-rs/utils/pty/src/process.rs b/codex-rs/utils/pty/src/process.rs index 61bdd3cd1b..8459f2b68f 100644 --- a/codex-rs/utils/pty/src/process.rs +++ b/codex-rs/utils/pty/src/process.rs @@ -2,9 +2,9 @@ use core::fmt; use std::io; #[cfg(unix)] use std::os::fd::RawFd; -use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::sync::Mutex as StdMutex; +use std::sync::atomic::AtomicBool; use anyhow::anyhow; use portable_pty::MasterPty; @@ -118,10 +118,10 @@ impl ProcessHandle { /// Returns a channel sender for writing raw bytes to the child stdin. pub fn writer_sender(&self) -> mpsc::Sender> { - if let Ok(writer_tx) = self.writer_tx.lock() { - if let Some(writer_tx) = writer_tx.as_ref() { - return writer_tx.clone(); - } + if let Ok(writer_tx) = self.writer_tx.lock() + && let Some(writer_tx) = writer_tx.as_ref() + { + return writer_tx.clone(); } let (writer_tx, writer_rx) = mpsc::channel(1); @@ -165,10 +165,10 @@ impl ProcessHandle { /// Attempts to kill the child while leaving the reader/writer tasks alive /// so callers can still drain output until EOF. pub fn request_terminate(&self) { - if let Ok(mut killer_opt) = self.killer.lock() { - if let Some(mut killer) = killer_opt.take() { - let _ = killer.kill(); - } + if let Ok(mut killer_opt) = self.killer.lock() + && let Some(mut killer) = killer_opt.take() + { + let _ = killer.kill(); } } @@ -176,25 +176,25 @@ impl ProcessHandle { pub fn terminate(&self) { self.request_terminate(); - if let Ok(mut h) = self.reader_handle.lock() { - if let Some(handle) = h.take() { - handle.abort(); - } + if let Ok(mut h) = self.reader_handle.lock() + && let Some(handle) = h.take() + { + handle.abort(); } if let Ok(mut handles) = self.reader_abort_handles.lock() { for handle in handles.drain(..) { handle.abort(); } } - if let Ok(mut h) = self.writer_handle.lock() { - if let Some(handle) = h.take() { - handle.abort(); - } + if let Ok(mut h) = self.writer_handle.lock() + && let Some(handle) = h.take() + { + handle.abort(); } - if let Ok(mut h) = self.wait_handle.lock() { - if let Some(handle) = h.take() { - handle.abort(); - } + if let Ok(mut h) = self.wait_handle.lock() + && let Some(handle) = h.take() + { + handle.abort(); } } } diff --git a/codex-rs/utils/pty/src/pty.rs b/codex-rs/utils/pty/src/pty.rs index e7bc9f9c97..4ae6facb7d 100644 --- a/codex-rs/utils/pty/src/pty.rs +++ b/codex-rs/utils/pty/src/pty.rs @@ -15,15 +15,15 @@ use std::path::Path; use std::process::Command as StdCommand; #[cfg(unix)] use std::process::Stdio; -use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::sync::Mutex as StdMutex; +use std::sync::atomic::AtomicBool; use std::time::Duration; use anyhow::Result; +use portable_pty::CommandBuilder; #[cfg(not(windows))] use portable_pty::native_pty_system; -use portable_pty::CommandBuilder; use tokio::sync::mpsc; use tokio::sync::oneshot; use tokio::task::JoinHandle; diff --git a/codex-rs/utils/pty/src/tests.rs b/codex-rs/utils/pty/src/tests.rs index e7135114d9..fa0e821a24 100644 --- a/codex-rs/utils/pty/src/tests.rs +++ b/codex-rs/utils/pty/src/tests.rs @@ -3,6 +3,8 @@ use std::path::Path; use pretty_assertions::assert_eq; +use crate::SpawnedProcess; +use crate::TerminalSize; use crate::combine_output_receivers; #[cfg(unix)] use crate::pipe::spawn_process_no_stdin_with_inherited_fds; @@ -11,18 +13,15 @@ use crate::pty::spawn_process_with_inherited_fds; use crate::spawn_pipe_process; use crate::spawn_pipe_process_no_stdin; use crate::spawn_pty_process; -use crate::SpawnedProcess; -use crate::TerminalSize; fn find_python() -> Option { for candidate in ["python3", "python"] { if let Ok(output) = std::process::Command::new(candidate) .arg("--version") .output() + && output.status.success() { - if output.status.success() { - return Some(candidate.to_string()); - } + return Some(candidate.to_string()); } } None @@ -855,8 +854,7 @@ async fn pty_spawn_with_inherited_fds_supports_resize() -> anyhow::Result<()> { let write_end = unsafe { std::fs::File::from_raw_fd(fds[1]) }; let env_map: HashMap = std::env::vars().collect(); - let script = - "stty -echo; printf 'start:%s\\n' \"$(stty size)\"; IFS= read _line; printf 'after:%s\\n' \"$(stty size)\""; + let script = "stty -echo; printf 'start:%s\\n' \"$(stty size)\"; IFS= read _line; printf 'after:%s\\n' \"$(stty size)\""; let spawned = spawn_process_with_inherited_fds( "/bin/sh", &["-c".to_string(), script.to_string()], diff --git a/codex-rs/utils/pty/src/win/conpty.rs b/codex-rs/utils/pty/src/win/conpty.rs index a0e5903810..ae490160ae 100644 --- a/codex-rs/utils/pty/src/win/conpty.rs +++ b/codex-rs/utils/pty/src/win/conpty.rs @@ -22,13 +22,13 @@ use crate::win::psuedocon::PsuedoCon; use anyhow::Error; use filedescriptor::FileDescriptor; use filedescriptor::Pipe; -use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::Child; use portable_pty::MasterPty; use portable_pty::PtyPair; use portable_pty::PtySize; use portable_pty::PtySystem; use portable_pty::SlavePty; +use portable_pty::cmdbuilder::CommandBuilder; use std::mem::ManuallyDrop; use std::os::windows::io::AsRawHandle; use std::os::windows::io::RawHandle; diff --git a/codex-rs/utils/pty/src/win/mod.rs b/codex-rs/utils/pty/src/win/mod.rs index bcd65eb487..78f5af2ce7 100644 --- a/codex-rs/utils/pty/src/win/mod.rs +++ b/codex-rs/utils/pty/src/win/mod.rs @@ -142,11 +142,7 @@ impl Child for WinChild { fn process_id(&self) -> Option { let res = unsafe { GetProcessId(self.proc.lock().unwrap().as_raw_handle() as _) }; - if res == 0 { - None - } else { - Some(res) - } + if res == 0 { None } else { Some(res) } } fn as_raw_handle(&self) -> Option { diff --git a/codex-rs/utils/pty/src/win/procthreadattr.rs b/codex-rs/utils/pty/src/win/procthreadattr.rs index 6d464e99de..c7cf68fed9 100644 --- a/codex-rs/utils/pty/src/win/procthreadattr.rs +++ b/codex-rs/utils/pty/src/win/procthreadattr.rs @@ -19,8 +19,8 @@ // SOFTWARE. use super::psuedocon::HPCON; -use anyhow::ensure; use anyhow::Error; +use anyhow::ensure; use std::io::Error as IoError; use std::mem; use std::ptr; diff --git a/codex-rs/utils/pty/src/win/psuedocon.rs b/codex-rs/utils/pty/src/win/psuedocon.rs index b1c72a739d..3476665633 100644 --- a/codex-rs/utils/pty/src/win/psuedocon.rs +++ b/codex-rs/utils/pty/src/win/psuedocon.rs @@ -21,9 +21,9 @@ use super::WinChild; use crate::win::procthreadattr::ProcThreadAttributeList; +use anyhow::Error; use anyhow::bail; use anyhow::ensure; -use anyhow::Error; use filedescriptor::FileDescriptor; use filedescriptor::OwnedHandle; use lazy_static::lazy_static; @@ -356,8 +356,8 @@ fn append_quoted(arg: &OsStr, cmdline: &mut Vec) { #[cfg(test)] mod tests { - use super::windows_build_number; use super::MIN_CONPTY_BUILD; + use super::windows_build_number; #[test] fn windows_build_number_returns_value() { diff --git a/codex-rs/windows-sandbox-rs/BUILD.bazel b/codex-rs/windows-sandbox-rs/BUILD.bazel index b09ee38ed0..e3f0112a85 100644 --- a/codex-rs/windows-sandbox-rs/BUILD.bazel +++ b/codex-rs/windows-sandbox-rs/BUILD.bazel @@ -7,5 +7,4 @@ codex_rust_crate( "Cargo.toml", "codex-windows-sandbox-setup.manifest", ], - crate_edition = "2021", ) diff --git a/codex-rs/windows-sandbox-rs/Cargo.toml b/codex-rs/windows-sandbox-rs/Cargo.toml index 91cb178435..0fb2192578 100644 --- a/codex-rs/windows-sandbox-rs/Cargo.toml +++ b/codex-rs/windows-sandbox-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] build = "build.rs" -edition = "2021" +edition = "2024" license.workspace = true name = "codex-windows-sandbox" version.workspace = true diff --git a/codex-rs/windows-sandbox-rs/src/audit.rs b/codex-rs/windows-sandbox-rs/src/audit.rs index d85c5dea8e..0f115323ea 100644 --- a/codex-rs/windows-sandbox-rs/src/audit.rs +++ b/codex-rs/windows-sandbox-rs/src/audit.rs @@ -33,10 +33,10 @@ const SKIP_DIR_SUFFIXES: &[&str] = &[ ]; fn unique_push(set: &mut HashSet, out: &mut Vec, p: PathBuf) { - if let Ok(abs) = p.canonicalize() { - if set.insert(abs.clone()) { - out.push(abs); - } + if let Ok(abs) = p.canonicalize() + && set.insert(abs.clone()) + { + out.push(abs); } } diff --git a/codex-rs/windows-sandbox-rs/src/conpty/mod.rs b/codex-rs/windows-sandbox-rs/src/conpty/mod.rs index 9c05e9ea67..c7b992f518 100644 --- a/codex-rs/windows-sandbox-rs/src/conpty/mod.rs +++ b/codex-rs/windows-sandbox-rs/src/conpty/mod.rs @@ -23,8 +23,8 @@ use windows_sys::Win32::Foundation::GetLastError; use windows_sys::Win32::Foundation::HANDLE; use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; use windows_sys::Win32::System::Console::ClosePseudoConsole; -use windows_sys::Win32::System::Threading::CreateProcessAsUserW; use windows_sys::Win32::System::Threading::CREATE_UNICODE_ENVIRONMENT; +use windows_sys::Win32::System::Threading::CreateProcessAsUserW; use windows_sys::Win32::System::Threading::EXTENDED_STARTUPINFO_PRESENT; use windows_sys::Win32::System::Threading::PROCESS_INFORMATION; use windows_sys::Win32::System::Threading::STARTF_USESTDHANDLES; diff --git a/codex-rs/windows-sandbox-rs/src/conpty/proc_thread_attr.rs b/codex-rs/windows-sandbox-rs/src/conpty/proc_thread_attr.rs index 3fe3870f9c..393172a0f8 100644 --- a/codex-rs/windows-sandbox-rs/src/conpty/proc_thread_attr.rs +++ b/codex-rs/windows-sandbox-rs/src/conpty/proc_thread_attr.rs @@ -8,8 +8,8 @@ use std::io; use windows_sys::Win32::Foundation::GetLastError; use windows_sys::Win32::System::Threading::DeleteProcThreadAttributeList; use windows_sys::Win32::System::Threading::InitializeProcThreadAttributeList; -use windows_sys::Win32::System::Threading::UpdateProcThreadAttribute; use windows_sys::Win32::System::Threading::LPPROC_THREAD_ATTRIBUTE_LIST; +use windows_sys::Win32::System::Threading::UpdateProcThreadAttribute; const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x00020016; diff --git a/codex-rs/windows-sandbox-rs/src/desktop.rs b/codex-rs/windows-sandbox-rs/src/desktop.rs index 822e43b071..d2aa129b92 100644 --- a/codex-rs/windows-sandbox-rs/src/desktop.rs +++ b/codex-rs/windows-sandbox-rs/src/desktop.rs @@ -89,7 +89,7 @@ struct PrivateDesktop { impl PrivateDesktop { fn create(logs_base_dir: Option<&Path>) -> Result { let mut rng = SmallRng::from_entropy(); - let name = format!("CodexSandboxDesktop-{:x}", rng.gen::()); + let name = format!("CodexSandboxDesktop-{:x}", rng.r#gen::()); let name_wide = to_wide(&name); let handle = unsafe { CreateDesktopW( diff --git a/codex-rs/windows-sandbox-rs/src/elevated/command_runner_win.rs b/codex-rs/windows-sandbox-rs/src/elevated/command_runner_win.rs index 82347fcca7..5572c1bf8e 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated/command_runner_win.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated/command_runner_win.rs @@ -8,34 +8,35 @@ //! path spawns the child directly and does not use this runner. #![cfg(target_os = "windows")] +#![allow(unsafe_op_in_unsafe_fn)] use anyhow::Context; use anyhow::Result; +use codex_windows_sandbox::PipeSpawnHandles; +use codex_windows_sandbox::SandboxPolicy; +use codex_windows_sandbox::StderrMode; +use codex_windows_sandbox::StdinMode; use codex_windows_sandbox::allow_null_device; use codex_windows_sandbox::convert_string_sid_to_sid; use codex_windows_sandbox::create_readonly_token_with_caps_from; use codex_windows_sandbox::create_workspace_write_token_with_caps_from; use codex_windows_sandbox::get_current_token_for_restriction; use codex_windows_sandbox::hide_current_user_profile_dir; -use codex_windows_sandbox::ipc_framed::decode_bytes; -use codex_windows_sandbox::ipc_framed::encode_bytes; -use codex_windows_sandbox::ipc_framed::read_frame; -use codex_windows_sandbox::ipc_framed::write_frame; use codex_windows_sandbox::ipc_framed::ErrorPayload; use codex_windows_sandbox::ipc_framed::ExitPayload; use codex_windows_sandbox::ipc_framed::FramedMessage; use codex_windows_sandbox::ipc_framed::Message; use codex_windows_sandbox::ipc_framed::OutputPayload; use codex_windows_sandbox::ipc_framed::OutputStream; +use codex_windows_sandbox::ipc_framed::decode_bytes; +use codex_windows_sandbox::ipc_framed::encode_bytes; +use codex_windows_sandbox::ipc_framed::read_frame; +use codex_windows_sandbox::ipc_framed::write_frame; use codex_windows_sandbox::log_note; use codex_windows_sandbox::parse_policy; use codex_windows_sandbox::read_handle_loop; use codex_windows_sandbox::spawn_process_with_pipes; use codex_windows_sandbox::to_wide; -use codex_windows_sandbox::PipeSpawnHandles; -use codex_windows_sandbox::SandboxPolicy; -use codex_windows_sandbox::StderrMode; -use codex_windows_sandbox::StdinMode; use std::ffi::c_void; use std::fs::File; use std::os::windows::io::FromRawHandle; @@ -46,9 +47,9 @@ use std::sync::Arc; use std::sync::Mutex as StdMutex; use windows_sys::Win32::Foundation::CloseHandle; use windows_sys::Win32::Foundation::GetLastError; -use windows_sys::Win32::Foundation::LocalFree; use windows_sys::Win32::Foundation::HANDLE; use windows_sys::Win32::Foundation::HLOCAL; +use windows_sys::Win32::Foundation::LocalFree; use windows_sys::Win32::Storage::FileSystem::CreateFileW; use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ; use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_WRITE; @@ -56,16 +57,16 @@ use windows_sys::Win32::Storage::FileSystem::OPEN_EXISTING; use windows_sys::Win32::System::Console::ClosePseudoConsole; use windows_sys::Win32::System::JobObjects::AssignProcessToJobObject; use windows_sys::Win32::System::JobObjects::CreateJobObjectW; +use windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; +use windows_sys::Win32::System::JobObjects::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; use windows_sys::Win32::System::JobObjects::JobObjectExtendedLimitInformation; use windows_sys::Win32::System::JobObjects::SetInformationJobObject; -use windows_sys::Win32::System::JobObjects::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; -use windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; use windows_sys::Win32::System::Threading::GetExitCodeProcess; use windows_sys::Win32::System::Threading::GetProcessId; -use windows_sys::Win32::System::Threading::TerminateProcess; -use windows_sys::Win32::System::Threading::WaitForSingleObject; use windows_sys::Win32::System::Threading::INFINITE; use windows_sys::Win32::System::Threading::PROCESS_INFORMATION; +use windows_sys::Win32::System::Threading::TerminateProcess; +use windows_sys::Win32::System::Threading::WaitForSingleObject; #[path = "cwd_junction.rs"] mod cwd_junction; @@ -332,13 +333,13 @@ fn spawn_output_reader( }, }, }; - if let Ok(mut guard) = writer.lock() { - if let Err(err) = write_frame(&mut *guard, &msg) { - log_note( - &format!("runner output write failed: {err}"), - log_dir.as_deref(), - ); - } + if let Ok(mut guard) = writer.lock() + && let Err(err) = write_frame(&mut *guard, &msg) + { + log_note( + &format!("runner output write failed: {err}"), + log_dir.as_deref(), + ); } }) } @@ -382,11 +383,11 @@ fn spawn_input_loop( } } Message::Terminate { .. } => { - if let Ok(guard) = process_handle.lock() { - if let Some(handle) = guard.as_ref() { - unsafe { - let _ = TerminateProcess(*handle, 1); - } + if let Ok(guard) = process_handle.lock() + && let Some(handle) = guard.as_ref() + { + unsafe { + let _ = TerminateProcess(*handle, 1); } } } @@ -544,10 +545,10 @@ pub fn main() -> Result<()> { }, }, }; - if let Ok(mut guard) = pipe_write.lock() { - if let Err(err) = write_frame(&mut *guard, &exit_msg) { - log_note(&format!("runner exit write failed: {err}"), log_dir); - } + if let Ok(mut guard) = pipe_write.lock() + && let Err(err) = write_frame(&mut *guard, &exit_msg) + { + log_note(&format!("runner exit write failed: {err}"), log_dir); } std::process::exit(exit_code); diff --git a/codex-rs/windows-sandbox-rs/src/elevated/ipc_framed.rs b/codex-rs/windows-sandbox-rs/src/elevated/ipc_framed.rs index 37590c34b9..d56ec1d0e0 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated/ipc_framed.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated/ipc_framed.rs @@ -8,8 +8,8 @@ //! through the elevated runner. use anyhow::Result; -use base64::engine::general_purpose::STANDARD; use base64::Engine as _; +use base64::engine::general_purpose::STANDARD; use serde::Deserialize; use serde::Serialize; use std::collections::HashMap; diff --git a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs index 365ac37d6f..cc2b7fdad6 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs @@ -16,37 +16,37 @@ pub struct ElevatedSandboxCaptureRequest<'a> { mod windows_impl { use super::ElevatedSandboxCaptureRequest; use crate::acl::allow_null_device; - use crate::allow::compute_allow_paths; use crate::allow::AllowDenyPaths; + use crate::allow::compute_allow_paths; use crate::cap::load_or_create_cap_sids; use crate::env::ensure_non_interactive_pager; use crate::env::inherit_path_env; use crate::env::normalize_null_device_env; - use crate::helper_materialization::resolve_helper_for_launch; use crate::helper_materialization::HelperExecutable; + use crate::helper_materialization::resolve_helper_for_launch; use crate::identity::require_logon_sandbox_creds; - use crate::ipc_framed::decode_bytes; - use crate::ipc_framed::read_frame; - use crate::ipc_framed::write_frame; use crate::ipc_framed::FramedMessage; use crate::ipc_framed::Message; use crate::ipc_framed::OutputStream; use crate::ipc_framed::SpawnRequest; + use crate::ipc_framed::decode_bytes; + use crate::ipc_framed::read_frame; + use crate::ipc_framed::write_frame; use crate::logging::log_failure; use crate::logging::log_note; use crate::logging::log_start; use crate::logging::log_success; - use crate::policy::parse_policy; use crate::policy::SandboxPolicy; + use crate::policy::parse_policy; use crate::token::convert_string_sid_to_sid; use crate::winutil::quote_windows_arg; use crate::winutil::resolve_sid; use crate::winutil::string_from_sid_bytes; use crate::winutil::to_wide; use anyhow::Result; - use rand::rngs::SmallRng; use rand::Rng; use rand::SeedableRng; + use rand::rngs::SmallRng; use std::collections::HashMap; use std::ffi::c_void; use std::fs::File; @@ -84,16 +84,16 @@ mod windows_impl { return Some(cur); } if marker.is_file() { - if let Ok(txt) = std::fs::read_to_string(&marker) { - if let Some(rest) = txt.trim().strip_prefix("gitdir:") { - let gitdir = rest.trim(); - let resolved = if Path::new(gitdir).is_absolute() { - PathBuf::from(gitdir) - } else { - cur.join(gitdir) - }; - return resolved.parent().map(|p| p.to_path_buf()).or(Some(cur)); - } + if let Ok(txt) = std::fs::read_to_string(&marker) + && let Some(rest) = txt.trim().strip_prefix("gitdir:") + { + let gitdir = rest.trim(); + let resolved = if Path::new(gitdir).is_absolute() { + PathBuf::from(gitdir) + } else { + cur.join(gitdir) + }; + return resolved.parent().map(|p| p.to_path_buf()).or(Some(cur)); } return Some(cur); } @@ -143,7 +143,11 @@ mod windows_impl { /// Generates a unique named-pipe path used to communicate with the runner process. fn pipe_name(suffix: &str) -> String { let mut rng = SmallRng::from_entropy(); - format!(r"\\.\pipe\codex-runner-{:x}-{}", rng.gen::(), suffix) + format!( + r"\\.\pipe\codex-runner-{:x}-{}", + rng.r#gen::(), + suffix + ) } /// Creates a named pipe whose DACL only allows the sandbox user to connect. @@ -507,8 +511,8 @@ pub use windows_impl::run_windows_sandbox_capture; #[cfg(not(target_os = "windows"))] mod stub { use super::ElevatedSandboxCaptureRequest; - use anyhow::bail; use anyhow::Result; + use anyhow::bail; #[derive(Debug, Default)] pub struct CaptureResult { diff --git a/codex-rs/windows-sandbox-rs/src/env.rs b/codex-rs/windows-sandbox-rs/src/env.rs index c69a0033e9..c3f85cfffd 100644 --- a/codex-rs/windows-sandbox-rs/src/env.rs +++ b/codex-rs/windows-sandbox-rs/src/env.rs @@ -30,15 +30,15 @@ pub fn ensure_non_interactive_pager(env_map: &mut HashMap) { // Keep PATH and PATHEXT stable for callers that rely on inheriting the parent process env. pub fn inherit_path_env(env_map: &mut HashMap) { - if !env_map.contains_key("PATH") { - if let Ok(path) = env::var("PATH") { - env_map.insert("PATH".into(), path); - } + if !env_map.contains_key("PATH") + && let Ok(path) = env::var("PATH") + { + env_map.insert("PATH".into(), path); } - if !env_map.contains_key("PATHEXT") { - if let Ok(pathext) = env::var("PATHEXT") { - env_map.insert("PATHEXT".into(), pathext); - } + if !env_map.contains_key("PATHEXT") + && let Ok(pathext) = env::var("PATHEXT") + { + env_map.insert("PATHEXT".into(), pathext); } } diff --git a/codex-rs/windows-sandbox-rs/src/firewall.rs b/codex-rs/windows-sandbox-rs/src/firewall.rs index a95d3b57e2..c0caa165fc 100644 --- a/codex-rs/windows-sandbox-rs/src/firewall.rs +++ b/codex-rs/windows-sandbox-rs/src/firewall.rs @@ -4,25 +4,25 @@ use anyhow::Result; use std::fs::File; use std::io::Write; -use windows::core::Interface; -use windows::core::BSTR; use windows::Win32::Foundation::VARIANT_TRUE; use windows::Win32::NetworkManagement::WindowsFirewall::INetFwPolicy2; use windows::Win32::NetworkManagement::WindowsFirewall::INetFwRule3; use windows::Win32::NetworkManagement::WindowsFirewall::INetFwRules; -use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2; -use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_ACTION_BLOCK; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_ANY; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_TCP; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_UDP; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_PROFILE2_ALL; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_OUT; +use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2; +use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule; +use windows::Win32::System::Com::CLSCTX_INPROC_SERVER; +use windows::Win32::System::Com::COINIT_APARTMENTTHREADED; use windows::Win32::System::Com::CoCreateInstance; use windows::Win32::System::Com::CoInitializeEx; use windows::Win32::System::Com::CoUninitialize; -use windows::Win32::System::Com::CLSCTX_INPROC_SERVER; -use windows::Win32::System::Com::COINIT_APARTMENTTHREADED; +use windows::core::BSTR; +use windows::core::Interface; use codex_windows_sandbox::SetupErrorCode; use codex_windows_sandbox::SetupFailure; @@ -40,8 +40,7 @@ const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY: &str = const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback UDP"; const OFFLINE_PROXY_ALLOW_RULE_NAME: &str = "codex_sandbox_offline_allow_loopback_proxy"; const LOOPBACK_REMOTE_ADDRESSES: &str = "127.0.0.0/8,::1"; -const NON_LOOPBACK_REMOTE_ADDRESSES: &str = - "0.0.0.0-126.255.255.255,128.0.0.0-255.255.255.255,::,::2-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; +const NON_LOOPBACK_REMOTE_ADDRESSES: &str = "0.0.0.0-126.255.255.255,128.0.0.0-255.255.255.255,::,::2-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; struct BlockRuleSpec<'a> { internal_name: &'a str, diff --git a/codex-rs/windows-sandbox-rs/src/helper_materialization.rs b/codex-rs/windows-sandbox-rs/src/helper_materialization.rs index 37db3c59c2..375199f5cc 100644 --- a/codex-rs/windows-sandbox-rs/src/helper_materialization.rs +++ b/codex-rs/windows-sandbox-rs/src/helper_materialization.rs @@ -45,12 +45,12 @@ pub(crate) fn helper_bin_dir(codex_home: &Path) -> PathBuf { } pub(crate) fn legacy_lookup(kind: HelperExecutable) -> PathBuf { - if let Ok(exe) = std::env::current_exe() { - if let Some(dir) = exe.parent() { - let candidate = dir.join(kind.file_name()); - if candidate.exists() { - return candidate; - } + if let Ok(exe) = std::env::current_exe() + && let Some(dir) = exe.parent() + { + let candidate = dir.join(kind.file_name()); + if candidate.exists() { + return candidate; } } PathBuf::from(kind.file_name()) @@ -377,4 +377,3 @@ mod tests { ); } } - diff --git a/codex-rs/windows-sandbox-rs/src/lib.rs b/codex-rs/windows-sandbox-rs/src/lib.rs index 11afda750d..b0de8c2e02 100644 --- a/codex-rs/windows-sandbox-rs/src/lib.rs +++ b/codex-rs/windows-sandbox-rs/src/lib.rs @@ -1,3 +1,7 @@ +// Rust 2024 surfaces this lint across the crate; keep the edition bump separate +// from the eventual unsafe cleanup. +#![allow(unsafe_op_in_unsafe_fn)] + macro_rules! windows_modules { ($($name:ident),+ $(,)?) => { $(#[cfg(target_os = "windows")] mod $name;)+ @@ -70,10 +74,10 @@ pub use dpapi::protect as dpapi_protect; #[cfg(target_os = "windows")] pub use dpapi::unprotect as dpapi_unprotect; #[cfg(target_os = "windows")] -pub use elevated_impl::run_windows_sandbox_capture as run_windows_sandbox_capture_elevated; -#[cfg(target_os = "windows")] pub use elevated_impl::ElevatedSandboxCaptureRequest; #[cfg(target_os = "windows")] +pub use elevated_impl::run_windows_sandbox_capture as run_windows_sandbox_capture_elevated; +#[cfg(target_os = "windows")] pub use helper_materialization::resolve_current_exe_for_launch; #[cfg(target_os = "windows")] pub use hide_users::hide_current_user_profile_dir; @@ -84,15 +88,21 @@ pub use identity::require_logon_sandbox_creds; #[cfg(target_os = "windows")] pub use identity::sandbox_setup_is_complete; #[cfg(target_os = "windows")] -pub use logging::log_note; -#[cfg(target_os = "windows")] pub use logging::LOG_FILE_NAME; #[cfg(target_os = "windows")] +pub use logging::log_note; +#[cfg(target_os = "windows")] pub use path_normalization::canonicalize_path; #[cfg(target_os = "windows")] +pub use policy::SandboxPolicy; +#[cfg(target_os = "windows")] pub use policy::parse_policy; #[cfg(target_os = "windows")] -pub use policy::SandboxPolicy; +pub use process::PipeSpawnHandles; +#[cfg(target_os = "windows")] +pub use process::StderrMode; +#[cfg(target_os = "windows")] +pub use process::StdinMode; #[cfg(target_os = "windows")] pub use process::create_process_as_user; #[cfg(target_os = "windows")] @@ -100,11 +110,11 @@ pub use process::read_handle_loop; #[cfg(target_os = "windows")] pub use process::spawn_process_with_pipes; #[cfg(target_os = "windows")] -pub use process::PipeSpawnHandles; +pub use setup::SETUP_VERSION; #[cfg(target_os = "windows")] -pub use process::StderrMode; +pub use setup::SandboxSetupRequest; #[cfg(target_os = "windows")] -pub use process::StdinMode; +pub use setup::SetupRootOverrides; #[cfg(target_os = "windows")] pub use setup::run_elevated_setup; #[cfg(target_os = "windows")] @@ -118,11 +128,11 @@ pub use setup::sandbox_dir; #[cfg(target_os = "windows")] pub use setup::sandbox_secrets_dir; #[cfg(target_os = "windows")] -pub use setup::SandboxSetupRequest; +pub use setup_error::SetupErrorCode; #[cfg(target_os = "windows")] -pub use setup::SetupRootOverrides; +pub use setup_error::SetupErrorReport; #[cfg(target_os = "windows")] -pub use setup::SETUP_VERSION; +pub use setup_error::SetupFailure; #[cfg(target_os = "windows")] pub use setup_error::extract_failure as extract_setup_failure; #[cfg(target_os = "windows")] @@ -132,12 +142,6 @@ pub use setup_error::setup_error_path; #[cfg(target_os = "windows")] pub use setup_error::write_setup_error_report; #[cfg(target_os = "windows")] -pub use setup_error::SetupErrorCode; -#[cfg(target_os = "windows")] -pub use setup_error::SetupErrorReport; -#[cfg(target_os = "windows")] -pub use setup_error::SetupFailure; -#[cfg(target_os = "windows")] pub use token::convert_string_sid_to_sid; #[cfg(target_os = "windows")] pub use token::create_readonly_token_with_cap_from; @@ -148,14 +152,14 @@ pub use token::create_workspace_write_token_with_caps_from; #[cfg(target_os = "windows")] pub use token::get_current_token_for_restriction; #[cfg(target_os = "windows")] +pub use windows_impl::CaptureResult; +#[cfg(target_os = "windows")] pub use windows_impl::run_windows_sandbox_capture; #[cfg(target_os = "windows")] pub use windows_impl::run_windows_sandbox_capture_with_extra_deny_write_paths; #[cfg(target_os = "windows")] pub use windows_impl::run_windows_sandbox_legacy_preflight; #[cfg(target_os = "windows")] -pub use windows_impl::CaptureResult; -#[cfg(target_os = "windows")] pub use winutil::quote_windows_arg; #[cfg(target_os = "windows")] pub use winutil::string_from_sid_bytes; @@ -168,14 +172,14 @@ pub use workspace_acl::protect_workspace_agents_dir; #[cfg(target_os = "windows")] pub use workspace_acl::protect_workspace_codex_dir; +#[cfg(not(target_os = "windows"))] +pub use stub::CaptureResult; #[cfg(not(target_os = "windows"))] pub use stub::apply_world_writable_scan_and_denies; #[cfg(not(target_os = "windows"))] pub use stub::run_windows_sandbox_capture; #[cfg(not(target_os = "windows"))] pub use stub::run_windows_sandbox_legacy_preflight; -#[cfg(not(target_os = "windows"))] -pub use stub::CaptureResult; #[cfg(target_os = "windows")] mod windows_impl { @@ -183,8 +187,8 @@ mod windows_impl { use super::acl::add_deny_write_ace; use super::acl::allow_null_device; use super::acl::revoke_ace; - use super::allow::compute_allow_paths; use super::allow::AllowDenyPaths; + use super::allow::compute_allow_paths; use super::cap::load_or_create_cap_sids; use super::cap::workspace_cap_sid_for_cwd; use super::env::apply_no_network_to_env; @@ -194,8 +198,8 @@ mod windows_impl { use super::logging::log_start; use super::logging::log_success; use super::path_normalization::canonicalize_path; - use super::policy::parse_policy; use super::policy::SandboxPolicy; + use super::policy::parse_policy; use super::process::create_process_as_user; use super::token::convert_string_sid_to_sid; use super::token::create_workspace_write_token_with_caps_from; @@ -211,13 +215,13 @@ mod windows_impl { use std::ptr; use windows_sys::Win32::Foundation::CloseHandle; use windows_sys::Win32::Foundation::GetLastError; - use windows_sys::Win32::Foundation::SetHandleInformation; use windows_sys::Win32::Foundation::HANDLE; use windows_sys::Win32::Foundation::HANDLE_FLAG_INHERIT; + use windows_sys::Win32::Foundation::SetHandleInformation; use windows_sys::Win32::System::Pipes::CreatePipe; use windows_sys::Win32::System::Threading::GetExitCodeProcess; - use windows_sys::Win32::System::Threading::WaitForSingleObject; use windows_sys::Win32::System::Threading::INFINITE; + use windows_sys::Win32::System::Threading::WaitForSingleObject; type PipeHandles = ((HANDLE, HANDLE), (HANDLE, HANDLE), (HANDLE, HANDLE)); @@ -355,15 +359,15 @@ mod windows_impl { }; unsafe { - if is_workspace_write { - if let Ok(base) = super::token::get_current_token_for_restriction() { - if let Ok(bytes) = super::token::get_logon_sid_bytes(base) { - let mut tmp = bytes.clone(); - let psid2 = tmp.as_mut_ptr() as *mut c_void; - allow_null_device(psid2); - } - windows_sys::Win32::Foundation::CloseHandle(base); + if is_workspace_write + && let Ok(base) = super::token::get_current_token_for_restriction() + { + if let Ok(bytes) = super::token::get_logon_sid_bytes(base) { + let mut tmp = bytes.clone(); + let psid2 = tmp.as_mut_ptr() as *mut c_void; + allow_null_device(psid2); } + windows_sys::Win32::Foundation::CloseHandle(base); } } @@ -384,23 +388,24 @@ mod windows_impl { } else { psid_generic }; - if let Ok(added) = add_allow_ace(p, psid) { - if added { - if persist_aces { - if p.is_dir() { - // best-effort seeding omitted intentionally - } - } else { - guards.push((p.clone(), psid)); + if let Ok(added) = add_allow_ace(p, psid) + && added + { + if persist_aces { + if p.is_dir() { + // best-effort seeding omitted intentionally } + } else { + guards.push((p.clone(), psid)); } } } for p in &deny { - if let Ok(added) = add_deny_write_ace(p, psid_generic) { - if added && !persist_aces { - guards.push((p.clone(), psid_generic)); - } + if let Ok(added) = add_deny_write_ace(p, psid_generic) + && added + && !persist_aces + { + guards.push((p.clone(), psid_generic)); } } allow_null_device(psid_generic); @@ -631,8 +636,8 @@ mod windows_impl { #[cfg(not(target_os = "windows"))] mod stub { - use anyhow::bail; use anyhow::Result; + use anyhow::bail; use codex_protocol::protocol::SandboxPolicy; use std::collections::HashMap; use std::path::Path; diff --git a/codex-rs/windows-sandbox-rs/src/logging.rs b/codex-rs/windows-sandbox-rs/src/logging.rs index f575ce3b8d..1c0d695f32 100644 --- a/codex-rs/windows-sandbox-rs/src/logging.rs +++ b/codex-rs/windows-sandbox-rs/src/logging.rs @@ -37,12 +37,11 @@ fn log_file_path(base_dir: &Path) -> Option { } fn append_line(line: &str, base_dir: Option<&Path>) { - if let Some(dir) = base_dir { - if let Some(path) = log_file_path(dir) { - if let Ok(mut f) = OpenOptions::new().create(true).append(true).open(path) { - let _ = writeln!(f, "{}", line); - } - } + if let Some(dir) = base_dir + && let Some(path) = log_file_path(dir) + && let Ok(mut f) = OpenOptions::new().create(true).append(true).open(path) + { + let _ = writeln!(f, "{line}"); } } diff --git a/codex-rs/windows-sandbox-rs/src/read_acl_mutex.rs b/codex-rs/windows-sandbox-rs/src/read_acl_mutex.rs index 0b3435fec1..643d70fa37 100644 --- a/codex-rs/windows-sandbox-rs/src/read_acl_mutex.rs +++ b/codex-rs/windows-sandbox-rs/src/read_acl_mutex.rs @@ -1,14 +1,14 @@ use anyhow::Result; use std::ffi::OsStr; use windows_sys::Win32::Foundation::CloseHandle; -use windows_sys::Win32::Foundation::GetLastError; use windows_sys::Win32::Foundation::ERROR_ALREADY_EXISTS; use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND; +use windows_sys::Win32::Foundation::GetLastError; use windows_sys::Win32::Foundation::HANDLE; use windows_sys::Win32::System::Threading::CreateMutexW; +use windows_sys::Win32::System::Threading::MUTEX_ALL_ACCESS; use windows_sys::Win32::System::Threading::OpenMutexW; use windows_sys::Win32::System::Threading::ReleaseMutex; -use windows_sys::Win32::System::Threading::MUTEX_ALL_ACCESS; use super::to_wide; diff --git a/codex-rs/windows-sandbox-rs/src/sandbox_users.rs b/codex-rs/windows-sandbox-rs/src/sandbox_users.rs index e006b696fb..13460b9444 100644 --- a/codex-rs/windows-sandbox-rs/src/sandbox_users.rs +++ b/codex-rs/windows-sandbox-rs/src/sandbox_users.rs @@ -1,27 +1,27 @@ #![cfg(target_os = "windows")] use anyhow::Result; -use base64::engine::general_purpose::STANDARD as BASE64; use base64::Engine; -use rand::rngs::SmallRng; +use base64::engine::general_purpose::STANDARD as BASE64; use rand::RngCore; use rand::SeedableRng; +use rand::rngs::SmallRng; use serde::Serialize; -use std::ffi::c_void; use std::ffi::OsStr; +use std::ffi::c_void; use std::fs::File; use std::path::Path; use std::path::PathBuf; +use windows_sys::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER; use windows_sys::Win32::Foundation::GetLastError; use windows_sys::Win32::Foundation::LocalFree; -use windows_sys::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER; +use windows_sys::Win32::NetworkManagement::NetManagement::LOCALGROUP_INFO_1; +use windows_sys::Win32::NetworkManagement::NetManagement::LOCALGROUP_MEMBERS_INFO_3; use windows_sys::Win32::NetworkManagement::NetManagement::NERR_Success; use windows_sys::Win32::NetworkManagement::NetManagement::NetLocalGroupAdd; use windows_sys::Win32::NetworkManagement::NetManagement::NetLocalGroupAddMembers; use windows_sys::Win32::NetworkManagement::NetManagement::NetUserAdd; use windows_sys::Win32::NetworkManagement::NetManagement::NetUserSetInfo; -use windows_sys::Win32::NetworkManagement::NetManagement::LOCALGROUP_INFO_1; -use windows_sys::Win32::NetworkManagement::NetManagement::LOCALGROUP_MEMBERS_INFO_3; use windows_sys::Win32::NetworkManagement::NetManagement::UF_DONT_EXPIRE_PASSWD; use windows_sys::Win32::NetworkManagement::NetManagement::UF_SCRIPT; use windows_sys::Win32::NetworkManagement::NetManagement::USER_INFO_1; @@ -34,14 +34,14 @@ use windows_sys::Win32::Security::LookupAccountNameW; use windows_sys::Win32::Security::LookupAccountSidW; use windows_sys::Win32::Security::SID_NAME_USE; +use codex_windows_sandbox::SETUP_VERSION; +use codex_windows_sandbox::SetupErrorCode; +use codex_windows_sandbox::SetupFailure; use codex_windows_sandbox::dpapi_protect; use codex_windows_sandbox::sandbox_dir; use codex_windows_sandbox::sandbox_secrets_dir; use codex_windows_sandbox::string_from_sid_bytes; use codex_windows_sandbox::to_wide; -use codex_windows_sandbox::SetupErrorCode; -use codex_windows_sandbox::SetupFailure; -use codex_windows_sandbox::SETUP_VERSION; pub const SANDBOX_USERS_GROUP: &str = "CodexSandboxUsers"; const SANDBOX_USERS_GROUP_COMMENT: &str = "Codex sandbox internal group (managed)"; diff --git a/codex-rs/windows-sandbox-rs/src/setup_error.rs b/codex-rs/windows-sandbox-rs/src/setup_error.rs index 23022450a0..64e618ace1 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_error.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_error.rs @@ -189,15 +189,16 @@ pub fn sanitize_setup_metric_tag_value(value: &str) -> String { fn redact_home_paths(value: &str) -> String { let mut usernames: Vec = Vec::new(); - if let Ok(username) = std::env::var("USERNAME") { - if !username.trim().is_empty() { - usernames.push(username); - } + if let Ok(username) = std::env::var("USERNAME") + && !username.trim().is_empty() + { + usernames.push(username); } - if let Ok(user) = std::env::var("USER") { - if !user.trim().is_empty() && !usernames.iter().any(|v| v.eq_ignore_ascii_case(&user)) { - usernames.push(user); - } + if let Ok(user) = std::env::var("USER") + && !user.trim().is_empty() + && !usernames.iter().any(|v| v.eq_ignore_ascii_case(&user)) + { + usernames.push(user); } redact_username_segments(value, &usernames) diff --git a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs index 8a72b22a91..5696432853 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs @@ -4,8 +4,13 @@ mod firewall; use anyhow::Context; use anyhow::Result; -use base64::engine::general_purpose::STANDARD as BASE64; use base64::Engine; +use base64::engine::general_purpose::STANDARD as BASE64; +use codex_windows_sandbox::LOG_FILE_NAME; +use codex_windows_sandbox::SETUP_VERSION; +use codex_windows_sandbox::SetupErrorCode; +use codex_windows_sandbox::SetupErrorReport; +use codex_windows_sandbox::SetupFailure; use codex_windows_sandbox::canonicalize_path; use codex_windows_sandbox::convert_string_sid_to_sid; use codex_windows_sandbox::ensure_allow_mask_aces_with_inheritance; @@ -25,16 +30,11 @@ use codex_windows_sandbox::string_from_sid_bytes; use codex_windows_sandbox::to_wide; use codex_windows_sandbox::workspace_cap_sid_for_cwd; use codex_windows_sandbox::write_setup_error_report; -use codex_windows_sandbox::SetupErrorCode; -use codex_windows_sandbox::SetupErrorReport; -use codex_windows_sandbox::SetupFailure; -use codex_windows_sandbox::LOG_FILE_NAME; -use codex_windows_sandbox::SETUP_VERSION; use serde::Deserialize; use serde::Serialize; use std::collections::HashSet; -use std::ffi::c_void; use std::ffi::OsStr; +use std::ffi::c_void; use std::fs::File; use std::io::Write; use std::os::windows::process::CommandExt; @@ -44,17 +44,17 @@ use std::process::Command; use std::process::Stdio; use std::sync::mpsc; use windows_sys::Win32::Foundation::GetLastError; -use windows_sys::Win32::Foundation::LocalFree; use windows_sys::Win32::Foundation::HLOCAL; +use windows_sys::Win32::Foundation::LocalFree; +use windows_sys::Win32::Security::ACL; use windows_sys::Win32::Security::Authorization::ConvertStringSidToSidW; -use windows_sys::Win32::Security::Authorization::SetEntriesInAclW; -use windows_sys::Win32::Security::Authorization::SetNamedSecurityInfoW; use windows_sys::Win32::Security::Authorization::EXPLICIT_ACCESS_W; use windows_sys::Win32::Security::Authorization::GRANT_ACCESS; use windows_sys::Win32::Security::Authorization::SE_FILE_OBJECT; +use windows_sys::Win32::Security::Authorization::SetEntriesInAclW; +use windows_sys::Win32::Security::Authorization::SetNamedSecurityInfoW; use windows_sys::Win32::Security::Authorization::TRUSTEE_IS_SID; use windows_sys::Win32::Security::Authorization::TRUSTEE_W; -use windows_sys::Win32::Security::ACL; use windows_sys::Win32::Security::CONTAINER_INHERIT_ACE; use windows_sys::Win32::Security::DACL_SECURITY_INFORMATION; use windows_sys::Win32::Security::OBJECT_INHERIT_ACE; diff --git a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs index 962a53d60c..f6583e0de4 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs @@ -10,22 +10,22 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; -use crate::allow::compute_allow_paths; use crate::allow::AllowDenyPaths; +use crate::allow::compute_allow_paths; use crate::helper_materialization::helper_bin_dir; use crate::logging::log_note; use crate::path_normalization::canonical_path_key; use crate::policy::SandboxPolicy; +use crate::setup_error::SetupErrorCode; +use crate::setup_error::SetupFailure; use crate::setup_error::clear_setup_error_report; use crate::setup_error::failure; use crate::setup_error::read_setup_error_report; -use crate::setup_error::SetupErrorCode; -use crate::setup_error::SetupFailure; -use anyhow::anyhow; use anyhow::Context; use anyhow::Result; -use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; +use anyhow::anyhow; use base64::Engine; +use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use windows_sys::Win32::Foundation::CloseHandle; use windows_sys::Win32::Foundation::GetLastError; @@ -332,10 +332,10 @@ fn profile_read_roots(user_profile: &Path) -> Vec { fn gather_helper_read_roots(codex_home: &Path) -> Vec { let mut roots = Vec::new(); - if let Ok(exe) = std::env::current_exe() { - if let Some(dir) = exe.parent() { - roots.push(dir.to_path_buf()); - } + if let Ok(exe) = std::env::current_exe() + && let Some(dir) = exe.parent() + { + roots.push(dir.to_path_buf()); } let helper_dir = helper_bin_dir(codex_home); let _ = std::fs::create_dir_all(&helper_dir); @@ -503,10 +503,10 @@ pub(crate) fn offline_proxy_settings_from_env( pub(crate) fn proxy_ports_from_env(env_map: &HashMap) -> Vec { let mut ports = BTreeSet::new(); for key in PROXY_ENV_KEYS { - if let Some(value) = env_map.get(*key) { - if let Some(port) = loopback_proxy_port_from_url(value) { - ports.insert(port); - } + if let Some(value) = env_map.get(*key) + && let Some(port) = loopback_proxy_port_from_url(value) + { + ports.insert(port); } } ports.into_iter().collect() @@ -570,12 +570,12 @@ fn quote_arg(arg: &str) -> String { } fn find_setup_exe() -> PathBuf { - if let Ok(exe) = std::env::current_exe() { - if let Some(dir) = exe.parent() { - let candidate = dir.join("codex-windows-sandbox-setup.exe"); - if candidate.exists() { - return candidate; - } + if let Ok(exe) = std::env::current_exe() + && let Some(dir) = exe.parent() + { + let candidate = dir.join("codex-windows-sandbox-setup.exe"); + if candidate.exists() { + return candidate; } } PathBuf::from("codex-windows-sandbox-setup.exe") @@ -606,11 +606,11 @@ fn run_setup_exe( codex_home: &Path, ) -> Result<()> { use windows_sys::Win32::System::Threading::GetExitCodeProcess; - use windows_sys::Win32::System::Threading::WaitForSingleObject; use windows_sys::Win32::System::Threading::INFINITE; - use windows_sys::Win32::UI::Shell::ShellExecuteExW; + use windows_sys::Win32::System::Threading::WaitForSingleObject; use windows_sys::Win32::UI::Shell::SEE_MASK_NOCLOSEPROCESS; use windows_sys::Win32::UI::Shell::SHELLEXECUTEINFOW; + use windows_sys::Win32::UI::Shell::ShellExecuteExW; let exe = find_setup_exe(); let payload_json = serde_json::to_string(payload).map_err(|err| { failure( @@ -801,13 +801,13 @@ fn filter_sensitive_write_roots(mut roots: Vec, codex_home: &Path) -> V #[cfg(test)] mod tests { + use super::WINDOWS_PLATFORM_DEFAULT_READ_ROOTS; use super::gather_legacy_full_read_roots; use super::gather_read_roots; use super::loopback_proxy_port_from_url; use super::offline_proxy_settings_from_env; use super::profile_read_roots; use super::proxy_ports_from_env; - use super::WINDOWS_PLATFORM_DEFAULT_READ_ROOTS; use crate::helper_materialization::helper_bin_dir; use crate::policy::SandboxPolicy; use codex_protocol::protocol::ReadOnlyAccess; @@ -1020,8 +1020,10 @@ mod tests { let policy = SandboxPolicy::ReadOnly { access: ReadOnlyAccess::Restricted { include_platform_defaults: false, - readable_roots: vec![AbsolutePathBuf::from_absolute_path(&readable_root) - .expect("absolute readable root")], + readable_roots: vec![ + AbsolutePathBuf::from_absolute_path(&readable_root) + .expect("absolute readable root"), + ], }, network_access: false, }; @@ -1036,9 +1038,11 @@ mod tests { assert!(roots.contains(&expected_helper)); assert!(roots.contains(&expected_cwd)); assert!(roots.contains(&expected_readable)); - assert!(canonical_windows_platform_default_roots() - .into_iter() - .all(|path| !roots.contains(&path))); + assert!( + canonical_windows_platform_default_roots() + .into_iter() + .all(|path| !roots.contains(&path)) + ); } #[test] @@ -1057,9 +1061,11 @@ mod tests { let roots = gather_read_roots(&command_cwd, &policy, &codex_home); - assert!(canonical_windows_platform_default_roots() - .into_iter() - .all(|path| roots.contains(&path))); + assert!( + canonical_windows_platform_default_roots() + .into_iter() + .all(|path| roots.contains(&path)) + ); } #[test] @@ -1071,8 +1077,10 @@ mod tests { fs::create_dir_all(&command_cwd).expect("create workspace"); fs::create_dir_all(&writable_root).expect("create writable root"); let policy = SandboxPolicy::WorkspaceWrite { - writable_roots: vec![AbsolutePathBuf::from_absolute_path(&writable_root) - .expect("absolute writable root")], + writable_roots: vec![ + AbsolutePathBuf::from_absolute_path(&writable_root) + .expect("absolute writable root"), + ], read_only_access: ReadOnlyAccess::Restricted { include_platform_defaults: false, readable_roots: Vec::new(), @@ -1099,8 +1107,10 @@ mod tests { let roots = gather_legacy_full_read_roots(&command_cwd, &policy, &codex_home); - assert!(canonical_windows_platform_default_roots() - .into_iter() - .all(|path| roots.contains(&path))); + assert!( + canonical_windows_platform_default_roots() + .into_iter() + .all(|path| roots.contains(&path)) + ); } } diff --git a/codex-rs/windows-sandbox-rs/src/token.rs b/codex-rs/windows-sandbox-rs/src/token.rs index 0cc9f59da4..56d97ff01e 100644 --- a/codex-rs/windows-sandbox-rs/src/token.rs +++ b/codex-rs/windows-sandbox-rs/src/token.rs @@ -130,7 +130,7 @@ pub unsafe fn world_sid() -> Result> { /// Caller is responsible for freeing the returned SID with `LocalFree`. pub unsafe fn convert_string_sid_to_sid(s: &str) -> Option<*mut c_void> { #[link(name = "advapi32")] - extern "system" { + unsafe extern "system" { fn ConvertStringSidToSidW(StringSid: *const u16, Sid: *mut *mut c_void) -> i32; } let mut psid: *mut c_void = std::ptr::null_mut(); @@ -153,7 +153,7 @@ pub unsafe fn get_current_token_for_restriction() -> Result { | TOKEN_ADJUST_PRIVILEGES; let mut h: HANDLE = 0; #[link(name = "advapi32")] - extern "system" { + unsafe extern "system" { fn OpenProcessToken( ProcessHandle: HANDLE, DesiredAccess: u32, From 6a0c4709ca2154e9f3ebb07e58fb156386630188 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 27 Mar 2026 11:30:22 +0100 Subject: [PATCH 034/191] feat: spawn v2 make task name as mandatory (#15986) --- .../src/tools/handlers/multi_agents_tests.rs | 34 +++++++++++++++++++ .../tools/handlers/multi_agents_v2/spawn.rs | 16 +++++---- codex-rs/core/src/tools/spec.rs | 10 +++--- codex-rs/core/src/tools/spec_tests.rs | 2 +- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs index e57491762c..7f1f966569 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs @@ -321,6 +321,40 @@ async fn spawn_agent_returns_agent_id_without_task_name() { assert_eq!(success, Some(true)); } +#[tokio::test] +async fn multi_agent_v2_spawn_requires_task_name() { + let (mut session, mut turn) = make_session_and_context().await; + let manager = thread_manager(); + let root = manager + .start_thread((*turn.config).clone()) + .await + .expect("root thread should start"); + session.services.agent_control = manager.agent_control(); + session.conversation_id = root.thread_id; + let mut config = (*turn.config).clone(); + config + .features + .enable(Feature::MultiAgentV2) + .expect("test config should allow feature update"); + turn.config = Arc::new(config); + + let invocation = invocation( + Arc::new(session), + Arc::new(turn), + "spawn_agent", + function_payload(json!({ + "message": "inspect this repo" + })), + ); + let Err(err) = SpawnAgentHandlerV2.handle(invocation).await else { + panic!("missing task_name should be rejected"); + }; + let FunctionCallError::RespondToModel(message) = err else { + panic!("missing task_name should surface as a model-facing error"); + }; + assert!(message.contains("missing field `task_name`")); +} + #[tokio::test] async fn spawn_agent_errors_when_manager_dropped() { let (session, turn) = make_session_and_context().await; diff --git a/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs b/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs index d67be936ec..6cdad3c1c2 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs @@ -83,7 +83,7 @@ impl ToolHandler for Handler { &turn.session_source, child_depth, role_name, - args.task_name.clone(), + Some(args.task_name.clone()), )?), SpawnAgentOptions { fork_parent_spawn_call_id: args.fork_context.then(|| call_id.clone()), @@ -132,7 +132,6 @@ impl ToolHandler for Handler { .and_then(|snapshot| snapshot.reasoning_effort) .unwrap_or(args.reasoning_effort.unwrap_or_default()); let nickname = new_agent_nickname.clone(); - let task_name = new_agent_path.clone(); session .send_event( &turn, @@ -150,16 +149,21 @@ impl ToolHandler for Handler { .into(), ) .await; - let new_thread_id = result?.thread_id; + let _ = result?; let role_tag = role_name.unwrap_or(DEFAULT_ROLE_NAME); turn.session_telemetry.counter( "codex.multi_agent.spawn", /*inc*/ 1, &[("role", role_tag)], ); + let task_name = new_agent_path.ok_or_else(|| { + FunctionCallError::RespondToModel( + "spawned agent is missing a canonical task name".to_string(), + ) + })?; Ok(SpawnAgentResult { - agent_id: task_name.is_none().then(|| new_thread_id.to_string()), + agent_id: None, task_name, nickname, }) @@ -170,7 +174,7 @@ impl ToolHandler for Handler { struct SpawnAgentArgs { message: Option, items: Option>, - task_name: Option, + task_name: String, agent_type: Option, model: Option, reasoning_effort: Option, @@ -181,7 +185,7 @@ struct SpawnAgentArgs { #[derive(Debug, Serialize)] pub(crate) struct SpawnAgentResult { agent_id: Option, - task_name: Option, + task_name: String, nickname: Option, } diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index ea85eaacdd..1a2f16b5c8 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -157,10 +157,10 @@ fn spawn_agent_output_schema_v2() -> JsonValue { "properties": { "agent_id": { "type": ["string", "null"], - "description": "Thread identifier for the spawned agent when no task name was assigned." + "description": "Legacy thread identifier for the spawned agent." }, "task_name": { - "type": ["string", "null"], + "type": "string", "description": "Canonical task name for the spawned agent." }, "nickname": { @@ -1215,13 +1215,13 @@ fn create_spawn_agent_tool_v1(config: &ToolsConfig) -> ToolSpec { fn create_spawn_agent_tool_v2(config: &ToolsConfig) -> ToolSpec { let available_models_description = spawn_agent_models_description(&config.available_models); - let return_value_description = "Returns the canonical task name when the spawned agent was named, otherwise the agent id, plus the user-facing nickname when available."; + let return_value_description = "Returns the canonical task name for the spawned agent, plus the user-facing nickname when available."; let mut properties = spawn_agent_common_properties(config); properties.insert( "task_name".to_string(), JsonSchema::String { description: Some( - "Optional task name for the new agent. Use lowercase letters, digits, and underscores." + "Task name for the new agent. Use lowercase letters, digits, and underscores." .to_string(), ), }, @@ -1237,7 +1237,7 @@ fn create_spawn_agent_tool_v2(config: &ToolsConfig) -> ToolSpec { defer_loading: None, parameters: JsonSchema::Object { properties, - required: None, + required: Some(vec!["task_name".to_string()]), additional_properties: Some(false.into()), }, output_schema: Some(spawn_agent_output_schema_v2()), diff --git a/codex-rs/core/src/tools/spec_tests.rs b/codex-rs/core/src/tools/spec_tests.rs index 5b9016ec6a..b7b9f001e5 100644 --- a/codex-rs/core/src/tools/spec_tests.rs +++ b/codex-rs/core/src/tools/spec_tests.rs @@ -454,7 +454,7 @@ fn test_build_specs_multi_agent_v2_uses_task_names_and_hides_resume() { panic!("spawn_agent should use object params"); }; assert!(properties.contains_key("task_name")); - assert_eq!(required.as_ref(), None); + assert_eq!(required.as_ref(), Some(&vec!["task_name".to_string()])); let output_schema = output_schema .as_ref() .expect("spawn_agent should define output schema"); From 270b7655cd46e8f7573aea122f87b619b225a754 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 27 Mar 2026 14:49:45 +0100 Subject: [PATCH 035/191] Use codex-utils-template for login error page (#16000) --- codex-rs/Cargo.lock | 1 + codex-rs/login/Cargo.toml | 1 + codex-rs/login/src/assets/error.html | 10 ++--- codex-rs/login/src/server.rs | 60 ++++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 17d5c35e83..03e8dc5b63 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2224,6 +2224,7 @@ dependencies = [ "codex-keyring-store", "codex-protocol", "codex-terminal-detection", + "codex-utils-template", "core_test_support", "keyring", "once_cell", diff --git a/codex-rs/login/Cargo.toml b/codex-rs/login/Cargo.toml index 7fd7815281..92ed39a6ce 100644 --- a/codex-rs/login/Cargo.toml +++ b/codex-rs/login/Cargo.toml @@ -17,6 +17,7 @@ codex-config = { workspace = true } codex-keyring-store = { workspace = true } codex-protocol = { workspace = true } codex-terminal-detection = { workspace = true } +codex-utils-template = { workspace = true } once_cell = { workspace = true } os_info = { workspace = true } rand = { workspace = true } diff --git a/codex-rs/login/src/assets/error.html b/codex-rs/login/src/assets/error.html index 96c1eb303c..e5fb4dba10 100644 --- a/codex-rs/login/src/assets/error.html +++ b/codex-rs/login/src/assets/error.html @@ -101,21 +101,21 @@

Codex login
-

__ERROR_TITLE__

-

__ERROR_MESSAGE__

+

{{error_title}}

+

{{error_message}}

Error code - __ERROR_CODE__ + {{error_code}}
Details - __ERROR_DESCRIPTION__ + {{error_description}}
-

__ERROR_HELP__

+

{{error_help}}

diff --git a/codex-rs/login/src/server.rs b/codex-rs/login/src/server.rs index b726eeed8f..c811fa36df 100644 --- a/codex-rs/login/src/server.rs +++ b/codex-rs/login/src/server.rs @@ -20,6 +20,7 @@ use std::net::TcpStream; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; +use std::sync::LazyLock; use std::thread; use std::time::Duration; @@ -35,6 +36,7 @@ use base64::Engine; use chrono::Utc; use codex_app_server_protocol::AuthMode; use codex_client::build_reqwest_client_with_custom_ca; +use codex_utils_template::Template; use rand::RngCore; use serde_json::Value as JsonValue; use tiny_http::Header; @@ -48,6 +50,10 @@ use tracing::warn; const DEFAULT_ISSUER: &str = "https://auth.openai.com"; const DEFAULT_PORT: u16 = 1455; +static LOGIN_ERROR_PAGE_TEMPLATE: LazyLock