From 3a3284abe23e143cbb638877f47958c82afa3367 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 19:33:49 +0100 Subject: fix(core): harden login rate limiting behind proxies Trust forwarded client addresses only for an explicitly configured number of reverse-proxy hops so per-IP limits cannot be bypassed or collapsed in proxied deployments. Add a separate hashed per-username login limit across IPs to reduce distributed credential-guessing attempts without storing raw usernames in limiter keys. --- tests/test_views.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'tests/test_views.py') diff --git a/tests/test_views.py b/tests/test_views.py index 2108b38..129bdbf 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -835,6 +835,32 @@ def test_endpoint_rate_limits_requests_per_ip( assert count == 2 +def test_endpoint_rate_limit_distinguishes_clients_behind_trusted_proxy( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from werkzeug.middleware.proxy_fix import ProxyFix + + app.config["ENDPOINT_RATE_LIMIT"] = "1/minute" + app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1) + form = mock_endpoint_form(views_module, monkeypatch, valid=True) + monkeypatch.setattr(views_module, "verify_webmention", Mock()) + + def attempt(client_ip: str, index: int) -> int: + form.source.data = f"https://source.example/proxy-post-{index}" + return client.post( + "/endpoint", + environ_overrides={"REMOTE_ADDR": "192.0.2.200"}, + headers={"X-Forwarded-For": client_ip}, + ).status_code + + assert attempt("198.51.100.11", 0) == 201 + assert attempt("198.51.100.11", 1) == 429 + assert attempt("198.51.100.12", 2) == 201 + + def test_endpoint_global_rate_limit( app: Flask, client: FlaskClient, @@ -1040,6 +1066,99 @@ def test_login_rate_limit_is_independent_per_ip( assert attempt("192.0.2.11") == 429 +def test_login_rate_limit_uses_forwarded_client_ip_when_proxy_is_trusted( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + from werkzeug.middleware.proxy_fix import ProxyFix + + app.config["LOGIN_RATE_LIMIT"] = "1/minute" + app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1) + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(client_ip: str) -> int: + response = client.post( + "/login", + environ_overrides={"REMOTE_ADDR": "192.0.2.200"}, + headers={"X-Forwarded-For": client_ip}, + ) + return response.status_code + + assert attempt("198.51.100.11") == 302 + # A forged earlier X-Forwarded-For entry cannot change the client bucket. + assert attempt("203.0.113.99, 198.51.100.11") == 429 + assert attempt("198.51.100.12") == 302 + + +def test_login_username_limit_throttles_across_ips( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "100/minute" + app.config["LOGIN_USERNAME_RATE_LIMIT"] = "2/minute" + + with monkeypatch.context() as patcher: + mock_login_form( + views_module, + patcher, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(ip: str, username: str) -> int: + return client.post( + "/login", + data={"username": username, "password": "wrong-password"}, + environ_overrides={"REMOTE_ADDR": ip}, + ).status_code + + assert attempt("192.0.2.10", "Admin") == 302 + assert attempt("192.0.2.11", " admin ") == 302 + assert attempt("192.0.2.12", "ADMIN") == 429 + # An unrelated username is not locked by attempts against Admin. + assert attempt("192.0.2.12", "another-user") == 302 + + # Restore the real form: the POST-only limits must not block a GET, + # even from the client IP that submitted the throttled attempt. + response = client.get("/login", environ_overrides={"REMOTE_ADDR": "192.0.2.12"}) + assert response.status_code == 200 + + +def test_login_missing_username_uses_per_ip_bucket( + app: Flask, + client: FlaskClient, + views_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + app.config["LOGIN_RATE_LIMIT"] = "100/minute" + app.config["LOGIN_USERNAME_RATE_LIMIT"] = "1/minute" + mock_login_form( + views_module, + monkeypatch, + valid=True, + username="unknown-user", + password="wrong-password", + ) + + def attempt(ip: str) -> int: + return client.post("/login", environ_overrides={"REMOTE_ADDR": ip}).status_code + + assert attempt("192.0.2.10") == 302 + assert attempt("192.0.2.10") == 429 + assert attempt("192.0.2.11") == 302 + + def test_login_rate_limits_requests_per_ip( app: Flask, client: FlaskClient, -- cgit v1.3.1