diff --git a/codex-rs/app-server/tests/suite/v2/config_server_tests.rs b/codex-rs/app-server/tests/suite/v2/config_server_tests.rs index 03edf525fc..bfcd506375 100644 --- a/codex-rs/app-server/tests/suite/v2/config_server_tests.rs +++ b/codex-rs/app-server/tests/suite/v2/config_server_tests.rs @@ -1,10 +1,12 @@ //! Exercises required HTTPS policy through real app-server startup and session RPCs. use super::https_mock::tls_listener; +use anyhow::Context; use anyhow::Result; use app_test_support::TestAppServer; use codex_app_server_protocol::LoginAccountResponse; use codex_app_server_protocol::LogoutAccountResponse; +use codex_app_server_protocol::RequestId; use codex_app_server_protocol::ThreadResumeParams; use codex_app_server_protocol::ThreadResumeResponse; use codex_app_server_protocol::ThreadStartParams; @@ -15,8 +17,10 @@ use codex_config::test_support::CloudConfigBundleFixture; use core_test_support::responses; use core_test_support::skip_if_no_network; use pretty_assertions::assert_eq; +use serde_json::json; use std::path::Path; use std::path::PathBuf; +use std::time::Duration; use tempfile::TempDir; use tempfile::tempdir; use tokio::net::TcpStream; @@ -32,10 +36,7 @@ async fn config_server_settings_are_fetched_and_applied_at_startup() -> Result<( skip_if_no_network!(Ok(())); let setup = ConfigServerSetup::new().await?; let mut app = setup.start_app().await?; - assert_eq!( - setup.config_server.received_requests().await.unwrap().len(), - 1 - ); + assert_eq!(setup.bundle_request_count().await?, 1); let started = app .start_thread(ThreadStartParams { model_provider: Some("openai".into()), @@ -50,6 +51,43 @@ async fn config_server_settings_are_fetched_and_applied_at_startup() -> Result<( Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn config_server_settings_recover_after_discovery_failure() -> Result<()> { + skip_if_no_network!(Ok(())); + let setup = ConfigServerSetup::new().await?; + let failure = Mock::given(method("GET")) + .and(path("/.well-known/oauth-authorization-server/team")) + .respond_with(ResponseTemplate::new(503)) + .expect(/*r*/ 1..) + .mount_as_scoped(&setup.config_server) + .await; + let mut app = setup.start_app_with_args(&[]).await?; + let request = app + .send_thread_start_request_with_auto_env(ThreadStartParams::default()) + .await?; + let error = tokio::time::timeout( + Duration::from_secs(/*secs*/ 30), + app.read_stream_until_error_message(RequestId::Integer(request)), + ) + .await??; + assert_eq!( + error.error.data, + Some(json!({ + "reason": "cloudConfigBundle", + "errorCode": "Internal", + "detail": "OAuth discovery failed", + })) + ); + assert_eq!(setup.bundle_request_count().await?, 0); + + // Missing metadata permits anonymous configuration retrieval. + drop(failure); + let started = app.start_thread(ThreadStartParams::default()).await?; + assert_eq!(started.model_provider, "company"); + app.shutdown_gracefully().await?; + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn config_server_settings_survive_openai_login_and_logout() -> Result<()> { skip_if_no_network!(Ok(())); @@ -89,10 +127,7 @@ async fn config_server_settings_are_cached_across_restart_and_resume() -> Result setup .assert_turn_uses_managed_instructions(&mut app, &resumed.thread.id) .await?; - assert_eq!( - setup.config_server.received_requests().await.unwrap().len(), - 1 - ); + assert_eq!(setup.bundle_request_count().await?, 1); app.shutdown_gracefully().await?; Ok(()) } @@ -152,7 +187,23 @@ wire_api = 'responses' }) } + async fn bundle_request_count(&self) -> Result { + let requests = self + .config_server + .received_requests() + .await + .context("config server request recording is disabled")?; + Ok(requests + .iter() + .filter(|request| request.url.path() == "/team/codex/config/bundle") + .count()) + } + async fn start_app(&self) -> Result { + self.start_app_with_args(&["--strict-config"]).await + } + + async fn start_app_with_args(&self, args: &[&str]) -> Result { let ca_path = self.ca_path.to_string_lossy(); let env = [ ("CODEX_CA_CERTIFICATE", Some(ca_path.as_ref())), @@ -161,7 +212,7 @@ wire_api = 'responses' ]; TestAppServer::builder() .with_codex_home(self.home.path()) - .with_args(&["--strict-config"]) + .with_args(args) .with_env_overrides(&env) .build_initialized() .await diff --git a/codex-rs/cli/src/git_credential.rs b/codex-rs/cli/src/git_credential.rs index 8f37b3fdd3..a00609b494 100644 --- a/codex-rs/cli/src/git_credential.rs +++ b/codex-rs/cli/src/git_credential.rs @@ -6,6 +6,7 @@ use chrono::Utc; use clap::Args; use clap::ValueEnum; use codex_app_server::in_process::EmbeddedNetworkPolicy; +use codex_cloud_config::anonymous_config_server_bundle_loader; use codex_cloud_config::managed_config_bundle_loader_for_auth; use codex_config::types::AuthCredentialsStoreMode; use codex_core::config::Config; @@ -133,23 +134,21 @@ async fn git_token_for_origin( return Ok(None); } let requirements = config.config_layer_stack.requirements_toml(); - // Config-server OAuth uses the OS keyring, which Git must never open. - ensure!( - requirements - .config_server - .as_ref() - .is_none_or(|server| server.oauth.is_none()), - "Git cannot verify Codex workspace policy using a protected login" - ); let login_changes = manager.auth_change_receiver(); let network = EmbeddedNetworkPolicy::load(&Default::default()).await; - let bundle = managed_config_bundle_loader_for_auth( - network.bind_bootstrap_auth(config.auth_config()), - &auth, - requirements, - ) - .await - .map_err(config_unavailable)?; + let bootstrap = network.bind_bootstrap_auth(config.auth_config()); + let bundle = if let Some(server) = &requirements.config_server { + anonymous_config_server_bundle_loader( + server, + bootstrap.codex_home, + &bootstrap.auth_route_config, + ) + .await? + } else { + managed_config_bundle_loader_for_auth(bootstrap, &auth, requirements) + .await + .map_err(config_unavailable)? + }; let effective = builder .cloud_config_bundle(bundle) .build() diff --git a/codex-rs/cli/src/login.rs b/codex-rs/cli/src/login.rs index 07042577dc..d91a012cd2 100644 --- a/codex-rs/cli/src/login.rs +++ b/codex-rs/cli/src/login.rs @@ -581,9 +581,13 @@ pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! { } } -async fn logout_config_server(config: &Config) -> std::io::Result { +async fn logout_config_server( + config: &Config, + loader_overrides: &codex_config::LoaderOverrides, +) -> std::io::Result { + let network = codex_app_server::in_process::EmbeddedNetworkPolicy::load(loader_overrides).await; match codex_cloud_config::managed_config_auth_manager( - config.auth_config(), + network.bind_bootstrap_auth(config.auth_config()), config.config_layer_stack.requirements_toml(), ) .await? @@ -601,7 +605,7 @@ pub async fn run_logout(cli_config_overrides: CliConfigOverrides) -> ! { .requirements_toml() .requires_remote_config() { - logout_config_server(&config).await + logout_config_server(&config, &Default::default()).await } else { Ok(false) }; @@ -743,16 +747,21 @@ mod storage_tests; #[cfg(test)] mod tests { + use codex_config::LoaderOverrides; use codex_config::types::AuthCredentialsStoreMode; + use codex_core::config::ConfigBuilder; use codex_login::AuthKeyringBackendKind; use codex_login::OAuthDeviceCodePrompt; use codex_login::load_auth_dot_json; use codex_login::login_with_api_key; use pretty_assertions::assert_eq; + use std::io::ErrorKind; + use std::net::TcpListener; use tempfile::tempdir; use super::clear_existing_auth_before_login; use super::config_server_device_code_message; + use super::logout_config_server; use super::safe_format_key; #[test] @@ -766,6 +775,51 @@ mod tests { insta::assert_snapshot!(config_server_device_code_message(&prompt), @"To sign in to the config server, open https://login.example/device and enter code ABCD-EFGH."); } + #[tokio::test] + async fn logout_config_server_respects_local_network_policy() -> anyhow::Result<()> { + let home = tempdir()?; + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let address = listener.local_addr()?; + let requirements_path = home.path().join("requirements.toml"); + std::fs::write( + &requirements_path, + format!( + r#" +[config_server] +base_url = "https://{address}" + +[application.network.domains] +"127.0.0.1" = "deny" +"#, + ), + )?; + let overrides = LoaderOverrides { + system_requirements_path: Some(requirements_path), + ignore_project_config: true, + ..LoaderOverrides::without_managed_config_for_tests() + }; + let config = ConfigBuilder::default() + .codex_home(home.path().to_path_buf()) + .loader_overrides(overrides.clone()) + .build() + .await?; + + let error = logout_config_server(&config, &overrides) + .await + .expect_err("logout discovery should fail for a denied destination"); + assert_eq!(error.to_string(), "OAuth discovery failed"); + + assert_eq!( + listener + .accept() + .expect_err("logout discovery connected to a denied destination") + .kind(), + ErrorKind::WouldBlock + ); + Ok(()) + } + #[tokio::test] async fn clears_existing_auth_before_login() { let codex_home = tempdir().expect("create temporary Codex home"); diff --git a/codex-rs/cloud-config/src/bundle_loader.rs b/codex-rs/cloud-config/src/bundle_loader.rs index adf3230d65..f295a38811 100644 --- a/codex-rs/cloud-config/src/bundle_loader.rs +++ b/codex-rs/cloud-config/src/bundle_loader.rs @@ -2,7 +2,6 @@ use crate::backend::BackendBundleClient; use crate::chatgpt::ChatGptSource; use crate::chatgpt::plan_requires_managed_config; use crate::config_source::BundleSource; -use crate::discovery::ConfigServerDiscovery; use crate::service::CLOUD_CONFIG_BUNDLE_TIMEOUT; use crate::service::CloudConfigBundleService; use codex_config::CloudConfigBundleLoader; @@ -29,13 +28,8 @@ pub async fn managed_config_auth_manager( let Some(service_config) = requirements.config_server.as_ref() else { return Ok(None); }; - crate::config_server_auth_manager( - service_config, - auth.codex_home, - &auth.auth_route_config, - ConfigServerDiscovery::Disabled, - ) - .await + crate::config_server_auth_manager(service_config, auth.codex_home, &auth.auth_route_config) + .await } /// How to sign in when the config server has no saved OAuth grant. @@ -94,7 +88,6 @@ pub async fn managed_config_bundle_loader( service_config.clone(), codex_home, &codex_login::AuthRouteConfig::from_http_client_factory(factory), - ConfigServerDiscovery::Disabled, ) .await; } @@ -122,7 +115,6 @@ pub async fn managed_config_bundle_loader_for_storage( service_config.clone(), auth_config.codex_home, &auth_config.auth_route_config, - ConfigServerDiscovery::Disabled, ) .await); } diff --git a/codex-rs/cloud-config/src/config_server.rs b/codex-rs/cloud-config/src/config_server.rs index 9248419d90..fc5a2f34f5 100644 --- a/codex-rs/cloud-config/src/config_server.rs +++ b/codex-rs/cloud-config/src/config_server.rs @@ -7,7 +7,6 @@ use crate::backend::RetryableFailureKind; use crate::backend::fetch_bundle; use crate::bundle_loader::managed_config_bundle_loader_for_service; use crate::config_source::BundleSource; -use crate::discovery::ConfigServerDiscovery; use crate::discovery::resolve_config_server; use crate::service::CLOUD_CONFIG_BUNDLE_TIMEOUT; use crate::service::CloudConfigBundleService; @@ -38,34 +37,47 @@ pub async fn config_server_bundle_loader( config: ConfigServer, codex_home: PathBuf, routes: &AuthRouteConfig, - discovery: ConfigServerDiscovery, ) -> CloudConfigBundleLoader { - let service = create_config_server_service(&config, &codex_home, routes, discovery).await; + let service = create_config_server_service(&config, &codex_home, routes).await; let loader = match service { Ok(service) => { let (loader, _) = managed_config_bundle_loader_for_service(service); loader } - Err(error) => config_server_loader_after_failure( - config.clone(), - codex_home, - routes.clone(), - discovery, - error, - ), + Err(error) => { + config_server_loader_after_failure(config.clone(), codex_home, routes.clone(), error) + } }; loader.with_config_server(config) } +/// Git must apply the same policy as Codex without opening the OS keyring. +/// Use the checked resolution directly so a second discovery cannot select OAuth. +pub async fn anonymous_config_server_bundle_loader( + config: &ConfigServer, + codex_home: PathBuf, + routes: &AuthRouteConfig, +) -> std::io::Result { + let resolved = resolve_config_server(config, routes).await?; + if resolved.oauth.is_some() { + return Err(std::io::Error::other( + "Git cannot verify Codex workspace policy using a protected login", + )); + } + let service = + config_server_service(resolved, &codex_home, routes).map_err(std::io::Error::other)?; + let (loader, _) = managed_config_bundle_loader_for_service(service); + Ok(loader.with_config_server(config.clone())) +} + async fn create_config_server_service( config: &ConfigServer, codex_home: &Path, routes: &AuthRouteConfig, - discovery: ConfigServerDiscovery, ) -> Result, CloudConfigBundleLoadError> { - let resolved = resolve_config_server(config, routes, discovery) + let resolved = resolve_config_server(config, routes) .await .map_err(|error| { CloudConfigBundleLoadError::new( @@ -75,6 +87,14 @@ async fn create_config_server_service( ) })?; + config_server_service(resolved, codex_home, routes) +} + +fn config_server_service( + resolved: ConfigServer, + codex_home: &Path, + routes: &AuthRouteConfig, +) -> Result, CloudConfigBundleLoadError> { let factory = config_server_http_client_factory(&resolved, routes); let codex_home = codex_home.to_path_buf(); let source = ConfigServerSource::from_config_server(resolved, codex_home.clone(), factory)?; @@ -90,7 +110,6 @@ fn config_server_loader_after_failure( config: ConfigServer, codex_home: PathBuf, routes: AuthRouteConfig, - discovery: ConfigServerDiscovery, error: CloudConfigBundleLoadError, ) -> CloudConfigBundleLoader { let initialized = Arc::new(OnceCell::new()); @@ -115,13 +134,8 @@ fn config_server_loader_after_failure( None => { initialized .get_or_try_init(|| async move { - let mut service = create_config_server_service( - &config, - &codex_home, - &routes, - discovery, - ) - .await?; + let mut service = + create_config_server_service(&config, &codex_home, &routes).await?; service.policy = policy; let (loader, _) = managed_config_bundle_loader_for_service(service); Ok(loader) @@ -153,9 +167,8 @@ pub async fn config_server_auth_manager( service_config: &ConfigServer, codex_home: PathBuf, routes: &AuthRouteConfig, - discovery: ConfigServerDiscovery, ) -> std::io::Result> { - let resolved = resolve_config_server(service_config, routes, discovery).await?; + let resolved = resolve_config_server(service_config, routes).await?; let factory = config_server_http_client_factory(&resolved, routes); oauth_session_manager(resolved.oauth.as_deref(), codex_home, &factory) } diff --git a/codex-rs/cloud-config/src/discovery.rs b/codex-rs/cloud-config/src/discovery.rs index d5bf1365b6..983cf3f4ae 100644 --- a/codex-rs/cloud-config/src/discovery.rs +++ b/codex-rs/cloud-config/src/discovery.rs @@ -13,24 +13,14 @@ use std::time::Duration; const DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5); -/// Controls OAuth discovery when the config server has no explicit OAuth settings. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum ConfigServerDiscovery { - #[default] - Disabled, - Enabled, -} - pub(crate) async fn resolve_config_server( config: &ConfigServer, routes: &AuthRouteConfig, - discovery: ConfigServerDiscovery, ) -> io::Result { config.validated_base_url().map_err(io::Error::other)?; let mut resolved = config.clone(); - if discovery == ConfigServerDiscovery::Enabled - && config.oauth.is_none() + if config.oauth.is_none() && let Some(oauth) = discover_oauth(&config.base_url, routes).await? { resolved.oauth = Some(Box::new(oauth)); diff --git a/codex-rs/cloud-config/src/discovery_tests.rs b/codex-rs/cloud-config/src/discovery_tests.rs index 0f33f9af19..e408603b75 100644 --- a/codex-rs/cloud-config/src/discovery_tests.rs +++ b/codex-rs/cloud-config/src/discovery_tests.rs @@ -2,7 +2,6 @@ //! Tests config-server OAuth defaults, discovery activation, and routing. -use super::ConfigServerDiscovery; use super::DISCOVERY_TIMEOUT; use super::discover_oauth; use super::resolve_config_server; @@ -100,7 +99,7 @@ async fn bounds_company_discovery_duration() { } #[tokio::test] -async fn preserves_explicit_oauth_and_flag_off_without_discovery() { +async fn preserves_explicit_oauth_without_discovery() { let listener = TcpListener::bind("127.0.0.1:0").unwrap(); listener.set_nonblocking(true).unwrap(); let address = listener.local_addr().unwrap(); @@ -116,25 +115,18 @@ async fn preserves_explicit_oauth_and_flag_off_without_discovery() { credentials_store: Default::default(), }; - for (discovery, oauth) in [ - (ConfigServerDiscovery::Disabled, None), - ( - ConfigServerDiscovery::Enabled, - Some(Box::new(explicit_oauth)), - ), - ] { - let config = ConfigServer { - base_url: format!("https://{address}"), - oauth, - }; - assert_eq!( - resolve_config_server(&config, &routes, discovery) - .await - .unwrap(), - config, - "discovery: {discovery:?}" - ); - } + let config = ConfigServer { + base_url: format!("https://{address}"), + oauth: Some(Box::new(explicit_oauth)), + }; + assert_eq!( + resolve_config_server(&config, &routes).await.unwrap(), + config + ); + let home = tempfile::tempdir().unwrap(); + crate::anonymous_config_server_bundle_loader(&config, home.path().into(), &routes) + .await + .expect_err("Git must refuse explicit OAuth without fetching anonymous policy"); assert_eq!( listener .accept() diff --git a/codex-rs/cloud-config/src/lib.rs b/codex-rs/cloud-config/src/lib.rs index 6a91454561..0a641133ea 100644 --- a/codex-rs/cloud-config/src/lib.rs +++ b/codex-rs/cloud-config/src/lib.rs @@ -23,6 +23,6 @@ pub use bundle_loader::managed_config_auth_manager; pub use bundle_loader::managed_config_bundle_loader; pub use bundle_loader::managed_config_bundle_loader_for_auth; pub use bundle_loader::managed_config_bundle_loader_for_storage; +pub use config_server::anonymous_config_server_bundle_loader; pub use config_server::config_server_auth_manager; pub use config_server::config_server_bundle_loader; -pub use discovery::ConfigServerDiscovery;