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.

55.5 KB
Raw Blame
"""Regression tests for four Flask -> FastAPI/slowapi migration bugs in
``web/dependencies/rate_limit.py``:

1. **Strategy silently downgraded.** origin/main's
   ``app_factory.py`` set ``RATELIMIT_STRATEGY = "moving-window"``; this
   branch built ``Limiter`` with no ``strategy`` kwarg, so slowapi fell
   back to ``"fixed-window"`` (``slowapi/extension.py``), which refills
   the whole quota at the clock boundary instead of enforcing a true
   rolling window (measured: 10 login attempts landed in 10.86s against a
   "5 per 10 seconds" policy -- exactly 2x nominal).

2. **Response headers dropped.** origin/main's
   ``security/rate_limiter.py`` passed ``headers_enabled=True``; slowapi
   defaults it to ``False``, so 429s carried no ``Retry-After`` /
   ``X-RateLimit-*`` headers.

3. **Credential leak on startup crash.** with no try/except around the
   module-level ``Limiter(**_limiter_kwargs)`` construction, a broken
   ``RATE_LIMIT_STORAGE_URI`` (e.g. an unsupported scheme) crashed with
   the *plaintext* credential embedded in the URI printed via
   ``limits.errors.ConfigurationError.args`` -- both in loguru's
   ``@logger.catch`` output and in the raw stderr traceback.

4. **Startup aborted over a backend that is never used.** main gated its
   ``validate_rate_limit_storage()`` call on ``if rate_limiting_enabled:``;
   the gate died in merge ``246c74c77`` and slowapi resolves storage
   eagerly, so ``LDR_DISABLE_RATE_LIMITING=true`` plus a stale
   ``RATELIMIT_STORAGE_URL`` (main's own variable name, read by slowapi
   directly) crashed the server at import. Issue #5964.

Each class below is independent and can be read/run on its own.
"""

import importlib
import io
import os
import subprocess
import sys
import uuid
from contextlib import contextmanager

import pytest
from limits.errors import ConfigurationError
from slowapi import Limiter
from slowapi.errors import RateLimitExceeded
from starlette.requests import Request
from starlette.responses import Response


def _rl():
    from local_deep_research.web.dependencies import rate_limit

    return rate_limit


def _reload_rate_limit():
    return importlib.reload(_rl())


@contextmanager
def _rate_limit_module_restored():
    """Snapshot the module namespace and put the ORIGINAL objects back.

    Same idiom as the sibling ``test_rate_limit_config.py`` /
    ``test_rate_limit_startup_validation_gap.py``: ``importlib.reload``
    rebinds ``limiter`` to a fresh, unmarked ``Limiter`` while routers
    stay decorated against the original instance, and a construction
    failure (regression 3's test below) can abort the reload part-way,
    leaving a half-rewritten namespace. Restoring the snapshot verbatim
    avoids leaking either state into later tests/files.
    """
    rate_limit = _rl()
    snapshot = dict(rate_limit.__dict__)
    try:
        yield rate_limit
    finally:
        rate_limit.__dict__.clear()
        rate_limit.__dict__.update(snapshot)


@pytest.fixture(autouse=True)
def _rate_limit_state_guard():
    rate_limit = _rl()
    original = dict(rate_limit.__dict__)
    yield
    assert rate_limit.__dict__ == original, (
        "rate_limit module globals leaked out of this test — a reload was "
        "not restored; later tests would see a fresh, unmarked Limiter."
    )


@pytest.fixture(autouse=True)
def _restore_module_state():
    with _rate_limit_module_restored():
        yield


@pytest.fixture()
def clean_env(monkeypatch):
    for name in (
        "LDR_DISABLE_RATE_LIMITING",
        "DISABLE_RATE_LIMITING",
        "RATE_LIMIT_STORAGE_URI",
        "RATELIMIT_STORAGE_URL",
    ):
        monkeypatch.delenv(name, raising=False)
    return monkeypatch


# ---------------------------------------------------------------------------
# Regression 1 — moving-window strategy
# ---------------------------------------------------------------------------


class TestMovingWindowStrategy:
    def test_limiter_kwargs_request_moving_window(self):
        """The config dict handed to ``Limiter(**_limiter_kwargs)`` must
        request "moving-window" explicitly -- omitting it silently
        downgrades to slowapi's "fixed-window" default."""
        mod = _rl()
        assert mod._limiter_kwargs["strategy"] == "moving-window"

    def test_live_limiter_uses_moving_window(self):
        """The actual singleton the routers are decorated against must
        reflect the requested strategy, not just the kwargs dict."""
        mod = _rl()
        assert mod.limiter._strategy == "moving-window"


# ---------------------------------------------------------------------------
# Regression 2 — headers_enabled -> Retry-After / X-RateLimit-*
# ---------------------------------------------------------------------------


def _fake_request(path):
    scope = {
        "type": "http",
        "method": "GET",
        "path": path,
        "query_string": b"",
        "headers": [],
        "client": ("203.0.113.7", 4242),
    }
    return Request(scope)


class TestHeadersEnabled:
    """`headers_enabled` must stay OFF; the 429 handler supplies the headers.

    main's Flask-Limiter passed `headers_enabled=True`, so restoring it looks
    like the obvious parity fix — and it is a trap. Under slowapi the flag also
    makes `Limiter.sync_wrapper` call `_inject_headers` on the return value of
    every rate-limited route, on success as well as on 429. A handler that
    returns a plain dict (most of them here) has no injectable
    `response: Response` parameter, so slowapi raises and the 200 becomes a 500.
    Observed on POST /auth/validate-password and the unified-search endpoints.

    The client-visible behaviour main actually had — Retry-After on a 429 — is
    restored in `fastapi_app.py::_rate_limit_exceeded`, which sets the headers
    directly. `tests/web/test_rate_limit_headers_on_429.py` pins that end to end.
    """

    def test_limiter_kwargs_does_not_request_headers_enabled(self):
        mod = _rl()
        assert mod._limiter_kwargs.get("headers_enabled") is not True, (
            "headers_enabled=True makes slowapi wrap every rate-limited route "
            "and raise on handlers returning a plain dict — 200s become 500s. "
            "Retry-After is set by the 429 handler in fastapi_app.py instead."
        )

    def test_live_limiter_has_headers_disabled(self):
        mod = _rl()
        assert mod.limiter._headers_enabled is False

    def test_dict_returning_rate_limited_route_still_returns_200(self):
        """The regression the flag caused, pinned at the HTTP layer.

        Without this, someone re-reads main's `headers_enabled=True`, sets it
        for parity, and every dict-returning rate-limited route starts 500ing.
        """
        from fastapi.testclient import TestClient

        from local_deep_research.web.fastapi_app import app

        with TestClient(app, raise_server_exceptions=False) as client:
            client.get("/auth/login")
            token = client.get("/auth/csrf-token").json()["csrf_token"]
            resp = client.post(
                "/auth/validate-password",
                data={"password": "Abc12345!", "csrf_token": token},
            )

        assert resp.status_code != 500, (
            "a rate-limited route returning a plain dict 500'd — this is the "
            "headers_enabled=True failure mode; see this class's docstring"
        )

    def test_exceeded_limit_response_carries_retry_after_header(self):
        """Functional proof, not just the flag: build a throwaway Limiter
        with the exact two config values this module now sets
        (strategy="moving-window", headers_enabled=True) — mirroring
        ``_limiter_kwargs`` without touching the shared module singleton
        or its process-global storage buckets — drive it past a 1-request
        quota, and confirm slowapi's own ``_inject_headers`` (the call
        its stock ``RateLimitExceeded`` handler makes) adds Retry-After
        and X-RateLimit-* to the resulting response.

        Note: the app's OWN 429 handler
        (``web/fastapi_app.py::_setup_rate_limiting._rate_limit_exceeded``)
        builds a bare ``JSONResponse`` and does not call
        ``limiter._inject_headers`` itself, so end-to-end 429s served by
        the real app still lack these headers today regardless of this
        flag. That gap lives in ``fastapi_app.py``, outside this module —
        see the task report. This test only certifies what
        ``rate_limit.py`` is responsible for: the Limiter is configured
        so a handler that calls ``_inject_headers`` (slowapi's default
        does) produces the headers.
        """
        unique_key = f"regression-probe-{uuid.uuid4().hex}"
        probe = Limiter(
            key_func=lambda request: unique_key,
            default_limits=["1 per hour"],
            headers_enabled=True,
            strategy="moving-window",
            enabled=True,
        )
        request = _fake_request("/__rate_limit_regression_probe__")

        # First request consumes the single slot.
        probe._check_request_limit(request, None, in_middleware=True)
        # Second is over quota.
        with pytest.raises(RateLimitExceeded):
            probe._check_request_limit(request, None, in_middleware=True)

        response = probe._inject_headers(
            Response(status_code=429), request.state.view_rate_limit
        )
        assert response.headers.get("Retry-After") is not None
        assert response.headers.get("X-RateLimit-Limit") == "1"
        assert response.headers.get("X-RateLimit-Remaining") == "0"


# ---------------------------------------------------------------------------
# Regression 3 — credential redaction on storage construction failure
# ---------------------------------------------------------------------------


_LEAKY_URI = (
    "rediss-bogus://rateuser:SuperSecretPW123@evil-internal-host:6379/0"
)
_SECRET = "SuperSecretPW123"

# RFC 3986 (and urllib.parse) split userinfo from host at the LAST
# "@", so this password genuinely parses as "Top@Secret" and a redactor
# stopping at the first "@" strips only its prefix, emitting the
# "Secret" suffix into the startup error.
_RAW_AT_LEAKY_URI = (
    "rediss-bogus://rateuser:Top@Secret@evil-internal-host:6379/0"
)
# Every fragment the password could disclose through: the whole value
# plus each raw-@-separated piece on either side of it.
_RAW_AT_SECRET_FRAGMENTS = ("Top@Secret", "Top", "Secret")


class TestStorageUriRedaction:
    def test_redact_helper_strips_credentials(self):
        mod = _rl()
        redacted = mod._redact_storage_uri(_LEAKY_URI)
        assert _SECRET not in redacted
        assert "rateuser" not in redacted
        # Structure (scheme/host/port/path) is preserved for diagnosis.
        assert redacted == "rediss-bogus://***@evil-internal-host:6379/0"

    def test_redact_helper_is_noop_without_credentials(self):
        mod = _rl()
        assert mod._redact_storage_uri("memory://") == "memory://"
        assert (
            mod._redact_storage_uri("redis://localhost:6379/0")
            == "redis://localhost:6379/0"
        )

    def test_redact_helper_strips_password_containing_raw_at(self):
        """The whole userinfo segment goes, not just up to the first "@".

        Reverting to a first-"@" regex redacts this URI to
        "rediss-bogus://***@Secret@evil-internal-host:6379/0", which
        still carries the password's suffix -- this test pins the
        last-"@" split so that revert fails.
        """
        mod = _rl()
        redacted = mod._redact_storage_uri(_RAW_AT_LEAKY_URI)
        for fragment in _RAW_AT_SECRET_FRAGMENTS:
            assert fragment not in redacted
        assert "rateuser" not in redacted
        # Host/port/path stay diagnosable.
        assert redacted == "rediss-bogus://***@evil-internal-host:6379/0"
        # A well-formed single-"@" authority must redact identically.
        assert mod._redact_storage_uri(_LEAKY_URI) == (
            "rediss-bogus://***@evil-internal-host:6379/0"
        )

    @pytest.mark.parametrize(
        ("uri", "secret_fragments", "expected"),
        [
            pytest.param(
                "redis://default:Pa#ss@redis:6379/0",
                ("Pa#ss", "Pa", "default"),
                "redis://***",
                id="raw-hash-in-password",
            ),
            pytest.param(
                "redis://default:pa?ss@redis:6379/0",
                ("pa?ss", "default"),
                "redis://***",
                id="raw-question-mark-in-password",
            ),
            pytest.param(
                "memcached://u:pa?ss@mc:11211",
                ("pa?ss", "u:"),
                "memcached://***",
                id="memcached-raw-question-mark",
            ),
            pytest.param(
                "redis://u:ab/cd@redis:6379/0",
                ("ab/cd", "u:ab"),
                "redis://***@redis:6379/0",
                id="raw-slash-in-password",
            ),
            pytest.param(
                "redis://u:ab/cd#ef?gh@ij@[::1]:6379/0",
                ("ab/cd", "#ef", "?gh", "ij@"),
                "redis://***",
                id="every-delimiter-in-password-ipv6-host",
            ),
            pytest.param(
                "redis://u:pw@[2001:db8::1]:6379/0",
                ("u:pw",),
                "redis://***@[2001:db8::1]:6379/0",
                id="ipv6-host",
            ),
            pytest.param(
                "redis+unix://:s3cr3t@/var/run/redis.sock",
                ("s3cr3t",),
                "redis+unix://***@/var/run/redis.sock",
                id="unix-socket-with-password",
            ),
            pytest.param(
                "redis+sentinel://u:pw#1@h1:26379,h2:26379/mymaster",
                ("pw#1",),
                "redis+sentinel://***",
                id="sentinel-host-list",
            ),
            pytest.param(
                "redis:u:s3cr3t@h:6379",
                ("s3cr3t", "u:"),
                "***@h:6379",
                id="missing-double-slash",
            ),
        ],
    )
    def test_redact_helper_strips_password_with_uri_delimiters(
        self, uri, secret_fragments, expected
    ):
        """This redaction runs precisely when the URI failed to parse,
        so the password may hold any delimiter raw. main's
        ``://[^@/]+@`` stopped at "/" and at the first "@", and a
        ``://[^/?#]*@`` variant stopped matching altogether at "#" or
        "?" -- both returned such a URI with the password intact. A raw
        "?" or "#" before the last "@" could equally open a query holding
        a raw "@", so those rows redact everything after the scheme."""
        mod = _rl()
        redacted = mod._redact_storage_uri(uri)
        for fragment in secret_fragments:
            assert fragment not in redacted
        assert redacted == expected

    @pytest.mark.parametrize(
        "uri",
        [
            "memory://",
            "redis://[::1]:6379/0",
            "redis+unix:///var/run/redis.sock",
            "memcached://h1:11211,h2:11211",
        ],
    )
    def test_redact_helper_leaves_userinfo_free_uris_unchanged(self, uri):
        assert _rl()._redact_storage_uri(uri) == uri

    @pytest.mark.parametrize(
        ("uri", "secret_fragments", "expected"),
        [
            pytest.param(
                "redis://cache:6379/0?password=hunter2",
                ("hunter2", "password="),
                "redis://cache:6379/0?***",
                id="password-query-no-userinfo",
            ),
            pytest.param(
                "redis://:pw@h:6379?ssl_password=kp&db=1",
                ("pw", "ssl_password", "kp"),
                "redis://***@h:6379?***",
                id="userinfo-and-query",
            ),
            pytest.param(
                "rediss://h:6380/0#token=abc",
                ("token", "abc"),
                "rediss://h:6380/0#***",
                id="fragment-no-userinfo",
            ),
            pytest.param(
                "redis+unix:///run/redis.sock?db=0&password=s3cr3t",
                ("s3cr3t",),
                "redis+unix:///run/redis.sock?***",
                id="unix-socket-query-password",
            ),
            pytest.param(
                "redis:h?password=s3cr3t",
                ("s3cr3t",),
                "redis:h?***",
                id="no-scheme-slashes-query",
            ),
        ],
    )
    def test_redact_helper_strips_query_string_credentials(
        self, uri, secret_fragments, expected
    ):
        """Storage clients take credentials as query parameters too
        (redis-py reads ``password`` and ``ssl_password`` from the
        query), and a value with no "@" used to come back verbatim --
        into the startup RuntimeError the ``@logger.catch`` on
        ``main()`` logs."""
        redacted = _rl()._redact_storage_uri(uri)
        for fragment in secret_fragments:
            assert fragment not in redacted
        assert redacted == expected

    @pytest.mark.parametrize(
        ("uri", "secret_fragments", "expected"),
        [
            pytest.param(
                "redis://h:6379/0?password=p@ssw0rd",
                ("p@ssw0rd", "ssw0rd"),
                "redis://***",
                id="at-in-query-password",
            ),
            pytest.param(
                "rediss://h/0?ssl_password=Sup3r@Secret#frag",
                ("Sup3r", "Secret", "frag"),
                "rediss://***",
                id="at-in-ssl-password-with-fragment",
            ),
            pytest.param(
                "redis://u:pw@h:6379/0?password=x@tail",
                ("pw", "x@tail", "tail"),
                "redis://***",
                id="userinfo-and-at-in-query-password",
            ),
            pytest.param(
                "redis+sentinel://h:26379/m?sentinel_kwargs=x&password=s3@cr3t",
                ("s3@cr3t", "cr3t"),
                "redis+sentinel://***",
                id="sentinel-at-in-query-password",
            ),
            pytest.param(
                "redis://h:6379/0?username=ops@corp&password=S3cret",
                ("S3cret", "corp"),
                "redis://***",
                id="at-in-earlier-parameter-then-password",
            ),
            pytest.param(
                "rediss://h:6380/0#token=a@bcdef",
                ("bcdef",),
                "rediss://***",
                id="at-in-fragment",
            ),
            pytest.param(
                "redis:h?password=p@ss",
                ("p@ss", "ss"),
                "***",
                id="at-in-query-no-scheme-slashes",
            ),
        ],
    )
    def test_redact_helper_strips_query_credentials_containing_raw_at(
        self, uri, secret_fragments, expected
    ):
        """RFC 3986 allows a raw "@" in a query, and redis-py reads the
        password from the query. Splitting at the last "@" first put the
        "?" in the redacted part and printed the password's tail as the
        host ("redis://***@ssw0rd")."""
        redacted = _rl()._redact_storage_uri(uri)
        for fragment in secret_fragments:
            assert fragment not in redacted
        assert redacted == expected

    def test_redact_helper_never_emits_a_secret_substring_fuzz(self):
        """Seeded fuzz over the shapes a credential can take: userinfo,
        query, fragment, each with raw delimiters. No 3+ character piece
        of the secret may survive (each piece checked carries one of the
        marker letters, so structural text like "://" cannot match)."""
        import random

        rl = _rl()
        rng = random.Random(4870)
        markers = "QZXJ"
        special = "@?#/:;&=[]%"
        filler = "abcdefghij0123456789" + special

        def word(k):
            return "".join(rng.choice(filler) for _ in range(k))

        for _ in range(3000):
            pw = "".join(
                rng.choice(markers + special) for _ in range(rng.randint(4, 12))
            )
            scheme = rng.choice(
                ["redis://", "rediss://", "redis+sentinel://", "redis:", ""]
            )
            host = rng.choice(["h", "cache:6379", "[::1]:6379", "h1:1,h2:2"])
            path = rng.choice(["", "/0", "/a@b", "/x?y"])
            user = rng.choice(["", "u", "ops@corp"])
            uri = rng.choice(
                [
                    f"{scheme}{user}:{pw}@{host}{path}",
                    f"{scheme}{host}{path}?{word(4)}&password={pw}&db=0",
                    f"{scheme}{user}:{word(5)}@{host}{path}"
                    f"?ssl_password={pw}#{word(3)}",
                    f"{scheme}{host}{path}#{pw}",
                    f"{scheme}{user}:{pw}@{host}{path}?password={pw}",
                ]
            )
            redacted = rl._redact_storage_uri(uri)
            for i in range(len(pw)):
                for j in range(i + 3, len(pw) + 1):
                    piece = pw[i:j]
                    if any(c in markers for c in piece):
                        assert piece not in redacted, (uri, redacted)

    def test_storage_scheme_log_never_echoes_a_scheme_less_value(self):
        """The "storage configured" info line logs only the scheme. A
        ``split("://")[0]`` returned the whole value, password and all,
        when the "//" was missing."""
        mod = _rl()
        assert mod._storage_uri_scheme("redis://u:pw@h:6379/0") == (
            "redis://..."
        )
        assert "s3cr3t" not in mod._storage_uri_scheme("redis:u:s3cr3t@h")

    def test_malformed_uri_with_raw_hash_password_never_leaks_secret(
        self, clean_env
    ):
        """End-to-end: ``limits`` rejects this URI (unknown scheme) and
        the startup error must not carry any part of the password."""
        clean_env.setenv(
            "RATE_LIMIT_STORAGE_URI",
            "rediss-bogus://rateuser:Pa#ss?wd@evil-internal-host:6379/0",
        )
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()
        exc = exc_info.value
        import traceback

        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        for surface in (str(exc), formatted):
            assert "Pa#ss" not in surface
            assert "ss?wd" not in surface
            assert "rateuser" not in surface
        # "#" and "?" precede the last "@", so the host cannot be told
        # apart from a query tail and everything after the scheme goes.
        assert "rediss-bogus://***" in str(exc)
        assert "evil-internal-host" not in str(exc)

    def test_malformed_credentialed_uri_never_leaks_secret(self, clean_env):
        """End-to-end: reload the module with a broken, credential-bearing
        RATE_LIMIT_STORAGE_URI (the reported PoC value) and confirm the
        secret is absent from both the raised exception's message and
        its fully formatted traceback -- the two places loguru's
        ``@logger.catch`` (web/app.py:main) and the raw interpreter
        traceback would otherwise print it.
        """
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", _LEAKY_URI)

        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        exc = exc_info.value
        assert _SECRET not in str(exc)

        import traceback

        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        assert _SECRET not in formatted

        # `from None` must have severed the exception chain -- otherwise
        # Python's default traceback printing (and loguru's) would still
        # render the ORIGINAL ConfigurationError's message ("... during
        # handling of the above exception ...", secret and all) even
        # though the outer exception's own text is clean.
        assert exc.__cause__ is None
        assert exc.__context__ is None

    def test_malformed_uri_with_raw_at_password_never_leaks_secret(
        self, clean_env
    ):
        """End-to-end raw-"@" variant of the test above: no fragment of a
        multi-"@" password may reach the raised message or the fully
        formatted traceback — the two surfaces loguru's ``@logger.catch``
        and the raw interpreter traceback actually print.
        """
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", _RAW_AT_LEAKY_URI)

        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        exc = exc_info.value
        for fragment in _RAW_AT_SECRET_FRAGMENTS:
            assert fragment not in str(exc)

        import traceback

        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        for fragment in _RAW_AT_SECRET_FRAGMENTS:
            assert fragment not in formatted
        assert "rateuser" not in formatted

        # The credential-bearing ConfigurationError must stay severed.
        assert exc.__cause__ is None
        assert exc.__context__ is None

    def test_malformed_uri_still_fails_fast(self, clean_env):
        """The redaction must not degrade into a silent in-memory
        fallback -- with rate limiting ENABLED, a broken storage URI must
        still prevent the module (and therefore the app) from loading
        successfully.

        Scoped to the enabled case (issue #5964). The wording used to be
        unconditional, but the fail-fast is deliberately NOT unconditional:
        with rate limiting switched off the backend is never contacted, and
        aborting startup over it strands an operator who has explicitly
        turned the subsystem off -- see
        ``TestStorageValidationSkippedWhenDisabled`` below, which pins that
        half. This test is the positive control for it: the guard must keep
        firing wherever the storage backend is actually going to be used.

        ``LDR_DISABLE_RATE_LIMITING=false`` is set explicitly rather than
        left to ``clean_env``'s delenv, so the precondition this test
        depends on is stated in the test instead of inferred from an absent
        variable.
        """
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "false")
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", _LEAKY_URI)
        with pytest.raises(RuntimeError):
            _reload_rate_limit()

    def test_bad_uri_without_credentials_still_redacts_cleanly(self, clean_env):
        """A malformed URI with no userinfo at all must still produce the
        friendly, non-leaking message (nothing to strip, but the scheme
        should still be visible for diagnosis)."""
        clean_env.setenv(
            "RATE_LIMIT_STORAGE_URI", "totally-bogus-scheme://unusable"
        )
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()
        assert "totally-bogus-scheme://unusable" in str(exc_info.value)

    def test_legacy_name_configuration_error_is_wrapped_and_redacted(
        self, clean_env
    ):
        """A ConfigurationError from slowapi's legacy-name fallback is
        wrapped and redacted, not propagated bare.

        This inverts the earlier contract deliberately. The handler used to
        re-raise unchanged when RATE_LIMIT_STORAGE_URI was unset, reasoning
        that a URI this module did not configure had "nothing to redact".
        It has exactly as much to redact: ``limits`` echoes the full URI
        into the exception, which loguru's ``@logger.catch`` then logs and
        the exit traceback prints again. And RATELIMIT_STORAGE_URL is the
        name main documented, so it is the only one an upgrading
        deployment will have set — the redaction was off precisely where
        it was needed.
        """
        import importlib.util

        clean_env.setenv(
            "RATELIMIT_STORAGE_URL",
            "redis://admin:[email protected]:6379",
        )
        if importlib.util.find_spec("redis") is not None:
            pytest.skip(
                "redis client package installed — legacy fallback would "
                "succeed rather than raise in this environment"
            )
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        message = str(exc_info.value)
        assert "hunter2" not in message
        assert "RATELIMIT_STORAGE_URL" in message
        # `from None` severs the chain so the credential-bearing original
        # never renders in the traceback either.
        assert exc_info.value.__cause__ is None
        assert not isinstance(exc_info.value, ConfigurationError)

    def test_legacy_name_raw_at_password_is_wrapped_and_redacted(
        self, clean_env
    ):
        """The same malformed-authority attack through the legacy
        RATELIMIT_STORAGE_URL name: every fragment of a raw-"@"
        password must be absent from the wrapped RuntimeError's message
        and its formatted traceback.

        Unlike the single-"@" legacy test above this needs no
        installed-client skip: the ``rediss-bogus`` scheme is not a
        registered limits storage scheme, so construction fails with
        ConfigurationError regardless of which clients are installed.
        """
        clean_env.setenv(
            "RATELIMIT_STORAGE_URL",
            "rediss-bogus://legacy-admin:Zed@[email protected]:6379",
        )
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        exc = exc_info.value
        for fragment in ("Zed@Marsh", "Zed", "Marsh"):
            assert fragment not in str(exc)
        assert "legacy-admin" not in str(exc)
        assert "RATELIMIT_STORAGE_URL" in str(exc)

        import traceback

        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        for fragment in ("Zed@Marsh", "Zed", "Marsh"):
            assert fragment not in formatted

        # `from None` keeps the original (credential-bearing) exception
        # out of the traceback chain as well.
        assert exc.__cause__ is None
        assert exc.__context__ is None


# ---------------------------------------------------------------------------
# Regression 4 — storage is not resolved at all when rate limiting is OFF
#
# main gated its `validate_rate_limit_storage()` call on
# `if rate_limiting_enabled:` (`web/app_factory.py`), with the reason in a
# comment: 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". Both files that held that logic were deleted by
# a modify/delete conflict in merge 246c74c77 and the exemption went with
# them; the fail-fast survived by accident, because slowapi's
# `Limiter.__init__` resolves storage eagerly.
#
# It resolves it UNCONDITIONALLY: `storage_from_string(...)` runs before and
# independent of `self.enabled` (slowapi/extension.py), and falls back to
# `get_app_config(C.STORAGE_URL, "memory://")` where `C.STORAGE_URL` is
# "RATELIMIT_STORAGE_URL" -- main's variable name, so an upgrading
# deployment's own env var is what blows the process up. `redis` is not a
# dependency of this project, so "URL set, client absent" is the realistic
# configuration, not a contrived one.
#
# The module now forces `storage_uri="memory://"` when disabled, which both
# skips the broken backend and stops the legacy-name fallback being consulted
# at all. See issue #5964.
_URLPARSE_LEAK_CASES = [
    # fullwidth U+FF03 NFKC-normalises to "#": urlparse() raises a bare
    # ValueError quoting the WHOLE netloc, password included.
    ("redis://:Hunter2\uff03xyz@cache:6379/0", ("Hunter2", "xyz")),
    # fullwidth U+FF20 NFKC-normalises to "@"; there is no ASCII "@" at
    # all, so a redactor that does not fold NFKC would emit it unchanged.
    ("redis://:Hunter2\uff20cache:6379/0", ("Hunter2",)),
    # raw "/" ends the netloc early; redis-py's ``.port`` ValueError and
    # limits' ConfigurationError both can quote the URI.
    ("redis://:Hun/ter2@cache:6379/0", ("Hun/ter2", "ter2")),
]


class TestStorageUriParseErrorsNeverLeak:
    """Any Limiter() construction failure -- not only ConfigurationError --
    must surface as a RuntimeError carrying the redacted URI only."""

    @pytest.mark.parametrize(
        "uri,fragments",
        _URLPARSE_LEAK_CASES,
        ids=["fullwidth-hash", "fullwidth-at", "raw-slash"],
    )
    @pytest.mark.parametrize(
        "var", ["RATE_LIMIT_STORAGE_URI", "RATELIMIT_STORAGE_URL"]
    )
    def test_no_leak_anywhere(self, clean_env, uri, fragments, var):
        import traceback

        from loguru import logger

        clean_env.setenv(var, uri)
        captured = []
        sink_id = logger.add(
            lambda m: captured.append(str(m)), diagnose=False, backtrace=True
        )
        try:

            @logger.catch(reraise=False)
            def _boom():
                _reload_rate_limit()

            with pytest.raises(RuntimeError) as exc_info:
                _reload_rate_limit()
            _boom()
        finally:
            logger.remove(sink_id)

        exc = exc_info.value
        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        logged = "".join(captured)
        assert logged, "logger.catch produced no output; test is vacuous"
        for fragment in fragments:
            assert fragment not in str(exc)
            assert fragment not in formatted
            assert fragment not in logged
        assert exc.__cause__ is None
        assert exc.__context__ is None
        assert var in str(exc)

    @pytest.mark.parametrize(
        "uri,fragments",
        _URLPARSE_LEAK_CASES
        + [
            (
                "rediss-bogus://u:SuperSecret@h:6379",
                ("SuperSecret",),
            )
        ],
    )
    def test_uri_supplied_only_via_dotenv_never_leaks(
        self, clean_env, tmp_path, uri, fragments
    ):
        """slowapi reads RATELIMIT_STORAGE_URL from ./.env when it is not
        in os.environ; the handler must still know a URI is in play."""
        import traceback

        (tmp_path / ".env").write_text(f"RATELIMIT_STORAGE_URL={uri}\n")
        clean_env.chdir(tmp_path)
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()
        exc = exc_info.value
        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        for fragment in fragments:
            assert fragment not in str(exc)
            assert fragment not in formatted
        assert "RATELIMIT_STORAGE_URL" in str(exc)
        assert exc.__cause__ is None and exc.__context__ is None

    def test_non_uri_failure_names_real_cause(self, clean_env):
        """A bool-cast error in a stray RATELIMIT_ENABLED is not a URI
        problem and must not be reported as one."""
        clean_env.setenv("RATELIMIT_ENABLED", "yes")
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", "memory://")
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()
        message = str(exc_info.value)
        assert "RATELIMIT_ENABLED" in message
        assert "ValueError" in message
        assert "percent-encode" not in message
        assert exc_info.value.__context__ is None

    def test_memcached_moving_window_reports_real_cause(self, clean_env):
        """limits' MemcachedStorage has no moving-window support, so the
        strategy constructor raises NotImplementedError (simulated here
        so the test does not need pymemcache installed)."""
        import slowapi

        def _raise(self, *args, **kwargs):
            raise NotImplementedError(
                "MovingWindowRateLimiting is not implemented for storage "
                "of type MemcachedStorage"
            )

        clean_env.setattr(slowapi.Limiter, "__init__", _raise)
        clean_env.setenv(
            "RATE_LIMIT_STORAGE_URI", "memcached://u:Pw123456@h:11211"
        )
        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()
        message = str(exc_info.value)
        assert "NotImplementedError" in message
        assert "MovingWindowRateLimiting" in message
        assert "percent-encode" not in message
        assert "Pw123456" not in message

    def test_redactor_folds_nfkc_separators(self):
        rl = _rl()
        assert "Hunter2" not in rl._redact_storage_uri(
            "redis://:Hunter2\uff20cache:6379/0"
        )
        assert "Hunter2" not in rl._redact_storage_uri(
            "redis://cache:6379/0\uff1fpassword=Hunter2"
        )


# ---------------------------------------------------------------------------


class TestStorageValidationSkippedWhenDisabled:
    def test_broken_storage_uri_does_not_abort_startup_when_disabled(
        self, clean_env
    ):
        """LDR_DISABLE_RATE_LIMITING=true + an unusable
        RATE_LIMIT_STORAGE_URI must still import cleanly."""
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", _LEAKY_URI)

        mod = _reload_rate_limit()

        assert mod._RATE_LIMITING_ENABLED is False
        assert mod.limiter.enabled is False
        # Forced onto the backend that cannot fail, rather than the
        # configured one.
        assert mod._limiter_kwargs["storage_uri"] == "memory://"
        assert "memory" in type(mod.limiter._storage).__module__.lower()

    def test_broken_legacy_storage_url_does_not_abort_startup_when_disabled(
        self, clean_env
    ):
        """The same for main's variable name, which slowapi reads straight
        from the environment without this module ever passing it on.

        This is the upgrade path the issue is actually about: the operator
        never set RATE_LIMIT_STORAGE_URI at all, only the RATELIMIT_STORAGE_URL
        that main documented.
        """
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")
        clean_env.setenv(
            "RATELIMIT_STORAGE_URL",
            "rediss-bogus://admin:[email protected]:6379",
        )

        mod = _reload_rate_limit()

        assert mod.limiter.enabled is False
        assert mod._limiter_kwargs["storage_uri"] == "memory://"
        assert "memory" in type(mod.limiter._storage).__module__.lower()

    def test_legacy_flag_spelling_also_exempts(self, clean_env):
        """The unprefixed legacy DISABLE_RATE_LIMITING is resolved by the
        same `is_rate_limiting_enabled()` helper, so it must exempt too --
        the exemption is keyed off the resolved flag, not off one spelling
        of the env var."""
        clean_env.setenv("DISABLE_RATE_LIMITING", "true")
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", _LEAKY_URI)

        mod = _reload_rate_limit()

        assert mod.limiter.enabled is False
        assert mod._limiter_kwargs["storage_uri"] == "memory://"

    def test_disabled_without_any_storage_uri_is_unchanged(self, clean_env):
        """Control: disabling rate limiting with no storage URI configured
        still yields in-memory storage and a working import."""
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")

        mod = _reload_rate_limit()

        assert mod.limiter.enabled is False
        assert "memory" in type(mod.limiter._storage).__module__.lower()

    def test_enabled_still_uses_the_configured_backend(self, clean_env):
        """Negative control for the forced override: with rate limiting ON,
        the operator's own storage URI must still be the one used.

        Without this, `storage_uri = "memory://"` applied unconditionally
        would pass every test above while silently dropping the shared Redis
        backend that multi-worker deploys rely on.
        """
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "false")
        clean_env.setenv("RATE_LIMIT_STORAGE_URI", "memory://explicit")

        mod = _reload_rate_limit()

        assert mod.limiter.enabled is True
        assert mod._limiter_kwargs["storage_uri"] == "memory://explicit"


# ---------------------------------------------------------------------------
# The legacy RATELIMIT_STORAGE_URL ./.env lookup must never abort the import
#
# The module looks the legacy key up in ./.env itself (as slowapi does), at
# import, whether or not rate limiting is enabled. Whatever goes wrong with
# that lookup has to degrade to "no .env", as slowapi's own
# ``os.path.isfile(".env")`` does -- not stop the server from starting.
# ---------------------------------------------------------------------------


class TestLegacyDotenvLookupFailsSafe:
    @pytest.mark.parametrize("disabled", ["true", "false"])
    def test_unsearchable_working_directory_does_not_abort_import(
        self, clean_env, disabled
    ):
        """On CPython 3.12 ``Path(".env").is_file()`` re-raises EACCES when
        the working directory is not searchable (e.g. ``sudo -u ldr ldr-web``
        from /root); ``os.path.isfile`` returns False there. Emulate the
        3.12 behaviour so the test bites on every interpreter."""
        import pathlib

        original_is_file = pathlib.Path.is_file

        denied_paths = []

        def _is_file(self, *args, **kwargs):
            if self.name == ".env":
                denied_paths.append(self)
                raise PermissionError(13, "Permission denied", str(self))
            return original_is_file(self, *args, **kwargs)

        clean_env.setattr(pathlib.Path, "is_file", _is_file)
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", disabled)

        mod = _reload_rate_limit()

        assert denied_paths, "The test must inject a .env permission failure"
        assert mod._LEGACY_STORAGE_URI == ""
        assert mod._LEGACY_STORAGE_SOURCE == ""
        assert mod.limiter.enabled is (disabled == "false")

    def test_unreadable_dotenv_is_logged_at_debug_without_contents(
        self, clean_env, tmp_path, loguru_caplog
    ):
        """A ./.env the lookup cannot parse is treated as absent and noted
        at DEBUG by exception type only -- the exception text can quote
        the file, and the file can hold a credential."""
        import starlette.config

        def _unreadable(*args, **kwargs):
            raise ValueError("line 1: RATELIMIT_STORAGE_URL=redis://:Hunter2@h")

        (tmp_path / ".env").write_text(
            "RATELIMIT_STORAGE_URL=redis://:Hunter2@h:6379/0\n"
        )
        clean_env.chdir(tmp_path)
        clean_env.setattr(starlette.config, "Config", _unreadable)
        clean_env.setenv("LDR_DISABLE_RATE_LIMITING", "true")

        with loguru_caplog.at_level("DEBUG"):
            mod = _reload_rate_limit()

        assert mod._LEGACY_STORAGE_URI == ""
        debug_lines = [
            r.getMessage()
            for r in loguru_caplog.records
            if r.levelname == "DEBUG" and ".env" in r.getMessage()
        ]
        assert debug_lines, "the unreadable .env was swallowed silently"
        assert any("ValueError" in line for line in debug_lines)
        assert "Hunter2" not in loguru_caplog.text

    def test_failed_lookup_still_redacts_slowapi_unknown_scheme_error(
        self, clean_env, tmp_path
    ):
        """Fail closed: if this module's own .env lookup fails while
        slowapi still reads an unknown-scheme URI from the same file,
        limits' ``unknown storage scheme : <uri>`` text must not reach the
        startup error unredacted."""
        import traceback

        import starlette.config

        def _unreadable(*args, **kwargs):
            raise OSError("simulated")

        (tmp_path / ".env").write_text(
            "RATELIMIT_STORAGE_URL=bogus://u:Pw0rdX9@h:6379\n"
        )
        clean_env.chdir(tmp_path)
        # Only the module's lookup sees the failure: slowapi bound its own
        # ``Config`` when it was imported.
        clean_env.setattr(starlette.config, "Config", _unreadable)

        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        exc = exc_info.value
        formatted = "".join(
            traceback.format_exception(type(exc), exc, exc.__traceback__)
        )
        assert "Pw0rdX9" not in str(exc)
        assert "Pw0rdX9" not in formatted
        assert exc.__cause__ is None and exc.__context__ is None


# ---------------------------------------------------------------------------
# Regression 5 — TRUST_PROXY_HEADERS + LDR_SERVER_TRUSTED_PROXY_COUNT is a
# TRI-STATE check, not a blanket hard-fail
#
# Round 4 escalated the combination of TRUST_PROXY_HEADERS=true and
# LDR_SERVER_TRUSTED_PROXY_COUNT>0 to an unconditional import-time
# RuntimeError. That over-corrected: the actual poisoning source is
# uvicorn's `--forwarded-allow-ips`, which `web/app.py` used to hardcode to
# "*" whenever TRUST_PROXY_HEADERS was set. With "*", uvicorn's
# ProxyHeadersMiddleware rewrites `scope["client"]` from the client-forgeable
# left-most X-Forwarded-For entry for ANY direct peer -- but uvicorn's own
# contract only does that rewrite for peers matching `--forwarded-allow-ips`.
# `LDR_SERVER_FORWARDED_ALLOW_IPS` (new; read identically by both
# `web/app.py` and this module) makes that allow-list configurable, so a
# RESTRICTED value (the proxy's real IP) makes the combination safe: an
# attacker connecting directly to LDR is not in the allow-list and keeps
# their real socket peer. Only "*" (the backward-compatible default) with
# both settings on remains unconditionally exploitable and hard-fails.
#
# Round 7 (commit f5db1daab) hoisted this guard to fire on
# `TRUST_PROXY_HEADERS` alone, independent of `LDR_SERVER_TRUSTED_PROXY_
# COUNT` -- which was correct for the rate-limiter's own trust logic (see
# ``_get_client_ip``) but, applied to the *startup guard*, meant EVERY
# pre-existing deployment that only ever set `TRUST_PROXY_HEADERS=true`
# (the original, documented opt-in that predates this PR) started hitting
# `RuntimeError` at import time after a plain upgrade -- with no config
# change on the operator's part, and no way to know they needed one until
# the server refused to start. This round restores the RuntimeError to an
# OPT-IN condition: it fires only when the operator has ALSO adopted one of
# this PR's new settings (`LDR_SERVER_TRUSTED_PROXY_COUNT > 0`, or an
# explicitly-set `LDR_SERVER_FORWARDED_ALLOW_IPS`, checked on the value the
# operator wrote having at least one non-empty entry, so an explicit `="*"`
# still counts and an empty `=""` or `=","` does not) while
# `LDR_SERVER_FORWARDED_ALLOW_IPS` still resolves to "*". The legacy,
# not-opted-in case now boots with a `logger.warning` instead of raising --
# see ``TestLegacyTrustProxyHeadersWarns`` below.
# ---------------------------------------------------------------------------


@pytest.fixture()
def clean_proxy_trust_env(monkeypatch):
    """Isolate the three env vars the tri-state check reads, on top of
    ``clean_env``'s rate-limiting/storage isolation -- CI or a dev shell
    could plausibly have any of ``TRUST_PROXY_HEADERS``,
    ``LDR_SERVER_TRUSTED_PROXY_COUNT``, or ``LDR_SERVER_FORWARDED_ALLOW_IPS``
    exported ambiently, and every case below depends on starting from a
    known-clean baseline.
    """
    for name in (
        "LDR_DISABLE_RATE_LIMITING",
        "DISABLE_RATE_LIMITING",
        "RATE_LIMIT_STORAGE_URI",
        "TRUST_PROXY_HEADERS",
        "LDR_SERVER_TRUSTED_PROXY_COUNT",
        "LDR_SERVER_FORWARDED_ALLOW_IPS",
    ):
        monkeypatch.delenv(name, raising=False)
    return monkeypatch


class TestProxyTrustComboGuard:
    def test_combo_with_default_wildcard_allow_ips_hard_fails(
        self, clean_proxy_trust_env
    ):
        """TRUST_PROXY_HEADERS=true + LDR_SERVER_TRUSTED_PROXY_COUNT>0, with
        LDR_SERVER_FORWARDED_ALLOW_IPS left unset (defaults to "*"): the
        genuinely poisoned configuration -- uvicorn trusts forwarded headers
        from literally any direct peer, so hop-count mode's
        fallback-to-direct-peer safety net is defeated for every request.
        Must still hard-fail at import time.
        """
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        clean_proxy_trust_env.setenv("LDR_SERVER_TRUSTED_PROXY_COUNT", "1")

        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        message = str(exc_info.value)
        assert "LDR_SERVER_FORWARDED_ALLOW_IPS" in message
        assert "UNSAFE" in message

    def test_combo_with_explicit_wildcard_allow_ips_hard_fails(
        self, clean_proxy_trust_env
    ):
        """Same as above but with LDR_SERVER_FORWARDED_ALLOW_IPS explicitly
        set to "*" rather than left unset -- an operator who copies the
        wildcard value in by hand must get the identical hard-fail, not a
        pass-through because the var happened to be present."""
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        clean_proxy_trust_env.setenv("LDR_SERVER_TRUSTED_PROXY_COUNT", "2")
        clean_proxy_trust_env.setenv("LDR_SERVER_FORWARDED_ALLOW_IPS", "*")

        with pytest.raises(RuntimeError):
            _reload_rate_limit()

    def test_explicit_wildcard_allow_ips_alone_hard_fails(
        self, clean_proxy_trust_env
    ):
        """Explicitly setting LDR_SERVER_FORWARDED_ALLOW_IPS="*" is itself
        an opt-in to this PR's new settings, even with
        LDR_SERVER_TRUSTED_PROXY_COUNT left unset/0 -- an operator who
        writes the wildcard in by hand has made a deliberate, unsafe
        choice and must not be let through only because the count-based
        opt-in wasn't used. Distinguished from the default/unset case
        (which only warns) by whether the value the operator wrote has any
        non-empty entry, not by the resolved value -- both resolve to the
        same string "*". An empty value ("" or ",") does not count as
        explicit."""
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        clean_proxy_trust_env.setenv("LDR_SERVER_FORWARDED_ALLOW_IPS", "*")

        with pytest.raises(RuntimeError) as exc_info:
            _reload_rate_limit()

        message = str(exc_info.value)
        assert "LDR_SERVER_FORWARDED_ALLOW_IPS" in message
        assert "UNSAFE" in message

    def test_restricted_allow_ips_alone_boots_cleanly(
        self, clean_proxy_trust_env
    ):
        """A restricted LDR_SERVER_FORWARDED_ALLOW_IPS with
        LDR_SERVER_TRUSTED_PROXY_COUNT left unset/0 -- the plain
        TRUST_PROXY_HEADERS=true + HTTPS-detection deployment, no
        multi-hop hop-counting involved -- boots cleanly. Not exploitable:
        uvicorn only rewrites request.client for a direct peer matching
        the restricted allow-list."""
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        clean_proxy_trust_env.setenv(
            "LDR_SERVER_FORWARDED_ALLOW_IPS", "10.0.0.5"
        )

        mod = _reload_rate_limit()

        assert mod._TRUSTED_PROXY_COUNT == 0
        assert mod._TRUST_PROXY_HEADERS is True
        assert mod._FORWARDED_ALLOW_IPS == "10.0.0.5"
        assert mod._PROXY_TRUST_OPTED_IN is True

    def test_combo_with_restricted_allow_ips_boots_cleanly(
        self, clean_proxy_trust_env
    ):
        """The fix: the same combination boots without error once
        LDR_SERVER_FORWARDED_ALLOW_IPS names the actual trusted proxy
        IP(s). This is now the RECOMMENDED HTTPS + hop-count deployment
        (docs/deployment/reverse-proxy.md)."""
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        clean_proxy_trust_env.setenv("LDR_SERVER_TRUSTED_PROXY_COUNT", "1")
        clean_proxy_trust_env.setenv(
            "LDR_SERVER_FORWARDED_ALLOW_IPS", "10.0.0.5"
        )

        mod = _reload_rate_limit()

        assert mod._TRUSTED_PROXY_COUNT == 1
        assert mod._TRUST_PROXY_HEADERS is True
        assert mod._FORWARDED_ALLOW_IPS == "10.0.0.5"

    def test_hop_count_alone_without_trust_proxy_headers_boots_cleanly(
        self, clean_proxy_trust_env
    ):
        """Negative control: LDR_SERVER_TRUSTED_PROXY_COUNT>0 by itself
        (TRUST_PROXY_HEADERS unset) never touches this guard at all -- it
        doesn't reach uvicorn's proxy-headers middleware, so there is
        nothing to poison."""
        clean_proxy_trust_env.setenv("LDR_SERVER_TRUSTED_PROXY_COUNT", "1")

        mod = _reload_rate_limit()

        assert mod._TRUSTED_PROXY_COUNT == 1
        assert mod._TRUST_PROXY_HEADERS is False

    def test_trust_proxy_headers_alone_without_hop_count_boots_cleanly(
        self, clean_proxy_trust_env, loguru_caplog_full
    ):
        """The legacy config -- TRUST_PROXY_HEADERS=true set by itself, hop
        count and LDR_SERVER_FORWARDED_ALLOW_IPS both left untouched --
        predates this PR and MUST keep booting on a plain upgrade (this is
        the defect round 7 introduced: it started raising RuntimeError
        here). It boots with a startup warning instead of the RuntimeError,
        since the operator has not opted into either of this PR's new
        settings and is not, therefore, relying on this PR's guarantees.
        """
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")

        with loguru_caplog_full.at_level("WARNING"):
            mod = _reload_rate_limit()

        assert mod._TRUSTED_PROXY_COUNT == 0
        assert mod._TRUST_PROXY_HEADERS is True
        assert mod._FORWARDED_ALLOW_IPS == "*"
        assert mod._PROXY_TRUST_OPTED_IN is False
        assert "TRUST_PROXY_HEADERS=true" in loguru_caplog_full.text
        assert "SPOOFABLE" in loguru_caplog_full.text
        assert "LDR_SERVER_FORWARDED_ALLOW_IPS" in loguru_caplog_full.text

    def test_default_forwarded_allow_ips_is_wildcard(
        self, clean_proxy_trust_env
    ):
        """Pin the backward-compatible default so a change to it is caught
        here rather than only showing up as a startup failure in an
        unrelated test."""
        mod = _reload_rate_limit()

        assert mod._FORWARDED_ALLOW_IPS == "*"


_PROXY_STARTUP_ENV_VARS = (
    "LDR_DISABLE_RATE_LIMITING",
    "DISABLE_RATE_LIMITING",
    "RATE_LIMIT_STORAGE_URI",
    "RATELIMIT_STORAGE_URL",
    "TRUST_PROXY_HEADERS",
    "LDR_SERVER_TRUSTED_PROXY_COUNT",
    "LDR_SERVER_FORWARDED_ALLOW_IPS",
)


class TestProxyStartupLinesReachAnExternalAsgiServer:
    """An external ASGI server must still show the forwarding-trust lines.

    ``local_deep_research/__init__.py`` disables the package's loguru
    namespace and only ``config_logger()`` (run by ``ldr-web``) enables
    it again, so ``uvicorn local_deep_research.web.fastapi_app:app``
    used to import this module with these warnings silently dropped,
    including the one the changelog and docs promise for a bare
    ``TRUST_PROXY_HEADERS=true``.
    """

    @pytest.mark.timeout(300)
    @pytest.mark.parametrize(
        ("env", "expected"),
        [
            ({"TRUST_PROXY_HEADERS": "true"}, "SPOOFABLE configuration"),
            (
                {"LDR_SERVER_FORWARDED_ALLOW_IPS": "10.0.0.0/8"},
                "trusts every peer inside",
            ),
            ({"LDR_SERVER_FORWARDED_ALLOW_IPS": "nginx"}, "matches no peer"),
        ],
        ids=["legacy-trust-proxy-headers", "broad-entry", "opaque-literal"],
    )
    def test_warning_reaches_stderr_while_package_logging_is_off(
        self, tmp_path, env, expected
    ):
        """A fresh interpreter, exactly as an ASGI server imports the app:
        nothing has enabled the package's logging."""
        child_env = {
            name: value
            for name, value in os.environ.items()
            if name not in _PROXY_STARTUP_ENV_VARS
        }
        child_env.update(env)
        child_env["LDR_DATA_DIR"] = str(tmp_path)

        result = subprocess.run(
            [
                sys.executable,
                "-c",
                "import local_deep_research.web.dependencies.rate_limit",
            ],
            capture_output=True,
            text=True,
            env=child_env,
            timeout=240,
        )

        assert result.returncode == 0, result.stderr
        assert result.stderr.count(expected) == 1, result.stderr

    def test_line_is_not_repeated_on_stderr_when_loguru_delivers_it(
        self, clean_proxy_trust_env, loguru_caplog
    ):
        """With logging enabled (``ldr-web``) loguru alone emits the line."""
        clean_proxy_trust_env.setenv("TRUST_PROXY_HEADERS", "true")
        stderr = io.StringIO()
        clean_proxy_trust_env.setattr(sys, "stderr", stderr)

        with loguru_caplog.at_level("WARNING"):
            _reload_rate_limit()

        assert loguru_caplog.text.count("SPOOFABLE configuration") == 1
        assert "SPOOFABLE" not in stderr.getvalue()