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. --- webmentions_ssg/url_security.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'webmentions_ssg/url_security.py') diff --git a/webmentions_ssg/url_security.py b/webmentions_ssg/url_security.py index a963635..ade64d6 100644 --- a/webmentions_ssg/url_security.py +++ b/webmentions_ssg/url_security.py @@ -56,3 +56,23 @@ def is_http_url(url: str) -> bool: return False return parsed.scheme.lower() in {"http", "https"} and parsed.hostname is not None + + +def is_safe_next_url(next_url: str) -> bool: + """Allow redirects only to paths on this site.""" + # Check the original string too: urlsplit("///evil.example").path is + # "/evil.example", even though browsers can treat the original as external. + if "\\" in next_url or not next_url.startswith("/") or next_url.startswith("//"): + return False + + try: + target = urlsplit(next_url) + except ValueError: + return False + + return ( + not target.scheme + and not target.netloc + and target.path.startswith("/") + and not target.path.startswith("//") + ) -- cgit v1.3.1