diff options
| author | Dennis Fink | 2026-10-31 19:33:49 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 19:33:49 +0100 |
| commit | 3a3284abe23e143cbb638877f47958c82afa3367 (patch) | |
| tree | 5409bb940d252756f502d545cc145b382f06a3a0 /webmentions_ssg/__init__.py | |
| parent | 40bdba0f2aa132f47326d2591e83039249ae2121 (diff) | |
| download | webmentions-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-- | webmentions_ssg/__init__.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index 4c185c7..f8fd8f1 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -19,6 +19,7 @@ from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy from flask_wtf.csrf import CSRFProtect from sqlalchemy.orm import DeclarativeBase +from werkzeug.middleware.proxy_fix import ProxyFix from webmentions_ssg.config import ( DefaultConfig, @@ -100,6 +101,14 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.config["IGNORED_HOSTNAMES"] = normalize_ignored_hostnames( app.config["IGNORED_HOSTNAMES"] ) + proxy_hops = app.config["PROXY_FIX_TRUSTED_HOPS"] + if type(proxy_hops) is not int or proxy_hops < 0: + raise ValueError("PROXY_FIX_TRUSTED_HOPS must be a non-negative integer.") + if proxy_hops: + # Trust only the configured number of proxies; never trust forwarded + # host, port or URL prefix headers for rate limiting. + app.wsgi_app = ProxyFix(app.wsgi_app, x_for=proxy_hops, x_proto=proxy_hops) + for key in ( "MAX_CONTENT_LENGTH", "MAX_WEBMENTION_URL_BYTES", |
