Repository navigation
fix: apply the redirect loopback rule to OAuth URLs that servers name - #412
Merged
jeanmatthieu merged 2 commits intoDec 1, 2026
Conversation
`OAuthRedirectGuard` follows a discovery redirect to a loopback host only from a loopback host. The URLs a server names directly were checked for private IP literals only: the `resource_metadata` URL of a `WWW-Authenticate` challenge, the `authorization_servers` of the protected resource metadata, the authorization, token and registration endpoints of the authorization server metadata, and the token endpoint in an identity provider's metadata. `isPrivateIPHost` leaves loopback out because the MCP endpoint itself may be on loopback, so these URLs could name a loopback host whatever the server that named them. Fix: `rejectPrivateIP` and `validateAuthorizationServerAndHost` take an `allowingLoopback` flag, and a URL a server names may be on a loopback host only when that server is (RFC 9728 section 7.7). The challenge URL and the listed authorization servers follow the MCP endpoint; the endpoints in authorization server or identity provider metadata follow that server. URLs in `endpointOverrides` and the URLs an application passes to `OAuthTokenExchange` are not affected. The loopback check of `OAuthRedirectGuard` moves to `OAuthURLValidator.isLoopbackHost`, so both use the same definition. Adds tests to `OAuthAuthorizerTests`, `OAuthDiscoveryClientTests`, `OAuthClientRegistrarTests`, `OAuthURLValidatorTests` and `OAuthJWTBearerTests`, and updates the protocol guide, `design/auth.md` and `design/threat_model.md`.
| - **HTTPS.** The MCP endpoint, resource metadata URLs and authorization server endpoints must be `https`; `http` is accepted only for loopback hosts (`localhost`, `127.0.0.1`, `::1`), and for authorization server endpoints not at all by default. URLs with fragments are rejected. | ||
| - **Private addresses.** `OAuthURLValidator.isPrivateIPHost` parses literal IP hosts with `inet_pton` and rejects `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10`, `169.254.0.0/16` (cloud metadata), `172.16.0.0/12`, `192.0.0.0/24`, `192.168.0.0/16`, `198.18.0.0/15`, `224.0.0.0/4`, `240.0.0.0/4`, `::`, `fc00::/7`, `fe80::/10`, `fec0::/10` and `ff00::/8`, in any spelling: bracketed, zoned (`%en0`), and IPv4 addresses embedded as IPv4-mapped, IPv4-compatible or NAT64 IPv6. Numeric hosts that are not a canonical dotted quad (`167772161`, `10.1`, `0x0a.0.0.1`, `012.0.0.1`) are rejected too. Host names and loopback addresses are never rejected. It is applied to the challenge's `resource_metadata` URL, authorization server candidates (skipped; when every candidate is refused, discovery throws the first refusal, `privateIPAddressBlocked` or `insecureAuthorizationServerEndpoint`), and the authorization, token and registration endpoints (`privateIPAddressBlocked`). | ||
| - **Private addresses.** `OAuthURLValidator.isPrivateIPHost` parses literal IP hosts with `inet_pton` and rejects `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10`, `169.254.0.0/16` (cloud metadata), `172.16.0.0/12`, `192.0.0.0/24`, `192.168.0.0/16`, `198.18.0.0/15`, `224.0.0.0/4`, `240.0.0.0/4`, `::`, `fc00::/7`, `fe80::/10`, `fec0::/10` and `ff00::/8`, in any spelling: bracketed, zoned (`%en0`), and IPv4 addresses embedded as IPv4-mapped, IPv4-compatible or NAT64 IPv6. Numeric hosts that are not a canonical dotted quad (`167772161`, `10.1`, `0x0a.0.0.1`, `012.0.0.1`) are rejected too. It never rejects host names or loopback addresses. It is applied to the challenge's `resource_metadata` URL, authorization server candidates (skipped; when every candidate is refused, discovery throws the first refusal, `privateIPAddressBlocked` or `insecureAuthorizationServerEndpoint`), the authorization, token and registration endpoints, and the token endpoint an identity provider's metadata names (`privateIPAddressBlocked`). | ||
| - **Loopback hosts.** A URL a server names may be on a loopback host (`localhost`, a name under `.localhost`, `127.0.0.0/8`, `::1`) only when that server is on one too: the challenge's `resource_metadata` URL and the resource's authorization servers when the MCP endpoint is, and the authorization, token and registration endpoints when the selected authorization server is. Otherwise they are refused like private addresses, before any request. URLs set in `endpointOverrides` may be on loopback; the servers a configured `protectedResourceMetadataURL` lists still follow the MCP endpoint, so set `authorizationServerURL` for a local authorization server. The token endpoint that an identity provider's metadata names (`OAuthTokenExchange.tokenEndpoint(issuer:)`, which `enterpriseManaged` uses with `.issuer`) follows that provider's issuer. Redirects follow the same rule. |
Contributor
There was a problem hiding this comment.
Add a bullet to the 1.0.0 (unreleased) section of CHANGELOG.md, for example:
- OAuth: a URL a server names (a challenge's `resource_metadata`, the listed authorization
servers, and the endpoints in authorization server or identity provider metadata, including
the one `OAuthTokenExchange.tokenEndpoint(issuer:)` returns) may be on a loopback host only
when that server is, and fails with `privateIPAddressBlocked` otherwise. Set
`endpointOverrides` for a local authorization server used with a remote MCP endpoint.Also add a row about the new refusal to the behaviour table in docs/src/migration.src.md, then run swift scripts/weave.swift to regenerate docs/migration.md.
jeanmatthieu
requested changes
Nov 30, 2026
The previous commit refuses a loopback URL that a remote server names (resource_metadata, authorization servers, and the endpoints in authorization server or identity provider metadata), and OAuthTokenExchange.tokenEndpoint(issuer:) now checks the endpoint it returns. Releases up to 0.12.x accepted these setups, so upgraders can hit privateIPAddressBlocked where they did not before. Add a bullet to the behavior changes of 1.0.0 in CHANGELOG.md, and a summary row and two Authorization bullets to the migration guide that point at endpointOverrides. docs/migration.md is regenerated with scripts/weave.swift. Assisted-by: Claude
jeanmatthieu
approved these changes
Dec 1, 2026
jeanmatthieu
merged commit Dec 1, 2026
1df55c4
into
modelcontextprotocol:rel/1.0.0
11 of 14 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
OAuthRedirectGuardfollows a discovery redirect to a loopback host only from a loopback host,but the URLs a server names directly did not follow that rule. The
resource_metadataURL of aWWW-Authenticatechallenge, the protected resource metadata'sauthorization_servers, theauthorization, token and registration endpoints of the authorization server metadata, and the
token endpoint in an identity provider's metadata were checked for private IP literals only.
isPrivateIPHostleaves loopback out on purpose, because the MCP endpoint itself may be onloopback. This applies the redirect rule to those URLs too (MCP 2025-11-25 security best
practices, RFC 9728 section 7.7):
rejectPrivateIPandvalidateAuthorizationServerAndHosttake anallowingLoopbackflag. AURL a server names may be on a loopback host (
localhost, a name under.localhost,127.0.0.0/8,::1) only when that server is; otherwise it fails withprivateIPAddressBlockedbefore any request.resource_metadataURL and the listed authorization servers follow the MCPendpoint. The authorization, token and registration endpoints follow the selected
authorization server, and the token endpoint
OAuthTokenExchange.tokenEndpoint(issuer:)readsfrom an identity provider's metadata follows that provider (
enterpriseManagedwith.issuerrelies on this check).
endpointOverrides, and the endpoint or issuer an application passes toOAuthTokenExchange.exchangeorOAuthTokenExchange.tokenEndpoint(issuer:), may still be onloopback.
OAuthRedirectGuard's loopback check moves toOAuthURLValidator.isLoopbackHost, so bothpaths use the same definition. The internal convenience overloads of
fetchAuthorizationServerMetadatarefuse loopback candidates unless asked.design/auth.md,design/threat_model.mdand the doc comments describethe rule;
AccessTokenProviderContextnow says its endpoints come unchecked from theauthorization server metadata.
How Has This Been Tested?
Adds tests to
OAuthAuthorizerTests(loopbackresource_metadatain seven spellings, loopbacktoken and authorization endpoints, the loopback endpoint and configured loopback server cases,
and which value the discovery and registration paths receive),
OAuthDiscoveryClientTests(loopback candidates, and an end-to-end flow that asserts no request reaches a loopback host),
OAuthClientRegistrarTests,OAuthURLValidatorTests(isLoopbackHost,isLoopback(_:),rejectPrivateIP(_:context:allowingLoopback:)) andOAuthJWTBearerTests(the token endpoints anidentity provider's metadata may name, through
tokenEndpoint(issuer:)andenterpriseManaged,refused issuers, and loopback URLs an application passes). Without the fix these fail.
swift build,swift test, the documentation build with--warnings-as-errorsand the clientconformance suite pass.
Breaking Changes
None in the API. A server that is not on loopback can no longer send the OAuth client to a
loopback host through the URLs it names; set
endpointOverrides.authorizationServerURL(ortokenEndpoint) for a local authorization server used with a remote MCP endpoint.OAuthTokenExchange.tokenEndpoint(issuer:)keeps its signature and now checks the endpoint itreturns as
exchangedoes: it throwsprivateIPAddressBlockedwhen the issuer's metadata names aprivate IP literal, or a loopback host while the issuer is not on loopback, and
insecureAuthorizationServerEndpointorinvalidResourceURIfor an endpoint that is nothttpsor has a fragment, checked first as in
exchange.Release notes
The CHANGELOG entry goes with the release notes of the release that ships this. Draft:
resource_metadata, the listed authorizationservers, and the endpoints in authorization server or identity provider metadata, including
the one
OAuthTokenExchange.tokenEndpoint(issuer:)returns) may be on a loopback host onlywhen that server is, and fails with
privateIPAddressBlockedotherwise. SetendpointOverridesfor a local authorization server used with a remote MCP endpoint.Types of changes
AI Assistance
Drafted with an AI coding agent, then reviewed by me.