diff options
| author | Dennis Fink | 2026-10-29 18:57:32 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-29 18:57:32 +0100 |
| commit | aa59080c78a815a7c2e4509c9a8fe73edc93f6c2 (patch) | |
| tree | 500802f1d421a5418f978a60f28401be93b9f830 /tests/test_url_security.py | |
| parent | 8fb010e7ae781e94eefe20fe713a98905b031a8f (diff) | |
| download | webmentions-ssg-aa59080c78a815a7c2e4509c9a8fe73edc93f6c2.tar.gz webmentions-ssg-aa59080c78a815a7c2e4509c9a8fe73edc93f6c2.zip | |
fix(core): prevent unsafe login redirects
Validate post-login redirect targets as local paths and reject
backslashes, scheme-relative URLs, and malformed paths that browsers may
interpret as external destinations.
Add regression coverage for safe local redirects and open redirect
variants.
Diffstat (limited to '')
| -rw-r--r-- | tests/test_url_security.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/test_url_security.py b/tests/test_url_security.py index dee38dd..80b45e9 100644 --- a/tests/test_url_security.py +++ b/tests/test_url_security.py @@ -11,6 +11,7 @@ from webmentions_ssg.url_security import ( AddressResolutionError, is_http_url, is_public_url, + is_safe_next_url, ) @@ -35,6 +36,36 @@ def test_is_http_url(url: str, expected: bool) -> None: assert is_http_url(url) is expected +@pytest.mark.parametrize( + ("next_url", "expected"), + [ + pytest.param("/", True, id="root"), + pytest.param("/received", True, id="local-path"), + pytest.param("/received?page=2", True, id="query-string"), + pytest.param("/received#details", True, id="fragment"), + pytest.param( + "/sent?next=https://evil.example", True, id="external-url-in-query" + ), + pytest.param("/foo//bar", True, id="double-slash-within-path"), + pytest.param("", False, id="empty"), + pytest.param("received", False, id="relative-path"), + pytest.param(" /received", False, id="leading-space"), + pytest.param("https://evil.example/phishing", False, id="absolute-https"), + pytest.param("http://evil.example", False, id="absolute-http"), + pytest.param("javascript:alert(1)", False, id="non-http-scheme"), + pytest.param("//evil.example/phishing", False, id="scheme-relative"), + pytest.param("///evil.example/phishing", False, id="triple-slash"), + pytest.param("////evil.example", False, id="four-slashes"), + pytest.param("//", False, id="two-slashes"), + pytest.param(r"/\evil.example", False, id="slash-backslash-host"), + pytest.param(r"/received\evil.example", False, id="backslash-in-path"), + pytest.param(r"\/evil.example", False, id="backslash-slash-host"), + ], +) +def test_is_safe_next_url(next_url: str, expected: bool) -> None: + assert is_safe_next_url(next_url) is expected + + @patch("webmentions_ssg.url_security.dns.resolver.resolve_name") @pytest.mark.parametrize( ("url", "resolved_addresses"), |
