diff --git a/codex-rs/windows-sandbox-service/src/ipc.rs b/codex-rs/windows-sandbox-service/src/ipc.rs index 57680e09e5..3c90977862 100644 --- a/codex-rs/windows-sandbox-service/src/ipc.rs +++ b/codex-rs/windows-sandbox-service/src/ipc.rs @@ -339,7 +339,7 @@ fn handle_request( } crate::service::log_error( crate::service::EVENT_REQUEST_REJECTED, - &format!("Codex sandbox provisioning was rejected by administrator policy: {error}"), + &policy_rejection_diagnostic(&error), ); return Err(error) .context("requested sandbox settings violate administrator-controlled machine policy"); @@ -353,6 +353,39 @@ fn handle_request( ) } +fn policy_rejection_diagnostic(error: &anyhow::Error) -> String { + // Only service-owned labels and typed codes belong in persistent events. + // Parser wrappers can stringify entire config lines, including credentials. + let stage = match error.to_string().as_str() { + "start managed configuration runtime" => "runtime", + "load bootstrap configuration" => "bootstrap", + "resolve cloud configuration authentication" => "resolve_auth", + "initialize cloud configuration authentication" => "initialize_auth", + "load managed configuration" => "managed", + "enforce managed provisioning requirements" => "enforce", + _ => "unknown", + }; + let detail = error.chain().find_map(|cause| { + let io = cause.downcast_ref::(); + let inner = io + .and_then(std::io::Error::get_ref) + .map(|inner| inner as &(dyn std::error::Error + 'static)) + .unwrap_or(cause); + if let Some(cloud) = inner.downcast_ref::() { + return Some(format!( + "cloud={:?} http={:?}", + cloud.code(), + cloud.status_code() + )); + } + io.map(|io| format!("io={:?} win32={:?}", io.kind(), io.raw_os_error())) + }); + let detail = detail.unwrap_or_else(|| "cause=unavailable".to_string()); + format!( + "Codex sandbox provisioning was rejected by administrator policy: stage={stage} {detail}" + ) +} + fn is_config_parse_error(error: &anyhow::Error) -> bool { error.chain().any(|cause| { cause.is::() diff --git a/codex-rs/windows-sandbox-service/src/ipc_tests.rs b/codex-rs/windows-sandbox-service/src/ipc_tests.rs index 00718a6db9..2a72163a42 100644 --- a/codex-rs/windows-sandbox-service/src/ipc_tests.rs +++ b/codex-rs/windows-sandbox-service/src/ipc_tests.rs @@ -7,6 +7,7 @@ use super::home::prepare_codex_home; use super::is_config_parse_error; use super::pin_existing_ancestors; use super::pipe_security_descriptor; +use super::policy_rejection_diagnostic; use super::refresh_session; use super::request::ProvisioningRequest; use super::response_error_message; @@ -264,6 +265,64 @@ fn config_parse_errors_are_distinguished_from_policy_and_io_failures() { } } +#[test] +fn policy_event_excludes_real_parser_values_but_retains_safe_codes() { + let contents = "Authorization = \"Bearer synthetic-secret"; + let requirements = + codex_config::compose_requirements([codex_config::RequirementsLayerEntry::from_toml( + codex_config::RequirementSource::Unknown, + contents, + )]) + .unwrap_err(); + let fragment = codex_config::cloud_config_layers_from_fragments( + [codex_config::CloudConfigFragment { + id: "synthetic-id".into(), + name: "private-name".into(), + contents: contents.into(), + }], + &Path::new(r"C:\CodexTest").try_into().unwrap(), + ) + .unwrap_err(); + for error in [ + std::io::Error::from(requirements), + std::io::Error::from(fragment), + ] { + let error = anyhow::Error::new(error).context("load managed configuration"); + assert!(format!("{error:#}").contains("synthetic-secret")); + assert!(!is_config_parse_error(&error)); + assert_eq!( + policy_rejection_diagnostic(&error), + "Codex sandbox provisioning was rejected by administrator policy: stage=managed io=InvalidData win32=None" + ); + } + + for (error, expected) in [ + ( + anyhow::Error::new(std::io::Error::from_raw_os_error(5)) + .context("load bootstrap configuration"), + "stage=bootstrap io=PermissionDenied win32=Some(5)", + ), + ( + anyhow::Error::new(codex_config::CloudConfigBundleLoadError::new( + codex_config::CloudConfigBundleLoadErrorCode::RequestFailed, + Some(503), + contents, + )) + .context("load managed configuration"), + "stage=managed cloud=RequestFailed http=Some(503)", + ), + ( + anyhow::anyhow!("{contents}"), + "stage=unknown cause=unavailable", + ), + ] { + assert_eq!( + policy_rejection_diagnostic(&error), + format!("Codex sandbox provisioning was rejected by administrator policy: {expected}") + ); + } +} + #[test] fn provisioning_request_preserves_home_spaces_and_unicode() { let request = framed_request(SandboxProvisioningRequest {