~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
"""How ``_get_client_ip`` derives the rate-limit key, and who controls it.
``web/dependencies/rate_limit.py::_get_client_ip`` is the limiter's
DEFAULT key function, so it decides the bucket for every per-IP limit in
the app — including ``@limiter.limit(LOGIN_RATE_LIMIT)`` on
``POST /auth/login`` (``web/routers/auth.py:139``). If a request header
can change that key, the login brute-force limit is not a limit.
The guard it implements is: honour ``X-Forwarded-For`` / ``X-Real-IP``
only when the DIRECT PEER is private/loopback, or when the operator set
``TRUST_PROXY_HEADERS=true``. ``tests/web/dependencies/test_rate_limit_
keys.py`` already pins that guard from every angle — a public direct
peer's forwarded headers are ignored, and rotating them does not move
the bucket. That half is correct and this file does not re-litigate it.
What this file covers is the half the guard does not reach: WHICH ENTRY
of the header is taken once the peer IS trusted.
``_get_client_ip`` joins every ``X-Forwarded-For`` header line (Starlette's
``headers.get`` would return only the first) and keys on the RIGHT-MOST
entry: the one added by the nearest proxy. In a forwarded chain the
left-most entry can be supplied by the original client; an appending proxy
puts the address it observed on the RIGHT (#5787). The shipped nginx guide
additionally overwrites both client-IP headers with ``$remote_addr``, so
the header carries a single entry; a contract below pins that guidance.
A private/LAN peer can still supply the header directly because
``_is_trusted_peer`` trusts private addresses; that documented residual is
characterized here too. Exactly one proxy hop is supported.
Scope note: the per-URL bucketing defect (slowapi's ``key_style="url"``)
is a separate, already-filed issue and is deliberately not touched here.
This file is about the KEY, not the scope.
These are pure unit tests: ``Request`` objects are built from raw ASGI
scope dicts and the limit arithmetic uses the ``limits`` library
directly. No app boot, no TestClient, no database.
"""
import ast
import re
from pathlib import Path
import pytest
from limits import parse as parse_limit
from limits.storage import MemoryStorage
from limits.strategies import STRATEGIES
from starlette.requests import Request
# Addresses kept in named constants rather than inline literals.
#
# Python's ``ipaddress`` classifies the RFC 5737 documentation ranges
# (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24) as PRIVATE, so they
# cannot play the "untrusted public peer" role — genuinely global
# addresses are used for that, matching the sibling unit-test file.
LOOPBACK_PEER = "127.0.0.1" # nginx on the same host, per the repo's own
# snippet: ``proxy_pass http://127.0.0.1:5000``
LAN_PEER = "192.168.1.50" # LAN / docker-bridge neighbour, no proxy
# A proxy on a private network that is NOT the limiter's own last-resort
# default ("127.0.0.1" when the scope has no client), so a fallback test can
# tell "keyed on the direct peer" apart from "keyed on the default".
PRIVATE_PROXY_PEER = "10.0.0.5"
PUBLIC_PEER = "8.8.8.8" # attacker connecting straight to the app
REAL_CLIENT = "93.184.216.34" # what an appending proxy observes and appends
REPO_ROOT = Path(__file__).resolve().parents[2]
REVERSE_PROXY_DOC = REPO_ROOT / "docs" / "deployment" / "reverse-proxy.md"
@pytest.fixture()
def rl():
"""The rate_limit module as currently loaded.
Resolved inside the fixture (not at import time) because sibling
test files reload this module; grabbing it per-test keeps the
functions and the module globals in sync.
"""
from local_deep_research.web.dependencies import rate_limit
return rate_limit
@pytest.fixture(autouse=True)
def _default_trust_flag(rl, monkeypatch):
"""Pin ``_TRUST_PROXY_HEADERS`` to its SHIPPED DEFAULT (off).
Every claim in this file is about the default configuration, so the
flag must not be inherited from the ambient environment or from a
sibling test that flipped it. Monkeypatching the module global (not
the env var) matches how the flag is read at call time; the env var
itself is only consulted once, at import.
"""
monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", False)
def make_request(peer=PUBLIC_PEER, headers=None, session=None, raw=None):
"""Build a minimal Starlette Request from a raw ASGI scope dict.
``raw`` is a list of ``(name, value)`` pairs appended after ``headers``
in order, so the same header name can appear on several lines -- which
a dict cannot express.
"""
pairs = list((headers or {}).items()) + list(raw or [])
scope = {
"type": "http",
"method": "POST",
"path": "/auth/login",
"query_string": b"",
"headers": [(k.lower().encode(), v.encode()) for k, v in pairs],
"client": (peer, 51234),
}
if session is not None:
scope["session"] = session
return Request(scope)
def appending_proxy_chain(forged, observed=REAL_CLIENT):
"""An X-Forwarded-For value produced by an appending proxy.
``$proxy_add_x_forwarded_for`` is defined by nginx as the client's
own ``X-Forwarded-For`` with ``$remote_addr`` appended after a
comma. So a client that sends ``X-Forwarded-For: <forged>`` from
address ``<observed>`` reaches the app as ``"<forged>, <observed>"``.
The shipped nginx guide overwrites the header instead. This helper
covers how ``_get_client_ip`` behaves when a single proxy appends.
"""
return f"{forged}, {observed}"
class TestTheDocumentedProxyOverwritesRatherThanAppends:
"""Pin the deployment mitigation introduced by #6046.
LDR keys on the right-most value, so one appending proxy is also safe
for the rate limiter; the guide still overwrites, which leaves a single
entry and gives uvicorn's left-most reader the real client address.
"""
def test_doc_exists_and_is_the_deployment_guide(self):
assert REVERSE_PROXY_DOC.is_file(), (
f"{REVERSE_PROXY_DOC} is missing; the overwrite mitigation "
"must remain part of the shipped deployment guidance"
)
def test_every_xff_directive_overwrites_with_remote_addr(self):
text = REVERSE_PROXY_DOC.read_text(encoding="utf-8")
directives = [
line.strip()
for line in text.splitlines()
if "proxy_set_header" in line and "X-Forwarded-For" in line
]
assert directives, (
"no X-Forwarded-For proxy_set_header directive found in "
f"{REVERSE_PROXY_DOC}"
)
overwriting = [
d for d in directives if d.split()[-1] == "$remote_addr;"
]
assert overwriting == directives, (
"expected every documented nginx X-Forwarded-For directive to "
"overwrite the client-supplied header with $remote_addr; "
f"got: {directives}"
)
assert all("$proxy_add_x_forwarded_for" not in d for d in directives), (
"the appending form leaves a client-chosen left-most entry, "
f"which uvicorn reads as the client address: {directives}"
)
def test_doc_states_that_ldr_keys_on_the_rightmost_entry(self):
text = re.sub(
r"\s+", " ", REVERSE_PROXY_DOC.read_text(encoding="utf-8")
).lower()
assert (
"rate limiter keys on the right-most `x-forwarded-for` entry"
in text
), (
"the deployment guide no longer describes which forwarded "
"entry LDR keys on; this file's premise needs re-checking"
)
assert "exactly one proxy hop is supported" in text
class TestForgedPrefixNeverBecomesTheKey:
"""A client-supplied prefix under a single appending proxy.
The shipped nginx guide overwrites the header; these tests pin that a
proxy which appends instead -- on the same line or as a separate
header line -- still yields the address the proxy observed (#5787).
"""
def test_control_key_is_the_observed_client_without_a_forged_header(
self, rl
):
"""CONTROL: an honest client keys on its real address.
The proxy still sends a single-entry X-Forwarded-For, so this is
the same code path as the forged-prefix cases below — the only
difference is whether the client supplied a header of its own.
"""
request = make_request(
peer=LOOPBACK_PEER,
headers={"X-Forwarded-For": REAL_CLIENT},
)
assert rl._get_client_ip(request) == REAL_CLIENT
def test_rotating_the_forged_prefix_mints_no_new_keys(self, rl):
"""FIXED behavior paired with its CONTROL, in one test.
Same peer, same real client, same route — the ONLY variable is
whether the attacker prepends a value of their own, and it must
change nothing: every attempt still lands in the one real
bucket."""
attempts = 32
control_keys = {
rl._get_client_ip(
make_request(
peer=LOOPBACK_PEER,
headers={"X-Forwarded-For": REAL_CLIENT},
)
)
for _ in range(attempts)
}
assert control_keys == {REAL_CLIENT}, (
"control: without a forged header every attempt must land in "
"one bucket"
)
forged_keys = {
rl._get_client_ip(
make_request(
peer=LOOPBACK_PEER,
headers={
"X-Forwarded-For": appending_proxy_chain(
f"203.0.113.{i}"
)
},
)
)
for i in range(attempts)
}
assert forged_keys == {REAL_CLIENT}, (
"bypass: rotating the forged prefix minted fresh rate-limit "
f"keys ({len(forged_keys)} distinct keys from {attempts} requests)"
)
def test_desired_key_is_the_address_the_trusted_proxy_observed(self, rl):
forged = "203.0.113.77"
request = make_request(
peer=LOOPBACK_PEER,
headers={"X-Forwarded-For": appending_proxy_chain(forged)},
)
assert rl._get_client_ip(request) == REAL_CLIENT
def test_proxy_appending_a_separate_header_line_is_keyed_on_its_line(
self, rl
):
"""HAProxy's ``option forwardfor`` adds its OWN X-Forwarded-For
line rather than extending the client's. Starlette's
``headers.get`` returns only the first line -- the client's -- so
every line must be considered, in order, and the last entry wins.
"""
keys = {
rl._get_client_ip(
make_request(
peer=LOOPBACK_PEER,
raw=[
("X-Forwarded-For", f"203.0.113.{i}"),
("X-Forwarded-For", REAL_CLIENT),
],
)
)
for i in range(8)
}
assert keys == {REAL_CLIENT}, (
"a client-sent X-Forwarded-For line placed before the proxy's "
f"own line chose the rate-limit key: {sorted(keys)}"
)
def test_separate_lines_each_carrying_a_chain_use_the_last_entry(self, rl):
request = make_request(
peer=LOOPBACK_PEER,
raw=[
("X-Forwarded-For", "203.0.113.1, 203.0.113.2"),
("X-Forwarded-For", f"203.0.113.3, {REAL_CLIENT}"),
],
)
assert rl._get_client_ip(request) == REAL_CLIENT
@pytest.mark.parametrize("value", ["203.0.113.9,", "203.0.113.9, ", " "])
def test_empty_rightmost_entry_falls_back_to_the_direct_peer(
self, rl, value
):
"""slowapi skips the limit when the key is falsy, so a trailing
comma or a whitespace-only value must never yield an empty key.
With ``TRUST_PROXY_HEADERS`` off (pinned by the autouse fixture) the
direct peer is the real TCP peer, so the left-most entry is not the
key either. The peer is deliberately not ``127.0.0.1``: that is
also the limiter's default, and a hard-coded fallback would pass.
"""
request = make_request(
peer=PRIVATE_PROXY_PEER, headers={"X-Forwarded-For": value}
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
class TestLoginBruteForceBudgetUnderAnAppendingProxy:
"""Login attempts under an appending proxy with a rotating prefix.
The arithmetic runs against the real ``limits`` primitives with the
app's OWN configured limit string and strategy — not a local
re-implementation of the limiter.
"""
@staticmethod
def _fresh_limiter(rl):
strategy = STRATEGIES[rl._limiter_kwargs["strategy"]]
return strategy(MemoryStorage()), parse_limit(rl.LOGIN_RATE_LIMIT)
def test_simulation_uses_the_apps_own_strategy_object(self, rl):
"""Guard on the two tests below: if the app's strategy or limit
string moves, the simulation must move with it."""
strategy, item = self._fresh_limiter(rl)
assert type(strategy) is type(rl.limiter.limiter), (
"the strategy simulated here is not the one the shipped "
f"Limiter uses ({type(rl.limiter.limiter)})"
)
assert item == parse_limit(rl.LOGIN_RATE_LIMIT)
assert item.amount >= 1
def test_control_limit_fires_for_an_unforged_client(self, rl):
"""CONTROL: the limit DOES work. Attempt ``amount + 1`` from the
same real client is refused."""
strategy, item = self._fresh_limiter(rl)
key = rl._get_client_ip(
make_request(
peer=LOOPBACK_PEER,
headers={"X-Forwarded-For": REAL_CLIENT},
)
)
allowed = [
strategy.hit(item, "auth-login", key) for _ in range(item.amount)
]
assert all(allowed), (
f"the first {item.amount} attempts should be permitted by "
f"'{rl.LOGIN_RATE_LIMIT}'"
)
assert strategy.hit(item, "auth-login", key) is False, (
f"attempt {item.amount + 1} from one client must be refused"
)
def test_forged_header_still_exhausts_the_budget(self, rl):
"""FIXED: same client, same route, ten times the quota — the
refusals arrive on schedule, because every attempt derives the
SAME key no matter what prefix the attacker rotates."""
strategy, item = self._fresh_limiter(rl)
attempts = item.amount * 10 + 5
keys = set()
refused = []
for i in range(attempts):
key = rl._get_client_ip(
make_request(
peer=LOOPBACK_PEER,
headers={
"X-Forwarded-For": appending_proxy_chain(
f"203.0.113.{i}"
)
},
)
)
keys.add(key)
if not strategy.hit(item, "auth-login", key):
refused.append(i)
assert keys == {REAL_CLIENT}, (
"every forged-prefix attempt must land in the one real "
"client's bucket"
)
assert refused, (
f"{attempts} login attempts from ONE client against a "
f"'{rl.LOGIN_RATE_LIMIT}' limit produced no refusal; the "
"per-IP login limit is bypassed by varying a header"
)
class TestPrivatePeerWithNoProxyAtAll:
"""The other route to the same key control: a private direct peer.
``_is_trusted_peer`` trusts any RFC1918 peer, so on a LAN or a shared
docker bridge every neighbour's forwarded header is honoured with no
proxy involved. ``docs/deployment/reverse-proxy.md`` already records
this residual -- "headers from a private/loopback peer are still
honoured" -- so it is deliberate, documented behaviour rather than an
oversight; it is pinned here only to contrast it with the public-peer
control, which is the case the guard handles correctly.
"""
def test_lan_neighbour_sets_its_own_key_verbatim(self, rl):
forged = "203.0.113.5"
request = make_request(
peer=LAN_PEER, headers={"X-Forwarded-For": forged}
)
assert rl._get_client_ip(request) == forged
def test_control_public_peer_cannot_move_its_key(self, rl):
"""CONTROL: the guard works where it applies. A peer that is not
private and not opted-in keys on its TCP address."""
request = make_request(
peer=PUBLIC_PEER, headers={"X-Forwarded-For": "203.0.113.5"}
)
assert rl._get_client_ip(request) == PUBLIC_PEER
class TestDerivedKeyIsValidatedAsAnAddress:
"""A forwarded entry becomes the key only if it parses as an IP address.
Before this was enforced, whatever sat in the header became the key
verbatim: unbounded key cardinality in the limiter's storage, and --
because the key is written to the 429 audit line -- a client-chosen
``field=value`` text in a security log. A non-address entry now falls
back to the direct peer, like an empty one.
"""
@pytest.mark.parametrize(
"token",
[
"not-an-address-at-all",
"1.1.1.1 user_agent=trusted-monitor endpoint=/healthz",
"x" * 1000,
"user:alice",
"1.2.3.4:http",
"1.2.3.4:",
"[2001:db8::1",
"[2001:db8::1]x",
"fe80::1%eth0",
"999.1.1.1",
],
)
def test_non_address_rightmost_entry_falls_back(self, rl, token):
request = make_request(
peer=PRIVATE_PROXY_PEER,
headers={
"X-Forwarded-For": appending_proxy_chain(
REAL_CLIENT, observed=token
)
},
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
@pytest.mark.parametrize("hop_count", [1, 2])
@pytest.mark.parametrize(
"token",
[
"user:alice",
"proxy.internal.example",
"1.1.1.1 user_agent=trusted-monitor endpoint=/healthz",
"unknown",
"1.2.3.4:http",
],
)
def test_non_address_selected_hop_falls_back(
self, rl, monkeypatch, hop_count, token
):
"""``LDR_SERVER_TRUSTED_PROXY_COUNT`` mode: the entry at the
configured hop is validated too, so a proxy that appends a
hostname, ``unknown`` or other text keys on the direct peer rather
than on that text. The token sits exactly at index ``-hop_count``;
the entries to its right are what the inner proxies appended."""
monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", hop_count)
monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
inner_hops = ["10.0.0.9"] * (hop_count - 1)
header = ", ".join([REAL_CLIENT, token, *inner_hops])
request = make_request(
peer=PRIVATE_PROXY_PEER, headers={"X-Forwarded-For": header}
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
@pytest.mark.parametrize("hop_count", [1, 2])
@pytest.mark.parametrize(
"raw_value", [b"", b" ", b"\xa0", b" \xa0 ", b",", b" , \xa0"]
)
def test_blank_forwarded_entries_at_the_hop_fall_back(
self, rl, monkeypatch, hop_count, raw_value
):
"""Hop-count mode with a header made only of empty, whitespace or
NBSP entries: nothing is selected, so the key is the direct peer,
never ``""`` (which would make slowapi skip the limit). Raw bytes
go in the scope because ``make_request`` UTF-8-encodes, which would
turn U+00A0 into two non-space bytes."""
monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", hop_count)
monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
request = Request(
{
"type": "http",
"method": "POST",
"path": "/auth/login",
"query_string": b"",
"headers": [(b"x-forwarded-for", raw_value)],
"client": (PRIVATE_PROXY_PEER, 51234),
}
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
@pytest.mark.parametrize(
"entry, expected",
[
(REAL_CLIENT, REAL_CLIENT),
(f"{REAL_CLIENT}:443", REAL_CLIENT),
("2001:db8::1", "2001:db8::1"),
("[2001:db8::1]", "2001:db8::1"),
("[2001:db8::1]:443", "2001:db8::1"),
("::1", "::1"),
("::ffff:8.8.8.8", "::ffff:8.8.8.8"),
],
)
def test_address_forms_are_accepted_and_the_port_dropped(
self, rl, entry, expected
):
"""IPv4 with a port, bare and bracketed IPv6 (with or without a
port) all key on the address alone -- so varying the port cannot
spread one client across many buckets."""
request = make_request(
peer=PRIVATE_PROXY_PEER, headers={"X-Forwarded-For": entry}
)
assert rl._get_client_ip(request) == expected
def test_rotating_non_address_tokens_mint_no_new_keys(self, rl):
keys = {
rl._get_client_ip(
make_request(
peer=PRIVATE_PROXY_PEER,
headers={"X-Forwarded-For": f"token-{i}"},
)
)
for i in range(32)
}
assert keys == {PRIVATE_PROXY_PEER}
def test_non_address_x_real_ip_falls_back(self, rl):
request = make_request(
peer=PRIVATE_PROXY_PEER,
headers={"X-Real-IP": "1.1.1.1 user_agent=trusted-monitor"},
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
def test_x_real_ip_with_port_keys_on_the_address(self, rl):
request = make_request(
peer=PRIVATE_PROXY_PEER,
headers={"X-Real-IP": f"{REAL_CLIENT}:8080"},
)
assert rl._get_client_ip(request) == REAL_CLIENT
@pytest.mark.parametrize("raw_value", [b"\xa0", b"\x85", b" \xa0 "])
def test_x_real_ip_that_strips_to_empty_falls_back(self, rl, raw_value):
"""uvicorn's HTTP parsers (httptools -- the default with
uvicorn[standard] -- and h11) both pass obs-text (0x80-0xFF) in
header values through, and Starlette decodes it as latin-1. (h11
also passes the control bytes 0x01, 0x1b and 0x7f, which httptools
rejects.)
``str.strip()`` treats U+00A0 and U+0085 as whitespace, so these
values strip to ``""`` -- a key that makes slowapi skip the limit.
The raw bytes are placed in the scope directly: ``make_request``
UTF-8-encodes, which would turn U+00A0 into two non-space bytes."""
request = Request(
{
"type": "http",
"method": "POST",
"path": "/auth/login",
"query_string": b"",
"headers": [(b"x-real-ip", raw_value)],
"client": (PRIVATE_PROXY_PEER, 51234),
}
)
assert request.headers["x-real-ip"].strip() == "", (
"premise: the raw value must strip to empty"
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
@pytest.fixture(params=["unrestricted", "restricted"])
def trusted_proxy_mode(self, request, rl, monkeypatch):
"""``TRUST_PROXY_HEADERS=true`` with no X-Forwarded-For: the
X-Real-IP fallback that keeps per-client keys behind an
X-Real-IP-only proxy. The flag-on branch has its OWN copy of the
address validation, which the flag-off tests above never reach.
Run it in both allow-list modes, with ``PRIVATE_PROXY_PEER``
entitled to supply the header in each: private in the
unrestricted case, listed in the restricted one."""
monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
restricted = request.param == "restricted"
monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", restricted)
if restricted:
monkeypatch.setattr(
rl, "_FORWARDED_ALLOW_ENTRIES", (PRIVATE_PROXY_PEER,)
)
return request.param
@pytest.mark.parametrize(
"token",
[
"1.1.1.1 user_agent=trusted-monitor",
"user:alice",
"not-an-address-at-all",
],
)
def test_non_address_x_real_ip_falls_back_under_the_flag(
self, rl, trusted_proxy_mode, token
):
request = make_request(
peer=PRIVATE_PROXY_PEER, headers={"X-Real-IP": token}
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
def test_x_real_ip_with_port_keys_on_the_address_under_the_flag(
self, rl, trusted_proxy_mode
):
request = make_request(
peer=PRIVATE_PROXY_PEER,
headers={"X-Real-IP": f"{REAL_CLIENT}:8080"},
)
assert rl._get_client_ip(request) == REAL_CLIENT
@pytest.mark.parametrize("raw_value", [b"\xa0", b"\x85", b" \xa0 "])
def test_x_real_ip_that_strips_to_empty_falls_back_under_the_flag(
self, rl, trusted_proxy_mode, raw_value
):
"""An empty key makes slowapi skip the limit; see the flag-off
version above for why these bytes reach the header verbatim."""
request = Request(
{
"type": "http",
"method": "POST",
"path": "/auth/login",
"query_string": b"",
"headers": [(b"x-real-ip", raw_value)],
"client": (PRIVATE_PROXY_PEER, 51234),
}
)
assert request.headers["x-real-ip"].strip() == "", (
"premise: the raw value must strip to empty"
)
assert rl._get_client_ip(request) == PRIVATE_PROXY_PEER
def test_valid_x_real_ip_is_honoured_under_the_flag(
self, rl, trusted_proxy_mode
):
"""CONTROL: the fallbacks above come from validation, not from
the header being ignored altogether in this branch."""
request = make_request(
peer=PRIVATE_PROXY_PEER, headers={"X-Real-IP": REAL_CLIENT}
)
assert rl._get_client_ip(request) == REAL_CLIENT
def test_unparseable_direct_peer_under_the_flag_shares_one_bucket(
self, rl, monkeypatch
):
"""With ``TRUST_PROXY_HEADERS`` on, uvicorn copies the LEFT-MOST
entry into ``client.host`` unvalidated, so the fallback itself can
be free text. Such a peer keys on one fixed value instead."""
monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
keys = {
rl._get_client_ip(
make_request(
peer=f"forged-{i} user_agent=x",
headers={"X-Forwarded-For": "junk"},
)
)
for i in range(8)
}
assert keys == {rl._UNPARSEABLE_PEER_KEY}
def test_unparseable_direct_peer_with_bad_x_real_ip_under_the_flag(
self, rl, monkeypatch
):
"""The X-Real-IP branch falls back to the same fixed value, not to
the unparseable peer text."""
monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
request = make_request(
peer="forged user_agent=x", headers={"X-Real-IP": "junk"}
)
assert rl._get_client_ip(request) == rl._UNPARSEABLE_PEER_KEY
def test_unparseable_direct_peer_without_the_flag(self, rl):
"""Flag off: a peer that is not an address is untrusted, so no
header is read -- and the final return is still the fixed value,
not the peer text."""
request = make_request(
peer="forged user_agent=x",
headers={"X-Forwarded-For": REAL_CLIENT},
)
assert rl._get_client_ip(request) == rl._UNPARSEABLE_PEER_KEY
def test_address_direct_peer_is_kept_verbatim(self, rl):
"""CONTROL for the test above: a real peer address is the key as
reported, including an IPv4-mapped IPv6 spelling."""
request = make_request(peer="::ffff:8.8.8.8")
assert rl._get_client_ip(request) == "::ffff:8.8.8.8"
def _rate_limit_exceeded_ast(rl):
"""AST of ``fastapi_app._rate_limit_exceeded`` without importing it.
The handler is a closure inside ``_setup_rate_limiting``, so it
cannot be imported and called without building an app. The source is
located relative to the already-imported ``rate_limit`` module so
this stays a static read of the shipped file.
"""
source = Path(rl.__file__).parents[1] / "fastapi_app.py"
tree = ast.parse(source.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if (
isinstance(node, ast.FunctionDef)
and node.name == "_rate_limit_exceeded"
):
return node
raise AssertionError(
f"_rate_limit_exceeded not found in {source}; the 429 handler "
"moved and these assertions no longer cover it"
)
class TestFourTwoNineResponseDoesNotLeakTheKey:
"""Does a 429 tell the client its derived key or bucket?
No — verified statically against the handler's AST. The body is two
constant strings and the only headers it sets are numeric. Worth
pinning: the handler HAS the key in scope (it logs it one line
above), so adding it to the response would be a one-word change.
"""
def test_response_body_is_constant_strings_only(self, rl):
handler = _rate_limit_exceeded_ast(rl)
bodies = [
call.args[0]
for call in ast.walk(handler)
if isinstance(call, ast.Call)
and getattr(call.func, "id", None) == "JSONResponse"
and call.args
]
assert bodies, "no JSONResponse construction found in the handler"
for body in bodies:
assert isinstance(body, ast.Dict), (
"the 429 body is no longer a literal dict; re-check that "
"it cannot carry the derived key"
)
for value in body.values:
assert isinstance(value, ast.Constant) and isinstance(
value.value, str
), (
"the 429 body interpolates a value "
f"({ast.unparse(value)}); the derived rate-limit key "
"must not become observable to the client"
)
def test_only_numeric_rate_limit_headers_are_set(self, rl):
handler = _rate_limit_exceeded_ast(rl)
allowed = {
"Retry-After",
"X-RateLimit-Limit",
"X-RateLimit-Remaining",
"X-RateLimit-Reset",
}
assigned = set()
for node in ast.walk(handler):
if not isinstance(node, ast.Assign):
continue
for target in node.targets:
if (
isinstance(target, ast.Subscript)
and isinstance(target.value, ast.Attribute)
and target.value.attr == "headers"
and isinstance(target.slice, ast.Constant)
):
assigned.add(target.slice.value)
assert assigned == allowed, (
"the 429 handler sets header(s) outside the known numeric "
f"rate-limit set: {sorted(assigned - allowed)}"
)
def test_audit_log_sanitises_and_quotes_every_field(self, rl):
"""The 429 audit line is space-delimited ``field=value`` and every
field is client-influenced (the path, the derived key, the
User-Agent). Every other logger call in the handler must log
constants only, and the audit line must interpolate exactly three
plain fields:
* ``ip`` -- one reviewed bounded wrapper around
``_get_client_ip(request)``, with ``max_length <= 64``;
* the other two -- ``json.dumps(<name>)`` with no keyword arguments
(``ensure_ascii=False`` or ``default=str`` would change what a
reader sees), where ``<name>`` is bound exactly once in the
handler, before the logger call, by ``<name> =
sanitize_for_log(..., max_length=<int <= 256>)`` (or the
redact-before-bound successor).
Both reviewed wrappers bound the value; the successor also redacts
credentials before truncation. The JSON quoting keeps a
key=value reader from finding a field boundary inside a value. The
behavioural half is ``tests/web/test_rate_limit_coverage.py::
TestRateLimitExceededHandler::
test_429_audit_line_cannot_be_given_a_forged_ip_field``."""
handler = _rate_limit_exceeded_ast(rl)
def root_name(node):
while isinstance(node, (ast.Attribute, ast.Call)):
node = node.func if isinstance(node, ast.Call) else node.value
return node.id if isinstance(node, ast.Name) else None
def constant_only(call):
return all(
isinstance(arg, ast.Constant)
for arg in [*call.args, *(kw.value for kw in call.keywords)]
)
# Every logger call except the audit line must log constants only
# (the handler's "could not attach headers" debug line); anything
# that interpolates request data has to be the one audited line.
logger_calls = [
node
for node in ast.walk(handler)
if isinstance(node, ast.Call) and root_name(node.func) == "logger"
]
interpolating = [c for c in logger_calls if not constant_only(c)]
assert len(interpolating) == 1, (
"the 429 handler must have exactly one logger call that logs "
"anything but constants (the audit line); found "
f"{[ast.unparse(c) for c in interpolating]}"
)
(warning,) = interpolating
assert ast.unparse(warning.func) == "logger.warning", ast.unparse(
warning.func
)
assert len(warning.args) == 1 and not warning.keywords
(message,) = warning.args
assert isinstance(message, ast.JoinedStr), ast.unparse(message)
fields = [
part
for part in message.values
if isinstance(part, ast.FormattedValue)
]
assert len(fields) == 3, [ast.unparse(f) for f in fields]
for field in fields:
assert field.conversion == -1 and field.format_spec is None, (
f"429 audit field {ast.unparse(field)!r} carries a "
"conversion or format spec"
)
values = [field.value for field in fields]
def max_length_at_most(call, limit):
if len(call.keywords) != 1 or call.keywords[0].arg != "max_length":
return False
cap = call.keywords[0].value
return (
isinstance(cap, ast.Constant)
and type(cap.value) is int
and 1 <= cap.value <= limit
)
def is_bounded_log_call(node, limit):
return (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id
in {"sanitize_for_log", "redact_and_bound_for_log"}
and len(node.args) == 1
and max_length_at_most(node, limit)
)
ip_expected = ast.dump(
ast.parse("_get_client_ip(request)", mode="eval").body
)
ip_fields = [
value
for value in values
if is_bounded_log_call(value, 64)
and ast.dump(value.args[0]) == ip_expected
]
assert len(ip_fields) == 1, (
"expected exactly one field of the form "
"bounded log wrapper around _get_client_ip(request), "
"max_length=<=64: "
f"{[ast.unparse(v) for v in values]}"
)
quoted_names = []
for value in values:
if value is ip_fields[0]:
continue
assert (
isinstance(value, ast.Call)
and ast.unparse(value.func) == "json.dumps"
and len(value.args) == 1
and not value.keywords
and isinstance(value.args[0], ast.Name)
), (
f"429 audit field {ast.unparse(value)!r} is not "
"json.dumps(<sanitised name>) without keyword arguments"
)
quoted_names.append(value.args[0].id)
assert len(set(quoted_names)) == 2, quoted_names
for name in quoted_names:
bindings = [
node
for node in ast.walk(handler)
if isinstance(node, ast.Name)
and node.id == name
and isinstance(node.ctx, ast.Store)
]
assert len(bindings) == 1, (
f"{name!r} must be bound exactly once in the 429 handler "
f"(found {len(bindings)} bindings, including augmented "
"assignments and walrus targets)"
)
assigns = [
node
for node in ast.walk(handler)
if isinstance(node, ast.Assign)
and len(node.targets) == 1
and node.targets[0] is bindings[0]
]
assert len(assigns) == 1, (
f"{name!r} is bound by something other than a plain "
"single-target assignment"
)
(assign,) = assigns
assert is_bounded_log_call(assign.value, 256), (
f"{name!r} is not assigned from "
"a bounded log wrapper (..., max_length=<=256): "
f"{ast.unparse(assign.value)}"
)
assert assign.lineno < warning.lineno, (
f"{name!r} is assigned after the audit line"
)
class TestPerUserKeysInheritTheSameHeaderControl:
"""``_user_key`` / ``_api_user_key``: can a user move their own key?
An AUTHENTICATED user cannot: the key is ``user:<session username>``,
and the session is server-signed, so the only way to change it is to
be a different account. The unauthenticated fallback, though, is
``_get_client_ip`` — so every per-user limit inherits whatever that
key derivation yields for anyone not logged in.
"""
def test_user_key_falls_back_to_the_observed_ip_key(self, rl):
forged = "203.0.113.88"
request = make_request(
peer=LOOPBACK_PEER,
session={},
headers={"X-Forwarded-For": appending_proxy_chain(forged)},
)
assert rl._user_key(request) == REAL_CLIENT
def test_user_prefix_keeps_a_username_out_of_the_ip_namespace(self, rl):
"""A user who registers a name shaped like an address cannot
collide with (or poison) that address's anonymous bucket —
``_user_key`` prefixes only the username branch."""
request = make_request(
peer=LOOPBACK_PEER, session={"username": LAN_PEER}
)
assert rl._user_key(request) == f"user:{LAN_PEER}"
anonymous = make_request(
peer=LOOPBACK_PEER,
session={},
headers={"X-Forwarded-For": LAN_PEER},
)
assert rl._user_key(request) != rl._user_key(anonymous)
def test_api_user_key_keeps_a_username_out_of_the_ip_namespace(self, rl):
"""``_api_user_key`` prefixes the two branches differently, so a
username shaped like an address is NOT the same key as an
anonymous caller from that address."""
named = make_request(peer=PUBLIC_PEER, session={"username": LAN_PEER})
anonymous = make_request(
peer=LOOPBACK_PEER,
session={},
headers={"X-Forwarded-For": LAN_PEER},
)
assert rl._api_user_key(named) == f"api_user:{LAN_PEER}"
assert rl._api_user_key(anonymous) == f"api_ip:{LAN_PEER}"
def test_forwarded_value_cannot_enter_the_user_namespace(self, rl):
"""``_user_key`` returns the bare IP key for anonymous callers, so
a forwarded ``user:<name>`` value would have shared that user's
bucket. Only an address can be an IP key now."""
victim = make_request(peer=LOOPBACK_PEER, session={"username": "alice"})
forged = make_request(
peer=PRIVATE_PROXY_PEER,
session={},
headers={"X-Forwarded-For": "user:alice"},
)
assert rl._user_key(victim) == "user:alice"
assert rl._user_key(forged) == PRIVATE_PROXY_PEER