Skip to content

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

49.1 KB
Raw Blame
"""Unit tests for the rate-limit key/trust functions in
``web/dependencies/rate_limit.py``.

The spoof guard is the security-critical piece: slowapi keys the login /
register brute-force limits on ``_get_client_ip``. If X-Forwarded-For were
honored from an arbitrary public peer, an attacker could rotate the header
value on every request and get a fresh rate-limit bucket each time,
nullifying the login lockout. The guard trusts forwarding headers only when
the DIRECT peer passes ``_peer_may_supply_forwarding_headers``:
membership in an explicitly configured, restricted
``LDR_SERVER_FORWARDED_ALLOW_IPS`` allow-list; otherwise (allow-list
unset or "*") the pre-existing private/loopback heuristic, plus the
Starlette TestClient sentinel and the TRUST_PROXY_HEADERS opt-in.

These are pure unit tests: minimal Starlette ``Request`` objects are built
from raw ASGI scope dicts — no server, no TestClient, no app import.

Note: 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 those
must not be used as the "untrusted public attacker" peer here — we use
genuinely global addresses (8.8.8.8, 93.184.216.34) instead.
"""

import asyncio

import pytest
from starlette.requests import Request


@pytest.fixture()
def rl():
    """The rate_limit module as currently loaded.

    Resolved inside the fixture (not at file import time) because a
    sibling test file reloads this module; grabbing it per-test keeps the
    functions and the module globals we monkeypatch in sync.
    """
    from local_deep_research.web.dependencies import rate_limit

    return rate_limit


def restrict_allow_list(rl, monkeypatch, allow_ips):
    """Put the module in restricted-allow-list mode for ``allow_ips``.

    Mirrors a deployment where ``LDR_SERVER_FORWARDED_ALLOW_IPS`` names the
    proxy: ``web/app.py`` hands the same value to uvicorn, so a test that
    stands ``peer`` in for uvicorn's computed client address describes this
    mode. With the unrestricted ``"*"`` the flag-on branch keys on the
    right-most X-Forwarded-For entry instead (#6442).
    """
    monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", True)
    monkeypatch.setattr(
        rl,
        "_FORWARDED_ALLOW_ENTRIES",
        tuple(e.strip() for e in allow_ips.split(",") if e.strip()),
    )


def make_request(peer="8.8.8.8", headers=None, session=None, no_client=False):
    """Build a minimal Starlette Request from a raw ASGI scope dict.

    ``headers`` is normally a dict (one value per header name). Pass a
    list of ``(name, value)`` pairs instead to emit MULTIPLE header lines
    for the same name -- e.g. two separate ``X-Forwarded-For`` lines, as a
    proxy that appends (rather than edits) the header produces. A dict
    can only ever represent one line per name; ASGI's ``headers`` list
    (and therefore ``Headers.getlist()``) can represent several, which is
    exactly the case the multi-line XFF tests below need to construct.
    """
    header_pairs = (
        headers.items() if isinstance(headers, dict) else (headers or [])
    )
    scope = {
        "type": "http",
        "method": "GET",
        "path": "/",
        "query_string": b"",
        "headers": [(k.lower().encode(), v.encode()) for k, v in header_pairs],
    }
    if not no_client:
        scope["client"] = (peer, 51234)
    if session is not None:
        scope["session"] = session
    return Request(scope)


class TestIsTrustedPeer:
    def test_testclient_sentinel_is_trusted(self, rl):
        assert rl._is_trusted_peer("testclient") is True

    @pytest.mark.parametrize(
        "host", ["127.0.0.1", "10.1.2.3", "172.16.0.9", "192.168.1.5"]
    )
    def test_loopback_and_rfc1918_peers_are_trusted(self, rl, host):
        assert rl._is_trusted_peer(host) is True

    @pytest.mark.parametrize("host", ["8.8.8.8", "93.184.216.34"])
    def test_public_peers_are_not_trusted(self, rl, host):
        assert rl._is_trusted_peer(host) is False

    def test_ipv6_loopback_peer_is_trusted(self, rl):
        """ASGI servers report the IPv6 loopback client as unbracketed
        '::1' — it must be trusted like 127.0.0.1."""
        assert rl._is_trusted_peer("::1") is True

    def test_ipv6_global_peer_is_not_trusted(self, rl):
        """The spoof guard must hold for IPv6 deployments too — a global
        IPv6 direct peer gets no forwarded-header trust."""
        assert rl._is_trusted_peer("2001:4860:4860::8888") is False

    def test_non_ip_peer_host_is_not_trusted(self, rl):
        """A peer host that isn't parseable as an IP (odd harness/UDS
        values) must fail closed, not default to trusted."""
        assert rl._is_trusted_peer("some-random-host") is False


#: One address per IPv6 transition range, each wrapping a PUBLIC IPv4 or
#: standing for a remote Teredo client. Python's ``is_private`` may report
#: the first three as private on some Python versions (which ranges it
#: treats as private has changed between releases); the well-known NAT64
#: one reads public. LDR excludes all four on every version, and all four
#: are pinned so the exclusion does not depend on ``ipaddress``.
TRANSITION_PEERS = [
    pytest.param("2002:808:808::1", id="6to4"),
    pytest.param("2001:0:4136:e378:8000:63bf:3fff:fdd2", id="teredo"),
    pytest.param("64:ff9b:1::808:808", id="nat64-local-use"),
    pytest.param("64:ff9b::808:808", id="nat64-well-known"),
]


class TestTransitionPeersAreNotTrusted:
    """A 6to4 / Teredo / NAT64 peer is a remote host arriving through a
    relay or translator, so in unrestricted mode it must not be treated as
    a private proxy -- otherwise it could pick its own rate-limit key with
    X-Forwarded-For or X-Real-IP."""

    @pytest.mark.parametrize("peer", TRANSITION_PEERS)
    def test_transition_peer_is_not_trusted(self, rl, peer):
        assert rl._is_trusted_peer(peer) is False

    @pytest.mark.parametrize("peer", TRANSITION_PEERS)
    def test_transition_peer_cannot_supply_forwarding_headers(
        self, rl, monkeypatch, peer
    ):
        monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
        assert rl._peer_may_supply_forwarding_headers(peer) is False

    @pytest.mark.parametrize("peer", TRANSITION_PEERS)
    @pytest.mark.parametrize("header", ["X-Forwarded-For", "X-Real-IP"])
    def test_transition_peer_cannot_choose_its_key(
        self, rl, monkeypatch, peer, header
    ):
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", False)
        monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
        request = make_request(peer=peer, headers={header: "10.0.0.99"})
        assert rl._get_client_ip(request) == peer

    def test_positive_control_private_peer_still_chooses_its_key(
        self, rl, monkeypatch
    ):
        """Without this, the tests above would also pass against a trust
        check that rejected every peer."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", False)
        monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
        request = make_request(
            peer="fd00::5", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    @pytest.mark.parametrize(
        "peer", ["2003::1", "2001:db8::1", "2001:1::1", "64:ff9b:2::1"]
    )
    def test_neighbours_of_the_transition_ranges_keep_their_classification(
        self, rl, peer
    ):
        """Range boundaries: the exclusion must not reach past the four
        transition prefixes. Each neighbour keeps whatever
        ``is_private_ip`` says about it."""
        from local_deep_research.security.network_utils import is_private_ip

        assert rl._is_trusted_peer(peer) is is_private_ip(peer)


class TestGetClientIpSpoofGuard:
    """X-Forwarded-For / X-Real-IP from an untrusted PUBLIC direct peer
    must be ignored — otherwise per-IP limits are trivially bypassed."""

    def test_xff_from_public_peer_is_ignored(self, rl):
        request = make_request(
            peer="8.8.8.8", headers={"X-Forwarded-For": "1.2.3.4"}
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_x_real_ip_from_public_peer_is_ignored(self, rl):
        request = make_request(peer="8.8.8.8", headers={"X-Real-IP": "1.2.3.4"})
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_xff_from_ipv6_global_peer_is_ignored(self, rl):
        request = make_request(
            peer="2001:4860:4860::8888",
            headers={"X-Forwarded-For": "1.2.3.4"},
        )
        assert rl._get_client_ip(request) == "2001:4860:4860::8888"

    def test_rotating_xff_from_public_peer_never_changes_key(self, rl):
        """The actual bypass attempt: a new spoofed value per request must
        keep landing in the same (direct-peer) bucket."""
        keys = {
            rl._get_client_ip(
                make_request(
                    peer="8.8.8.8",
                    headers={"X-Forwarded-For": f"10.0.0.{i}"},
                )
            )
            for i in range(5)
        }
        assert keys == {"8.8.8.8"}


class TestGetClientIpTrustedPeer:
    """Behind a private-network proxy the forwarded headers ARE the real
    client identity and must be honored."""

    def test_xff_honored_from_private_peer(self, rl):
        request = make_request(
            peer="10.0.0.5", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_xff_honored_from_loopback_peer(self, rl):
        request = make_request(
            peer="127.0.0.1", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_xff_honored_from_testclient_sentinel_peer(self, rl):
        """Lets the test suite hand out unique IPs per module so suites
        don't share rate-limit buckets."""
        request = make_request(
            peer="testclient", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_xff_chain_uses_last_entry_stripped(self, rl):
        """The right-most entry — the address the appending proxy
        observed — is the key (#5787); padding is still stripped."""
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "  93.184.216.34 , 10.0.0.1, 10.0.0.2"},
        )
        assert rl._get_client_ip(request) == "10.0.0.2"

    def test_x_real_ip_used_when_no_xff(self, rl):
        request = make_request(
            peer="10.0.0.5", headers={"X-Real-IP": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_xff_takes_precedence_over_x_real_ip(self, rl):
        request = make_request(
            peer="10.0.0.5",
            headers={
                "X-Forwarded-For": "93.184.216.34",
                "X-Real-IP": "8.8.4.4",
            },
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_xff_honored_from_ipv6_loopback_peer(self, rl):
        request = make_request(
            peer="::1", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_empty_xff_falls_through_to_x_real_ip(self, rl):
        """An XFF header that is present but empty is falsy — the lookup
        must fall through to X-Real-IP rather than return ''."""
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "", "X-Real-IP": "93.184.216.34"},
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_no_forwarding_headers_returns_direct_peer(self, rl):
        request = make_request(peer="10.0.0.5")
        assert rl._get_client_ip(request) == "10.0.0.5"

    def test_missing_client_defaults_to_loopback(self, rl):
        """ASGI scope without a "client" entry (e.g. some test harnesses)
        must not crash; it falls back to 127.0.0.1."""
        request = make_request(no_client=True)
        assert rl._get_client_ip(request) == "127.0.0.1"


class TestTrustProxyHeadersOverride:
    """TRUST_PROXY_HEADERS=true (operator opt-in for a public-facing
    reverse proxy) means uvicorn's own ``ProxyHeadersMiddleware`` --
    running as the OUTERMOST ASGI layer, before this module's code ever
    sees the request -- is the sole authority on the client address.
    ``_get_client_ip`` must NOT re-parse X-Forwarded-For itself whenever
    this flag is true, at ANY ``LDR_SERVER_TRUSTED_PROXY_COUNT`` value
    (including its default of 0/unset) -- see the module-level tri-state
    guard's comment and ``_get_client_ip``'s docstring.

    Regression coverage for the defect fixed here: the combo-mode "trust
    uvicorn's request.client, don't re-parse XFF" rule was originally
    wired ONLY inside the ``if _TRUSTED_PROXY_COUNT > 0:`` branch, so a
    deployment with ``TRUST_PROXY_HEADERS=true`` + a restricted
    ``LDR_SERVER_FORWARDED_ALLOW_IPS`` (the documented, recommended
    combination) but ``LDR_SERVER_TRUSTED_PROXY_COUNT`` left at its
    default of 0 fell through to the legacy branch below, which honored
    the left-most (client-forgeable) X-Forwarded-For entry -- exploitable
    even though uvicorn itself was already computing the correct client
    address one layer up. The fix hoists the "trust direct_peer" rule
    above the ``_TRUSTED_PROXY_COUNT`` check so it is unconditional on
    ``TRUST_PROXY_HEADERS`` alone.

    The env var is evaluated once at module import into
    ``_TRUST_PROXY_HEADERS``; ``_get_client_ip`` reads that module global
    at call time, so we flip the global (monkeypatch restores it) instead
    of reloading the module — reloads would recreate the shared limiter
    out from under concurrently-running tests.
    """

    def test_flag_trusts_direct_peer_and_ignores_xff(self, rl, monkeypatch):
        """``peer`` here stands in for whatever uvicorn's
        ProxyHeadersMiddleware already resolved ``request.client`` to (the
        real client, once uvicorn has run) -- this function must return it
        as-is and must NOT let a forged X-Forwarded-For entry override it,
        at the default LDR_SERVER_TRUSTED_PROXY_COUNT of 0. That holds for a
        RESTRICTED allow-list, the only mode in which uvicorn's answer is
        verified."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        restrict_allow_list(rl, monkeypatch, "10.0.0.5")
        request = make_request(
            peer="8.8.8.8", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_flag_with_unrestricted_allow_list_keys_on_rightmost_entry(
        self, rl, monkeypatch
    ):
        """With the unrestricted ``"*"``, uvicorn rewrote ``request.client``
        from the LEFT-most (client-forgeable) entry, so the key is the
        RIGHT-most entry -- the one the nearest proxy appended (#6442) --
        not ``direct_peer``. A rotating forged prefix under an appending
        proxy therefore mints no new buckets.

        The peers are PUBLIC addresses (``8.8.{i}.1``), as uvicorn produces
        them here: it copies the forged left-most entry into
        ``request.client``, and a forger picks public addresses. The
        right-most rule must hold without any private/loopback peer check
        -- the RFC 5737 ranges would pass that check and hide a regression
        that gates this branch on it."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
        keys = {
            rl._get_client_ip(
                make_request(
                    peer=f"8.8.{i}.1",
                    headers={"X-Forwarded-For": f"8.8.{i}.1, 93.184.216.34"},
                )
            )
            for i in range(5)
        }
        assert keys == {"93.184.216.34"}

    def test_flag_with_unrestricted_allow_list_trusts_public_peer_xff(
        self, rl, monkeypatch
    ):
        """With the unrestricted ``"*"`` a PUBLIC direct peer's single-entry
        X-Forwarded-For is the key, exactly as before the allow-list setting
        existed: under ``TRUST_PROXY_HEADERS`` the header is not gated on
        the peer being private or loopback."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        monkeypatch.setattr(rl, "_ALLOW_LIST_IS_RESTRICTED", False)
        request = make_request(
            peer="8.8.8.8", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_flag_off_keeps_spoof_guard(self, rl, monkeypatch):
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", False)
        request = make_request(
            peer="8.8.8.8", headers={"X-Forwarded-For": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "8.8.8.8"


class TestTrustProxyHeadersXRealIpFallback:
    """Regression coverage for the defect fixed here: ``_get_client_ip``'s
    ``TRUST_PROXY_HEADERS=true`` branch unconditionally trusted
    ``direct_peer`` and never looked at ``X-Real-IP`` at all, even when no
    X-Forwarded-For was present.

    uvicorn's ``ProxyHeadersMiddleware`` ONLY ever inspects
    X-Forwarded-For (see ``uvicorn/middleware/proxy_headers.py`` --
    ``scope["client"]`` is rewritten only inside
    ``if x_forwarded_for_values:``); it has no notion of X-Real-IP
    whatsoever. So a reverse proxy configured with only
    ``proxy_set_header X-Real-IP $remote_addr`` (a common nginx pattern)
    or any request that simply arrives without X-Forwarded-For leaves
    ``request.client`` as the proxy's own raw socket address -- collapsing
    every client behind that proxy into ONE shared rate-limit bucket
    unless this module restores the X-Real-IP fallback itself.

    Restoring it naively (main's pre-PR behaviour: trust X-Real-IP
    unconditionally whenever ``TRUST_PROXY_HEADERS`` is true) would reopen
    the exact spoof this PR closes, because uvicorn provides no
    equivalent gate for X-Real-IP the way it does for X-Forwarded-For --
    so the fallback must be gated by
    ``_peer_may_supply_forwarding_headers(direct_peer)``, the SAME
    allow-list-aware trust check used for every other forwarding
    decision in this module (restricted LDR_SERVER_FORWARDED_ALLOW_IPS
    membership when explicitly configured; the private/loopback
    heuristic otherwise). These tests build real Starlette
    ``Request``/``Headers`` objects via ``make_request`` (not dict mocks)
    since ``_get_forwarded_for`` depends on ``Headers.getlist``, which a
    dict does not have.
    """

    def test_real_ip_used_when_no_xff_and_peer_trusted(self, rl, monkeypatch):
        """Direct peer is a trusted proxy (private/loopback) and the
        request carries no X-Forwarded-For at all -- uvicorn had nothing
        to rewrite ``request.client`` from, so it is still the proxy's
        own raw address here. The X-Real-IP fallback must kick in."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = make_request(
            peer="10.0.0.5", headers={"X-Real-IP": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"

    def test_real_ip_ignored_when_no_xff_and_peer_untrusted(
        self, rl, monkeypatch
    ):
        """Direct peer is a public (untrusted) address -- an attacker
        connecting straight to LDR without going through any reverse
        proxy. uvicorn does not gate X-Real-IP the way it gates
        X-Forwarded-For, so this function is the ONLY thing standing
        between an attacker-supplied X-Real-IP and the rate limiter; it
        must be ignored, and the client must not get to pick its own
        rate-limit bucket."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = make_request(
            peer="8.8.8.8", headers={"X-Real-IP": "203.0.113.99"}
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_rotating_real_ip_from_untrusted_peer_never_changes_key(
        self, rl, monkeypatch
    ):
        """The actual bypass attempt: a new spoofed X-Real-IP value per
        request must keep landing in the same (direct-peer) bucket."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        keys = {
            rl._get_client_ip(
                make_request(
                    peer="8.8.8.8",
                    headers={"X-Real-IP": f"203.0.113.{i}"},
                )
            )
            for i in range(5)
        }
        assert keys == {"8.8.8.8"}

    def test_xff_present_takes_precedence_over_real_ip(self, rl, monkeypatch):
        """When X-Forwarded-For IS present, uvicorn has already had its
        say (rewriting ``request.client`` for a trusted peer, or leaving
        it alone for an untrusted one) -- either way this function must
        trust ``direct_peer`` as-is and must NOT fall through to
        X-Real-IP, even though both headers are present here. (Restricted
        allow-list: with ``"*"`` the right-most entry would be the key.)

        ``peer`` is the allow-listed proxy itself with a header uvicorn
        would have rewritten it from, so this is not a request uvicorn
        produces. The construct is deliberate: it is the case in which
        this function's answer (``direct_peer``) differs from what the
        header-reading branches would return, so it fails if the
        restricted-mode rule is moved back under
        ``_TRUSTED_PROXY_COUNT > 0`` (the legacy branch would key on
        ``203.0.113.7``) or if X-Real-IP is consulted (``8.8.4.4``)."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        restrict_allow_list(rl, monkeypatch, "10.0.0.5")
        request = make_request(
            peer="10.0.0.5",
            headers={
                "X-Forwarded-For": "203.0.113.7",
                "X-Real-IP": "8.8.4.4",
            },
        )
        assert rl._get_client_ip(request) == "10.0.0.5"

    def test_real_ip_used_when_no_xff_and_peer_is_testclient_sentinel(
        self, rl, monkeypatch
    ):
        """The testclient sentinel is trusted like a private-network peer
        -- lets the test suite hand out unique IPs via X-Real-IP too."""
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = make_request(
            peer="testclient", headers={"X-Real-IP": "93.184.216.34"}
        )
        assert rl._get_client_ip(request) == "93.184.216.34"


class TestUserKey:
    """Per-user bucket key on REAL Starlette requests.

    (tests/web/test_rate_limit_coverage.py covers the alice/IP cases with
    Mock objects; these drive the actual ``"session" in request.scope``
    guard and Request.session property against raw ASGI scopes, plus the
    missing empty-session and spoof-interaction cases.)
    """

    def test_authenticated_session_gets_user_prefixed_key(self, rl):
        request = make_request(session={"username": "bob"})
        assert rl._user_key(request) == "user:bob"

    def test_empty_session_falls_back_to_ip(self, rl):
        """SessionMiddleware installed but user not logged in."""
        request = make_request(peer="10.0.0.5", session={})
        assert rl._user_key(request) == "10.0.0.5"

    def test_no_session_scope_falls_back_to_ip(self, rl):
        """No SessionMiddleware at all: Request.session would raise
        AssertionError if _user_key touched it without the scope check."""
        request = make_request(peer="10.0.0.5")
        assert rl._user_key(request) == "10.0.0.5"

    def test_empty_username_falls_back_to_ip(self, rl):
        """A present-but-empty username is falsy — the key must not become
        'user:' (one shared bucket for every such session)."""
        request = make_request(peer="10.0.0.5", session={"username": ""})
        assert rl._user_key(request) == "10.0.0.5"

    def test_anonymous_ip_fallback_applies_spoof_guard(self, rl):
        """The IP fallback goes through _get_client_ip, so an anonymous
        public peer cannot mint per-user-sized buckets via XFF."""
        request = make_request(
            peer="8.8.8.8",
            session={},
            headers={"X-Forwarded-For": "1.2.3.4"},
        )
        assert rl._user_key(request) == "8.8.8.8"


class TestApiUserKey:
    def test_authenticated_shape(self, rl):
        request = make_request(session={"username": "carol"})
        assert rl._api_user_key(request) == "api_user:carol"

    def test_anonymous_shape_uses_ip(self, rl):
        request = make_request(peer="10.0.0.5", session={})
        assert rl._api_user_key(request) == "api_ip:10.0.0.5"

    def test_no_session_scope_uses_ip(self, rl):
        """No SessionMiddleware in the stack: the scope guard must keep
        Request.session from raising and key by IP instead."""
        request = make_request(peer="10.0.0.5")
        assert rl._api_user_key(request) == "api_ip:10.0.0.5"

    def test_api_and_user_buckets_are_namespaced_apart(self, rl):
        """The /api/v1 bucket and the settings/upload per-user bucket for
        the SAME user must be distinct keys — collapsing them would let
        API traffic burn a user's settings quota (and vice versa)."""
        request = make_request(session={"username": "carol"})
        assert rl._api_user_key(request) != rl._user_key(request)

    def test_anonymous_spoofed_xff_from_public_peer_ignored(self, rl):
        request = make_request(
            peer="8.8.8.8",
            session={},
            headers={"X-Forwarded-For": "1.2.3.4"},
        )
        assert rl._api_user_key(request) == "api_ip:8.8.8.8"

    def test_anonymous_behind_trusted_proxy_uses_forwarded_ip(self, rl):
        request = make_request(
            peer="10.0.0.5",
            session={},
            headers={"X-Forwarded-For": "93.184.216.34"},
        )
        assert rl._api_user_key(request) == "api_ip:93.184.216.34"


class TestSelectTrustedHop:
    """Pure tests for the hop-counting helper -- the core of the
    explicit-hop-count trust mode (LDR_SERVER_TRUSTED_PROXY_COUNT > 0).
    """

    def test_hop_count_zero_returns_none(self, rl):
        assert rl._select_trusted_hop("1.2.3.4, 5.6.7.8", 0) is None

    def test_hop_count_negative_returns_none(self, rl):
        assert rl._select_trusted_hop("1.2.3.4, 5.6.7.8", -1) is None

    def test_fewer_entries_than_hop_count_returns_none(self, rl):
        assert rl._select_trusted_hop("1.2.3.4", 2) is None

    def test_missing_header_returns_none(self, rl):
        assert rl._select_trusted_hop("", 1) is None

    def test_hop_count_one_picks_rightmost_entry(self, rl):
        assert (
            rl._select_trusted_hop("1.2.3.4, 5.6.7.8, 9.9.9.9", 1) == "9.9.9.9"
        )

    def test_hop_count_two_picks_second_from_right(self, rl):
        assert (
            rl._select_trusted_hop("1.2.3.4, 5.6.7.8, 9.9.9.9", 2) == "5.6.7.8"
        )

    def test_entry_count_equal_to_hop_count_picks_leftmost(self, rl):
        assert rl._select_trusted_hop("1.2.3.4, 5.6.7.8", 2) == "1.2.3.4"

    def test_extra_leftmost_forged_entries_are_ignored(self, rl):
        """The whole point of hop counting: a prefix the client prepends
        before the trusted chain ever sees the request must never be
        selectable, no matter how many fake entries are prepended."""
        assert (
            rl._select_trusted_hop("forged-1, forged-2, forged-3, 9.9.9.9", 1)
            == "9.9.9.9"
        )

    def test_whitespace_around_entries_is_stripped(self, rl):
        assert rl._select_trusted_hop("  1.2.3.4  ,  5.6.7.8  ", 1) == "5.6.7.8"


class TestGetClientIpTrustedProxyCount:
    """LDR_SERVER_TRUSTED_PROXY_COUNT > 0: explicit hop-count selection.

    Still gated on the direct peer being trusted (private/loopback, the
    testclient sentinel, or TRUST_PROXY_HEADERS=true) -- a declared count
    is not itself permission to trust whoever connects directly; see the
    _get_client_ip docstring.
    """

    def test_count_one_from_trusted_peer_picks_rightmost_entry(
        self, rl, monkeypatch
    ):
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "93.184.216.34, 10.0.0.9"},
        )
        assert rl._get_client_ip(request) == "10.0.0.9"

    def test_count_two_from_trusted_peer_picks_second_from_right(
        self, rl, monkeypatch
    ):
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "93.184.216.34, 10.0.0.8, 10.0.0.9"},
        )
        assert rl._get_client_ip(request) == "10.0.0.8"

    def test_count_mode_ignores_client_forged_leftmost_prefix(
        self, rl, monkeypatch
    ):
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        request = make_request(
            peer="10.0.0.5",
            headers={
                "X-Forwarded-For": "forged-1, forged-2, forged-3, 10.0.0.9"
            },
        )
        assert rl._get_client_ip(request) == "10.0.0.9"

    def test_count_mode_untrusted_direct_peer_ignores_forged_xff(
        self, rl, monkeypatch
    ):
        """A public direct peer isn't behind ANY declared proxy hop -- the
        connection reaching this process directly IS the client, so a
        crafted XFF with >= count entries must not be honored even though
        it satisfies the length check."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        request = make_request(
            peer="8.8.8.8",
            headers={"X-Forwarded-For": "1.2.3.4, 5.6.7.8"},
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_count_mode_short_xff_falls_back_to_direct_peer(
        self, rl, monkeypatch
    ):
        """Fewer entries than the declared hop count is a missing/under-
        filled chain -- fall back to the direct peer rather than trust an
        ambiguous, possibly attacker-supplied short list."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "93.184.216.34"},
        )
        assert rl._get_client_ip(request) == "10.0.0.5"

    def test_count_mode_missing_xff_falls_back_to_direct_peer(
        self, rl, monkeypatch
    ):
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        request = make_request(peer="10.0.0.5")
        assert rl._get_client_ip(request) == "10.0.0.5"

    def test_count_mode_trust_proxy_headers_true_ignores_xff_and_trusts_peer(
        self, rl, monkeypatch
    ):
        """Combo mode (TRUST_PROXY_HEADERS=true + count > 0): this
        function must NOT hop-count X-Forwarded-For itself -- uvicorn's
        ProxyHeadersMiddleware already ran as the outermost ASGI layer
        and computed the correct `request.client` before this code ever
        saw the request (see the tri-state guard's module-level comment
        and `_get_client_ip`'s docstring). This is an
        application-level unit test of that combo-mode branch only; it
        does NOT exercise uvicorn's own --proxy-headers rewrite (a
        separate, outer ASGI layer these pure Request-object tests never
        go through) -- see ``TestComboModeAgainstRealUvicornMiddleware``
        below for that. Here, `peer` stands in for whatever
        `request.client.host` uvicorn already resolved it to (whether
        that's a direct attacker's own raw IP, left untouched because
        they're outside LDR_SERVER_FORWARDED_ALLOW_IPS, or a
        proxy-computed address for a trusted peer) -- either way this
        function must return it as-is and must NOT let the forged
        X-Forwarded-For header override it with entries[-N]. (A positive
        count with the unrestricted ``"*"`` refuses to start, so this mode
        always has a restricted allow-list.)"""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        restrict_allow_list(rl, monkeypatch, "10.0.0.5")
        request = make_request(
            peer="8.8.8.8",
            headers={"X-Forwarded-For": "1.2.3.4, 9.9.9.9"},
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_count_zero_is_pre_existing_heuristic_untrusted_peer(
        self, rl, monkeypatch
    ):
        """count == 0 is the documented 'unset' sentinel -- an untrusted
        public peer's forged XFF must still be ignored regardless of entry
        count (no hop-counting kicks in)."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        request = make_request(
            peer="8.8.8.8",
            headers={"X-Forwarded-For": "1.2.3.4, 5.6.7.8"},
        )
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_count_zero_is_pre_existing_heuristic_trusted_peer(
        self, rl, monkeypatch
    ):
        """count == 0 from a trusted peer honors the RIGHT-most entry (the
        pre-existing heuristic since #6442), whatever the chain length --
        no hop counting kicks in."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        request = make_request(
            peer="10.0.0.5",
            headers={"X-Forwarded-For": "93.184.216.34, 10.0.0.8, 10.0.0.9"},
        )
        assert rl._get_client_ip(request) == "10.0.0.9"


class TestComboModeAgainstRealUvicornMiddleware:
    """Drives requests through uvicorn's OWN ``ProxyHeadersMiddleware`` --
    not a stand-in -- before handing the resulting scope to
    ``_get_client_ip``, to verify the premise behind combo mode
    (``TRUST_PROXY_HEADERS=true``, at ANY ``LDR_SERVER_TRUSTED_PROXY_COUNT``
    value): that uvicorn, not this module, is the authority on
    ``request.client`` in that mode, and that authority already accounts
    for whether the direct peer is in ``LDR_SERVER_FORWARDED_ALLOW_IPS``.

    Regression coverage for two defects fixed here, at different times:

    - Originally, the count-mode branch's ``if _TRUST_PROXY_HEADERS or
      _is_trusted_peer(direct_peer):`` gate short-circuited on the global
      flag alone, so -- in exactly the combination a restricted
      ``LDR_SERVER_FORWARDED_ALLOW_IPS`` is meant to make safe -- a
      direct-connecting attacker's forged X-Forwarded-For was still
      hop-counted and ``entries[-N]`` (fully attacker-chosen) became the
      rate-limit key, because this module never consulted
      ``request.client`` before parsing the header itself. Fixed by
      returning ``direct_peer`` unconditionally inside the
      ``_TRUSTED_PROXY_COUNT > 0`` branch, trusting only what uvicorn
      already computed. Covered by the ``count_one``/``count_two``/
      ``multi_hop`` tests below.
    - That fix was then wired ONLY inside ``if _TRUSTED_PROXY_COUNT > 0:``,
      so a deployment with the recommended combo (``TRUST_PROXY_HEADERS=
      true`` + a restricted allow-list) but ``LDR_SERVER_TRUSTED_PROXY_
      COUNT`` left at its default of 0 fell through to the legacy branch,
      which honored the left-most (forgeable) XFF entry -- exploitable
      even though uvicorn itself was already computing the correct client
      address one layer up. Fixed by hoisting the "trust direct_peer" rule
      so it is unconditional on ``TRUST_PROXY_HEADERS`` alone, independent
      of the hop count. Since #6442 the legacy branch keys on the
      RIGHT-most entry, which for a single trusted hop is uvicorn's answer
      too, so ``test_count_zero_proxied_request_gets_uvicorn_computed_client``
      passes either way and is a characterisation test. The multi-hop
      tests below set a hop count of 2, so they never reach the branch the
      hoist moved and do not fence it either. What fails if the hoist is
      reverted: with the legacy gate's ``_TRUST_PROXY_HEADERS or``
      short-circuit restored as well,
      ``test_count_zero_direct_attacker_outside_allow_list_keeps_raw_peer``
      here, ``TestTrustProxyHeadersOverride.test_flag_trusts_direct_peer_and_ignores_xff``,
      ``TestTrustProxyHeadersXRealIpFallback``'s
      ``test_real_ip_ignored_when_no_xff_and_peer_untrusted``,
      ``test_rotating_real_ip_from_untrusted_peer_never_changes_key`` and
      ``test_xff_present_takes_precedence_over_real_ip``, and in
      ``test_rate_limit_restricted_allow_list.py``
      ``test_private_non_allowlisted_peer_varying_real_ip_mints_no_new_keys``,
      ``test_private_non_allowlisted_peer_xff_present_keeps_raw_peer`` and
      ``test_non_private_proxy_x_real_ip_is_ignored``. With the gate left
      as it is, ``test_xff_present_takes_precedence_over_real_ip`` and the
      two unrestricted-allow-list tests in ``TestTrustProxyHeadersOverride``.
    """

    @staticmethod
    async def _run_through_uvicorn(peer, headers, trusted_hosts):
        """Wrap a capturing ASGI app in uvicorn's real
        ``ProxyHeadersMiddleware`` and drive one HTTP request through it,
        returning the ``scope`` the inner app observed (post-middleware).
        """
        from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware

        captured = {}

        async def inner_app(scope, receive, send):
            captured["scope"] = scope

        middleware = ProxyHeadersMiddleware(
            inner_app, trusted_hosts=trusted_hosts
        )
        scope = {
            "type": "http",
            "method": "GET",
            "path": "/",
            "query_string": b"",
            "client": (peer, 51234),
            "headers": [(k.lower().encode(), v.encode()) for k, v in headers],
        }

        async def receive():
            return {"type": "http.disconnect"}

        async def send(message):
            pass

        await middleware(scope, receive, send)
        return captured["scope"]

    @pytest.fixture(autouse=True)
    def _module_mirrors_uvicorn_allow_list(self, rl, monkeypatch):
        """Keep the module's allow-list in step with the one each test hands
        uvicorn, as ``web/app.py`` does from one env var."""
        self._rl = rl
        self._monkeypatch = monkeypatch

    def _request_after_uvicorn(self, peer, headers, trusted_hosts):
        restrict_allow_list(self._rl, self._monkeypatch, trusted_hosts)
        scope = asyncio.run(
            self._run_through_uvicorn(peer, headers, trusted_hosts)
        )
        return Request(scope)

    def test_direct_attacker_outside_allow_list_keeps_raw_peer(
        self, rl, monkeypatch
    ):
        """A restricted LDR_SERVER_FORWARDED_ALLOW_IPS (the trusted
        proxy's real IP, ``10.0.0.5``): an attacker connecting straight
        to LDR is NOT in that allow-list, so uvicorn's own middleware --
        exercised here for real -- leaves ``scope["client"]`` untouched.
        ``_get_client_ip``'s combo branch must return that raw attacker
        peer, never a forged ``entries[-N]`` picked out of their XFF."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="8.8.8.8",
            headers=[
                (
                    "X-Forwarded-For",
                    "1.2.3.4, 5.6.7.8, 9.9.9.9, 6.6.6.6",
                ),
            ],
            trusted_hosts="10.0.0.5",
        )
        assert request.client.host == "8.8.8.8"
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_proxied_request_gets_uvicorn_computed_client(
        self, rl, monkeypatch
    ):
        """The trusted proxy (``10.0.0.5``) connects directly to LDR and
        appends the client it observed to X-Forwarded-For. uvicorn's
        real middleware rewrites ``scope["client"]`` to that upstream
        client (``203.0.113.7``); ``_get_client_ip``'s combo branch must
        return exactly that computed value, never re-deriving anything
        from the header itself."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="10.0.0.5",
            headers=[("X-Forwarded-For", "203.0.113.7")],
            trusted_hosts="10.0.0.5",
        )
        assert request.client.host == "203.0.113.7"
        assert rl._get_client_ip(request) == "203.0.113.7"

    def test_count_zero_direct_attacker_outside_allow_list_keeps_raw_peer(
        self, rl, monkeypatch
    ):
        """Same as ``test_direct_attacker_outside_allow_list_keeps_raw_peer``
        but at ``LDR_SERVER_TRUSTED_PROXY_COUNT``'s default of 0/unset --
        the exact configuration the fixed defect left exploitable: an
        operator following the documented, recommended combo (restricted
        allow-list) who never touches the count setting at all."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="8.8.8.8",
            headers=[
                (
                    "X-Forwarded-For",
                    "1.2.3.4, 5.6.7.8, 9.9.9.9, 6.6.6.6",
                ),
            ],
            trusted_hosts="10.0.0.5",
        )
        assert request.client.host == "8.8.8.8"
        assert rl._get_client_ip(request) == "8.8.8.8"

    def test_count_zero_proxied_request_gets_uvicorn_computed_client(
        self, rl, monkeypatch
    ):
        """Same as ``test_proxied_request_gets_uvicorn_computed_client``
        but at ``LDR_SERVER_TRUSTED_PROXY_COUNT=0`` (default/unset): the
        real client the trusted proxy observed and appended to XFF must
        still be returned -- never the request's own left-most
        (client-forgeable) XFF entry, which is what the pre-fix legacy
        branch would have returned here.

        The header carries a forged left-most entry (``1.2.3.4``) ahead of
        the address the trusted proxy appended. uvicorn walks from the
        right and hands back ``203.0.113.7``; since #6442 the legacy branch
        keys on the right-most entry and returns the same address, so this
        test no longer fences the hoist (see the class docstring for the
        tests that do)."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="10.0.0.5",
            headers=[("X-Forwarded-For", "1.2.3.4, 203.0.113.7")],
            trusted_hosts="10.0.0.5",
        )
        assert request.client.host == "203.0.113.7"
        assert rl._get_client_ip(request) == "203.0.113.7"

    def test_proxied_multi_hop_requires_every_trusted_hop_listed(
        self, rl, monkeypatch
    ):
        """Two trusted hops (``10.0.0.5`` nearest to LDR, ``10.0.0.6``
        further out) each append the peer they observed. With BOTH IPs
        in ``forwarded_allow_ips``, uvicorn's right-to-left walk skips
        both trusted entries and returns the true client -- confirming
        the docs' claim that ``LDR_SERVER_FORWARDED_ALLOW_IPS`` must
        list EVERY trusted hop, not just the nearest one."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="10.0.0.5",
            headers=[("X-Forwarded-For", "203.0.113.7, 10.0.0.6")],
            trusted_hosts="10.0.0.5,10.0.0.6",
        )
        assert request.client.host == "203.0.113.7"
        assert rl._get_client_ip(request) == "203.0.113.7"

    def test_proxied_multi_hop_missing_inner_hop_from_allow_list_is_wrong(
        self, rl, monkeypatch
    ):
        """Negative control for the claim above: if
        LDR_SERVER_FORWARDED_ALLOW_IPS lists only the NEAREST hop
        (``10.0.0.5``), uvicorn's reverse walk stops at the first entry
        it doesn't recognise -- the inner proxy's own address
        (``10.0.0.6``) -- and returns THAT instead of the true client.
        Not a security hole (``10.0.0.6`` is still not
        attacker-controlled), but operationally wrong, which is exactly
        why the docs and the tri-state guard's comment insist on listing
        every trusted hop."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        monkeypatch.setattr(rl, "_TRUST_PROXY_HEADERS", True)
        request = self._request_after_uvicorn(
            peer="10.0.0.5",
            headers=[("X-Forwarded-For", "203.0.113.7, 10.0.0.6")],
            trusted_hosts="10.0.0.5",  # missing 10.0.0.6
        )
        assert request.client.host == "10.0.0.6"
        assert rl._get_client_ip(request) == "10.0.0.6"


class TestMultiLineXForwardedFor:
    """A proxy chain that APPENDS its own X-Forwarded-For line (rather
    than editing the client-sent one) produces a request with MULTIPLE
    X-Forwarded-For header lines -- e.g. HAProxy's ``option forwardfor``
    without ``if-none``, or any of the multi-hop CDN/tunnel setups this
    module's docs bless. Starlette's ``Headers.get()`` returns only the
    FIRST such line, so naive code sees just the client's own (fully
    forgeable) line and never the proxy-appended one. ``_get_client_ip``
    must instead comma-join every line (mirroring WSGI's
    ``HTTP_X_FORWARDED_FOR`` semantics, which Werkzeug's ``ProxyFix``
    relies on) before hop-counting or taking the legacy right-most entry.
    """

    def test_hop_count_one_resolves_to_proxy_appended_line_not_forged_first_line(
        self, rl, monkeypatch
    ):
        """The attacker sends their own X-Forwarded-For line first; the
        trusted proxy appends a SECOND, separate X-Forwarded-For line
        rather than editing the first. Under N=1, the rate-limit key must
        be the entry from the proxy-appended line -- never the forged
        first line, no matter how many entries it contains."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 1)
        request = make_request(
            peer="10.0.0.5",
            headers=[
                ("X-Forwarded-For", "9.9.9.9, forged-2, forged-3"),
                ("X-Forwarded-For", "10.0.0.9"),
            ],
        )
        assert rl._get_client_ip(request) == "10.0.0.9"

    def test_hop_count_two_resolves_across_both_header_lines(
        self, rl, monkeypatch
    ):
        """Hop counting must operate on the FULL comma-joined chain
        spanning every X-Forwarded-For line, not just whichever line
        Headers.get() happens to return first."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 2)
        request = make_request(
            peer="10.0.0.5",
            headers=[
                ("X-Forwarded-For", "forged, 93.184.216.34"),
                ("X-Forwarded-For", "10.0.0.8, 10.0.0.9"),
            ],
        )
        assert rl._get_client_ip(request) == "10.0.0.8"

    def test_legacy_heuristic_mode_also_joins_multiple_header_lines(
        self, rl, monkeypatch
    ):
        """count == 0 (the pre-existing heuristic) reads the right-most
        entry of the joined chain (#6442) -- i.e. the proxy-appended second
        line, not the client's own forged first line. This pins that a
        second header line is no longer silently dropped."""
        monkeypatch.setattr(rl, "_TRUSTED_PROXY_COUNT", 0)
        request = make_request(
            peer="10.0.0.5",
            headers=[
                ("X-Forwarded-For", "93.184.216.34, forged-2"),
                ("X-Forwarded-For", "10.0.0.9"),
            ],
        )
        assert rl._get_client_ip(request) == "10.0.0.9"

    def test_forwarded_for_helper_joins_all_lines_and_skips_empties(self, rl):
        request = make_request(
            peer="10.0.0.5",
            headers=[
                ("X-Forwarded-For", "1.2.3.4"),
                ("X-Forwarded-For", ""),
                ("X-Forwarded-For", "5.6.7.8"),
            ],
        )
        assert rl._get_forwarded_for(request) == "1.2.3.4,5.6.7.8"


class TestApiExempt:
    """app.api_rate_limit = 0 disables the /api/v1 limit via the
    request-scoped contextvar (parity with main's exempt_when)."""

    @pytest.fixture(autouse=True)
    def _fresh_ctx(self, rl):
        """Pin the contextvar to its default and restore afterwards so
        these tests neither depend on nor leak contextvar state."""
        token = rl._api_rate_limit_ctx.set(rl.API_RATE_LIMIT_DEFAULT)
        yield
        rl._api_rate_limit_ctx.reset(token)

    def test_default_limit_is_not_exempt(self, rl):
        assert rl._api_exempt() is False

    def test_zero_disables_the_limit(self, rl):
        rl.set_request_api_rate_limit(0)
        assert rl._api_exempt() is True

    def test_nonzero_custom_limit_stays_enforced(self, rl):
        rl.set_request_api_rate_limit(120)
        assert rl._api_exempt() is False

    def test_concurrent_tasks_keep_independent_request_limits(self, rl):
        """One request disabling its limit must not exempt another request.

        Both tasks set their value before either checks exemption.  A plain
        module global would therefore make both tasks observe the last write;
        the ContextVar keeps each decision bound to its own request task.
        """

        async def _scenario():
            both_ready = asyncio.Event()
            ready_count = 0

            async def _decision(limit):
                nonlocal ready_count
                rl.set_request_api_rate_limit(limit)
                ready_count += 1
                if ready_count == 2:
                    both_ready.set()
                await both_ready.wait()
                return rl._api_exempt()

            return await asyncio.gather(_decision(0), _decision(120))

        assert asyncio.run(_scenario()) == [True, False]
        # Child-task writes must not leak back into the caller's context.
        assert rl._api_exempt() is False