Enforce issuer binding for MCP OAuth endpoints (#39935)

## Why

Interactive MCP OAuth must not trust authorization metadata that could route an
authorization code or PKCE verifier to an unrelated token endpoint.

## What changed

- Require an advertised issuer to match the origin that served authorization
  metadata.
- Validate authorization and token endpoint origins before starting both
  pre-registered and dynamically registered client flows.
- Allow delegated endpoint origins when the server advertises issuer-bound
  authorization responses, while retaining narrow compatibility exceptions for
  existing providers.

## Testing

Add coverage for rejected untrusted metadata, accepted issuer-bound delegation,
legacy provider exceptions, and both client registration paths.

GitOrigin-RevId: 43c9f94c9f6ba2d23d7e373576cf9fa26861cfc3
This commit is contained in:
jif authored and copyberry committed 2026-08-21 13:28:28 +00:00
1 parent 9c3da20b3f
commit 7f9832d0d0
8 files changed
+395 -7

No files matched your search

@@ -21,6 +21,7 @@ use http::header::TRANSFER_ENCODING;
use http::header::USER_AGENT;
use oauth2::HttpRequest;
use oauth2::HttpResponse;
use rmcp::transport::auth::AuthorizationMetadata;
use rmcp::transport::auth::OAuthHttpClient;
use rmcp::transport::auth::OAuthHttpClientError;
use rmcp::transport::auth::OAuthHttpClientFuture;
@@ -47,6 +48,8 @@ enum OAuthHttpClientAdapterError {
TooManyRedirects,
#[error("OAuth HTTP response body exceeds {maximum_bytes} bytes")]
ResponseBodyTooLarge { maximum_bytes: usize },
#[error("OAuth authorization server issuer does not match authorization metadata origin")]
AuthorizationMetadataIssuerOriginMismatch,
}
fn oauth_http_client_error(
@@ -284,6 +287,18 @@ impl OAuthHttpClientAdapter {
request_url = next_url;
redirects += 1;
};
if response.status == StatusCode::OK.as_u16()
&& let Ok(metadata) = serde_json::from_slice::<AuthorizationMetadata>(&body)
&& let Some(issuer) = metadata.issuer.as_deref()
&& Url::parse(issuer)
.map_err(oauth_http_client_error)?
.origin()
!= request_url.origin()
{
return Err(oauth_http_client_error(
OAuthHttpClientAdapterError::AuthorizationMetadataIssuerOriginMismatch,
));
}
let mut builder = oauth2::http::Response::builder().status(response.status);
for header in response.headers {
builder = builder.header(header.name, header.value);