aboutsummaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-31 19:33:49 +0100
committerDennis Fink2026-10-31 19:33:49 +0100
commit3a3284abe23e143cbb638877f47958c82afa3367 (patch)
tree5409bb940d252756f502d545cc145b382f06a3a0 /tests/test_config.py
parent40bdba0f2aa132f47326d2591e83039249ae2121 (diff)
downloadwebmentions-ssg-3a3284abe23e143cbb638877f47958c82afa3367.tar.gz
webmentions-ssg-3a3284abe23e143cbb638877f47958c82afa3367.zip
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.
Diffstat (limited to '')
-rw-r--r--tests/test_config.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
index aae7cce..c483ee7 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -6,6 +6,9 @@
from pathlib import Path
+from flask import request
+from flask_limiter.util import get_remote_address
+
import pytest
from wtforms import Field, ValidationError
from wtforms.form import BaseForm
@@ -288,3 +291,84 @@ def test_invalid_resource_limits_fail_at_startup(
setattr(InvalidResourceLimit, key, value)
with pytest.raises(ValueError, match=key):
create_app(InvalidResourceLimit)
+
+
+def test_proxy_fix_disabled_by_default(tmp_path: Path) -> None:
+ class DirectConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "direct.log")
+
+ app = create_app(DirectConfig)
+
+ @app.get("/_proxy-check")
+ def proxy_check() -> dict[str, str]:
+ return {
+ "ip": get_remote_address(),
+ "scheme": request.scheme,
+ "host": request.host,
+ }
+
+ response = app.test_client().get(
+ "/_proxy-check",
+ environ_overrides={"REMOTE_ADDR": "192.0.2.50"},
+ headers={
+ "X-Forwarded-For": "198.51.100.99",
+ "X-Forwarded-Proto": "https",
+ "X-Forwarded-Host": "attacker.example",
+ },
+ )
+ assert response.json == {"ip": "192.0.2.50", "scheme": "http", "host": "localhost"}
+
+
+def test_proxy_fix_trusts_only_configured_proxy_hops(tmp_path: Path) -> None:
+ class TwoProxyConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "proxy.log")
+ PROXY_FIX_TRUSTED_HOPS = 2
+
+ app = create_app(TwoProxyConfig)
+
+ @app.get("/_proxy-check")
+ def proxy_check() -> dict[str, str]:
+ return {
+ "ip": get_remote_address(),
+ "scheme": request.scheme,
+ "host": request.host,
+ }
+
+ response = app.test_client().get(
+ "/_proxy-check",
+ environ_overrides={"REMOTE_ADDR": "192.0.2.50"},
+ headers={
+ "X-Forwarded-For": "198.51.100.99, 203.0.113.4, 192.0.2.10",
+ "X-Forwarded-Proto": "http, https",
+ "X-Forwarded-Host": "attacker.example",
+ },
+ )
+ assert response.json == {"ip": "203.0.113.4", "scheme": "http", "host": "localhost"}
+
+
+@pytest.mark.parametrize("hops", [-1, True, False, None, "1", 1.5])
+def test_invalid_proxy_fix_hop_counts_fail_at_startup(
+ tmp_path: Path, hops: object
+) -> None:
+ class InvalidProxyConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "invalid-proxy.log")
+ PROXY_FIX_TRUSTED_HOPS = hops
+
+ with pytest.raises(ValueError, match="PROXY_FIX_TRUSTED_HOPS"):
+ create_app(InvalidProxyConfig)
+
+
+def test_proxy_fix_can_be_configured_via_environment(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ config_path = tmp_path / "development.cfg"
+ config_path.write_text("# proxy configuration supplied by environment\n")
+ monkeypatch.setenv("WEBMENTIONS_SSG_PROXY_FIX_TRUSTED_HOPS", "1")
+
+ class EnvProxyConfig(DevelopmentConfig):
+ CONFIG_FILE = str(config_path)
+ LOG_FILE = str(tmp_path / "development.log")
+
+ app = create_app(EnvProxyConfig)
+ assert app.config["PROXY_FIX_TRUSTED_HOPS"] == 1
+ assert app.wsgi_app.__class__.__name__ == "ProxyFix"