aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/url_security.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-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("//")
+ )