From aa59080c78a815a7c2e4509c9a8fe73edc93f6c2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 29 Oct 2026 18:57:32 +0100 Subject: 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. --- tests/test_url_security.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/test_url_security.py') 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"), -- cgit v1.3.1