diff options
| author | Dennis Fink | 2026-10-29 18:57:32 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-29 18:57:32 +0100 |
| commit | aa59080c78a815a7c2e4509c9a8fe73edc93f6c2 (patch) | |
| tree | 500802f1d421a5418f978a60f28401be93b9f830 /tests/test_views.py | |
| parent | 8fb010e7ae781e94eefe20fe713a98905b031a8f (diff) | |
| download | webmentions-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 '')
| -rw-r--r-- | tests/test_views.py | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/tests/test_views.py b/tests/test_views.py index 3de4c3d..c7f86d2 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -404,11 +404,21 @@ def test_login_rejects_invalid_credentials( assert ("danger", "Invalid username or password") in session["_flashes"] +@pytest.mark.parametrize( + "next_url", + [ + pytest.param("/", id="root"), + pytest.param("/received", id="local-path"), + pytest.param("/received?page=2", id="query-string"), + pytest.param("/received#details", id="fragment"), + ], +) def test_login_redirects_to_local_next_page( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, + next_url: str, ) -> None: with app.app_context(): user = User(username="admin", password="test") @@ -418,32 +428,55 @@ def test_login_redirects_to_local_next_page( mock_login_form(views_module, monkeypatch, valid=True) - response = client.post("/login?next=/received?page=2") + response = client.post("/login", query_string={"next": next_url}) assert response.status_code == 302 - assert response.headers["Location"].endswith("/received?page=2") + assert response.headers["Location"] == next_url with client.session_transaction() as session: assert session["_user_id"] == str(user_id) -def test_login_rejects_external_next_page( +@pytest.mark.parametrize( + "next_url", + [ + pytest.param(None, id="missing"), + pytest.param("", id="empty"), + pytest.param("received", id="relative-path"), + pytest.param("https://evil.example/phishing", id="absolute-https"), + pytest.param("http://evil.example", id="absolute-http"), + pytest.param("javascript:alert(1)", id="non-http-scheme"), + pytest.param("//evil.example/phishing", id="scheme-relative"), + pytest.param("///evil.example/phishing", id="triple-slash"), + pytest.param("////evil.example", id="four-slashes"), + pytest.param(r"/\evil.example", id="slash-backslash-host"), + pytest.param(r"/received\evil.example", id="backslash-in-path"), + pytest.param(r"\/evil.example", id="backslash-slash-host"), + ], +) +def test_login_rejects_unsafe_next_page( app: Flask, client: FlaskClient, views_module: ModuleType, monkeypatch: pytest.MonkeyPatch, + next_url: str | None, ) -> None: with app.app_context(): - db.session.add(User(username="admin", password="test")) + user = User(username="admin", password="test") + db.session.add(user) db.session.commit() + user_id = user.id mock_login_form(views_module, monkeypatch, valid=True) - response = client.post("/login?next=https://evil.example/phishing") + query_string = {} if next_url is None else {"next": next_url} + response = client.post("/login", query_string=query_string) assert response.status_code == 302 - assert response.headers["Location"].endswith("/") - assert "evil.example" not in response.headers["Location"] + assert response.headers["Location"] == "/" + + with client.session_transaction() as session: + assert session["_user_id"] == str(user_id) def test_login_redirects_authenticated_user(app: Flask, client: FlaskClient) -> None: |
