aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/url_security.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-29 18:57:32 +0100
committerDennis Fink2026-10-29 18:57:32 +0100
commitaa59080c78a815a7c2e4509c9a8fe73edc93f6c2 (patch)
tree500802f1d421a5418f978a60f28401be93b9f830 /webmentions_ssg/url_security.py
parent8fb010e7ae781e94eefe20fe713a98905b031a8f (diff)
downloadwebmentions-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 'webmentions_ssg/url_security.py')
-rw-r--r--webmentions_ssg/url_security.py20
1 files changed, 20 insertions, 0 deletions
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("//")
+ )