~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Rate limiting for FastAPI via slowapi.
Replaces Flask-Limiter. Provides the same rate limit decorators
used by auth routes (login, register, change-password).
Client-IP trust model (see docs/deployment/reverse-proxy.md for the
operator-facing version):
* ``TRUST_PROXY_HEADERS=true``: uvicorn's ``ProxyHeadersMiddleware``
(the outermost ASGI layer) is the authority on ``request.client``
when a restricted ``LDR_SERVER_FORWARDED_ALLOW_IPS`` is configured;
``_get_client_ip`` then never re-parses X-Forwarded-For itself. With
the legacy unrestricted ``"*"`` it keys on the right-most
X-Forwarded-For entry instead of uvicorn's left-most one. Werkzeug's
``ProxyFix`` is NOT installed anywhere in the FastAPI stack. When no X-Forwarded-For header is present at all,
uvicorn leaves the raw socket peer in place, so this module falls
back to X-Real-IP itself -- gated by
``_peer_may_supply_forwarding_headers`` (allow-list membership when
a restricted ``LDR_SERVER_FORWARDED_ALLOW_IPS`` is explicitly
configured, private/loopback peer otherwise).
* ``TRUST_PROXY_HEADERS=false`` + ``LDR_SERVER_TRUSTED_PROXY_COUNT=N``
(> 0): hop-count mode -- reads the Nth entry from the right of
X-Forwarded-For, port-stripped, gated by the same peer check.
* ``TRUST_PROXY_HEADERS=false`` + count 0 (default): pre-existing
heuristic -- right-most X-Forwarded-For entry / X-Real-IP honoured
when the direct peer passes the same check.
Rate-limit VALUES (default/login/registration/...) load from
``web.server_config.load_server_config()`` (env var > legacy
server_config.json > built-in default) -- this module deliberately
still depends on that sibling web module for its configuration.
"""
import unicodedata
import asyncio
import inspect
import ipaddress
import os
import re
import sys
from collections.abc import Callable
from contextvars import ContextVar
from functools import lru_cache, wraps
from pathlib import Path
from typing import Any
from limits.errors import ConfigurationError
from loguru import logger
from slowapi import Limiter
from starlette.config import Config as StarletteConfig
from starlette.requests import Request
from starlette.responses import Response
from ...security.log_sanitizer import redact_secrets
from ..server_config import load_server_config
from ...security.network_utils import is_private_peer_ip
from ...settings.env_definitions.server import TRUSTED_PROXY_COUNT
from ...settings.env_registry import is_rate_limiting_enabled
def _log_startup(level: str, message: str) -> None:
"""Log one forwarding-trust startup line where the operator sees it.
``local_deep_research/__init__.py`` disables the package's loguru
namespace, and only ``config_logger()`` enables it again. The
``ldr-web`` launcher calls that before uvicorn imports the app, but
an external ASGI server (``uvicorn
local_deep_research.web.fastapi_app:app``, gunicorn's
``UvicornWorker``) never does, so there a plain ``logger.warning``
from this module's import is dropped without a trace. A temporary
loguru handler records whether loguru delivered the line; when it
did not, the line goes to stderr instead. Each line is emitted once
either way.
That stderr write is a CodeQL ``py/clear-text-logging-sensitive-data``
sink, and the query treats any value held under a name containing
"trusted" (or "secret", "password" and the like) as a secret. So
``message`` must never interpolate such a value, directly or through
another variable: ``_TRUSTED_PROXY_COUNT`` tripped it, although it
only holds a hop count.
Two limits of the probe, both accepted:
* "Delivered" means the probe handler saw the record, which happens
exactly when the package's namespace is enabled. It does not mean a
sink displayed the line: an embedder that enabled the namespace but
installed no sink, or only sinks that filter the line out, sees
nothing and gets no stderr fallback. ``config_logger()`` installs
INFO-level sinks, so ``ldr-web`` shows these lines.
* The probe is removed by its id. A bare ``logger.remove()`` (which
drops every handler) from another thread between the add and the
remove would make the remove raise ``ValueError`` and abort the
import. ``config_logger()`` is the only such call on the web
start-up path and ``ldr-web`` runs it before the app is imported,
so nothing does this today.
"""
delivered: list[str] = []
probe = logger.add(
delivered.append,
level=0,
format="{message}",
filter=lambda record: (
record["function"] == "_log_startup" and record["name"] == __name__
),
)
try:
logger.log(level, message)
finally:
logger.remove(probe)
if delivered or sys.stderr is None:
return
try:
sys.stderr.write(f"{level}:{__name__}:{message}\n")
sys.stderr.flush()
except (OSError, ValueError):
# A closed or broken stderr must not abort the import that is
# starting the server; there is nowhere left to report it.
pass
# When the app is reachable over the public internet without a reverse
# proxy, X-Forwarded-For is attacker-controlled and must NOT be trusted.
# Operators behind a real proxy (nginx, caddy, traefik) should set
# `TRUST_PROXY_HEADERS=true`. The default is "trust if the direct peer is
# on a private/loopback network", which is safe for typical Docker/k8s
# deployments and refuses spoofing from public peers.
#
# This is read from a non-LDR_-prefixed env var so it can be loaded at
# module import time, before SettingsManager is initialised.
_TRUST_PROXY_HEADERS = os.environ.get("TRUST_PROXY_HEADERS", "").lower() in (
"true",
"1",
"yes",
)
# Which direct peers uvicorn trusts forwarded headers from when
# TRUST_PROXY_HEADERS=true -- mirrors `web/app.py`'s `--forwarded-allow-ips`
# read exactly (same env var, same default, read the same way at import
# time) so this module's view of the setting matches what uvicorn was
# actually launched with. Defaults to "*" (any peer), the value web/app.py
# hard-coded before this variable existed, so every deployment that predates
# it behaves as before. (That is NOT uvicorn's own default, which is
# "127.0.0.1" when uvicorn is launched directly.)
#
# This is the setting that determines whether TRUST_PROXY_HEADERS=true is
# safe or exploitable, independent of LDR_SERVER_TRUSTED_PROXY_COUNT below
# -- see the check immediately following.
#
# Empty entries are dropped before anything else looks at the value, so a
# stray trailing comma cannot turn the wildcard into a "restricted" list
# that matches nothing: `"*,"` normalises to `"*"` (and therefore trips the
# unrestricted check below) rather than silently collapsing every client
# into one bucket while logging a "safe combination" line.
#
# `web/app.py`'s `_normalise_forwarded_allow_ips` is the TWIN of the two
# helpers below and the thing that actually hands uvicorn its
# `--forwarded-allow-ips`. Both must turn the same raw value into the same
# string, or every message this module logs describes a different
# allow-list from the one uvicorn was launched with (`","` used to reach
# uvicorn verbatim while this module read it as unset). The two are pinned
# together by tests/security/test_middleware_and_proxy_trust_fastapi.py::
# TestTrustProxyHeadersUvicornWiring::
# test_app_and_rate_limit_normalise_identically; they are duplicated rather
# than shared because `web/app.py` must not import this module before
# uvicorn starts (importing it runs the startup guard, freezes the
# allow-list at that moment, and drags slowapi/limits and the server
# config in with it).
def _forwarded_allow_entries(raw: str) -> tuple[str, ...]:
"""The non-empty, whitespace-stripped entries of ``raw``."""
return tuple(item.strip() for item in raw.split(",") if item.strip())
def _normalise_forwarded_allow_ips(raw: str) -> str:
"""The exact allow-list string uvicorn is handed for ``raw``."""
return ",".join(_forwarded_allow_entries(raw)) or "*"
_FORWARDED_ALLOW_IPS_RAW = os.environ.get("LDR_SERVER_FORWARDED_ALLOW_IPS", "")
_FORWARDED_ALLOW_ENTRIES_CONFIGURED: tuple[str, ...] = _forwarded_allow_entries(
_FORWARDED_ALLOW_IPS_RAW
)
_FORWARDED_ALLOW_IPS = _normalise_forwarded_allow_ips(_FORWARDED_ALLOW_IPS_RAW)
# Stronger, explicit alternative to the peer heuristic above: the exact
# number of trusted reverse-proxy hops that append to X-Forwarded-For
# (`LDR_SERVER_TRUSTED_PROXY_COUNT`, ported from PR #4870's Flask-side
# design). Defaults to 0, which is treated as "unset" below, so a
# deployment that never configures this setting gets byte-identical
# behaviour to before this setting existed (see the `else` branch of
# `_get_client_ip`). IntegerSetting reads straight from the environment
# (no DB/SettingsManager needed, safe at import time) and
# `raise_on_validation_error=True` makes a malformed, non-numeric,
# negative or absurdly large value abort startup instead of silently
# degrading the trust boundary; an EMPTY value is treated as unset (the
# repo-wide `LDR_X=""` contract, see
# tests/test_config_env_var_contracts.py::
# test_empty_env_var_is_treated_as_unset_not_as_false).
#
# The raw `os.environ` read next to it is deliberate and not merely
# duplicated work: it is what makes this import-time read visible to the
# AST sweeps that pin the port's frozen-at-import configuration
# (tests/web/test_startup_ordering_contracts.py's `_IMPORT_TIME_CONFIG`
# and tests/test_config_env_var_contracts.py's `_KNOWN_IMPORT_TIME_READS`,
# both of which match `os.environ.get`/`os.getenv` call sites and cannot
# see a typed setting object's `.get_value()`).
_TRUSTED_PROXY_COUNT_RAW = os.environ.get("LDR_SERVER_TRUSTED_PROXY_COUNT", "")
_TRUSTED_PROXY_COUNT: int = TRUSTED_PROXY_COUNT.get_value() or 0
# Whether the operator has actually touched either of the two NEW knobs
# this PR introduces, as opposed to only ever having set the pre-existing
# `TRUST_PROXY_HEADERS=true` (which predates this PR and is documented on
# its own). Checked against what the operator actually WROTE rather than
# the resolved `_FORWARDED_ALLOW_IPS`/`_TRUSTED_PROXY_COUNT` values,
# because an operator who explicitly writes
# `LDR_SERVER_FORWARDED_ALLOW_IPS="*"` has made a deliberate (unsafe)
# choice and must still hard-fail below, even though "*" is also what an
# *unset* var resolves to. An EMPTY value (`LDR_SERVER_FORWARDED_ALLOW_
# IPS=""`, which orchestrators such as Compose/k8s emit routinely for an
# unconfigured variable) contributes no entries and therefore does NOT
# count as explicitly set -- same `LDR_X=""` means unset contract as the
# hop count above.
_FORWARDED_ALLOW_IPS_EXPLICIT = bool(_FORWARDED_ALLOW_ENTRIES_CONFIGURED)
_PROXY_TRUST_OPTED_IN = (
_TRUSTED_PROXY_COUNT > 0 or _FORWARDED_ALLOW_IPS_EXPLICIT
)
# uvicorn's own trusted-host container, used below to decide -- by uvicorn's
# semantics rather than by string comparison against the literal "*" --
# whether the configured allow-list actually restricts anything. It is
# PRIVATE API (leading underscore): pyproject requires
# `uvicorn[standard]~=0.34` and pdm.lock resolves 0.54.0, where
# `_TrustedHosts.__init__(trusted_hosts)` / `.always_trust` /
# `.trusted_hosts` / `.trusted_networks` all exist, but that pin admits any
# 0.x, and a RENAME in a future one raises AttributeError or TypeError --
# not ImportError -- at construction or at attribute-read time. So BOTH the
# import and every use of the class are guarded with a bare `Exception`,
# and the failure degrades to `_LocalTrustedHosts` below instead of
# aborting import with a raw traceback.
def _warn_uvicorn_parser_unavailable(detail: str) -> None:
"""Name the fallback classifier taking over.
Emitted from exactly one of the two guarded paths per process: if
the import failed there is nothing left to construct, and if it
succeeded the import never warned.
"""
_log_startup(
"WARNING",
f"uvicorn's private _TrustedHosts class is unusable ({detail}), so "
"LDR cannot classify LDR_SERVER_FORWARDED_ALLOW_IPS with uvicorn's "
"own parser. Falling back to LDR's own allow-list parser, which "
"fails CLOSED: a list counts as unrestricted only if the whole "
'value is literally "*" or the entries cover a whole address '
"family, so an explicitly restricted allow-list STAYS restricted (no "
"private/loopback peer silently regains forwarding trust) and "
"TRUST_PROXY_HEADERS=true with an exact proxy address still "
"starts. Please report this against the uvicorn version in "
"pdm.lock.",
)
# The except branch is reached only by a module reload with the import
# broken -- see TestUvicornParserFallback in
# tests/web/dependencies/test_rate_limit_restricted_allow_list.py -- never
# at real import time, which is what the pragma records.
try:
from uvicorn.middleware.proxy_headers import (
_TrustedHosts as _UvicornTrustedHosts,
)
except Exception as exc: # pragma: no cover - TestUvicornParserFallback
_UvicornTrustedHosts = None
_warn_uvicorn_parser_unavailable(f"import failed: {exc!r}")
class _LocalTrustedHosts:
"""LDR's own parse of an allow-list, shaped like uvicorn's.
Exposes exactly the three attributes
``_allow_list_covers_whole_families`` and
``_describe_unrestricted_allow_list`` read, parsed the same way
``_peer_in_forwarded_allow_list`` already parses the same entries
(``ipaddress``: an entry containing ``/`` is a network, otherwise an
address, otherwise an opaque literal). Used only when uvicorn's
private class cannot be imported or cannot be used.
Matches uvicorn's ``always_trust`` exactly: it is the whole
(normalised) value being ``"*"``, not any entry spelled ``"*"``.
``"*,*"`` and ``"10.0.0.5,*"`` are therefore restricted here just as
they are under uvicorn's own parser -- a stray ``*`` among other
entries is neither a network nor a valid address, so it becomes an
opaque literal that matches no peer (the existing "matches no peer"
warning from ``_warn_about_imprecise_allow_list_entries`` already
covers it) rather than silently widening the list.
"""
__slots__ = ("always_trust", "trusted_hosts", "trusted_networks")
def __init__(self, allow_ips: str) -> None:
entries = _forwarded_allow_entries(allow_ips)
self.always_trust: bool = entries == ("*",)
self.trusted_hosts: set = set()
self.trusted_networks: set = set()
if self.always_trust:
return
for entry in entries:
if "/" in entry:
try:
self.trusted_networks.add(ipaddress.ip_network(entry))
except ValueError:
continue
else:
try:
self.trusted_hosts.add(ipaddress.ip_address(entry))
except ValueError:
continue
#: The IPv6 range every IPv4 address is reachable through on a listener
#: that accepts IPv6 (``8.8.8.8`` arrives as ``::ffff:8.8.8.8``). An entry
#: spanning it therefore trusts the whole IPv4 family there, even though
#: 2**32 addresses out of 2**128 never reach the family-coverage sum below.
_IPV4_MAPPED_RANGE = ipaddress.ip_network("::ffff:0.0.0.0/96")
#: The NAT64 well-known prefix (RFC 6052). Behind a NAT64/SIIT translator an
#: IPv4 peer reaches an IPv6 listener as ``64:ff9b::<ipv4>``, so a /96 entry
#: here trusts every IPv4 host coming through the translator -- the same
#: breadth as ``0.0.0.0/0`` for that path, though only a /96 in IPv6 terms.
_NAT64_WELL_KNOWN_RANGE = ipaddress.ip_network("64:ff9b::/96")
#: The NAT64 local-use prefix (RFC 8215). An operator carves their own
#: translator prefix out of it, and every IPv4 host coming through that
#: translator is embedded under it just as under the well-known /96.
_NAT64_LOCAL_USE_RANGE = ipaddress.ip_network("64:ff9b:1::/48")
#: Teredo (RFC 4380). The low 32 bits of a Teredo address are its
#: client's (obfuscated) public IPv4 address, and the 32 bits above them
#: its port and flags, so a /64 inside this range is one relay server with
#: every client behind it, not one host.
_TEREDO_RANGE = ipaddress.ip_network("2001::/32")
#: IPv6 ranges whose addresses stand for IPv4 hosts in their low 32 bits.
#: An allow-list entry that overlaps any of them -- whether it sits inside
#: one or contains it -- is judged by the IPv4 breadth limit, not the IPv6
#: one.
_IPV4_EMBEDDING_RANGES = (
_IPV4_MAPPED_RANGE,
_NAT64_WELL_KNOWN_RANGE,
_NAT64_LOCAL_USE_RANGE,
_TEREDO_RANGE,
)
def _strip_v6_scope(network):
"""``network`` with its zone/scope id (``%eth0``) discarded.
``ipaddress`` compares a scoped ``IPv6Network`` by scope id as well as
address range, so ``::ffff:0:0%eth0/96`` is neither equal to nor a
superset/subset of the unscoped ``::ffff:0:0/96`` even though they
denote the exact same range: a scope id only disambiguates which
interface a link-local address is reachable on, it is not part of the
address itself, and uvicorn's own matching (``get_trusted_client_address``
compares the integer address) ignores it entirely. Family-coverage and
mapped-range containment checks must therefore compare the
scope-stripped range, or a scoped spelling of an otherwise-covering
entry silently evades them.
"""
if network.version != 6 or network.network_address.scope_id is None:
return network
return ipaddress.IPv6Network(
(int(network.network_address), network.prefixlen)
)
def _family_blocks(trusted, version: int) -> list:
"""Every entry of ``trusted`` in ``version``, as networks."""
blocks = [
ipaddress.ip_network(host)
for host in trusted.trusted_hosts
if host.version == version
]
blocks += [
network
for network in trusted.trusted_networks
if network.version == version
]
if version == 6:
blocks = [_strip_v6_scope(b) for b in blocks]
return blocks
def _family_is_fully_covered(blocks: list, bits: int) -> bool:
"""Whether ``blocks`` span every address of a ``bits``-wide family.
True when any entry is a zero-length prefix OR when the entries,
merged, span the whole family -- ``0.0.0.0/1,128.0.0.0/1`` is
``0.0.0.0/0`` written in two pieces and uvicorn treats it
identically. Merging with ``collapse_addresses`` first is what makes
the sum exact: three overlapping entries such as
``0.0.0.0/1,0.0.0.0/2,64.0.0.0/2`` sum to 2**31 + 2**30 + 2**30 ==
2**32 uncollapsed -- a false "whole family" positive for a
configuration that in fact only restricts to ``0.0.0.0/1`` -- while
``collapse_addresses`` merges the three into that single
``0.0.0.0/1`` (2**31 addresses, correctly short of 2**32).
"""
if not blocks:
return False
if any(network.prefixlen == 0 for network in blocks):
return True
merged = sum(
network.num_addresses
for network in ipaddress.collapse_addresses(blocks)
)
return merged >= 1 << bits
def _allow_list_covers_whole_families(trusted) -> tuple[str, ...]:
"""Address families ``trusted`` trusts in their entirety.
Returns the canonical CIDR spelling of each fully covered family
(``0.0.0.0/0`` / ``::/0``), or an empty tuple.
IPv4 is also fully covered by any IPv6 entry (or entries) that
together span ``::ffff:0.0.0.0/96``: on a listener that accepts
IPv6 every IPv4 peer arrives IPv4-mapped, so ``::ffff:0.0.0.0/96``
trusts all of them (verified against the real middleware:
``peer="::ffff:8.8.8.8"`` has its forged X-Forwarded-For honoured)
while contributing only 2**32 of the 2**128 addresses the IPv6 sum
needs. Checked against ``collapse_addresses(v6_blocks)`` rather
than each entry alone, so a split pair such as
``::ffff:0.0.0.0/97,::ffff:128.0.0.0/97`` -- two entries, neither
of which alone contains the mapped range -- is recognised exactly
like the single ``::ffff:0.0.0.0/96`` it merges into. Reported
under the covering range's own canonical spelling (``::ffff:0.0.0.0/96``),
not the literal entry the operator wrote -- ``::/1``, ``::/80`` and the
``::ffff:0.0.0.0/97,::ffff:128.0.0.0/97`` split pair are all reported as
``::ffff:0.0.0.0/96`` -- and only when ``::/0`` is not already being
reported. The RuntimeError this feeds
(``_describe_unrestricted_allow_list`` /
``_ALLOW_LIST_UNRESTRICTED_REASON``) separately names the raw configured
``LDR_SERVER_FORWARDED_ALLOW_IPS`` value verbatim, so an operator sees
both: which range made the list unrestricted, and exactly what they set.
The NAT64 (``64:ff9b::/96``, ``64:ff9b:1::/48``) and Teredo
(``2001::/32``) ranges are not counted the same way, although each is
as broad as ``0.0.0.0/0`` on its own path: a peer arrives inside one
only through a translator or relay in front of LDR, which this module
cannot see, never merely by connecting over IPv4. An entry covering
one therefore gets the broad-entry warning from
``_warn_about_imprecise_allow_list_entries``, not the startup refusal.
"""
v4_blocks = _family_blocks(trusted, 4)
v6_blocks = _family_blocks(trusted, 6)
v6_covered = _family_is_fully_covered(v6_blocks, 128)
covered: list[str] = []
if _family_is_fully_covered(v4_blocks, 32):
covered.append("0.0.0.0/0")
elif not v6_covered and any(
_IPV4_MAPPED_RANGE.subnet_of(network)
for network in ipaddress.collapse_addresses(v6_blocks)
):
covered.append("::ffff:0.0.0.0/96")
if v6_covered:
covered.append("::/0")
return tuple(covered)
def _describe_unrestricted_allow_list(allow_ips: str) -> str | None:
"""Why ``allow_ips`` lets uvicorn trust an entire address family.
Returns a human-readable reason, or ``None`` when the allow-list
genuinely restricts which direct peers uvicorn honours forwarded
headers from. This is the SEMANTIC replacement for comparing the
configured value against the literal ``"*"``: uvicorn's
``_TrustedHosts.get_trusted_client_address`` returns the LEFT-most
(client-forgeable) X-Forwarded-For entry whenever ``always_trust`` is
set, and falls through to the same left-most entry when every entry
in the chain is trusted -- so ``0.0.0.0/0``, ``::/0``,
``0.0.0.0/0,::/0`` and ``0.0.0.0/1,128.0.0.0/1`` are all exactly as
exploitable as ``"*"`` while spelling nothing like it.
``::/0`` alone is reported as unrestricted even though uvicorn keeps
the raw peer for an IPv4 connection (an ``IPv4Address`` is never
``in`` an IPv6 network). LDR's own listener is NOT dual-stack by
default: ``web/server_config.py``'s ``_DEFAULTS`` sets
``"host": "127.0.0.1"``, and binding wider is an explicit opt-in via
``LDR_WEB_HOST=0.0.0.0`` (which the Dockerfile sets) -- both IPv4
only. The classification deliberately does not depend on that:
wherever the listener DOES accept IPv6 (``LDR_WEB_HOST=::``, an
external ASGI server, a v6-capable ingress), ``::/0`` trusts every
peer arriving over it, and an operator who wrote it meant
"everything". Fail closed rather than ship a trust boundary whose
safety depends on the bind address.
When uvicorn's private class cannot be imported or cannot be used,
the same questions are answered from ``_LocalTrustedHosts`` -- LDR's
own parse of the same entries -- rather than by declaring every
allow-list unrestricted, which fails OPEN in the
``TRUST_PROXY_HEADERS=false`` modes (``_ALLOW_LIST_IS_RESTRICTED``
would go silently False and private peers would regain forwarding
trust) and refuses to boot the recommended configuration.
"""
if _UvicornTrustedHosts is not None:
try:
return _classify_allow_list(_UvicornTrustedHosts(allow_ips))
except Exception as exc:
_warn_uvicorn_parser_unavailable(
f"{type(exc).__name__} while classifying {allow_ips!r}: {exc}"
)
return _classify_allow_list(_LocalTrustedHosts(allow_ips))
def _classify_allow_list(trusted) -> str | None:
"""Why ``trusted`` trusts an entire address family, or ``None``."""
if trusted.always_trust:
return (
'"*" -- uvicorn\'s _TrustedHosts.always_trust shortcut, which '
"trusts every direct peer"
)
covered = _allow_list_covers_whole_families(trusted)
if covered:
return (
"entries covering an entire address family ("
+ ", ".join(covered)
+ '), which uvicorn trusts exactly as broadly as "*"'
)
return None
# Whether the resolved allow-list restricts ANYTHING at all, decided once
# and reused by both consumers (the startup guard below and
# `_ALLOW_LIST_IS_RESTRICTED`) so the two can never disagree about the
# same configuration -- the defect that let `0.0.0.0/0` boot while being
# logged as "a safe combination".
_ALLOW_LIST_UNRESTRICTED_REASON = _describe_unrestricted_allow_list(
_FORWARDED_ALLOW_IPS
)
_ALLOW_LIST_IS_UNRESTRICTED = _ALLOW_LIST_UNRESTRICTED_REASON is not None
# Restricted mode: the operator has EXPLICITLY configured
# LDR_SERVER_FORWARDED_ALLOW_IPS to something that really does restrict
# which direct peers uvicorn trusts. In this mode the allow-list is the
# ONLY source of application-level forwarding trust -- a merely-private
# direct peer outside the list must NOT get to choose its rate-limit
# bucket via X-Real-IP or a crafted X-Forwarded-For, exactly as
# docs/deployment/reverse-proxy.md promises for the restricted
# combination ("an attacker connecting directly to LDR keeps their real
# raw socket peer"). An explicit "*" -- or any spelling of it, see
# `_describe_unrestricted_allow_list` -- is treated as unrestricted (it
# only reaches request handling when TRUST_PROXY_HEADERS is false, since
# the startup guard below already refuses TRUST_PROXY_HEADERS=true plus
# an explicit unrestricted list), preserving the documented
# private/loopback heuristic for legacy configurations.
_ALLOW_LIST_IS_RESTRICTED = (
_FORWARDED_ALLOW_IPS_EXPLICIT and not _ALLOW_LIST_IS_UNRESTRICTED
)
# Comma-separated entries of the allow-list above (same shapes
# uvicorn's --forwarded-allow-ips accepts: exact IPs, CIDR networks,
# and non-IP literals). Parsed once per distinct tuple and cached by
# _parsed_forwarded_allow_entries, which _peer_in_forwarded_allow_list
# consults on every lookup.
_FORWARDED_ALLOW_ENTRIES: tuple[str, ...] = (
_FORWARDED_ALLOW_ENTRIES_CONFIGURED if _ALLOW_LIST_IS_RESTRICTED else ()
)
#: Longest prefix that is still "a few addresses" rather than "a network".
#: An entry broader than this trusts peers the operator never enumerated:
#: on a Kubernetes pod CIDR or a Docker bridge subnet, every co-tenant
#: workload can pick its own rate-limit bucket. uvicorn's semantics, not a
#: defect -- but the startup line used to call it "a safe combination".
_BROAD_PREFIX_LIMIT = {4: 24, 6: 64}
def _warn_about_imprecise_allow_list_entries(entries: tuple[str, ...]) -> None:
"""Warn per entry that is not one proxy's exact address.
Two shapes, both legal for uvicorn and both silently wrong for most
deployments. Neither is a hard failure: a pod CIDR is sometimes the
only stable identifier an operator has, and a hostname entry is
merely useless rather than dangerous.
* A NETWORK broader than /24 (v4) or /64 (v6). uvicorn trusts every
peer inside it, so any co-tenant of that network can send a
forged X-Forwarded-For and choose its own bucket. An IPv6 entry
that is itself a subnet of ``::ffff:0.0.0.0/96`` is really an
IPv4-mapped IPv4 network in disguise (``::ffff:10.0.0.0/104`` is
``10.0.0.0/8`` written for a dual-stack listener), so it is held
to the /24 IPv4 limit against its mapped prefix length rather
than the /64 IPv6 one -- otherwise a mapped ``/8``-equivalent
entry would stay silent for being "only" a /104. The NAT64
well-known prefix ``64:ff9b::/96`` and the RFC 8215 local-use
prefix ``64:ff9b:1::/48`` embed IPv4 the same way and are judged
the same way, as is Teredo ``2001::/32``, whose low 32 bits are
each client's (obfuscated) IPv4 address, so a Teredo ``/64`` is
one relay server with every client behind it. The test is OVERLAP, not containment: an entry that
contains one of these ranges (``64:ff9b::/64``, ``64:ff9b::/95``)
trusts every IPv4 host embedded in it -- as broad as ``0.0.0.0/0``
on that path -- yet its raw prefix length is not below the /64
IPv6 limit, so it would otherwise stay silent. Its effective IPv4
prefix length comes out negative and it warns. The embedded IPv4
is assumed to occupy the last 32 bits (the well-known prefix's
fixed layout, and the usual /96 local-use layout); under a shorter
RFC 6052 local-use layout this can only over-warn, never stay
silent about an entry that spans a whole translator prefix.
* An entry uvicorn parses as an opaque LITERAL -- a hostname
(``nginx``), a network with host bits set (``10.0.0.5/8``), a
stray ``*`` in a multi-entry list. ``_TrustedHosts`` compares
those verbatim against the peer string, which for a TCP
connection is always an IP address, so they match no peer: the
proxy itself is untrusted and every client behind it collapses
into one bucket (availability, not spoofing).
"""
for entry in dict.fromkeys(entries):
if "/" in entry:
try:
network = ipaddress.ip_network(entry)
except ValueError:
pass
else:
if network.version == 6 and any(
_strip_v6_scope(network).overlaps(embedded)
for embedded in _IPV4_EMBEDDING_RANGES
):
# A mapped, NAT64-translated or Teredo IPv4 network,
# or an entry spanning one of those ranges: judge it by
# the IPv4 limit against the prefix length within the
# low 32 bits, not the raw (IPv6-scaled) prefix length. An
# entry containing a whole range yields a negative
# effective length and always warns.
effective_prefixlen = network.prefixlen - 96
limit = _BROAD_PREFIX_LIMIT[4]
else:
effective_prefixlen = network.prefixlen
limit = _BROAD_PREFIX_LIMIT[network.version]
if effective_prefixlen < limit:
_log_startup(
"WARNING",
"LDR_SERVER_FORWARDED_ALLOW_IPS entry "
f"{entry!r} trusts every peer inside {network}; "
"name the proxy's exact addresses instead. "
"uvicorn honours a forwarded client address from "
"any direct peer in that range, so anything else "
"sharing the network (another container, pod or "
"LAN host) can choose its own rate-limit bucket.",
)
continue
else:
try:
ipaddress.ip_address(entry)
except ValueError:
pass
else:
continue
_log_startup(
"WARNING",
f"LDR_SERVER_FORWARDED_ALLOW_IPS entry {entry!r} is neither an "
"IP address nor a network, so uvicorn keeps it as an opaque "
"literal and it matches no peer; clients behind the proxy "
"share one bucket, making the login and registration lockouts "
"deployment-global. Use the proxy's resolved IP address(es) "
"(a hostname is never compared, a network needs its host bits "
"cleared, e.g. 10.0.0.0/8 rather than 10.0.0.5/8).",
)
if _ALLOW_LIST_IS_RESTRICTED:
_warn_about_imprecise_allow_list_entries(_FORWARDED_ALLOW_ENTRIES)
if _TRUST_PROXY_HEADERS:
# Whether this is safe hinges entirely on `_FORWARDED_ALLOW_IPS` above,
# which controls uvicorn's `--forwarded-allow-ips` (see `web/app.py`).
# `TRUST_PROXY_HEADERS=true` makes `web/app.py` launch uvicorn with
# `proxy_headers=True`, which installs uvicorn's
# ProxyHeadersMiddleware as the OUTERMOST ASGI layer -- wrapping this
# entire FastAPI app, so it runs before any code here ever sees the
# request. For a direct peer that matches `forwarded_allow_ips`, that
# middleware walks X-Forwarded-For from the RIGHT and rewrites
# `scope["client"]` to the first entry that is NOT itself one of
# `forwarded_allow_ips`'s trusted hosts (uvicorn
# `_TrustedHosts.get_trusted_client_address`) -- i.e. it skips over
# every entry it recognises as a trusted proxy's own address and
# returns the first one it doesn't. For a direct peer that does NOT
# match `forwarded_allow_ips`, `scope["client"]` is left completely
# alone: it stays the raw socket peer, exactly as if `proxy_headers`
# were off.
#
# With a RESTRICTED allow-list, `_get_client_ip` below trusts
# uvicorn's computed `request.client` when `TRUST_PROXY_HEADERS` is
# true -- it never re-parses X-Forwarded-For itself in that case,
# regardless of `LDR_SERVER_TRUSTED_PROXY_COUNT` (which only takes
# effect when `TRUST_PROXY_HEADERS` is false; see `_get_client_ip`).
# With the unrestricted "*" it keys on the RIGHT-MOST X-Forwarded-For
# entry instead (#6442), which defeats a rotating forged prefix under
# an appending proxy but not a client connecting straight to LDR, who
# writes the whole header. That is what makes the allow-list below
# the ENTIRE trust boundary here:
#
# * `forwarded_allow_ips == "*"` (the default, unset): EVERY direct
# peer matches `forwarded_allow_ips`, so the middleware ALWAYS
# takes the "always_trust" shortcut (uvicorn's own
# `_TrustedHosts.always_trust`) and returns the LEFT-most
# X-Forwarded-For entry verbatim, for ANY request carrying that
# header -- including one from an attacker connecting straight to
# LDR, and irrespective of hop count. It does not touch
# `scope["headers"]`, so it cannot be detected or undone from
# here, and the original raw socket peer is gone by the time this
# module's `_get_client_ip` runs -- there is no `request.scope`
# field left to recover it from. `_get_client_ip` therefore keys
# on the RIGHT-most X-Forwarded-For entry in this mode rather
# than on `direct_peer`, but an attacker connecting straight to
# LDR writes the whole header, right-most entry included, so they
# still choose their key outright by sending any X-Forwarded-For
# value they like. This is an unconditionally exploitable,
# genuinely poisoned configuration.
# Whether that is a hard-fail or a warning depends on whether the
# operator has actually opted into the NEW knobs this PR adds --
# see `_PROXY_TRUST_OPTED_IN` and the tri-state branch below.
# * A restricted `forwarded_allow_ips` (the reverse proxy's actual
# IP(s)): the middleware only rewrites `scope["client"]` for
# connections that arrive directly from that trusted proxy, and it
# does so by walking X-Forwarded-For for the first entry it does
# NOT recognise as one of those trusted IPs -- so `request.client`
# (== `direct_peer` in `_get_client_ip`) is the client address the
# listed proxies reported once uvicorn has run (correct as long as
# no client connects from a listed address itself), for both
# cases: an attacker connecting straight to LDR (bypassing the
# proxy) is not in the allow-list, so their raw socket peer is
# preserved end to end; a request that actually came through the
# trusted proxy chain gets uvicorn's own computed answer.
# `_get_client_ip` below takes advantage of this: whenever
# `TRUST_PROXY_HEADERS` is true it does NOT hop-count
# X-Forwarded-For itself at all -- it keys on `direct_peer`
# (validated as an address), because uvicorn has already done the
# (more precise, IP-membership-based rather than blind
# position-based) equivalent computation as the outermost ASGI
# layer. This is the RECOMMENDED way to get both
# HTTPS-behind-proxy detection (which only TRUST_PROXY_HEADERS
# provides -- see Secure cookies / HSTS / WebSocket same-origin
# checks) and correct client-IP resolution at once — permitted,
# not hard-failed. IMPORTANT: because uvicorn trusts by IP
# membership rather than counting hops,
# `LDR_SERVER_FORWARDED_ALLOW_IPS` must list EVERY trusted proxy
# hop in a multi-hop chain whose inner hops append (see "Proxy
# chains" in docs/deployment/reverse-proxy.md), not just the
# nearest one -- otherwise
# uvicorn's walk stops one hop too early and returns an
# intermediate proxy's own address instead of the true client.
# `LDR_SERVER_TRUSTED_PROXY_COUNT`'s own hop-count value is UNUSED
# whenever TRUST_PROXY_HEADERS is true, whatever it is set to
# (including its default of 0/unset) -- it only takes effect when
# TRUST_PROXY_HEADERS is false (see `_get_client_ip`).
#
# Whether the "*" case above is a hard startup error or only a warning
# is gated by `_PROXY_TRUST_OPTED_IN`:
#
# * The operator has ADOPTED one of the two new knobs this PR adds
# (a positive LDR_SERVER_TRUSTED_PROXY_COUNT, or an explicitly-set
# LDR_SERVER_FORWARDED_ALLOW_IPS -- including one explicitly set
# to "*", a deliberate unsafe choice): they are relying on this
# PR's guarantees, so the unconditionally exploitable "*" case
# must hard-fail exactly like the module's other trust-boundary
# inputs already do (a malformed/negative
# LDR_SERVER_TRUSTED_PROXY_COUNT, see TRUSTED_PROXY_COUNT's
# raise_on_validation_error=True above) -- fail loudly rather than
# rely on an operator noticing a log line.
# * The operator has NOT touched either new knob -- i.e. this is a
# deployment that only ever set `TRUST_PROXY_HEADERS=true`, the
# original, documented opt-in that predates this PR entirely.
# Hard-failing THIS case on a plain upgrade would break every such
# deployment at import time with no config change on the
# operator's part, contradicting this PR's own changelog promise
# that "no action is required" for deployments that don't touch
# the new variables. Warn instead: `_get_client_ip` below already
# makes this configuration no worse than before this PR existed
# (see its comment), so it is safe to boot -- merely still
# spoofable exactly as far as it always was.
#
# The restricted-allow-list case (the `else` below) is not exploitable
# in the same way (an attacker cannot reach the trusted-peer code path
# at all), so it boots normally -- logged, not raised or warned.
#
# "Trusts every peer" is decided by `_ALLOW_LIST_IS_UNRESTRICTED` --
# uvicorn's OWN _TrustedHosts semantics -- not by comparing the
# configured string to the literal "*": `0.0.0.0/0`, `::/0`,
# `0.0.0.0/0,::/0` and `0.0.0.0/1,128.0.0.0/1` all trust every peer
# just as completely, and the same predicate decides
# `_ALLOW_LIST_IS_RESTRICTED` above, so the guard and the request-time
# trust gate cannot disagree about one configuration.
if _ALLOW_LIST_IS_UNRESTRICTED:
if _PROXY_TRUST_OPTED_IN:
raise RuntimeError(
"TRUST_PROXY_HEADERS=true is set together with "
"LDR_SERVER_TRUSTED_PROXY_COUNT and/or an explicit "
"LDR_SERVER_FORWARDED_ALLOW_IPS, and "
f"LDR_SERVER_FORWARDED_ALLOW_IPS={_FORWARDED_ALLOW_IPS!r} "
"leaves uvicorn trusting forwarded headers from ANY direct "
f"peer: {_ALLOW_LIST_UNRESTRICTED_REASON}. Note that "
'this is NOT only the literal "*" -- a whole-family CIDR '
"such as 0.0.0.0/0 or ::/0 (including the two written as "
"0.0.0.0/0,::/0, or split into 0.0.0.0/1,128.0.0.0/1) is "
"exactly as permissive, because uvicorn returns the "
"left-most X-Forwarded-For entry once every hop in the "
"chain is trusted. This combination is UNSAFE and LDR "
"refuses to "
"start, regardless of LDR_SERVER_TRUSTED_PROXY_COUNT "
f"(currently {_TRUSTED_PROXY_COUNT}): uvicorn's "
"--proxy-headers rewrites the request's client address "
"from the left-most (client-forgeable) X-Forwarded-For "
"entry before the rate limiter ever sees the request, "
"and with an unrestricted allow-list the rate limiter "
"keys on an X-Forwarded-For entry the direct peer wrote "
"-- which lets a client connecting straight to LDR "
"bypass per-IP rate limits by sending a short "
"X-Forwarded-For header. Fix by setting "
"LDR_SERVER_FORWARDED_ALLOW_IPS to your reverse proxy's "
"IP address(es) as LDR sees them (comma-separated; same "
"syntax as uvicorn's --forwarded-allow-ips), e.g. "
"LDR_SERVER_FORWARDED_ALLOW_IPS=10.0.0.5. With LDR in "
"Docker behind a proxy on the host, that is the Docker "
"network's gateway address (e.g. 172.17.0.1), not "
"127.0.0.1 -- this makes "
"uvicorn only rewrite the client address for connections "
"that actually came from that proxy, which is safe. If "
"LDR is started by an external ASGI server (not "
"`ldr-web` / web/app.py) that already passes its own "
"restricted --forwarded-allow-ips on the command line, "
"set LDR_SERVER_FORWARDED_ALLOW_IPS to that SAME value "
"here so this check reflects your actual deployment "
"instead of the unrelated default. See 'Combining "
"TRUST_PROXY_HEADERS with LDR_SERVER_TRUSTED_PROXY_COUNT' "
"in docs/deployment/reverse-proxy.md."
)
_log_startup(
"WARNING",
"TRUST_PROXY_HEADERS=true is set with no "
"LDR_SERVER_TRUSTED_PROXY_COUNT and no "
"LDR_SERVER_FORWARDED_ALLOW_IPS configured -- LDR will start, "
"but this is a SPOOFABLE configuration: uvicorn's "
"--proxy-headers is launched with --forwarded-allow-ips=* (the "
"default), so it trusts the left-most (client-forgeable) "
"X-Forwarded-For entry from ANY direct peer, including an "
"attacker connecting straight to LDR without going through "
"your reverse proxy at all -- letting them choose the "
"rate-limit key on every request and bypass per-IP login/"
"register lockouts. This is the SAME exposure this "
"configuration has always had; this version does not make it "
"worse. BREAKING CHANGE in this release, though: X-Real-IP is "
"now honoured only from a private, loopback or link-local "
"direct peer (before this release, from any peer). It is the "
"fallback used when a request carries no X-Forwarded-For at "
"all, as from an nginx that sets only "
"`proxy_set_header X-Real-IP $remote_addr`. So if your proxy "
"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) and sends X-Real-IP "
"without X-Forwarded-For, its X-Real-IP is now IGNORED and "
"every client behind it shares ONE rate-limit bucket -- which "
"makes the login and registration lockouts deployment-global. "
"To restore per-client keying, set "
"LDR_SERVER_FORWARDED_ALLOW_IPS to that proxy's IP address as "
"LDR sees it. That also closes the spoofing exposure above: "
"set it to your reverse proxy's IP address(es), e.g. "
"LDR_SERVER_FORWARDED_ALLOW_IPS=10.0.0.5 (with LDR in Docker "
"behind a proxy on the host, the Docker network's gateway "
"address such as 172.17.0.1, not 127.0.0.1: listing an "
"address the proxy does not connect from makes uvicorn and "
"the rate limiter ignore its headers, so every client shares "
"one rate-limit bucket). Either way a single "
"proxy must OVERWRITE X-Forwarded-For with the address it "
"observed (nginx: proxy_set_header X-Forwarded-For "
"$remote_addr), and X-Real-IP likewise, never pass a "
"client-sent value through: an unchanged client header lets "
"that client choose its key, and an appended one leaves the "
"client's own entries on the left, which uvicorn reports as "
"the client address while LDR_SERVER_FORWARDED_ALLOW_IPS is "
"unset. Behind a chain of proxies (CDN -> nginx) that setup keys "
"every client on the outer proxy's address, so all clients "
"share one rate-limit bucket, unless the inner proxy resolves "
"the real client itself (nginx realip) and overwrites with it, "
"or every inner proxy appends and every hop is listed in "
"LDR_SERVER_FORWARDED_ALLOW_IPS -- see 'Proxy chains' in "
"docs/deployment/reverse-proxy.md.",
)
else:
_log_startup(
"INFO",
"TRUST_PROXY_HEADERS=true is set, with "
f"LDR_SERVER_FORWARDED_ALLOW_IPS={_FORWARDED_ALLOW_IPS!r} "
"restricting which direct peers uvicorn trusts. This is the "
"recommended combination, and it is safe exactly to the "
"extent that every entry above is one proxy's exact address "
"-- each entry is a peer allowed to choose a client's "
"rate-limit bucket, so a network entry extends that to "
"everything inside it (warned about separately above). "
"uvicorn computes the client address "
"itself, as the outermost ASGI layer, before this module "
"ever sees the request -- so _get_client_ip trusts "
"request.client directly in this mode instead of "
"re-deriving it from X-Forwarded-For, and "
"LDR_SERVER_TRUSTED_PROXY_COUNT's own hop-count logic does "
"not run (whatever it is set to has no effect while "
"TRUST_PROXY_HEADERS is true). Behind a chain "
"of proxies whose inner hops append, "
"LDR_SERVER_FORWARDED_ALLOW_IPS must list EVERY hop, not just "
"the nearest one -- uvicorn trusts by IP membership, not hop "
"count, so an incomplete list makes it stop its walk one hop "
"too early (see 'Proxy chains' in "
"docs/deployment/reverse-proxy.md).",
)
def _is_trusted_peer(host: str) -> bool:
"""Whether to trust X-Forwarded-For from this direct peer.
Trusted peers: private/loopback IPs and Starlette TestClient's
"testclient" sentinel (lets the test suite use unique IPs to avoid
sharing rate-limit buckets across modules). 6to4, Teredo and NAT64
peers are NOT trusted, on any Python version, although Python's
``is_private`` may report them private on some versions: they are remote hosts arriving through a tunnel relay or
translator (see ``network_utils.is_private_peer_ip``).
This is the UNRESTRICTED-mode heuristic: it applies whenever no
restricted LDR_SERVER_FORWARDED_ALLOW_IPS is explicitly configured.
Restricted mode uses _peer_may_supply_forwarding_headers instead,
which consults the allow-list alone.
"""
if host == "testclient":
return True
return is_private_peer_ip(host)
@lru_cache(maxsize=8)
def _parsed_forwarded_allow_entries(entries: tuple[str, ...]) -> tuple:
"""``entries`` parsed once, not on every request.
Networks for ``/`` entries, addresses for the rest; an entry that
parses as neither is dropped here (it can only ever match verbatim,
which the caller checks for a non-IP host). Keyed on the tuple itself,
so a reassigned ``_FORWARDED_ALLOW_ENTRIES`` is re-parsed.
"""
parsed: list = []
for entry in entries:
try:
parsed.append(
ipaddress.ip_network(entry)
if "/" in entry
else ipaddress.ip_address(entry)
)
except ValueError:
continue
return tuple(parsed)
def _peer_in_forwarded_allow_list(host: str) -> bool:
"""Whether ``host`` matches the configured allow-list.
Mirrors uvicorn's ``_TrustedHosts`` membership semantics
(uvicorn/middleware/proxy_headers.py): compare by parsed IP address
so IPv6's many spellings agree, honour CIDR network entries, and
compare non-IP literals verbatim. Only meaningful when
``_ALLOW_LIST_IS_RESTRICTED`` is true.
"""
try:
ip = ipaddress.ip_address(host)
except ValueError:
return host in _FORWARDED_ALLOW_ENTRIES
for trusted in _parsed_forwarded_allow_entries(_FORWARDED_ALLOW_ENTRIES):
if isinstance(trusted, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
if ip in trusted:
return True
elif ip == trusted:
return True
return False
def _peer_may_supply_forwarding_headers(host: str) -> bool:
"""Whether this direct peer's forwarding headers may be trusted.
Application-level gate for every trust decision ``_get_client_ip``
makes on its own (the X-Real-IP fallback, hop-count mode's direct-
peer check, and the default right-most heuristic):
* Restricted mode (an explicit LDR_SERVER_FORWARDED_ALLOW_IPS other
than "*"): the allow-list is the ONLY source of forwarding
trust. Private-ness alone must not confer trust here -- a peer on
the same private network as LDR but outside the allow-list is
exactly the direct-connecting client the restricted
configuration exists to defend against, so it keeps its raw
socket peer as its rate-limit key no matter what spoofed
forwarding header it sends.
* Otherwise (allow-list unset, or explicitly "*"): the pre-existing
private/loopback heuristic (``_is_trusted_peer``) applies,
unchanged.
The "testclient" sentinel stays trusted in both modes so the test
suite can hand out unique IPs (it never appears as a real network
peer).
"""
if host == "testclient":
return True
if _ALLOW_LIST_IS_RESTRICTED:
return _peer_in_forwarded_allow_list(host)
return _is_trusted_peer(host)
def _strip_forwarded_port(entry: str) -> str:
"""Strip an observed source port from a forwarded address entry.
X-Forwarded-For entries may legitimately carry the port the
appending proxy observed (``address:port``), and uvicorn's own
forwarded parser normalises that form away before the address
reaches ``scope["client"]``. Rate-limit keys must do the same, or a
proxy that appends the observed address WITH its TCP source port
hands the same client a DIFFERENT bucket on every connection --
connection churn alone would replenish per-IP quotas.
Mirrors uvicorn's ``_parse_host_port``
(uvicorn/middleware/proxy_headers.py) exactly, including its
conservative treatment of malformed values -- anything unparseable
is returned unchanged so trust checks never silently normalise
arbitrary input:
* ``198.51.100.20:41000`` -> ``198.51.100.20`` (bare IPv4 + port)
* ``[2001:db8::1]:443`` -> ``2001:db8::1`` (bracketed IPv6 + port;
the brackets are dropped, as uvicorn drops them)
* ``2001:db8::1`` -> unchanged (bare IPv6 has multiple colons and
is never treated as host:port)
* ``198.51.100.20:notaport`` -> unchanged (uvicorn keeps the raw
value when the IPv4 port does not parse)
"""
if entry.startswith("["):
bracket_end = entry.find("]")
if bracket_end == -1:
return entry
host = entry[1:bracket_end]
remainder = entry[bracket_end + 1 :]
if not remainder:
return host
if not remainder.startswith(":"):
return entry
try:
int(remainder[1:])
except ValueError:
return host
return host
if entry.count(":") == 1:
host, port = entry.rsplit(":", 1)
try:
int(port)
except ValueError:
return entry
return host
return entry
def _select_trusted_hop(header_value: str, hop_count: int) -> str | None:
"""Return the address appended by the outermost trusted proxy.
Mirrors Werkzeug's ``ProxyFix`` hop-counting: with ``hop_count``
trusted proxies each appending their peer's address, the value the
*first* trusted proxy saw is the ``hop_count``-th entry counting from
the right (index ``-hop_count``) -- never the raw first (leftmost)
entry. That position is always attacker-controlled: a client can
prepend arbitrary fake entries to its own X-Forwarded-For header
before the first trusted hop ever sees the request. Returns ``None``
if the header has fewer entries than the configured hop count (a
missing or under-filled chain), so the caller falls back to the
direct socket peer instead of trusting a short, ambiguous list.
The selected entry is normalised through ``_strip_forwarded_port``
before being returned, so an appending proxy that includes the
observed source port (``address:port``, a form uvicorn's own
forwarded parser accepts) still keys the client by address alone --
a fresh TCP source port per connection must not mint a fresh
rate-limit bucket.
Caveat for ``hop_count >= 2`` (mirrors Werkzeug ``ProxyFix``'s
documented limitation): this only verifies that the DIRECT peer is
trusted -- it has no way to verify that every hop in between is also
unreachable directly. If an attacker can connect straight to an
INTERMEDIATE proxy in the chain (skipping the outer hops), that
proxy appends the attacker's real address at its own position, and
the attacker fully controls every entry to the left of it -- which
can include the position this function reads. Trusting a hop count
> 1 is only sound when EVERY intermediate hop, not just the
outermost one, is unreachable except from the hop immediately in
front of it (e.g. firewalled to that peer only). See "Every hop must
be unreachable directly" under "Proxy chains" in
docs/deployment/reverse-proxy.md.
"""
entries = [
entry.strip() for entry in header_value.split(",") if entry.strip()
]
if hop_count <= 0 or len(entries) < hop_count:
return None
return _strip_forwarded_port(entries[-hop_count])
def _get_forwarded_for(request: Request) -> str:
"""Join every X-Forwarded-For header line into one comma-separated value.
Starlette's ``Headers.get()`` returns only the FIRST matching header
line. A proxy chain that APPENDS its own X-Forwarded-For line rather
than editing the existing one (e.g. HAProxy's ``option forwardfor``
without ``if-none``, or the multi-hop CDN/tunnel setups this module's
docs bless) produces a request with MULTIPLE X-Forwarded-For header
lines -- the client's own forged line first, the proxy-appended line
after. ``.get()`` would silently hand hop-counting the client's fully
attacker-controlled first line and never see the proxy-appended one,
letting a forged entry become the rate-limit key even under a
trusted-hop-count >= 1. WSGI-based stacks (Werkzeug's ``ProxyFix``
included) never see this ambiguity: the WSGI spec requires the server
to comma-join repeated header lines into a single
``HTTP_X_FORWARDED_FOR`` value before handing it to the app. Do the
same here explicitly via ``getlist`` so ASGI/Starlette's per-line
header model doesn't silently drop the proxy-appended lines.
"""
return ",".join(
value
for value in request.headers.getlist("x-forwarded-for")
if value.strip()
)
_PORT_RE = re.compile(r"[0-9]{1,5}")
def _forwarded_address(entry: str) -> str | None:
"""The IP address in one forwarded-header entry, or ``None``.
Accepts a bare IPv4/IPv6 address, ``IPv4:port`` and ``[IPv6]`` or
``[IPv6]:port`` (surrounding whitespace ignored) and returns the address
without the port, so one client cannot spread its attempts across
per-port buckets. Anything else -- empty, a hostname, free text, an
IPv6 zone ID -- returns ``None`` so the caller falls back instead of
keying the limiter on (and logging) client-chosen text.
"""
candidate = entry.strip()
if candidate.startswith("["):
end = candidate.find("]")
if end == -1:
return None
host, rest = candidate[1:end], candidate[end + 1 :]
if rest and not (rest[0] == ":" and _PORT_RE.fullmatch(rest[1:])):
return None
elif candidate.count(":") == 1:
host, port = candidate.split(":")
if not _PORT_RE.fullmatch(port):
return None
else:
host = candidate
if "%" in host:
return None
try:
ipaddress.ip_address(host)
except ValueError:
return None
return host
# Shared key for a direct peer that is not an address (see _get_client_ip).
_UNPARSEABLE_PEER_KEY = "unparseable-peer"
def _get_client_ip(request: Request) -> str:
"""Get client IP, respecting X-Forwarded-For ONLY when trusted.
Without this guard, slowapi's per-IP rate limit can be bypassed by
sending X-Forwarded-For: <random> on every request.
Which trust model applies depends FIRST on ``TRUST_PROXY_HEADERS``,
independent of ``LDR_SERVER_TRUSTED_PROXY_COUNT``:
- ``TRUST_PROXY_HEADERS=true`` (the startup guard above hard-fails
this only when the operator has ALSO opted into
``LDR_SERVER_TRUSTED_PROXY_COUNT`` or an explicit
``LDR_SERVER_FORWARDED_ALLOW_IPS`` while the latter still resolves
to "*"; a legacy deployment that sets only
``TRUST_PROXY_HEADERS=true`` boots with a warning instead -- see
the guard's comment above): uvicorn's ``ProxyHeadersMiddleware``
runs as the outermost ASGI layer, before this function ever sees
the request, and already computed ``request.client``. With a
RESTRICTED ``LDR_SERVER_FORWARDED_ALLOW_IPS`` that is correct, by
walking X-Forwarded-For for the first entry that is not itself one
of the allow-list's trusted hosts, and leaving it untouched (the
raw socket peer) for a direct peer outside that allow-list. At the
default ``LDR_SERVER_FORWARDED_ALLOW_IPS="*"`` (the legacy,
warned-about case) uvicorn instead ALWAYS rewrites
``request.client`` from the left-most (spoofable)
X-Forwarded-For entry, so in that case this function re-reads the
header and keys on its RIGHT-MOST entry -- the one the nearest
proxy added (#6442) -- exactly as the ``TRUST_PROXY_HEADERS=false``
heuristic below does. With a RESTRICTED allow-list this function
does NOT re-parse X-Forwarded-For, AT ANY
``LDR_SERVER_TRUSTED_PROXY_COUNT`` VALUE (including its default of
0/unset) -- doing so would only let a direct-connecting attacker
reintroduce a forged entry uvicorn had no reason to trust. It
simply trusts ``direct_peer`` whenever an X-Forwarded-For header
was present at all (uvicorn has then already had its say, one way
or the other -- see the startup guard's comment above for its
exact algorithm). ``LDR_SERVER_TRUSTED_PROXY_COUNT``'s hop-counting
logic below never runs while this is true.
X-Real-IP fallback: uvicorn's ``ProxyHeadersMiddleware`` ONLY ever
inspects X-Forwarded-For -- it has no knowledge of X-Real-IP and
never rewrites ``scope["client"]`` because of it. So when a
request carries NO X-Forwarded-For header at all, uvicorn had
nothing to rewrite from and ``direct_peer`` is still the raw
socket peer, exactly as if ``TRUST_PROXY_HEADERS`` were false. A
reverse proxy configured with only
``proxy_set_header X-Real-IP $remote_addr`` (a common nginx
pattern) never sends X-Forwarded-For, so without a fallback here
every client behind such a proxy would collapse into ONE shared
rate-limit bucket -- turning the login/registration limits into a
global lockout switch reachable by any visitor. This function
restores that fallback, gated the way the hop-count and
heuristic branches below gate X-Forwarded-For: only when
``direct_peer`` passes ``_peer_may_supply_forwarding_headers``
-- allow-list membership when a restricted
``LDR_SERVER_FORWARDED_ALLOW_IPS`` is explicitly configured,
private/loopback (or the testclient sentinel) otherwise. With a
restricted allow-list an unlisted direct peer -- an attacker
connecting straight to LDR, including a merely-PRIVATE one --
must not be able to pick its own rate-limit bucket by sending an
arbitrary X-Real-IP; since uvicorn does not gate X-Real-IP for us
the way it gates X-Forwarded-For, this function is the ONLY thing
standing between that header and the rate limiter. With the
legacy unrestricted ``"*"`` the gate protects nothing a direct
peer could not do anyway -- that mode reads X-Forwarded-For from
every peer, so it can still choose its key with that -- and its
only effect is the deliberate breaking change recorded in
docs/deployment/reverse-proxy.md (the previous release honoured
X-Real-IP from any peer): a proxy on a public or
carrier-grade-NAT address that sends X-Real-IP without
X-Forwarded-For is keyed on its own address, ONE shared bucket,
until ``LDR_SERVER_FORWARDED_ALLOW_IPS`` is set to that proxy's
address.
- ``TRUST_PROXY_HEADERS=false`` and ``LDR_SERVER_TRUSTED_PROXY_COUNT``
> 0, AND the direct peer passes
``_peer_may_supply_forwarding_headers`` (allow-list membership
when a restricted LDR_SERVER_FORWARDED_ALLOW_IPS is explicitly
configured; private/loopback or the testclient sentinel
otherwise): LDR sees the RAW socket peer (uvicorn's
proxy-headers middleware is not installed), so the operator has
declared exactly how many trusted proxy hops sit in front of LDR
and this function does its own hop-counting: it reads the
corresponding hop's entry from the right of X-Forwarded-For --
port-stripped, never the spoofable leftmost entry. Declaring a
hop count is not,
by itself, authorisation to trust whoever is directly connected --
an untrusted direct peer still gets ``direct_peer`` back regardless
of what its X-Forwarded-For says.
- ``TRUST_PROXY_HEADERS=false`` and ``LDR_SERVER_TRUSTED_PROXY_COUNT
== 0`` (the default): the pre-existing heuristic applies instead:
trust the right-most X-Forwarded-For entry (#6442), else
X-Real-IP, when the direct peer passes the same
``_peer_may_supply_forwarding_headers`` gate (private/loopback
when no restricted allow-list is explicitly configured). Unchanged
from before the hop-count setting existed, so deployments that
never configure either setting see identical behaviour.
In every mode a forwarded value that is not an IP address (empty, a
hostname, free text), and a direct peer that is not one, keys on a
fixed fallback rather than on client-chosen text (#6442).
Note: the env var is `TRUST_PROXY_HEADERS`, not `LDR_TRUST_PROXY_HEADERS`
— matches the convention used by `web/app.py` for the uvicorn
`--proxy-headers` toggle.
"""
# NOTE: ``direct_peer`` is the TCP peer ONLY while TRUST_PROXY_HEADERS
# is off, or while it is on but the request carries no X-Forwarded-For.
# When the flag is on and the header is present, uvicorn's
# ProxyHeaders middleware has already rewritten ``request.client.host``
# (the real peer is no longer visible in the scope): with a RESTRICTED
# LDR_SERVER_FORWARDED_ALLOW_IPS to the first entry, from the right,
# that is not a listed address (or left it as the raw peer for a peer
# outside the list), but
# with the legacy unrestricted ``"*"`` to the LEFT-MOST X-Forwarded-For
# entry, which under an appending proxy is client-chosen. The
# unrestricted case therefore re-reads the header below and keys on
# its RIGHT-MOST entry (#6442).
direct_peer = request.client.host if request.client else "127.0.0.1"
# The fallback key. With the flag on, uvicorn copies the left-most entry
# into ``client.host`` without checking that it is an address, so a
# peer-sent "x, junk" would otherwise key -- and log -- free text (or a
# ``user:<name>`` string that collides with ``_user_key``'s namespace).
# Such values share one fixed bucket instead. "testclient" is Starlette's
# TestClient sentinel.
if direct_peer == "testclient" or _forwarded_address(direct_peer) == (
direct_peer
):
fallback = direct_peer
else:
fallback = _UNPARSEABLE_PEER_KEY
if _TRUST_PROXY_HEADERS:
forwarded = _get_forwarded_for(request)
if forwarded:
if _ALLOW_LIST_IS_RESTRICTED:
# RESTRICTED allow-list: uvicorn's ProxyHeadersMiddleware --
# the OUTERMOST ASGI layer, already run by the time this
# function sees the request -- is the authority on the
# client address. For a direct peer in
# LDR_SERVER_FORWARDED_ALLOW_IPS it rewrote request.client to
# the first X-Forwarded-For entry, walking from the right,
# that is not itself a listed address (the left-most entry
# when every one is listed); for a direct peer outside it (an
# attacker connecting straight to LDR) it left request.client
# as that attacker's raw socket peer, which is the correct
# answer. uvicorn verifies only that the DIRECT peer is
# listed, not what that peer wrote: a client that itself
# connects from a listed address (the proxy host, or inside
# a network entry) still chooses its key. Do NOT re-parse
# X-Forwarded-For here, at any LDR_SERVER_TRUSTED_PROXY_COUNT
# (including its default of 0/unset): re-parsing would let a
# direct-connecting attacker reintroduce a forged entry
# uvicorn had no reason to trust, and for a trusted multi-hop
# chain would disagree with uvicorn's more precise walk. See
# the startup guard's comment above for uvicorn's exact
# algorithm. ``fallback`` is ``direct_peer`` whenever that is
# an address.
return fallback
# Unrestricted allow-list (the legacy, warned-about "*" -- the
# startup guard refuses it whenever the operator opted into
# either new knob): uvicorn has already rewritten request.client
# from the LEFT-MOST, client-forgeable entry, so ``direct_peer``
# carries no information uvicorn verified. Key on the RIGHT-MOST
# entry instead -- the one added by the nearest proxy (#6442).
# The documented nginx config overwrites the header with
# $remote_addr, so it carries a single entry and left-most ==
# right-most; a proxy that APPENDS instead
# ($proxy_add_x_forwarded_for, or a separate header line) puts
# the client's own header first, so the left-most entry would
# let a rotating forged prefix mint a fresh rate-limit bucket
# per request (#5787). This does not make the configuration
# safe against a client connecting straight to LDR -- such a
# client writes the whole header -- which is what the startup
# warning is about. A trailing comma, a whitespace-only value or
# a non-address entry keys on the fallback (an empty key makes
# slowapi skip the limit, and free text would be an unbounded
# key that is also written to the 429 audit log).
return _forwarded_address(forwarded.rsplit(",", 1)[-1]) or fallback
# No X-Forwarded-For at all: uvicorn's ProxyHeadersMiddleware had
# nothing to rewrite scope["client"] from (see the `if
# x_forwarded_for_values:` guard in its source -- it never even
# looks at X-Real-IP), so `direct_peer` above is still the RAW
# socket peer, exactly as when TRUST_PROXY_HEADERS is false. A
# reverse proxy set up with only
# `proxy_set_header X-Real-IP $remote_addr` (a common nginx
# pattern) never sends X-Forwarded-For, so stopping here would
# collapse every client behind such a proxy into ONE rate-limit
# bucket -- keep main's pre-PR X-Real-IP fallback to avoid that.
# Unlike main, which trusted it unconditionally whenever
# TRUST_PROXY_HEADERS was true, gate it here with
# _peer_may_supply_forwarding_headers -- the same gate this
# module uses for X-Forwarded-For everywhere else it hop-counts
# or heuristically trusts a header: allow-list membership when a
# restricted LDR_SERVER_FORWARDED_ALLOW_IPS is explicitly
# configured, private/loopback (or the testclient sentinel)
# otherwise. uvicorn provides no equivalent guard for X-Real-IP
# -- unlike X-Forwarded-For, an untrusted direct peer's X-Real-IP
# reaches this function completely unfiltered. In restricted mode
# this check is what keeps an unlisted peer from choosing its own
# rate-limit bucket with an arbitrary X-Real-IP: the allow-list
# alone decides, and a merely-private peer outside the list keeps
# its raw socket peer as its key. With the legacy unrestricted
# "*" it protects nothing a direct peer could not do anyway (that
# mode reads X-Forwarded-For from every peer, so a direct peer
# still chooses its key with it, as the startup warning says);
# there it only stops a public or carrier-grade-NAT peer's
# X-Real-IP from counting, the breaking change documented in
# docs/deployment/reverse-proxy.md; naming that proxy in
# LDR_SERVER_FORWARDED_ALLOW_IPS undoes it.
# The value is parsed like the heuristic branch's X-Real-IP below.
if _peer_may_supply_forwarding_headers(direct_peer):
real_ip = request.headers.get("x-real-ip")
if real_ip:
return _forwarded_address(real_ip) or fallback
return fallback
if _TRUSTED_PROXY_COUNT > 0:
# TRUST_PROXY_HEADERS is false: LDR sees the raw socket peer (no
# uvicorn header rewriting happens), so a declared hop count is
# not, on its own, permission to trust X-Forwarded-For: it only
# says how many entries to count from the right IF the request
# actually arrived through that trusted proxy chain. A client
# that reaches this process directly (bypassing the proxy
# entirely -- e.g. the origin is also reachable on a secondary
# interface/port) is not behind ANY proxy hop, so nothing has
# verified its X-Forwarded-For. Without this check, such a
# client could send a crafted header with >= count entries and
# have entries[-count] (fully attacker-controlled) trusted as
# its identity, spoofing the rate-limit key. The gate is
# _peer_may_supply_forwarding_headers: allow-list membership
# when a restricted LDR_SERVER_FORWARDED_ALLOW_IPS is explicitly
# configured, private/loopback otherwise -- a merely-private
# peer outside an explicit allow-list is a direct-connecting
# client the restricted configuration exists to defend against.
if _peer_may_supply_forwarding_headers(direct_peer):
forwarded = _get_forwarded_for(request)
if forwarded:
trusted_hop = _select_trusted_hop(
forwarded, _TRUSTED_PROXY_COUNT
)
if trusted_hop is not None:
# The selected hop must still be an address: a
# non-address entry would be a free-text key that is
# also written to the 429 audit log (#6442).
return _forwarded_address(trusted_hop) or fallback
# Header missing, empty, or shorter than the configured hop
# count: do NOT fall back to an untrustworthy entry. Keep the
# direct peer (the nearest proxy's own address) as the
# identity -- this collapses clients behind that proxy into
# one bucket, which is safe (if operationally noisy) rather
# than exploitable.
return fallback
# Direct peer is untrusted: the connection did not come through
# the declared proxy chain at all, so X-Forwarded-For is
# attacker-controlled. The direct connection IS the client.
return fallback
# TRUST_PROXY_HEADERS is false and LDR_SERVER_TRUSTED_PROXY_COUNT is
# 0/unset: the pre-existing heuristic (right-most entry, #6442) --
# except that a RESTRICTED, explicitly configured allow-list overrides
# the private/loopback peer check (see
# _peer_may_supply_forwarding_headers).
if _peer_may_supply_forwarding_headers(direct_peer):
forwarded = _get_forwarded_for(request)
if forwarded:
# Take the RIGHT-MOST entry: the one added by the nearest
# (trusted) proxy. The documented nginx config overwrites the
# header with $remote_addr, so it carries a single entry and
# left-most == right-most. A proxy that APPENDS instead
# ($proxy_add_x_forwarded_for, or a separate header line) puts
# the client's own header first, so reading the left-most
# entry would let a rotating forged prefix mint a fresh
# rate-limit bucket per request (#5787). Exactly ONE proxy hop
# is supported here: behind a chain (CDN -> nginx) the
# right-most entry is the outer hop's address unless the inner
# proxy overwrites the header with the client address it
# resolved itself (nginx's realip module trusting the outer
# hop makes $remote_addr that address; without realip it is
# the outer hop's). Appending chains need
# LDR_SERVER_TRUSTED_PROXY_COUNT (above) instead -- see "Proxy
# chains" in docs/deployment/reverse-proxy.md.
rightmost = _forwarded_address(forwarded.rsplit(",", 1)[-1])
# An empty key makes slowapi skip the limit entirely, and a
# non-address entry would be an unbounded, free-text key (it is
# also written to the 429 audit log). A trailing comma, a
# whitespace-only value or garbage therefore keys on the
# fallback -- see the NOTE above for what that is.
return rightmost or fallback
real_ip = request.headers.get("x-real-ip")
if real_ip:
# Parsed like the X-Forwarded-For entry above: padding is
# stripped (main's get_client_ip stripped both, and a padded
# value must not key a DIFFERENT bucket than the same address
# unpadded), and a value that is empty after stripping or is
# not an address falls back instead of becoming the key.
# uvicorn's HTTP parsers (httptools -- the default with
# uvicorn[standard] -- and h11) both pass obs-text (0x80-0xFF)
# through, so ``"\xa0"`` or ``"\x85"`` arrive intact, and
# ``str.strip()`` reduces them to ``""``, which slowapi treats
# as "no limit". h11 also passes the control bytes 0x01, 0x1b
# and 0x7f, which httptools rejects.
return _forwarded_address(real_ip) or fallback
return fallback
# Create the limiter instance.
# Resolve the on/off flag through the canonical helper so BOTH
# LDR_DISABLE_RATE_LIMITING (canonical, what CI passes to the test
# server) and the legacy unprefixed DISABLE_RATE_LIMITING work. The
# migration briefly read only the legacy name here, which left rate
# limiting ENABLED in CI UI runs (LDR_DISABLE_RATE_LIMITING=true was
# ignored) — the 3/hour register limit then failed every UI suite that
# registered more than a couple of users per server.
_RATE_LIMITING_ENABLED = is_rate_limiting_enabled()
if not _RATE_LIMITING_ENABLED:
# Surface the disabled state at startup — a .env file copied from
# dev can silently drop brute-force protection from /auth/login in
# production, and without this log line the operator has no
# indication why lockouts stopped working.
logger.warning(
"Rate limiting is DISABLED via LDR_DISABLE_RATE_LIMITING — "
"do not use this setting in production"
)
# Storage backend for rate-limit counters.
# - Default: in-memory (per-worker bucket; resets on restart).
# - Set RATE_LIMIT_STORAGE_URI=redis://host:6379 (or memcached://, etc.)
# in any multi-worker uvicorn deployment so login-bruteforce limits
# are shared across workers and survive restarts. Without it, a
# `--workers N` deploy effectively multiplies the per-IP limit by N
# and a restart wipes the lockout state.
# - slowapi accepts any limits-library URI; see
# https://limits.readthedocs.io/en/stable/storage.html for options.
_RATE_LIMIT_STORAGE_URI = os.environ.get("RATE_LIMIT_STORAGE_URI", "").strip()
# slowapi reads the Flask-Limiter-era name itself, from the environment
# or, failing that, from a `.env` file in the working directory:
# `Limiter.__init__` builds `starlette.config.Config(".env")` when
# `os.path.isfile(".env")` and falls back to
# `get_app_config(C.STORAGE_URL, "memory://")` where `C.STORAGE_URL` is
# "RATELIMIT_STORAGE_URL" (slowapi/extension.py:49,159-163,244). That is
# the name `main` documented, so it is the only one an existing deployment
# will have set — and it keeps working here, silently, without ever
# appearing in `_limiter_kwargs`.
#
# Tracked purely so this module can tell the truth about such a
# deployment: without it, the "storage is in-memory" warning below fires
# at an operator whose Redis backend is in fact active, and the
# ConfigurationError handler below cannot redact a credential it does not
# know exists. To know what slowapi will see, this module repeats that
# one lookup — the only `.env` read anywhere in LDR, limited to this one
# key; nothing from the file is ever applied to `os.environ`.
#
# The environment half is read here at module scope (frozen at import,
# like the rest of this block) and handed to the resolver.
_LEGACY_STORAGE_URI_ENV = os.environ.get("RATELIMIT_STORAGE_URL", "")
def _resolve_legacy_storage_uri(environ_value: str) -> tuple[str, str]:
"""Return (value, source) for RATELIMIT_STORAGE_URL, as slowapi sees it.
``environ_value`` is the variable's ``os.environ`` value. slowapi
builds ``starlette.config.Config(".env")`` when
``os.path.isfile(".env")`` is true and reads this key through it, so
the value can come from a file as well as from the environment
(environment wins, as in starlette). Resolving it the same way is what
lets the construction-failure handler know a credential-bearing URI is
in play.
The existence check sits inside the ``try``: on Python 3.12
``Path.is_file`` re-raises EACCES when the working directory is not
searchable, where slowapi's ``os.path.isfile`` just returns False. Any
failure while checking for or reading the file is therefore treated
as "no .env", as slowapi treats it, and logged at DEBUG by exception
type only, so this lookup can never abort the import — not even with
rate limiting disabled. If slowapi cannot read the file either, it
fails on it when the ``Limiter`` is constructed (enabled or not; that
is slowapi's own behaviour), and the handler below reports that
failure under its real cause.
"""
value = environ_value.strip()
if value:
return value, "environment"
try:
if not (Path.cwd() / ".env").is_file():
return "", ""
value = str(
StarletteConfig(".env")("RATELIMIT_STORAGE_URL", default="")
).strip()
except Exception as exc:
# Type only: the exception text can quote the file, and the file
# can hold a credential.
logger.debug(
"Could not read RATELIMIT_STORAGE_URL from ./.env "
f"({type(exc).__name__}); treating it as unset"
)
return "", ""
if value:
return value, ".env file"
return "", ""
_LEGACY_STORAGE_URI, _LEGACY_STORAGE_SOURCE = _resolve_legacy_storage_uri(
_LEGACY_STORAGE_URI_ENV
)
# An RFC 3986 scheme followed by "://". A scheme can contain neither
# ":" nor "@", so this prefix can never carry userinfo.
_STORAGE_URI_SCHEME_RE = re.compile(r"[A-Za-z][A-Za-z0-9+.-]*://")
def _redact_storage_uri(uri: str) -> str:
"""Strip embedded credentials from a storage URI.
Used ONLY to keep credentials configured via RATE_LIMIT_STORAGE_URI
out of logs/exception text — never pass the raw value to logger or
a raised exception.
This runs precisely when the URI failed to parse, so nothing about
its shape can be trusted: a userinfo password may hold any delimiter
raw ("redis://u:p@ss@h", "redis://u:Pa#ss@h", "redis://u:a/b@h",
"redis://u:pa?ss@h"), and a query value may hold a raw "@"
("redis://h/0?password=p@ss", which RFC 3986 allows and redis-py
reads as the password). Two places can carry a credential: the
userinfo (``user:pass@``) and the query string or fragment
(``?password=...``, ``?ssl_password=...``). Let "@" be the LAST "@"
in the value and "delimiter" the FIRST "?" or "#":
* Neither "@" nor a delimiter: returned unchanged.
* "@" only: everything between the scheme's "://" and the "@" is
replaced by "***" (``redis://***@host:6379/0``). The "@" is the
last one because a password may contain "@", "/", "?" or "#", so
any earlier stopping point would emit part of it.
* A delimiter only: everything after the delimiter is replaced by
"***" (``redis://host:6379/0?***``). Without an "@" there is no
userinfo, so the text before the delimiter holds no credential.
* Both, delimiter after the "@": the userinfo and the query are both
replaced (``redis://***@host:6379?***``). No "@" follows the last
"@" and no delimiter precedes the first, so the host shown between
them can hold neither a userinfo nor a query credential.
* Both, delimiter before the "@": userinfo with a raw "?" or "#" and
a query with a raw "@" cannot be told apart, so the host cannot be
located and everything after the scheme's "://" is replaced
(``redis://***``).
When a value contains an "@" but no well-formed "scheme://" prefix,
the text before the "@" (in the ambiguous case, the whole value) is
redacted rather than guessed at (``redis:u:pw@h`` gives ``***@h``).
The result can over-redact (an "@" in a path
hides the host too), which only costs diagnostic detail; it never
emits any part of the userinfo, query or fragment. Credentials in
other places (for example a ``;password=`` path parameter) are not
recognised.
"""
# urlparse() rejects (and quotes) any netloc that NFKC-normalises to a
# delimiter, so a fullwidth "@" / "#" / "?" is a real separator
# for the code that failed. Fold to NFKC first so the redactor sees
# the same boundaries; this only runs on the failure path, where
# over-redaction costs nothing.
uri = unicodedata.normalize("NFKC", uri)
at = uri.rfind("@")
delimiters = [i for i in (uri.find("?"), uri.find("#")) if i != -1]
cut = min(delimiters) if delimiters else -1
if at == -1 and cut == -1:
return uri
scheme = _STORAGE_URI_SCHEME_RE.match(uri)
if at != -1 and cut != -1 and cut < at:
# Ambiguous boundary: redact everything after the scheme.
return f"{scheme.group(0)}***" if scheme else "***"
if at == -1:
return f"{uri[: cut + 1]}***"
prefix = scheme.group(0) if scheme and scheme.end() <= at else ""
rest = uri[at + 1 :]
if cut != -1:
rest = "{}***".format(rest[: cut - at])
return f"{prefix}***@{rest}"
def _storage_uri_scheme(uri: str) -> str:
"""Return only ``scheme://...`` for an info log line, never userinfo.
``uri.split("://", 1)[0]`` echoes the whole value, password included,
when an operator drops the "//" (``redis:user:pass@host``).
"""
scheme = _STORAGE_URI_SCHEME_RE.match(uri)
return f"{scheme.group(0)}..." if scheme else "<unrecognised scheme>"
_limiter_kwargs: dict = {
"key_func": _get_client_ip,
"enabled": _RATE_LIMITING_ENABLED,
# Parity with main's Flask-Limiter config (app_factory.py /
# security/rate_limiter.py). Without an explicit strategy, slowapi
# falls back to "fixed-window" (extension.py), which refills the
# WHOLE quota at the clock boundary — e.g. a "5 per 10 seconds" login
# limit lets 5 attempts through right before the boundary and 5 more
# right after, doubling the effective rate. "moving-window" enforces
# the limit over a true rolling window.
"strategy": "moving-window",
# NOT headers_enabled=True, despite main's Flask-Limiter passing it.
#
# Under slowapi the flag does something Flask-Limiter's equivalent did
# not: it makes `Limiter.sync_wrapper` call `_inject_headers` on the
# return value of EVERY rate-limited route, on success as well as on
# 429. When a handler returns a plain dict rather than a Response,
# slowapi looks for an injectable `response: Response` parameter on the
# endpoint, finds none, and raises
# Exception: parameter `response` must be an instance of
# starlette.responses.Response
# turning ordinary 200s into 500s. Reproduced on
# POST /auth/validate-password and the unified-search endpoints; most
# routes here return dicts, so the blast radius is wide.
#
# Getting the flag's real benefit — Retry-After on 429 — would mean
# adding a `response: Response` parameter to every rate-limited route.
# Instead the 429 handler in fastapi_app.py sets those headers itself,
# which is the only place main's behaviour is actually observable to a
# client. See `_rate_limit_exceeded` there.
}
if not _RATE_LIMITING_ENABLED:
# Rate limiting is OFF, so no counter is ever read or written and the
# backend is dead weight. Force the in-memory URI so slowapi resolves
# something that cannot fail.
#
# This is main's exemption, restored. `app_factory.py` gated its
# `validate_rate_limit_storage()` call on `if rate_limiting_enabled:`
# with the reason spelled out: a stale/broken RATELIMIT_STORAGE_URL
# left over from a prior deployment "must not abort startup for an
# operator who has explicitly turned rate limiting off". main got that
# for free besides — Flask-Limiter resolves storage lazily at
# init_app/first use. slowapi does not: `Limiter.__init__` calls
# `storage_from_string(...)` unconditionally, BEFORE and independent of
# `self.enabled` (slowapi/extension.py). Without this branch a
# `redis://` URI with the `redis` client absent — the realistic case,
# since `redis` is not a dependency of this project — raises
# ConfigurationError at import and the server never starts, over a
# subsystem the operator switched off.
#
# Passing storage_uri explicitly is also what stops slowapi falling
# back to `get_app_config(C.STORAGE_URL, "memory://")`, i.e. reading
# main's RATELIMIT_STORAGE_URL from the environment or a ./.env file.
_limiter_kwargs["storage_uri"] = "memory://"
if _RATE_LIMIT_STORAGE_URI or _LEGACY_STORAGE_URI:
_ignored_var = (
"RATE_LIMIT_STORAGE_URI"
if _RATE_LIMIT_STORAGE_URI
else "RATELIMIT_STORAGE_URL"
)
logger.info(
f"Rate limiting is disabled; ignoring {_ignored_var} and using "
"in-memory storage. The backend is not contacted and a broken "
"URI here will not abort startup."
)
elif _RATE_LIMIT_STORAGE_URI:
_limiter_kwargs["storage_uri"] = _RATE_LIMIT_STORAGE_URI
logger.info(
"Rate-limit storage configured: "
f"{_storage_uri_scheme(_RATE_LIMIT_STORAGE_URI)}"
)
elif _LEGACY_STORAGE_URI:
# Backend IS shared — slowapi picked the legacy name up on its own.
# Say so rather than warning about in-memory storage that isn't in use.
logger.info(
"Rate-limit storage configured via the legacy "
f"RATELIMIT_STORAGE_URL: {_storage_uri_scheme(_LEGACY_STORAGE_URI)}"
f" (read directly by slowapi from the {_LEGACY_STORAGE_SOURCE}). Prefer RATE_LIMIT_STORAGE_URI, "
"which this application manages explicitly."
)
else:
# Only reached when rate limiting is actually on; for disabled
# deployments the storage backend is irrelevant (branch above).
logger.warning(
"Rate-limit storage is in-memory (per-worker, lost on restart). "
"For multi-worker uvicorn deploys, set RATE_LIMIT_STORAGE_URI "
"(e.g. redis://localhost:6379) so brute-force limits are shared."
)
# Load rate limits from config (UI/env-configurable; see server_config).
_config = load_server_config()
# Global default for every endpoint without an explicit limit — enforced
# by SlowAPIMiddleware (registered in fastapi_app._setup_rate_limiting).
# Flask-Limiter applied the same default via Limiter(default_limits=...)
# on main; routes with their own decorator override it.
DEFAULT_RATE_LIMIT = _config.get(
"rate_limit_default", "5000 per hour;50000 per day"
)
_limiter_kwargs["default_limits"] = [DEFAULT_RATE_LIMIT]
# slowapi defaults to key_style="url", which keys each counter off the
# literal request URL. On a parameterised path that hands every distinct
# id value its own fresh bucket, so rotating one path segment resets the
# limit — measured: 8 requests to /api/chat/{cid}/send under a 5/minute
# limit, 0 refused. Flask-Limiter keyed its fallback off request.endpoint
# (the route, param-independent), so "endpoint" restores parity rather
# than inventing a new policy. Explicit `shared_limit(scope=...)` sites
# are keyed by their own scope and are unaffected either way.
_limiter_kwargs["key_style"] = "endpoint"
_limiter_init_failed = False
_limiter_init_from_uri = False
_limiter_init_cause = ""
_limiter_init_config_error = False
try:
limiter = Limiter(**_limiter_kwargs)
except Exception as _init_exc:
# ANY construction failure, not just limits' ConfigurationError.
# storage_from_string() calls urlparse() first, which raises a bare
# ValueError on a netloc containing a character that NFKC-normalises
# to one of / ? # @ : (e.g. a fullwidth U+FF03 in the password), and
# that message quotes the WHOLE netloc, password included. redis-py's
# ``.port`` ValueError on a raw "/" in the password does the same.
# Nothing here may let the original exception, its message or its
# traceback reach the log, so only redacted strings are recorded; the
# new exception is raised below, OUTSIDE this block, because raising
# inside it would keep the original reachable as ``__context__``
# (``from None`` only suppresses its display).
_limiter_init_failed = True
# Only a failure inside limits' storage_from_string() is a problem
# with the URI. Anything else (a RATELIMIT_ENABLED=yes cast error, a
# NotImplementedError for memcached with moving-window, an unreadable
# .env) is reported under its real cause.
_limiter_init_config_error = isinstance(_init_exc, ConfigurationError)
_tb = _init_exc.__traceback__
while _tb is not None:
if _tb.tb_frame.f_code.co_name == "storage_from_string":
_limiter_init_from_uri = True
_tb = _tb.tb_next
_limiter_init_cause = f"{type(_init_exc).__name__}: " + _redact_storage_uri(
redact_secrets(
str(_init_exc),
_RATE_LIMIT_STORAGE_URI,
_LEGACY_STORAGE_URI,
min_length=1,
)
)
if _limiter_init_failed:
# Whichever name or source (environment or a ./.env that slowapi
# reads) supplied the URI, the credential in it is equally sensitive.
_configured_uri = _RATE_LIMIT_STORAGE_URI or _LEGACY_STORAGE_URI
_configured_var = (
"RATE_LIMIT_STORAGE_URI"
if _RATE_LIMIT_STORAGE_URI
else "RATELIMIT_STORAGE_URL"
)
if _limiter_init_from_uri and _configured_uri:
_redacted_uri = _redact_storage_uri(_configured_uri)
if _limiter_init_config_error:
_message = (
"Rate-limit storage backend could not be initialised "
f"({_configured_var}={_redacted_uri}). Install the "
"required client package for this backend (e.g. `pip "
"install redis` for redis:// URIs) or unset "
f"{_configured_var} to use per-process in-memory limits."
)
else:
_message = (
"Rate-limit storage URI could not be parsed or "
f"initialised ({_configured_var}={_redacted_uri}). Check "
"that it is a well-formed URI (percent-encode special "
f"characters in the password) or unset {_configured_var} "
"to use per-process in-memory limits."
)
else:
_message = (
"Rate limiter could not be initialised "
f"({_limiter_init_cause}). Check the RATELIMIT_* / "
"RATE_LIMIT_* settings and any ./.env file in the working "
"directory."
)
# Deliberately NOT logged here before raising: the RuntimeError aborts
# startup and is reported once by the `@logger.catch` on web/app.py's
# main(). It must also not become logger.exception(), and it is raised
# outside the `except` so there is no __context__/__cause__ to render.
raise RuntimeError(_message)
# slowapi's Limiter.__init__ consults the Flask-era RATELIMIT_ENABLED env
# var (starlette Config) and overrides the `enabled` kwarg with its RAW
# STRING value. Re-assert the resolved flag so the canonical
# LDR_DISABLE_RATE_LIMITING contract stays authoritative over stale env.
limiter.enabled = _RATE_LIMITING_ENABLED
LOGIN_RATE_LIMIT = _config.get("rate_limit_login", "5 per 15 minutes")
REGISTRATION_RATE_LIMIT = _config.get(
"rate_limit_registration", "10 per minute;300 per hour"
)
# Use a separate config key for password-change so tightening login limits
# doesn't accidentally lock users out of their own settings.
PASSWORD_CHANGE_RATE_LIMIT = _config.get(
"rate_limit_password_change",
_config.get("rate_limit_login", "5 per 15 minutes"),
)
# Validate-password is the strength-check API the register and
# change-password forms call as the user types. It needs its own bucket
# so users typing (and re-typing) a password don't burn their login
# rate-limit quota — previously it shared LOGIN_RATE_LIMIT, so 6
# keystrokes locked out the actual login.
VALIDATE_PASSWORD_RATE_LIMIT = _config.get(
"rate_limit_validate_password", "30 per minute"
)
# /auth/integrity-check runs a full SQLite `integrity_check` scan (not the
# cheap `quick_check`), so a tight per-route bucket keeps a signed-in user
# from re-triggering the expensive path back-to-back; it otherwise falls
# through to the global default (5000/hour).
INTEGRITY_CHECK_RATE_LIMIT = _config.get(
"rate_limit_integrity_check", "10 per minute"
)
# Settings-mutation endpoints (save/update/delete/import/reset/fix).
SETTINGS_RATE_LIMIT = _config.get("rate_limit_settings", "30 per minute")
# File uploads — separate per-user and per-IP buckets so an authenticated
# user from a single IP isn't double-capped beyond either limit's intent.
UPLOAD_RATE_LIMIT_USER = _config.get(
"rate_limit_upload_user", "60 per minute;1000 per hour"
)
UPLOAD_RATE_LIMIT_IP = _config.get(
"rate_limit_upload_ip", "60 per minute;1000 per hour"
)
def _user_key(request: Request) -> str:
"""Per-authenticated-user bucket key; falls back to the client IP.
Same pattern as the chat router's per-user key: without it, users
behind a shared NAT/proxy share one bucket and can starve each other.
"""
username = (
request.session.get("username") if "session" in request.scope else None
)
return f"user:{username}" if username else _get_client_ip(request)
def async_shared_limit(
limiter_instance: Limiter,
limit_value: str,
*,
scope: str,
key_func: Callable[[Request], str],
):
"""Apply a static shared limit without blocking an async HTTP endpoint.
SlowAPI's async decorator still checks synchronous storage on the event
loop. Register with its normal decorator to preserve the route registry
and middleware exemption, then move the check and optional header lookup
to workers. Checks still run after authentication dependencies, with the
request's session and copied context variables available to the key func.
This mirrors SlowAPI's wrapper contract (including its per-request
completion flag). Keep the adapter restricted to static shared limits:
callable limits also run in middleware, before authentication/session
dependencies are available. Sync routes should use shared_limit directly.
"""
if not isinstance(limit_value, str):
raise TypeError("async_shared_limit requires a static limit string")
def decorator(func):
if not asyncio.iscoroutinefunction(func):
raise TypeError("async_shared_limit requires an async endpoint")
parameters = list(inspect.signature(func).parameters)
if "request" not in parameters:
raise TypeError("async_shared_limit requires a request parameter")
request_index = parameters.index("request")
registered = limiter_instance.shared_limit(
limit_value, scope=scope, key_func=key_func
)(func)
@wraps(registered)
async def wrapper(*args, **kwargs):
if limiter_instance.enabled:
request = kwargs.get("request")
if request is None and len(args) > request_index:
request = args[request_index]
if not isinstance(request, Request):
raise TypeError("request must be a Starlette Request")
if limiter_instance._auto_check and not getattr(
request.state, "_rate_limiting_complete", False
):
await asyncio.to_thread(
limiter_instance._check_request_limit,
request,
func,
False,
)
request.state._rate_limiting_complete = True
response = await func(*args, **kwargs)
if limiter_instance.enabled and limiter_instance._headers_enabled:
await asyncio.to_thread(
limiter_instance._inject_headers,
response
if isinstance(response, Response)
else kwargs.get("response"),
request.state.view_rate_limit,
)
return response
return wrapper
return decorator
# Shared limits ported from main's security/rate_limiter.py (Flask-Limiter
# shared_limit) — one bucket per scope across all decorated routes.
settings_limit = limiter.shared_limit(
SETTINGS_RATE_LIMIT, scope="settings", key_func=_user_key
)
upload_rate_limit_user = limiter.shared_limit(
UPLOAD_RATE_LIMIT_USER, scope="upload_user", key_func=_user_key
)
# Default key_func (per client IP) — pairs with the per-user limit above.
upload_rate_limit_ip = limiter.shared_limit(
UPLOAD_RATE_LIMIT_IP, scope="upload_ip"
)
# ---------------------------------------------------------------------------
# /api/v1 per-user rate limiting. Port of main's api_rate_limit shared limit.
#
# Each decorated route registers TWO limits, of which exactly one is live
# per request:
#
# * a STATIC default (API_RATE_LIMIT_DEFAULT per minute), enforced while
# the user runs at the default value, and
# * a DYNAMIC (callable) limit, enforced at the user's custom nonzero
# app.api_rate_limit when one is set - this is what restores main's
# per-user custom values, which an earlier revision of this module
# dropped by registering only the static limit (#5988).
#
# The static registration is load-bearing: slowapi's SlowAPIMiddleware
# exempts a route from middleware-time checking only when its name is in
# _route_limits (static limits - see _should_exempt in slowapi/middleware
# .py); _dynamic_route_limits does not count. A dynamic limit registered
# ALONE therefore puts the route under the middleware - which runs OUTSIDE
# SessionMiddleware and before route dependencies, where the per-user key
# collapses to per-IP and the middleware additionally applies the global
# default limits. With the static limit alongside it, the route stays
# middleware-exempt and BOTH limits are checked by the decorator at call
# time (in_middleware=False reads _route_limits AND _dynamic_route_limits),
# after require_api_access has cached the user's setting.
#
# The exempt_when hooks make the two mutually exclusive, so a request is
# never counted against both: value == default → static enforces; custom
# nonzero → dynamic enforces at that value; 0 → both step aside (main's
# kill switch).
#
# The hooks and _api_custom_limit_value trust the cache to hold only 0 or
# a positive int, which set_request_api_rate_limit guarantees (anything
# else is logged and replaced by the default; see its docstring for why
# that matters).
# ---------------------------------------------------------------------------
API_RATE_LIMIT_DEFAULT = 60 # requests per minute
# How much of an invalid app.api_rate_limit value the warning echoes.
_INVALID_VALUE_LOG_CHARS = 60
# Longest string still read as a number. A rate limit is a dozen digits at
# most, and the value is client-writable and re-read on every request, so
# anything longer is not scanned at all and is treated as invalid.
_NUMERIC_VALUE_MAX_CHARS = 32
# Invalid values already warned about, by their truncated echo, so a stored
# bad value warns once per process rather than on every request (repeats
# log at debug). Bounded; once full, new values keep warning every time.
_WARNED_INVALID_VALUES_MAX = 256
_warned_invalid_values: set[str] = set()
# Cached at call time by the api_v1 router's require_api_access dependency
# (which already reads the user's settings for the app.enable_api gate).
# ContextVar keeps it request-scoped under asyncio. Consumed by the
# exempt hooks and _api_custom_limit_value at the decorator's call-time
# check.
_api_rate_limit_ctx: ContextVar[int] = ContextVar(
"ldr_api_rate_limit", default=API_RATE_LIMIT_DEFAULT
)
def set_request_api_rate_limit(value: object) -> None:
"""Cache the authenticated user's app.api_rate_limit for this request.
Only 0 (the kill switch) and positive ints are cached as given. A
string of ASCII digits (surrounding whitespace allowed) is cached as
the int it spells: app.api_rate_limit has no defaults-JSON entry, so
SettingsManager.get_setting returns an env override
(LDR_APP_API_RATE_LIMIT=120) unconverted, and that override or a
text-typed settings row reaches here as the raw string "120", which
main's Flask code honoured ("0" therefore disables the limit just as
0 does). ASCII only: str.isdecimal() also passes other scripts'
digits, which int() converts, but a number typed into an env var or
a text column is [0-9], so anything else is refused rather than
quietly honoured. A string longer than _NUMERIC_VALUE_MAX_CHARS is
not scanned or converted at all: no rate limit is that long, and
this runs on every request. The default is cached in place of
anything else, with a warning naming the value, because the settings
layer checks a value only against its declared type, if any, so a
negative, fractional, boolean or non-numeric value can arrive here.
Cached as-is, such a value would count as "custom": the static limit
steps aside, and slowapi, unable to parse the limit string built from
it, logs an error and skips the dynamic limit - leaving the user with
no limit at all. A stored invalid value comes back on every request,
so the warning is emitted once per distinct value per process
(bounded, see _WARNED_INVALID_VALUES_MAX) and repeats are logged at
debug.
"""
if isinstance(value, str) and len(value) <= _NUMERIC_VALUE_MAX_CHARS:
stripped = value.strip()
if stripped.isascii() and stripped.isdigit():
value = int(stripped)
if isinstance(value, bool) or not isinstance(value, int) or value < 0:
# The app.* namespace is client-writable, so the echoed value is
# capped rather than logged whole, and a string is cut BEFORE repr
# so a multi-MB value is never copied whole just to echo its head.
if isinstance(value, str):
shown = repr(value[:_INVALID_VALUE_LOG_CHARS])
else:
shown = repr(value)
if len(shown) > _INVALID_VALUE_LOG_CHARS:
shown = shown[:_INVALID_VALUE_LOG_CHARS] + "..."
message = (
f"Ignoring invalid app.api_rate_limit {shown}: expected 0 "
"(disabled) or a positive integer; applying the default of "
f"{API_RATE_LIMIT_DEFAULT} per minute"
)
if shown in _warned_invalid_values:
logger.debug(message)
else:
if len(_warned_invalid_values) < _WARNED_INVALID_VALUES_MAX:
_warned_invalid_values.add(shown)
logger.warning(message)
value = API_RATE_LIMIT_DEFAULT
_api_rate_limit_ctx.set(value)
def _reset_invalid_value_warnings() -> None:
"""Forget which invalid values have been warned about (for tests)."""
_warned_invalid_values.clear()
def _api_user_key(request: Request) -> str:
username = (
request.session.get("username") if "session" in request.scope else None
)
# Separate prefixes for the two branches, as ``_user_key`` does: with one
# shared ``api_user:`` prefix a username shaped like an address was the
# same bucket as an anonymous caller from that address.
if username:
return f"api_user:{username}"
return f"api_ip:{_get_client_ip(request)}"
def _api_exempt() -> bool:
"""app.api_rate_limit = 0 disables the limit (parity with main).
Kill switch only - deliberately unaware of custom values, because the
log-export limiter (routers/research.py) reuses it: a custom API rate
must not turn off the 10/min export cap.
"""
return not _api_rate_limit_ctx.get()
def _api_custom_limit_active() -> bool:
"""Whether the cached app.api_rate_limit is a custom nonzero value."""
value = _api_rate_limit_ctx.get()
return bool(value) and value != API_RATE_LIMIT_DEFAULT
def _api_default_exempt() -> bool:
"""Skip the static default limit when the user disabled the limit
(0, via _api_exempt) or set a custom value (the dynamic limit below
enforces that instead)."""
return _api_exempt() or _api_custom_limit_active()
def _api_custom_exempt() -> bool:
"""Skip the dynamic limit unless a custom nonzero value is active."""
return not _api_custom_limit_active()
def _api_custom_limit_value() -> str:
"""The user's custom limit in slowapi string form.
Returns the default string while the custom limit is inactive:
slowapi parses the provider's return value on every request BEFORE
consulting exempt_when, so it must always be a valid limit string.
0 means "disabled" and is handled by the exempt path (_api_custom_exempt
skips this limit) rather than by handing slowapi a zero-amount rule, so
the value returned here is never enforced in that case.
"""
if _api_custom_limit_active():
return f"{_api_rate_limit_ctx.get()} per minute"
return f"{API_RATE_LIMIT_DEFAULT} per minute"
_api_default_rate_limit = limiter.shared_limit(
f"{API_RATE_LIMIT_DEFAULT} per minute",
scope="api_v1",
key_func=_api_user_key,
exempt_when=_api_default_exempt,
)
_api_custom_rate_limit = limiter.shared_limit(
_api_custom_limit_value,
scope="api_v1",
key_func=_api_user_key,
exempt_when=_api_custom_exempt,
)
def api_rate_limit(func: Callable[..., Any]) -> Callable[..., Any]:
"""Apply the static-default + dynamic-custom limit pair to a route.
The second decorator call is for its registration side effect only and
its wrapper is discarded: one slowapi wrapper already checks every
limit registered under the endpoint's name, and keeping a single
wrapper preserves ``route.__wrapped__`` as the authored body, which
tests unwrap through in one hop.
"""
wrapped = _api_default_rate_limit(func)
_api_custom_rate_limit(wrapped)
return wrapped