-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Validates registered redirect_uris for DCR are a secure schema with no fragments #3206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6c62757
fb893dd
9060ac0
0561256
4e77490
a0e41d2
e9bb792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| """MCP OAuth server authorization components.""" | ||
|
|
||
| from .url_validators import validate_issuer_url, validate_redirect_uri |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """OAuth 2.0 URL validation helpers for MCP authorization servers. | ||
|
|
||
| RFC 9700 4.1.1 and RFC 7591 2 require HTTPS for authorization endpoint URLs | ||
| and registered redirect_uris, with an HTTP loopback exception for local | ||
| development. | ||
| """ | ||
|
|
||
| from pydantic import AnyUrl | ||
|
|
||
|
|
||
| def validate_issuer_url(url: AnyHttpUrl): | ||
| """Validate that the issuer URL meets OAuth 2.0 requirements. | ||
|
|
||
| Args: | ||
| url: The issuer URL to validate. | ||
|
|
||
| Raises: | ||
| ValueError: If the issuer URL is invalid. | ||
| """ | ||
| if url.scheme != "https" and url.host not in ("localhost", "127.0.0.1", "[::1]"): | ||
| raise ValueError("Issuer URL must be HTTPS") | ||
|
|
||
| if url.fragment: | ||
| raise ValueError("Issuer URL must not have a fragment") | ||
| if url.query: | ||
| raise ValueError("Issuer URL must not have a query string") | ||
|
|
||
|
|
||
| def validate_redirect_uri(url: AnyUrl): | ||
| """Validate a registered redirect_uri for DCR. | ||
|
|
||
| RFC 9700 section 4.1.1 and RFC 7591 section 2 require HTTPS for | ||
| redirect_uris, with an HTTP loopback exception for local development. | ||
|
|
||
| Args: | ||
| url: The redirect URI to validate. | ||
|
|
||
| Raises: | ||
| ValueError: If the redirect URI uses an unsafe scheme or contains | ||
| a fragment. | ||
| """ | ||
| if url.scheme not in ("http", "https"): | ||
| raise ValueError("Redirect URI must use an HTTP(S) scheme") | ||
|
|
||
| if url.fragment is not None: | ||
| raise ValueError("Redirect URI must not contain a fragment") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import pytest | ||
| from pydantic import AnyHttpUrl | ||
| from pydantic import AnyHttpUrl, AnyUrl | ||
|
|
||
| from mcp.server.auth.routes import build_metadata, validate_issuer_url | ||
| from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions, RevocationOptions | ||
| from mcp.server.auth.url_validators import validate_redirect_uri | ||
|
|
||
|
|
||
| def test_validate_issuer_url_https_allowed(): | ||
|
|
@@ -70,3 +71,43 @@ def test_build_metadata_serves_issuer_without_trailing_slash(): | |
| assert served["issuer"] == "https://as.example.com" | ||
| assert served["authorization_endpoint"] == "https://as.example.com/authorize" | ||
| assert served["token_endpoint"] == "https://as.example.com/token" | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_https_allowed(): | ||
| validate_redirect_uri(AnyHttpUrl("https://example.com/cb")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_http_localhost_allowed(): | ||
| validate_redirect_uri(AnyHttpUrl("http://localhost:3000/cb")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_http_127_0_0_1_allowed(): | ||
| validate_redirect_uri(AnyHttpUrl("http://127.0.0.1:8080/cb")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_http_ipv6_loopback_allowed(): | ||
| validate_redirect_uri(AnyHttpUrl("http://[::1]:9090/cb")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_javascript_scheme_rejected(): | ||
| with pytest.raises(ValueError, match="Redirect URI must use an HTTP"): | ||
| validate_redirect_uri(AnyUrl("javascript:alert(1)")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_file_scheme_rejected(): | ||
| with pytest.raises(ValueError, match="Redirect URI must use an HTTP"): | ||
| validate_redirect_uri(AnyUrl("file:///etc/passwd")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_http_non_loopback_allowed(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: validate_redirect_uri accepts http:// for any host, not just loopback. The docstring promises RFC 9700 HTTPS enforcement with loopback exception, but only non-HTTP(S) schemes are rejected — http://evil.com/cb passes. This exposes authorization codes to interception over plain HTTP for non-loopback redirect URIs. Prompt for AI agents |
||
| validate_redirect_uri(AnyHttpUrl("http://evil.com/cb")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_fragment_rejected(): | ||
| with pytest.raises(ValueError, match="Redirect URI must not contain a fragment"): | ||
| validate_redirect_uri(AnyHttpUrl("https://example.com/cb#frag")) | ||
|
|
||
|
|
||
| def test_validate_redirect_uri_empty_fragment_rejected(): | ||
| with pytest.raises(ValueError, match="Redirect URI must not contain a fragment"): | ||
| validate_redirect_uri(AnyHttpUrl("https://example.com/cb#")) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P0: Importing the auth URL validators now fails at module initialization because
validate_issuer_urlstill evaluates an undefinedAnyHttpUrlannotation. KeepAnyHttpUrlimported alongsideAnyUrlso the existing issuer validator and all auth imports remain usable.Prompt for AI agents