diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/test_auth.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py index a6117b2..3b87923 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -4,10 +4,11 @@ """Test the session user loader.""" +import pytest from flask import Flask from webmentions_ssg import DATABASE as db -from webmentions_ssg.auth import AUTH, load_user +from webmentions_ssg.auth import AUTH, is_safe_next_url, load_user from webmentions_ssg.models import User @@ -89,3 +90,33 @@ def test_password_change_invalidates_existing_session(app: Flask) -> None: ) assert response.status_code == 302 assert client.get("/received").status_code == 200 + + +@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 |
