Skip to content

fix: apply the redirect loopback rule to OAuth URLs that servers name - #412

Merged
jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/oauth-restrict-discovery-to-resource-host
Dec 1, 2026
Merged

jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/oauth-restrict-discovery-to-resource-host

Conversation

@canarik

@canarik canarik commented Nov 30, 2026

Copy link
Copy Markdown

Motivation and Context

OAuthRedirectGuard follows 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_metadata URL of a
WWW-Authenticate challenge, the protected resource metadata's authorization_servers, the
authorization, 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.
isPrivateIPHost leaves loopback out on purpose, because the MCP endpoint itself may be on
loopback. This applies the redirect rule to those URLs too (MCP 2025-11-25 security best
practices, RFC 9728 section 7.7):

  • rejectPrivateIP and validateAuthorizationServerAndHost take an allowingLoopback flag. 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; otherwise it fails with
    privateIPAddressBlocked before any request.
  • The challenge's resource_metadata URL and the listed authorization servers follow the MCP
    endpoint. The authorization, token and registration endpoints follow the selected
    authorization server, and the token endpoint OAuthTokenExchange.tokenEndpoint(issuer:) reads
    from an identity provider's metadata follows that provider (enterpriseManaged with .issuer
    relies on this check).
  • URLs in endpointOverrides, and the endpoint or issuer an application passes to
    OAuthTokenExchange.exchange or OAuthTokenExchange.tokenEndpoint(issuer:), may still be on
    loopback.
  • OAuthRedirectGuard's loopback check moves to OAuthURLValidator.isLoopbackHost, so both
    paths use the same definition. The internal convenience overloads of
    fetchAuthorizationServerMetadata refuse loopback candidates unless asked.
  • The protocol guide, design/auth.md, design/threat_model.md and the doc comments describe
    the rule; AccessTokenProviderContext now says its endpoints come unchecked from the
    authorization server metadata.

How Has This Been Tested?

Adds tests to OAuthAuthorizerTests (loopback resource_metadata in seven spellings, loopback
token 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:)) and OAuthJWTBearerTests (the token endpoints an
identity provider's metadata may name, through tokenEndpoint(issuer:) and enterpriseManaged,
refused issuers, and loopback URLs an application passes). Without the fix these fail.
swift build, swift test, the documentation build with --warnings-as-errors and the client
conformance 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 (or
tokenEndpoint) for a local authorization server used with a remote MCP endpoint.
OAuthTokenExchange.tokenEndpoint(issuer:) keeps its signature and now checks the endpoint it
returns as exchange does: it throws privateIPAddressBlocked when the issuer's metadata names a
private IP literal, or a loopback host while the issuer is not on loopback, and
insecureAuthorizationServerEndpoint or invalidResourceURI for an endpoint that is not https
or 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:

  • A URL an OAuth 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.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

AI Assistance

Drafted with an AI coding agent, then reviewed by me.

`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`.
@canarik
canarik requested a review from a team as a code owner November 30, 2026 20:10
Comment thread docs/src/protocol.src.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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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
jeanmatthieu merged commit 1df55c4 into modelcontextprotocol:rel/1.0.0 Dec 1, 2026
11 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants