mirror of
https://github.com/openai/codex.git
synced 2026-11-28 23:34:06 +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
@@ -10,6 +10,10 @@ use rmcp::transport::auth::OAuthState;
|
||||
use url::Url;
|
||||
|
||||
use crate::oauth::validate_authorization_server_endpoints;
|
||||
use crate::oauth_callback::McpOAuthCallbackMode;
|
||||
use crate::oauth_callback::append_callback_id_to_redirect_uri;
|
||||
use crate::oauth_callback::callback_mode;
|
||||
use crate::oauth_callback::validate_callback_redirect;
|
||||
|
||||
/// OAuth client-registration strategy for one interactive HTTP MCP login.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
@@ -27,6 +31,7 @@ pub enum McpOAuthClientRegistration {
|
||||
pub(crate) struct PreparedOAuthLogin {
|
||||
pub(crate) oauth_state: OAuthState,
|
||||
pub(crate) authorization_server_issuer: Option<String>,
|
||||
pub(crate) redirect_uri: String,
|
||||
}
|
||||
|
||||
pub(crate) async fn start_authorization(
|
||||
@@ -43,6 +48,7 @@ pub(crate) async fn start_authorization(
|
||||
let metadata = auth_manager.resolve_metadata().await?.metadata;
|
||||
validate_authorization_server_endpoints(&metadata)?;
|
||||
let authorization_server_issuer = metadata.issuer.clone();
|
||||
let callback_mode = callback_mode(&metadata)?;
|
||||
|
||||
let cimd_advertised = metadata
|
||||
.additional_fields
|
||||
@@ -55,18 +61,30 @@ pub(crate) async fn start_authorization(
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.is_some_and(|methods| methods.iter().any(|method| method.as_str() == Some("none")));
|
||||
|
||||
let parsed_redirect_uri = Url::parse(redirect_uri)?;
|
||||
let uses_shared_callback = callback_mode == McpOAuthCallbackMode::IssuerBound;
|
||||
let redirect_uri = if uses_shared_callback {
|
||||
redirect_uri.to_string()
|
||||
} else {
|
||||
append_callback_id_to_redirect_uri(redirect_uri, callback_id)?
|
||||
};
|
||||
let parsed_redirect_uri = Url::parse(&redirect_uri)?;
|
||||
let expected_callback_path = if uses_shared_callback {
|
||||
"/callback".to_string()
|
||||
} else {
|
||||
format!("/callback/{callback_id}")
|
||||
};
|
||||
let native_redirect_supported = parsed_redirect_uri.scheme() == "http"
|
||||
&& matches!(
|
||||
parsed_redirect_uri.host_str(),
|
||||
Some("127.0.0.1" | "localhost")
|
||||
)
|
||||
&& parsed_redirect_uri.port().is_some_and(|port| port > 0)
|
||||
&& parsed_redirect_uri.path() == format!("/callback/{callback_id}")
|
||||
&& parsed_redirect_uri.path() == expected_callback_path
|
||||
&& parsed_redirect_uri.query().is_none()
|
||||
&& parsed_redirect_uri.fragment().is_none()
|
||||
&& parsed_redirect_uri.username().is_empty()
|
||||
&& parsed_redirect_uri.password().is_none();
|
||||
validate_callback_redirect(&redirect_uri, callback_id, callback_mode)?;
|
||||
// MCP 2026-07-28 priority: pre-registered clients never reach this path; offer
|
||||
// advertised CIMD here and otherwise let rmcp fall back to DCR.
|
||||
// https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration
|
||||
@@ -82,7 +100,7 @@ pub(crate) async fn start_authorization(
|
||||
}
|
||||
if !native_redirect_supported {
|
||||
bail!(
|
||||
"MCP OAuth CIMD requires an ephemeral loopback callback at `/callback/{callback_id}`"
|
||||
"MCP OAuth CIMD requires an ephemeral loopback callback at `{expected_callback_path}`"
|
||||
);
|
||||
}
|
||||
true
|
||||
@@ -91,16 +109,19 @@ pub(crate) async fn start_authorization(
|
||||
};
|
||||
|
||||
auth_manager.set_metadata(metadata);
|
||||
let mut request = AuthorizationRequest::new(redirect_uri)
|
||||
let mut request = AuthorizationRequest::new(redirect_uri.clone())
|
||||
.with_scopes(scopes.iter().copied())
|
||||
.with_client_name("Codex");
|
||||
if offer_cimd {
|
||||
// CIMD is an active IETF Internet-Draft: this HTTPS client identifier resolves
|
||||
// to its self-referential JSON metadata document.
|
||||
// https://datatracker.ietf.org/doc/draft-ietf-oauth-client-id-metadata-document/
|
||||
request = request.with_client_metadata_url(format!(
|
||||
"https://chatgpt.com/oauth/codex/{callback_id}/client.json"
|
||||
));
|
||||
let client_metadata_url = if uses_shared_callback {
|
||||
"https://chatgpt.com/oauth/codex/client.json".to_string()
|
||||
} else {
|
||||
format!("https://chatgpt.com/oauth/codex/{callback_id}/client.json")
|
||||
};
|
||||
request = request.with_client_metadata_url(client_metadata_url);
|
||||
}
|
||||
let session = AuthorizationSession::new(auth_manager, request)
|
||||
.await
|
||||
@@ -109,6 +130,7 @@ pub(crate) async fn start_authorization(
|
||||
Ok(PreparedOAuthLogin {
|
||||
oauth_state: OAuthState::Session(session),
|
||||
authorization_server_issuer,
|
||||
redirect_uri,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user