|
| 1 | +import pytest |
| 2 | +from pydantic import AnyHttpUrl |
| 3 | + |
| 4 | +from mcp.server.auth.routes import validate_issuer_url |
| 5 | + |
| 6 | + |
| 7 | +def test_validate_issuer_url_https_allowed(): |
| 8 | + validate_issuer_url(AnyHttpUrl("https://example.com/path")) |
| 9 | + |
| 10 | + |
| 11 | +def test_validate_issuer_url_http_localhost_allowed(): |
| 12 | + validate_issuer_url(AnyHttpUrl("http://localhost:8080/path")) |
| 13 | + |
| 14 | + |
| 15 | +def test_validate_issuer_url_http_127_0_0_1_allowed(): |
| 16 | + validate_issuer_url(AnyHttpUrl("http://127.0.0.1:8080/path")) |
| 17 | + |
| 18 | + |
| 19 | +def test_validate_issuer_url_http_ipv6_loopback_allowed(): |
| 20 | + validate_issuer_url(AnyHttpUrl("http://[::1]:8080/path")) |
| 21 | + |
| 22 | + |
| 23 | +def test_validate_issuer_url_http_non_loopback_rejected(): |
| 24 | + with pytest.raises(ValueError, match="Issuer URL must be HTTPS"): |
| 25 | + validate_issuer_url(AnyHttpUrl("http://evil.com/path")) |
| 26 | + |
| 27 | + |
| 28 | +def test_validate_issuer_url_http_127_prefix_domain_rejected(): |
| 29 | + """A domain like 127.0.0.1.evil.com is not loopback.""" |
| 30 | + with pytest.raises(ValueError, match="Issuer URL must be HTTPS"): |
| 31 | + validate_issuer_url(AnyHttpUrl("http://127.0.0.1.evil.com/path")) |
| 32 | + |
| 33 | + |
| 34 | +def test_validate_issuer_url_http_127_prefix_subdomain_rejected(): |
| 35 | + """A domain like 127.0.0.1something.example.com is not loopback.""" |
| 36 | + with pytest.raises(ValueError, match="Issuer URL must be HTTPS"): |
| 37 | + validate_issuer_url(AnyHttpUrl("http://127.0.0.1something.example.com/path")) |
| 38 | + |
| 39 | + |
| 40 | +def test_validate_issuer_url_fragment_rejected(): |
| 41 | + with pytest.raises(ValueError, match="fragment"): |
| 42 | + validate_issuer_url(AnyHttpUrl("https://example.com/path#frag")) |
| 43 | + |
| 44 | + |
| 45 | +def test_validate_issuer_url_query_rejected(): |
| 46 | + with pytest.raises(ValueError, match="query"): |
| 47 | + validate_issuer_url(AnyHttpUrl("https://example.com/path?q=1")) |
0 commit comments