[codex] close final WIF review findings [ci changed_files]

This commit is contained in:
Cooper Gamble
2026-06-17 21:37:46 +00:00
parent 392e4c582c
commit 0f97352684
7 changed files with 226 additions and 39 deletions
@@ -112,14 +112,19 @@ impl McpRequestProcessor {
async fn auth_for_mcp_server(
&self,
config: &Config,
server_name: &str,
apps_enabled: bool,
) -> Result<Option<CodexAuth>, JSONRPCErrorError> {
if apps_enabled && server_name == CODEX_APPS_MCP_SERVER_NAME {
self.auth_manager
.auth()
.await
.map_err(|err| internal_error(format!("failed to resolve auth: {err}")))
if config.model_provider.requires_openai_auth {
self.auth_manager
.auth()
.await
.map_err(|err| internal_error(format!("failed to resolve auth: {err}")))
} else {
Ok(self.auth_manager.auth_for_optional_use().await)
}
} else {
Ok(self.auth_manager.auth_cached())
}
@@ -156,7 +161,7 @@ impl McpRequestProcessor {
.runtime_config(&config)
.await;
let auth = self
.auth_for_mcp_server(&name, mcp_config.apps_enabled)
.auth_for_mcp_server(&config, &name, mcp_config.apps_enabled)
.await?;
let effective_servers = codex_mcp::effective_mcp_servers(&mcp_config, auth.as_ref());
let Some(server) = effective_servers
@@ -413,7 +418,7 @@ impl McpRequestProcessor {
.runtime_config(&config)
.await;
let auth = self
.auth_for_mcp_server(&server, mcp_config.apps_enabled)
.auth_for_mcp_server(&config, &server, mcp_config.apps_enabled)
.await?;
let environment_manager = self.thread_manager.environment_manager();
// This threadless resource-read path has no turn cwd or turn-selected
@@ -177,7 +177,7 @@ async fn list_apps_local_provider_does_not_resolve_workload_identity() -> Result
"access_token": access_token.clone(),
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": 3600,
"expires_in": 2,
"user_id": "user_test",
"chatgpt_account_id": "workspace_test",
"chatgpt_plan_type": "enterprise",
@@ -205,6 +205,7 @@ async fn list_apps_local_provider_does_not_resolve_workload_identity() -> Result
TestAppServer::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
std::fs::remove_file(&token_path)?;
tokio::time::sleep(Duration::from_secs(3)).await;
let AppsListResponse { data, next_cursor } = request_apps_list(&mut mcp).await?;
assert!(data.is_empty());
@@ -7,9 +7,12 @@ use anyhow::Result;
use app_test_support::ChatGptAuthFixture;
use app_test_support::TestAppServer;
use app_test_support::configure_expiring_workload_identity;
use app_test_support::configure_expiring_workload_identity_without_cloud_config_mock;
use app_test_support::to_response;
use app_test_support::write_chatgpt_auth;
use axum::Json;
use axum::Router;
use axum::routing::get;
use codex_app_server::in_process;
use codex_app_server::in_process::InProcessStartArgs;
use codex_app_server_protocol::ClientInfo;
@@ -491,6 +494,67 @@ apps = true
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn codex_apps_resource_read_with_local_provider_does_not_resolve_wif() -> Result<()> {
let (apps_server_url, _apps_server_calls, apps_server_handle) =
start_resource_apps_mcp_server().await?;
let codex_home = TempDir::new()?;
let auth_server = MockServer::start().await;
std::fs::write(
codex_home.path().join("config.toml"),
format!(
r#"
model_provider = "local"
chatgpt_base_url = "{apps_server_url}"
mcp_oauth_credentials_store = "file"
[model_providers.local]
name = "Local test provider"
base_url = "http://127.0.0.1:9/v1"
wire_api = "responses"
requires_openai_auth = false
supports_websockets = false
[features]
apps = true
"#
),
)?;
let workload_identity = configure_expiring_workload_identity_without_cloud_config_mock(
codex_home.path(),
&auth_server,
)
.await?;
let mut mcp = TestAppServer::new(codex_home.path()).await?;
timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??;
workload_identity.remove_and_wait_for_expiry().await?;
let read_request_id = mcp
.send_mcp_resource_read_request(McpResourceReadParams {
thread_id: None,
server: "codex_apps".to_string(),
uri: TEST_RESOURCE_URI.to_string(),
})
.await?;
let read_response: JSONRPCResponse = timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(read_request_id)),
)
.await??;
assert_eq!(
to_response::<McpResourceReadResponse>(read_response)?,
expected_resource_read_response()
);
auth_server.verify().await;
apps_server_handle.abort();
let _ = apps_server_handle.await;
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_resource_read_for_configured_server_survives_unavailable_wif() -> Result<()> {
let (mcp_server_url, _mcp_server_calls, mcp_server_handle) =
@@ -668,7 +732,12 @@ async fn start_resource_apps_mcp_server()
Arc::new(LocalSessionManager::default()),
StreamableHttpServerConfig::default(),
);
let router = Router::new().nest_service("/api/codex/ps/mcp", mcp_service);
let router = Router::new()
.route(
"/api/codex/config/bundle",
get(|| async { Json(json!({})) }),
)
.nest_service("/api/codex/ps/mcp", mcp_service);
let apps_server_handle = tokio::spawn(async move {
let _ = axum::serve(listener, router).await;
});
@@ -192,12 +192,20 @@ impl RequestPluginInstallHandler {
.is_some_and(|response| response.action == ElicitationAction::Accept);
let completed = if user_confirmed {
let auth = session
.services
.auth_manager
.auth()
.await
.map_err(model_safe_plugin_install_auth_error)?;
let auth = if plugin_install_verification_requires_apps_auth(&tool) {
if turn.config.model_provider.requires_openai_auth {
session
.services
.auth_manager
.auth()
.await
.map_err(model_safe_plugin_install_auth_error)?
} else {
session.services.auth_manager.auth_for_optional_use().await
}
} else {
session.services.auth_manager.auth_cached()
};
verify_request_plugin_install_completed(&session, &turn, &tool, auth.as_ref()).await
} else {
false
@@ -362,6 +370,15 @@ fn is_remote_plugin_install_suggestion(plugin_id: &str) -> bool {
.is_some_and(|(_, marketplace_name)| marketplace_name == REMOTE_GLOBAL_MARKETPLACE_NAME)
}
fn plugin_install_verification_requires_apps_auth(tool: &DiscoverableTool) -> bool {
match tool {
DiscoverableTool::Connector(_) => true,
DiscoverableTool::Plugin(plugin) => {
!is_remote_plugin_install_suggestion(&plugin.id) && !plugin.app_connector_ids.is_empty()
}
}
}
async fn refresh_missing_requested_connectors(
session: &crate::session::session::Session,
turn: &crate::session::turn_context::TurnContext,
@@ -68,6 +68,22 @@ fn remote_plugin_install_suggestions_skip_core_installed_verification() {
assert!(!is_remote_plugin_install_suggestion("Plugin_123"));
}
#[test]
fn plugin_install_verification_only_requires_auth_for_apps_refresh() {
assert!(plugin_install_verification_requires_apps_auth(
&connector_tool("connector_calendar", "Google Calendar")
));
assert!(!plugin_install_verification_requires_apps_auth(
&plugin_tool("slack@openai-curated", &[])
));
assert!(plugin_install_verification_requires_apps_auth(
&plugin_tool("slack@openai-curated", &["connector_slack"])
));
assert!(!plugin_install_verification_requires_apps_auth(
&plugin_tool("slack@openai-curated-remote", &["connector_slack"])
));
}
#[test]
fn plugin_install_auth_error_redacts_credential_details() {
let error = model_safe_plugin_install_auth_error(std::io::Error::other(
@@ -254,3 +270,18 @@ fn connector_tool(id: &str, name: &str) -> DiscoverableTool {
plugin_display_names: Vec::new(),
}))
}
fn plugin_tool(id: &str, app_connector_ids: &[&str]) -> DiscoverableTool {
DiscoverableTool::Plugin(Box::new(DiscoverablePluginInfo {
id: id.to_string(),
remote_plugin_id: None,
name: id.to_string(),
description: None,
has_skills: false,
mcp_server_names: Vec::new(),
app_connector_ids: app_connector_ids
.iter()
.map(|connector_id| (*connector_id).to_string())
.collect(),
}))
}
@@ -1,7 +1,11 @@
#![cfg(not(target_os = "windows"))]
#![allow(clippy::unwrap_used)]
use std::sync::Arc;
use anyhow::Result;
use base64::Engine;
use codex_config::types::AuthCredentialsStoreMode;
use codex_config::types::ToolSuggestDisabledTool;
use codex_config::types::ToolSuggestDiscoverable;
use codex_config::types::ToolSuggestDiscoverableType;
@@ -20,6 +24,8 @@ use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::ThreadSettingsOverrides;
use codex_protocol::user_input::UserInput;
use codex_workload_identity::CredentialSourceConfig;
use codex_workload_identity::WorkloadIdentityConfig;
use core_test_support::apps_test_server::AppsTestServer;
use core_test_support::responses::ev_assistant_message;
use core_test_support::responses::ev_completed;
@@ -37,6 +43,7 @@ use core_test_support::wait_for_event;
use core_test_support::wait_for_event_match;
use serde_json::Value;
use serde_json::json;
use tempfile::TempDir;
use wiremock::Mock;
use wiremock::ResponseTemplate;
use wiremock::matchers::method;
@@ -274,7 +281,7 @@ async fn endpoint_mode_injects_candidates_hides_list_and_rejects_invented_ids()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn endpoint_recommendation_adds_install_identity_only_to_elicitation_metadata() -> Result<()>
async fn local_provider_recommends_and_installs_remote_plugin_without_resolving_wif() -> Result<()>
{
skip_if_no_network!(Ok(()));
@@ -324,7 +331,56 @@ async fn endpoint_recommendation_adds_install_identity_only_to_elicitation_metad
],
)
.await;
let test = build_test(&server, &apps_server).await?;
let home = Arc::new(TempDir::new()?);
let header = base64::engine::general_purpose::URL_SAFE_NO_PAD
.encode(serde_json::to_vec(&json!({"alg": "none", "typ": "JWT"}))?);
let payload =
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(serde_json::to_vec(&json!({
"email": "user@example.com",
"https://api.openai.com/auth": {
"chatgpt_plan_type": "enterprise",
"chatgpt_user_id": "user_id",
"chatgpt_account_id": "account_id"
}
}))?);
let id_token = format!("{header}.{payload}.c2ln");
std::fs::write(
home.path().join("auth.json"),
serde_json::to_vec_pretty(&json!({
"auth_mode": "chatgpt",
"OPENAI_API_KEY": null,
"tokens": {
"id_token": id_token,
"access_token": "chatgpt-token",
"refresh_token": "refresh-token",
"account_id": "account_id"
},
"last_refresh": chrono::Utc::now()
}))?,
)?;
let missing_token_path = home.path().join("missing-workload-token");
let token_url = format!("{}/oauth/token", server.uri());
let mut builder = test_codex()
.with_home(home)
.with_auth_manager_from_config()
.with_config({
let apps_base_url = apps_server.chatgpt_base_url.clone();
move |config| {
configure_apps_without_search_tool(config, apps_base_url.as_str());
config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File;
config.model_provider.requires_openai_auth = false;
config.workload_identity = Some(WorkloadIdentityConfig {
identity_provider_id: "idp_test".to_string(),
identity_provider_mapping_id: "idpm_test".to_string(),
audience: "api://codex-test".to_string(),
token_url,
credential_source: CredentialSourceConfig::File {
path: missing_token_path,
},
});
}
});
let test = builder.build(&server).await?;
let (sandbox_policy, permission_profile) =
turn_permission_fields(PermissionProfile::Disabled, test.config.cwd.as_path());
@@ -372,7 +428,7 @@ async fn endpoint_recommendation_adds_install_identity_only_to_elicitation_metad
.submit(Op::ResolveElicitation {
server_name: elicitation.server_name,
request_id: elicitation.id,
decision: ElicitationAction::Decline,
decision: ElicitationAction::Accept,
content: None,
meta: None,
})
@@ -384,6 +440,15 @@ async fn endpoint_recommendation_adds_install_identity_only_to_elicitation_metad
let requests = mock.requests();
assert_eq!(requests.len(), 2);
let contextual_user_message = requests[0].message_input_texts("user").join("\n");
assert!(contextual_user_message.contains("<recommended_plugins>"));
assert!(contextual_user_message.contains("github@openai-curated-remote"));
let output = requests[1]
.function_call_output_text(call_id)
.expect("request tool output");
let output: Value = serde_json::from_str(&output)?;
assert_eq!(output["user_confirmed"], true);
assert_eq!(output["completed"], true);
for request in requests {
let body = request.body_json().to_string();
assert!(!body.contains(REMOTE_PLUGIN_ID));
+21 -22
View File
@@ -15,7 +15,6 @@ use super::ExternalAuthTokens;
pub(super) struct WorkloadIdentityExternalAuth {
client: Result<ConfiguredWorkloadIdentityClient, String>,
process_isolation_error: Option<String>,
}
struct SharedWorkloadIdentityExternalAuth {
@@ -46,13 +45,14 @@ pub(super) fn shared_workload_identity_external_auth(
return auth;
}
let auth = Arc::new(WorkloadIdentityExternalAuth::new(
no_redirect_http
.map(|http| build_workload_identity_client(config.clone(), client_id.clone(), http))
.map_err(|error| {
let client = initialize_workload_identity_process_isolation()
.and_then(|()| {
no_redirect_http.map_err(|error| {
format!("workload identity HTTP client initialization failed: {error}")
}),
));
})
})
.map(|http| build_workload_identity_client(config.clone(), client_id.clone(), http));
let auth = Arc::new(WorkloadIdentityExternalAuth::new(client));
shared_auths.push(SharedWorkloadIdentityExternalAuth {
config,
client_id,
@@ -61,26 +61,25 @@ pub(super) fn shared_workload_identity_external_auth(
auth
}
#[cfg(target_os = "windows")]
fn initialize_workload_identity_process_isolation() -> Result<(), String> {
// Core config forces WIF sessions onto the Windows restricted-token sandbox, which keeps
// model-controlled child processes from opening the parent process.
Ok(())
}
#[cfg(not(target_os = "windows"))]
fn initialize_workload_identity_process_isolation() -> Result<(), String> {
codex_process_hardening::disable_process_inspection()
.map_err(|error| format!("workload identity process isolation failed: {error}"))
}
impl WorkloadIdentityExternalAuth {
pub(super) fn new(client: Result<ConfiguredWorkloadIdentityClient, String>) -> Self {
#[cfg(target_os = "windows")]
// Core config forces WIF sessions onto the Windows restricted-token sandbox, which keeps
// model-controlled child processes from opening the parent process.
let process_isolation_error = None;
#[cfg(not(target_os = "windows"))]
let process_isolation_error = codex_process_hardening::disable_process_inspection()
.err()
.map(|error| format!("workload identity process isolation failed: {error}"));
Self {
client,
process_isolation_error,
}
Self { client }
}
async fn tokens(&self, force_refresh: bool) -> std::io::Result<ExternalAuthTokens> {
if let Some(error) = self.process_isolation_error.as_ref() {
return Err(std::io::Error::other(error.clone()));
}
let client = self
.client
.as_ref()