mirror of
https://github.com/openai/codex.git
synced 2026-11-18 07:00:27 +00:00
Harden MCP OAuth callback handling (#40691)
## Why MCP servers can share an OAuth callback URL. Without a validated issuer or a server-specific callback path, an authorization response could be associated with the wrong server. ## What changed - Use stable callbacks when authorization metadata advertises issuer-bound responses, and validate the returned issuer before exchanging the code. - Retain server-specific callback IDs for providers without issuer support, including fallback to the global or default callback for legacy registered clients. - Persist registered callback URLs for MCP servers and plugins, and insert the active listener port into portless loopback redirects. ## Testing Add coverage for issuer validation, callback-mode discovery, registered and legacy clients, plugin OAuth, CLI persistence, and loopback listener ports. GitOrigin-RevId: 2878c92e237fc17fd3def0bd2e1cce3e104a3db8
This commit is contained in:
1 parent
32ce703a80
commit
9be8d6e1c3
28 files changed
+1096
-127
No files matched your search
+56
-16
@@ -29,14 +29,17 @@ use codex_mcp::apply_http_headers_helper;
|
||||
use codex_mcp::compute_auth_statuses;
|
||||
use codex_mcp::discover_supported_scopes;
|
||||
use codex_mcp::oauth_login_support;
|
||||
use codex_mcp::resolve_oauth_callback;
|
||||
use codex_mcp::resolve_oauth_scopes;
|
||||
use codex_mcp::should_retry_without_scopes;
|
||||
use codex_protocol::protocol::McpAuthStatus;
|
||||
use codex_rmcp_client::McpOAuthCallbackMode;
|
||||
use codex_rmcp_client::McpOAuthClientRegistration;
|
||||
use codex_rmcp_client::OAuthDiscoveryTimeout;
|
||||
use codex_rmcp_client::StreamableHttpRedirectMode;
|
||||
use codex_rmcp_client::delete_oauth_tokens;
|
||||
use codex_rmcp_client::perform_oauth_login;
|
||||
use codex_rmcp_client::resolve_mcp_oauth_callback_url;
|
||||
use codex_utils_cli::CliConfigOverrides;
|
||||
use codex_utils_cli::format_env_display;
|
||||
|
||||
@@ -270,6 +273,7 @@ async fn perform_oauth_login_retry_without_scopes(
|
||||
oauth_resource: Option<&str>,
|
||||
callback_port: Option<u16>,
|
||||
callback_url: Option<&str>,
|
||||
global_callback_url: Option<&str>,
|
||||
http_client: Arc<dyn HttpClient>,
|
||||
) -> Result<()> {
|
||||
match perform_oauth_login(
|
||||
@@ -285,6 +289,7 @@ async fn perform_oauth_login_retry_without_scopes(
|
||||
oauth_resource,
|
||||
callback_port,
|
||||
callback_url,
|
||||
global_callback_url,
|
||||
Arc::clone(&http_client),
|
||||
)
|
||||
.await
|
||||
@@ -305,6 +310,7 @@ async fn perform_oauth_login_retry_without_scopes(
|
||||
oauth_resource,
|
||||
callback_port,
|
||||
callback_url,
|
||||
global_callback_url,
|
||||
http_client,
|
||||
)
|
||||
.await
|
||||
@@ -346,9 +352,6 @@ async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Re
|
||||
validate_server_name(&name)?;
|
||||
|
||||
let codex_home = find_codex_home().context("failed to resolve CODEX_HOME")?;
|
||||
let mut servers = load_global_mcp_servers(&codex_home)
|
||||
.await
|
||||
.with_context(|| format!("failed to load MCP servers from {}", codex_home.display()))?;
|
||||
|
||||
let (transport, oauth_client_id, client_registration, oauth_resource) = match transport_args {
|
||||
AddMcpTransportArgs {
|
||||
@@ -405,6 +408,43 @@ async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Re
|
||||
AddMcpTransportArgs { .. } => bail!("exactly one of --command or --url must be provided"),
|
||||
};
|
||||
|
||||
// Discover once before saving so a new registered client keeps the exact
|
||||
// callback its provider expects, including issuer-bound stable callbacks.
|
||||
let http_client: Arc<dyn HttpClient> = Arc::new(
|
||||
RouteAwareHttpClient::new(config.http_client_factory()).with_tls_backend_fallback(),
|
||||
);
|
||||
let login_support = oauth_login_support(
|
||||
&transport,
|
||||
Arc::clone(&http_client),
|
||||
OAuthDiscoveryTimeout::LOCAL,
|
||||
StreamableHttpRedirectMode::Legacy,
|
||||
)
|
||||
.await;
|
||||
// A stable callback requires confirmed issuer support. Discovery failures
|
||||
// and servers without support both retain the callback-ID mix-up defense.
|
||||
let callback_mode = match &login_support {
|
||||
McpOAuthLoginSupport::Supported(oauth_config) => oauth_config.callback_mode,
|
||||
McpOAuthLoginSupport::Unsupported | McpOAuthLoginSupport::Unknown(_) => {
|
||||
McpOAuthCallbackMode::CallbackSpecific
|
||||
}
|
||||
};
|
||||
|
||||
// DCR and CIMD register their callbacks during login; only an existing
|
||||
// client ID has a redirect that must be persisted with the MCP server.
|
||||
let callback_url = oauth_client_id
|
||||
.as_ref()
|
||||
.map(|_| {
|
||||
let McpServerTransportConfig::StreamableHttp { url, .. } = &transport else {
|
||||
bail!("OAuth client IDs require a streamable HTTP MCP server");
|
||||
};
|
||||
resolve_mcp_oauth_callback_url(
|
||||
url,
|
||||
config.mcp_oauth_callback_url.as_deref(),
|
||||
callback_mode,
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let new_entry = McpServerConfig {
|
||||
auth: Default::default(),
|
||||
transport: transport.clone(),
|
||||
@@ -424,12 +464,16 @@ async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Re
|
||||
.clone()
|
||||
.map(|client_id| McpServerOAuthConfig {
|
||||
client_id: Some(client_id),
|
||||
callback_url: callback_url.clone(),
|
||||
callback_port: None,
|
||||
}),
|
||||
oauth_resource: oauth_resource.clone(),
|
||||
tools: HashMap::new(),
|
||||
};
|
||||
|
||||
let mut servers = load_global_mcp_servers(&codex_home)
|
||||
.await
|
||||
.with_context(|| format!("failed to load MCP servers from {}", codex_home.display()))?;
|
||||
servers.insert(name.clone(), new_entry);
|
||||
|
||||
ConfigEditsBuilder::new(&codex_home)
|
||||
@@ -439,19 +483,9 @@ async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Re
|
||||
.with_context(|| format!("failed to write MCP servers to {}", codex_home.display()))?;
|
||||
|
||||
println!("Added global MCP server '{name}'.");
|
||||
|
||||
// `mcp add` assigns new servers to the local environment, so its immediate
|
||||
// OAuth discovery uses the local route-aware HTTP client.
|
||||
let http_client: Arc<dyn HttpClient> = Arc::new(
|
||||
RouteAwareHttpClient::new(config.http_client_factory()).with_tls_backend_fallback(),
|
||||
);
|
||||
let login_support = oauth_login_support(
|
||||
&transport,
|
||||
Arc::clone(&http_client),
|
||||
OAuthDiscoveryTimeout::LOCAL,
|
||||
StreamableHttpRedirectMode::Legacy,
|
||||
)
|
||||
.await;
|
||||
if let Some(callback_url) = &callback_url {
|
||||
println!("OAuth callback URL: {callback_url}");
|
||||
}
|
||||
match login_support {
|
||||
McpOAuthLoginSupport::Supported(oauth_config) => {
|
||||
println!("Detected OAuth support. Starting OAuth flow…");
|
||||
@@ -472,6 +506,9 @@ async fn run_add(config_overrides: &CliConfigOverrides, add_args: AddArgs) -> Re
|
||||
client_registration,
|
||||
oauth_resource.as_deref(),
|
||||
config.mcp_oauth_callback_port,
|
||||
callback_url
|
||||
.as_deref()
|
||||
.or(config.mcp_oauth_callback_url.as_deref()),
|
||||
config.mcp_oauth_callback_url.as_deref(),
|
||||
http_client,
|
||||
)
|
||||
@@ -577,6 +614,8 @@ async fn run_login(config: &Config, login_args: LoginArgs) -> Result<()> {
|
||||
let resolved_scopes =
|
||||
resolve_oauth_scopes(explicit_scopes, server.scopes.clone(), discovered_scopes);
|
||||
let credential_name = server.oauth_credential_name(&name);
|
||||
let callback_url =
|
||||
resolve_oauth_callback(server, &url, config.mcp_oauth_callback_url.as_deref())?;
|
||||
|
||||
perform_oauth_login_retry_without_scopes(
|
||||
credential_name.as_ref(),
|
||||
@@ -590,6 +629,7 @@ async fn run_login(config: &Config, login_args: LoginArgs) -> Result<()> {
|
||||
client_registration,
|
||||
server.oauth_resource.as_deref(),
|
||||
server.oauth_callback_port(config.mcp_oauth_callback_port),
|
||||
callback_url.as_deref(),
|
||||
config.mcp_oauth_callback_url.as_deref(),
|
||||
http_client,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user