Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

12.4 KB
Raw Blame

Breaking change for deployments behind a reverse proxy, whether or not they set TRUST_PROXY_HEADERS. Up to v1.10.7 the rate limiter keyed on the left-most X-Forwarded-For entry, or else X-Real-IP, sent by any peer, so any client could choose its own rate-limit bucket. What it does now depends on the configuration. With TRUST_PROXY_HEADERS unset/false it reads those headers only from a direct peer that is private, loopback or link-local (6to4 2002::/16, Teredo 2001::/32 and NAT64 64:ff9b::/96/64:ff9b:1::/48 peers do not count on any Python version, although Python's ipaddress may report some of them private, depending on the version), or, once LDR_SERVER_FORWARDED_ALLOW_IPS is set to a restricted list, only from a peer in that list; and it keys on the right-most X-Forwarded-For entry, or X-Real-IP when no X-Forwarded-For is present. With TRUST_PROXY_HEADERS=true and a restricted LDR_SERVER_FORWARDED_ALLOW_IPS only a listed peer's headers count: the key is the client address uvicorn derived from a listed peer's X-Forwarded-For, or, when no X-Forwarded-For is present, a listed peer's X-Real-IP; any other peer is keyed on its own address. With TRUST_PROXY_HEADERS=true and LDR_SERVER_FORWARDED_ALLOW_IPS unset the key is the right-most X-Forwarded-For entry from any peer, public addresses included. That is still an improvement on v1.10.7's left-most entry (a client behind one appending proxy can no longer choose its bucket with a forged prefix), but a client connecting straight to LDR writes the whole header and still chooses its key, so set LDR_SERVER_FORWARDED_ALLOW_IPS; LDR logs a startup warning saying so. In this mode X-Real-IP is read only from a private, loopback or link-local peer. What changes for an existing deployment that never set TRUST_PROXY_HEADERS: (1) a proxy that reaches LDR from a public or carrier-grade-NAT address — a cloud load balancer, a Tailscale/CGNAT peer in 100.64.0.0/10, a public nginx host — has its forwarded headers ignored, collapsing every client behind it into one rate-limit bucket and making the login (5/15min) and registration (10/minute and 300/hour) lockouts deployment-global; set LDR_SERVER_FORWARDED_ALLOW_IPS=<proxy address(es)>. (2) A single proxy on a private or Docker network keeps per-client keying, provided it overwrites X-Forwarded-For and X-Real-IP with the address it observed: a client-sent header passed through unchanged still lets that client choose its key, and a proxy that sets only X-Real-IP must also clear X-Forwarded-For, which LDR prefers. An appending proxy now yields the address it observed rather than the client's forged left-most entry. (3) Behind a chain of proxies (CDN → nginx) the default settings key every client on the outer proxy's address, whether the inner proxy appends or overwrites with a plain $remote_addr, so all of them share one bucket and the login and registration lockouts become deployment-global. Either have the inner proxy resolve the real client itself (nginx realip) and overwrite with that address, or make every inner proxy append and set a multi-hop setting: LDR_SERVER_TRUSTED_PROXY_COUNT (below), or TRUST_PROXY_HEADERS=true with every hop listed in LDR_SERVER_FORWARDED_ALLOW_IPS (an allow-list without TRUST_PROXY_HEADERS=true still keys on the right-most entry). See "Proxy chains" in docs/deployment/reverse-proxy.md. What changes for a deployment with TRUST_PROXY_HEADERS=true and no LDR_SERVER_FORWARDED_ALLOW_IPS: points (2) and (3) apply (for an appending chain, list every hop in LDR_SERVER_FORWARDED_ALLOW_IPS; LDR_SERVER_TRUSTED_PROXY_COUNT has no effect in this mode, and setting it without the allow-list makes LDR refuse to start), but a public-address proxy's X-Forwarded-For is still honoured; only its X-Real-IP is ignored (v1.10.7 honoured it from any peer). So a proxy on a public or carrier-grade-NAT address that sends X-Real-IP and no X-Forwarded-For of its own, such as an nginx with only proxy_set_header X-Real-IP $remote_addr, is keyed on its own address and collapses all its clients into one bucket until you set LDR_SERVER_FORWARDED_ALLOW_IPS to that proxy's IP address as LDR sees it; having the proxy overwrite X-Forwarded-For with the address it observed also restores per-client keys. Appending to X-Forwarded-For yields the right rate-limit key there too, but while LDR_SERVER_FORWARDED_ALLOW_IPS is unset uvicorn reports the left-most, client-written entry as the request's client address, so a single proxy should overwrite (for a chain, see (3)).

Rate limiting can now derive the client IP from an explicit, admin-declared trusted-proxy hop count instead of only the pre-existing private-peer heuristic. Set LDR_SERVER_TRUSTED_PROXY_COUNT to the exact number of X-Forwarded-For entries your trusted proxies append (one per appending hop for an ordinary proxy); with TRUST_PROXY_HEADERS unset/false and the direct peer private/loopback or listed in a restricted LDR_SERVER_FORWARDED_ALLOW_IPS, the FastAPI rate limiter then reads the corresponding entry from the right end of X-Forwarded-For (the address appended by the outermost trusted hop), never the raw first (leftmost) entry — which a client can always forge, even through a real proxy. An X-Forwarded-For that is missing or has fewer entries than the configured count, or a direct peer that passes neither check (for example a cloud load balancer on a public address with no allow-list), falls back to the direct socket peer rather than trusting an ambiguous chain; such a public-address proxy must be listed in LDR_SERVER_FORWARDED_ALLOW_IPS, or every client behind it shares one rate-limit bucket. The value must be an ASCII decimal integer from 0 to 16. Surrounding whitespace (Unicode whitespace included) is ignored, and -0 reads as 0. Anything else (a negative value, 1_0, +3, a non-ASCII digit, an out-of-range hop count) prevents startup instead of failing silently, and an empty or whitespace-only value counts as unset. The setting defaults to 0, which keeps the pre-existing behaviour of trusting X-Forwarded-For/X-Real-IP when the direct peer is itself private/loopback (or, once a restricted LDR_SERVER_FORWARDED_ALLOW_IPS is set, only when the direct peer is listed there); beyond the forwarding-trust change described above, deployments that do not set the new variable need no further action.

A new LDR_SERVER_FORWARDED_ALLOW_IPS variable controls which direct peers uvicorn itself trusts to set X-Forwarded-For when TRUST_PROXY_HEADERS=true (mirrors uvicorn's own --forwarded-allow-ips). LDR's default is *, the value the FastAPI server hard-coded before this setting existed; note that this is not uvicorn's own default, which is 127.0.0.1 when uvicorn is launched directly. Existing deployments that only set TRUST_PROXY_HEADERS=true still boot: LDR starts normally and logs a startup warning describing the (unchanged, pre-existing) risk that X-Forwarded-For is spoofable by a client connecting directly to LDR, plus the forwarding-trust change above. LDR refuses to start only if the operator has additionally adopted one of this release's new settings — LDR_SERVER_TRUSTED_PROXY_COUNT set to a positive value, or LDR_SERVER_FORWARDED_ALLOW_IPS set explicitly (including to *) — while the allow-list still trusts every peer; that combination is unconditionally exploitable, so it hard-fails at import time. "Trusts every peer" is decided by uvicorn's own _TrustedHosts semantics rather than by the literal string *, so a whole-family CIDR (0.0.0.0/0, ::/0, 0.0.0.0/0,::/0, or the same range split as 0.0.0.0/1,128.0.0.0/1) is caught too. Empty entries are dropped before the value is used, by both the launcher that passes uvicorn --forwarded-allow-ips and the rate limiter that classifies it, so uvicorn now receives * for "*," (and for ",") instead of a one-entry allow-list matching no peer. A value made up entirely of empty entries (e.g. LDR_SERVER_FORWARDED_ALLOW_IPS=",") therefore resolves to * too, and is warned about exactly like an unset allow-list. When a restricted allow-list contains a network broader than /24 (IPv4) or /64 (IPv6) — an entry overlapping the IPv4-mapped (::ffff:0:0/96), NAT64 well-known (64:ff9b::/96), NAT64 local-use (64:ff9b:1::/48) or Teredo (2001::/32) range is held to the IPv4 limit, since it embeds IPv4 addresses in its low 32 bits, so one that contains such a range (64:ff9b::/64) gets the broad-entry warning an over-wide IPv4 entry would get, rather than passing as an ordinary IPv6 /64 — or an entry uvicorn keeps as an opaque literal because it parses as neither an IP nor a network (nginx, 10.0.0.5/8), LDR logs a startup warning; neither is a hard failure. To silence the warning and lock this down, set LDR_SERVER_FORWARDED_ALLOW_IPS to your reverse proxy's real IP address(es). With TRUST_PROXY_HEADERS=true and such a restricted list, the rate limiter keys on the client address uvicorn computed from the list instead of the right-most X-Forwarded-For entry, so a multi-hop chain whose hops are all listed resolves to the real client. Together with the hop count above, this supersedes the earlier note (#6442) that only one proxy hop is supported. See docs/deployment/reverse-proxy.md. The one-shot warning for an X-Forwarded-Proto: https that is not being honoured now also points at LDR_SERVER_FORWARDED_ALLOW_IPS, and it distinguishes an untrusted proxy from a proxy sending a value uvicorn ignores (HTTPS, or a comma-separated https, https: uvicorn honours only a single exact lower-case value), telling the operator to fix the proxy's header in that case. It never names a client address as the one to allow-list: with TRUST_PROXY_HEADERS=true that address may be one uvicorn copied from a client-supplied X-Forwarded-For.

Two hardening corrections ship with this: (1) whenever LDR_SERVER_FORWARDED_ALLOW_IPS is explicitly configured to a restricted list, that allow-list is the only source of application-level forwarding trust in the rate limiter — a merely-private direct peer outside the list (another container/pod on the same network, a LAN host) can no longer mint fresh rate-limit buckets by rotating a spoofed X-Real-IP or a crafted X-Forwarded-For, in any trust mode; private-ness alone confers forwarding trust only when no restricted allow-list is configured. (2) In trusted-hop-count mode, the selected X-Forwarded-For entry is now normalised by stripping an observed source port (address:port and [v6]:port forms, the same ones uvicorn's own forwarded parser accepts), so a proxy that appends the port it observed no longer hands the same client a fresh per-IP bucket on every new TCP connection.

The startup error raised when the rate-limit storage backend cannot be initialised now also redacts credentials passed in the query string or fragment of RATE_LIMIT_STORAGE_URI / RATELIMIT_STORAGE_URL (for example redis://cache:6379/0?password=...), not only user:pass@ userinfo; a value without @ was previously echoed verbatim. When a ? or # comes before the last @, the value is ambiguous (a password may contain a raw ? or #, and a query value may contain a raw @, as in ?password=p@ss), so everything after scheme:// is replaced and the host is no longer shown. Credentials in other positions, such as a ;password= path parameter, are not redacted. The same redaction now also covers a storage URI that fails before limits builds the backend (a fullwidth #/@/? in the password, which Python rejects with an error quoting the whole host part, or a raw / in the password): a failure in parsing or initialising that URI, whether it came from the environment or from a RATELIMIT_STORAGE_URL line in a .env file in the working directory (which slowapi reads), is reported as a startup error carrying only the redacted URI, with the original error not chained or logged. Any other limiter construction failure (for example a stray RATELIMIT_ENABLED=yes) is reported under its real cause, with the text redacted. Fullwidth separators are folded to their ASCII forms before redacting. LDR looks that .env key up itself only so it can recognise and redact the URI; if the working directory or the file cannot be checked or read, that lookup treats it as having no .env instead of aborting the import (a .env that exists but cannot be read still makes slowapi's own read of it abort the import).