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:
stevenlee-oai authored and copyberry committed 2026-08-25 20:26:23 +00:00
1 parent 32ce703a80
commit 9be8d6e1c3
28 files changed
+1096 -127

No files matched your search

+48 -5
View File
@@ -15,6 +15,8 @@ use tracing::debug;
use crate::http_client_adapter::StreamableHttpRedirectMode;
use crate::oauth::StoredOAuthTokenStatus;
use crate::oauth::oauth_token_status;
use crate::oauth_callback::McpOAuthCallbackMode;
use crate::oauth_callback::callback_mode;
use crate::oauth_http_client::OAuthHttpClientAdapter;
use crate::utils::build_default_headers;
use codex_config::types::AuthKeyringBackendKind;
@@ -39,6 +41,7 @@ impl OAuthDiscoveryTimeout {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamableHttpOAuthDiscovery {
pub scopes_supported: Option<Vec<String>>,
pub callback_mode: McpOAuthCallbackMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -263,9 +266,14 @@ async fn discover_streamable_http_oauth_with_manager(
) -> Result<Option<StreamableHttpOAuthDiscovery>> {
match authorization_manager.resolve_metadata().boxed().await {
Ok(resolution) if !resolution.source.is_discovered() => Ok(None),
Ok(resolution) => Ok(Some(StreamableHttpOAuthDiscovery {
scopes_supported: normalize_scopes(resolution.metadata.scopes_supported),
})),
Ok(resolution) => {
let metadata = resolution.metadata;
Ok(Some(StreamableHttpOAuthDiscovery {
callback_mode: callback_mode(&metadata)
.unwrap_or(McpOAuthCallbackMode::CallbackSpecific),
scopes_supported: normalize_scopes(metadata.scopes_supported),
}))
}
Err(AuthError::NoAuthorizationSupport) => Ok(None),
Err(err) => Err(err.into()),
}
@@ -668,6 +676,7 @@ mod tests {
let server = spawn_oauth_discovery_server(serde_json::json!({
"authorization_endpoint": "https://example.com/authorize",
"token_endpoint": "https://example.com/token",
"authorization_response_iss_parameter_supported": true,
"scopes_supported": ["profile", " email ", "profile", "", " "],
}))
.await;
@@ -685,8 +694,42 @@ mod tests {
.expect("oauth support should be detected");
assert_eq!(
discovery.scopes_supported,
Some(vec!["profile".to_string(), "email".to_string()])
discovery,
StreamableHttpOAuthDiscovery {
scopes_supported: Some(vec!["profile".to_string(), "email".to_string()]),
callback_mode: McpOAuthCallbackMode::IssuerBound,
}
);
}
#[tokio::test]
async fn issuer_support_without_a_metadata_issuer_falls_back_to_distinct_callbacks() {
let server = spawn_oauth_discovery_server(serde_json::json!({
"issuer": null,
"authorization_endpoint": "https://example.com/authorize",
"token_endpoint": "https://example.com/token",
"authorization_response_iss_parameter_supported": true,
}))
.await;
let discovery = discover_streamable_http_oauth(
&server.url,
/*http_headers*/ None,
/*env_http_headers*/ None,
test_http_client(),
OAuthDiscoveryTimeout::LOCAL,
StreamableHttpRedirectMode::Legacy,
)
.await
.expect("discovery should succeed")
.expect("oauth support should be detected");
assert_eq!(
discovery,
StreamableHttpOAuthDiscovery {
scopes_supported: None,
callback_mode: McpOAuthCallbackMode::CallbackSpecific,
}
);
}