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

+80
View File
@@ -10,12 +10,14 @@ use codex_config::types::OAuthCredentialsStoreMode;
use codex_exec_server::HttpClient;
use codex_login::CodexAuth;
use codex_rmcp_client::McpAuthState;
use codex_rmcp_client::McpOAuthCallbackMode;
use codex_rmcp_client::OAuthDiscoveryTimeout;
use codex_rmcp_client::OAuthProviderError;
use codex_rmcp_client::StreamableHttpRedirectMode;
use codex_rmcp_client::determine_streamable_http_auth_status;
use codex_rmcp_client::determine_streamable_http_auth_status_from_credentials;
use codex_rmcp_client::discover_streamable_http_oauth;
use codex_rmcp_client::resolve_mcp_oauth_callback_url;
use futures::FutureExt;
use futures::future::join_all;
use tracing::warn;
@@ -30,6 +32,7 @@ pub struct McpOAuthLoginConfig {
pub http_headers: Option<HashMap<String, String>>,
pub env_http_headers: Option<HashMap<String, String>>,
pub discovered_scopes: Option<Vec<String>>,
pub callback_mode: McpOAuthCallbackMode,
}
#[derive(Debug)]
@@ -53,6 +56,35 @@ pub struct ResolvedMcpOAuthScopes {
pub source: McpOAuthScopesSource,
}
/// Keeps registered callbacks tied to their client while preserving legacy redirects.
pub fn resolve_oauth_callback(
server: &McpServerConfig,
server_url: &str,
global_callback_url: Option<&str>,
) -> Result<Option<String>> {
if let Some(callback_url) = server
.oauth
.as_ref()
.and_then(|oauth| oauth.callback_url.as_deref())
{
return Ok(Some(callback_url.to_string()));
}
if server
.oauth_client_id()
.is_none_or(|client_id| client_id.trim().is_empty())
{
return Ok(global_callback_url.map(ToOwned::to_owned));
}
resolve_mcp_oauth_callback_url(
server_url,
global_callback_url,
McpOAuthCallbackMode::CallbackSpecific,
)
.map(Some)
}
#[derive(Debug, Clone)]
pub struct McpAuthStatusEntry {
pub config: Option<McpServerConfig>,
@@ -80,6 +112,7 @@ pub async fn oauth_login_support(
{
Ok(Some(discovery)) => {
config.discovered_scopes = discovery.scopes_supported;
config.callback_mode = discovery.callback_mode;
McpOAuthLoginSupport::Supported(config)
}
Ok(None) => McpOAuthLoginSupport::Unsupported,
@@ -106,6 +139,7 @@ fn oauth_login_candidate(transport: &McpServerTransportConfig) -> Option<McpOAut
http_headers: http_headers.clone(),
env_http_headers: env_http_headers.clone(),
discovered_scopes: None,
callback_mode: McpOAuthCallbackMode::CallbackSpecific,
})
}
@@ -300,9 +334,55 @@ mod tests {
use super::McpOAuthScopesSource;
use super::OAuthProviderError;
use super::ResolvedMcpOAuthScopes;
use super::resolve_oauth_callback;
use super::resolve_oauth_scopes;
use super::should_retry_without_scopes;
#[test]
fn callback_resolution_preserves_registered_and_legacy_clients() -> anyhow::Result<()> {
for (client_id, saved_callback, global_callback, expected_callback) in [
(
Some("registered-client"),
Some("http://127.0.0.1/callback"),
Some("https://override.example/callback"),
Some("http://127.0.0.1/callback"),
),
(
Some("legacy-client"),
None,
None,
Some("http://127.0.0.1/callback/epMNJ6P1xGQ9"),
),
(
None,
Some("https://plugin.example/callback"),
Some("https://override.example/callback"),
Some("https://plugin.example/callback"),
),
(
None,
None,
Some("https://override.example/callback"),
Some("https://override.example/callback"),
),
] {
let server = serde_json::from_value(serde_json::json!({
"url": "https://mcp.example.com/mcp",
"oauth": {
"client_id": client_id,
"callback_url": saved_callback,
},
}))?;
assert_eq!(
resolve_oauth_callback(&server, "https://mcp.example.com/mcp", global_callback)?
.as_deref(),
expected_callback
);
}
Ok(())
}
#[test]
fn resolve_oauth_scopes_prefers_explicit() {
let resolved = resolve_oauth_scopes(
+1
View File
@@ -6,6 +6,7 @@ pub use auth::ResolvedMcpOAuthScopes;
pub use auth::compute_auth_statuses;
pub use auth::discover_supported_scopes;
pub use auth::oauth_login_support;
pub use auth::resolve_oauth_callback;
pub use auth::resolve_oauth_scopes;
pub use auth::should_retry_without_scopes;