|
1 | 1 | import pytest |
2 | | -from pydantic import AnyHttpUrl |
| 2 | +from pydantic import AnyHttpUrl, AnyUrl |
3 | 3 |
|
4 | | -from mcp.server.auth.routes import validate_issuer_url |
| 4 | +from mcp.server.auth.routes import validate_issuer_url, validate_registered_redirect_uri |
| 5 | +from mcp.shared.auth import InvalidRedirectUriError |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def test_validate_issuer_url_https_allowed(): |
@@ -45,3 +46,59 @@ def test_validate_issuer_url_fragment_rejected(): |
45 | 46 | def test_validate_issuer_url_query_rejected(): |
46 | 47 | with pytest.raises(ValueError, match="query"): |
47 | 48 | validate_issuer_url(AnyHttpUrl("https://example.com/path?q=1")) |
| 49 | + |
| 50 | + |
| 51 | +def test_validate_registered_redirect_uri_https_allowed(): |
| 52 | + validate_registered_redirect_uri(AnyUrl("https://example.com/cb")) |
| 53 | + |
| 54 | + |
| 55 | +def test_validate_registered_redirect_uri_https_with_query_allowed(): |
| 56 | + validate_registered_redirect_uri(AnyUrl("https://example.com/cb?foo=bar")) |
| 57 | + |
| 58 | + |
| 59 | +def test_validate_registered_redirect_uri_http_localhost_allowed(): |
| 60 | + validate_registered_redirect_uri(AnyUrl("http://localhost:8080/cb")) |
| 61 | + |
| 62 | + |
| 63 | +def test_validate_registered_redirect_uri_http_127_0_0_1_allowed(): |
| 64 | + validate_registered_redirect_uri(AnyUrl("http://127.0.0.1:8080/cb")) |
| 65 | + |
| 66 | + |
| 67 | +def test_validate_registered_redirect_uri_http_ipv6_loopback_allowed(): |
| 68 | + validate_registered_redirect_uri(AnyUrl("http://[::1]:8080/cb")) |
| 69 | + |
| 70 | + |
| 71 | +def test_validate_registered_redirect_uri_javascript_scheme_rejected(): |
| 72 | + with pytest.raises(InvalidRedirectUriError, match="must use https"): |
| 73 | + validate_registered_redirect_uri(AnyUrl("javascript:alert(1)")) |
| 74 | + |
| 75 | + |
| 76 | +def test_validate_registered_redirect_uri_data_scheme_rejected(): |
| 77 | + with pytest.raises(InvalidRedirectUriError, match="must use https"): |
| 78 | + validate_registered_redirect_uri(AnyUrl("data:text/html,x")) |
| 79 | + |
| 80 | + |
| 81 | +def test_validate_registered_redirect_uri_file_scheme_rejected(): |
| 82 | + with pytest.raises(InvalidRedirectUriError, match="must use https"): |
| 83 | + validate_registered_redirect_uri(AnyUrl("file:///etc/passwd")) |
| 84 | + |
| 85 | + |
| 86 | +def test_validate_registered_redirect_uri_ftp_scheme_rejected(): |
| 87 | + with pytest.raises(InvalidRedirectUriError, match="must use https"): |
| 88 | + validate_registered_redirect_uri(AnyUrl("ftp://attacker.example/cb")) |
| 89 | + |
| 90 | + |
| 91 | +def test_validate_registered_redirect_uri_http_non_loopback_rejected(): |
| 92 | + with pytest.raises(InvalidRedirectUriError, match="must use https for non-loopback"): |
| 93 | + validate_registered_redirect_uri(AnyUrl("http://attacker.example/cb")) |
| 94 | + |
| 95 | + |
| 96 | +def test_validate_registered_redirect_uri_http_127_prefix_domain_rejected(): |
| 97 | + """A domain like 127.0.0.1.evil.com is NOT loopback.""" |
| 98 | + with pytest.raises(InvalidRedirectUriError, match="must use https for non-loopback"): |
| 99 | + validate_registered_redirect_uri(AnyUrl("http://127.0.0.1.evil.com/cb")) |
| 100 | + |
| 101 | + |
| 102 | +def test_validate_registered_redirect_uri_fragment_rejected(): |
| 103 | + with pytest.raises(InvalidRedirectUriError, match="must not have a fragment"): |
| 104 | + validate_registered_redirect_uri(AnyUrl("https://example.com/cb#frag")) |
0 commit comments