|
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.validation import validate_issuer_url, validate_registered_redirect_uri |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def test_validate_issuer_url_https_allowed(): |
@@ -45,3 +45,33 @@ def test_validate_issuer_url_fragment_rejected(): |
45 | 45 | def test_validate_issuer_url_query_rejected(): |
46 | 46 | with pytest.raises(ValueError, match="query"): |
47 | 47 | validate_issuer_url(AnyHttpUrl("https://example.com/path?q=1")) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "redirect_uri", |
| 52 | + [ |
| 53 | + "https://example.com/callback", |
| 54 | + "http://localhost:8080/callback", |
| 55 | + "http://127.0.0.1:8080/callback", |
| 56 | + "http://[::1]:8080/callback", |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_validate_registered_redirect_uri_allowed(redirect_uri: str): |
| 60 | + validate_registered_redirect_uri(AnyUrl(redirect_uri)) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + "redirect_uri,message", |
| 65 | + [ |
| 66 | + ("javascript:alert(1)", "HTTPS or HTTP"), |
| 67 | + ("data:text/html,<script>alert(1)</script>", "HTTPS or HTTP"), |
| 68 | + ("file:///etc/passwd", "HTTPS or HTTP"), |
| 69 | + ("vbscript:msgbox(1)", "HTTPS or HTTP"), |
| 70 | + ("ftp://attacker.example/cb", "HTTPS or HTTP"), |
| 71 | + ("http://attacker.example/cb", "unless loopback"), |
| 72 | + ("https://example.com/cb#frag", "fragment"), |
| 73 | + ], |
| 74 | +) |
| 75 | +def test_validate_registered_redirect_uri_rejected(redirect_uri: str, message: str): |
| 76 | + with pytest.raises(ValueError, match=message): |
| 77 | + validate_registered_redirect_uri(AnyUrl(redirect_uri)) |
0 commit comments