Details
Root Causes
- A client-controlled HTTP header (
X-Forwarded-For) is used directly in security decisions (rate-limit bucketing and audit attribution) without any trusted-proxy validation (CWE-807). - The leftmost XFF token is selected, which is always the value supplied by the original client; this is spoofable both in direct-exposure and behind-a-proxy deployments (CWE-348).
- No
ProxyFix/ trusted-proxy-hop configuration exists, and the server terminates client connections directly, so there is no basis for trusting forwarded headers at all. - Inconsistent handling across the codebase (loopback checks distrust these headers while rate-limit/logging trust them) indicates the trust is accidental rather than designed.
Suggested Fix
- By default, use
request.remote_addr(the real TCP peer) for rate limiting and logging; do not trustX-Forwarded-Forunless a proxy is explicitly configured. - Add an explicit, operator-configured trusted-proxy count (default 0 = trust none). When set, apply Werkzeug's
ProxyFix(app.wsgi_app, x_for=N)and read the correct hop (the value inserted by the trusted proxy — typically the Nth-from-right token — not the leftmost client-supplied token). - Derive the trusted client IP once in a single helper and use it consistently for rate limiting, logging, and the loopback checks, removing the duplicated inline expressions.
- Consider defense-in-depth: cap the number of distinct rate-limit buckets and/or additionally rate-limit on
remote_addrso a spoofed header cannot create unbounded buckets.
Invariant to restore: security decisions and audit attribution must be based on a connection-level
or trusted-proxy-validated address, never on a raw client-supplied header.