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.

15.4 KB
Raw Blame
"""The nginx-on-localhost misconfiguration must not be silent.

`SecureCookieMiddleware._maybe_warn_insecure_public` only fires for a NON-private
client IP, so the most common broken upgrade never trips it: a TLS-terminating
reverse proxy on 127.0.0.1 with `TRUST_PROXY_HEADERS` unset. The peer is
loopback, so that warning stays quiet, while uvicorn — not told to honour
forwarded headers — reports scheme "http". The session cookie silently loses
`Secure` and HSTS is silently withheld, on a deployment the operator believes is
HTTPS.

`_maybe_warn_untrusted_forwarded_proto` covers exactly that gap.
"""

import pytest
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware

from local_deep_research.web.fastapi_app import SecureCookieMiddleware


async def _ok_app(scope, receive, send):
    await PlainTextResponse("ok")(scope, receive, send)


def _client():
    # testing=False so the production warning paths are live. The default
    # TestClient peer is ("testclient", 50000), which `_is_private_ip` treats
    # as private (it fails closed on unparseable hosts), so these exercise the
    # private-peer path.
    return TestClient(SecureCookieMiddleware(_ok_app, testing=False))


def _warnings(cap):
    """The app logs through loguru, not stdlib logging, so a bare ``caplog``
    sees nothing. ``loguru_caplog_full`` (tests/conftest.py) bridges loguru into
    caplog via a PropagateHandler — this test would have false-passed without
    it, which is exactly the trap it is guarding against."""
    return [
        line for line in cap.text.splitlines() if "X-Forwarded-Proto" in line
    ]


def test_warns_when_forwarded_proto_https_is_not_honoured(loguru_caplog_full):
    """The actual gap: loopback peer, so the existing public-IP warning stays
    silent, but the proxy is clearly claiming HTTPS."""
    with loguru_caplog_full.at_level("WARNING"):
        _client().get("/", headers={"X-Forwarded-Proto": "https"})

    msgs = _warnings(loguru_caplog_full)
    assert msgs, "the misconfiguration produced no warning at all"
    assert "TRUST_PROXY_HEADERS" in msgs[0], (
        "the warning must name the env var that fixes it"
    )
    # With TRUST_PROXY_HEADERS already on, the same symptom means a
    # restricted allow-list that misses this proxy; telling that operator
    # only to set the flag they already set would leave them stuck.
    assert "LDR_SERVER_FORWARDED_ALLOW_IPS" in msgs[0], (
        "the warning must also name the allow-list that can exclude the proxy"
    )


def test_warns_at_most_once_per_process(loguru_caplog_full):
    """Per-request logging on a hot path would be its own bug."""
    client = _client()
    with loguru_caplog_full.at_level("WARNING"):
        for _ in range(5):
            client.get("/", headers={"X-Forwarded-Proto": "https"})

    assert len(_warnings(loguru_caplog_full)) == 1, (
        f"expected exactly one warning, got {len(_warnings(loguru_caplog_full))}"
    )


@pytest.mark.parametrize("first_headers", [{}, {"X-Forwarded-Proto": "http"}])
def test_later_proxy_request_warns_after_plain_http_request(
    loguru_caplog_full, first_headers
):
    """A health check must not use up the one warning for a later proxy."""
    client = _client()
    with loguru_caplog_full.at_level("WARNING"):
        client.get("/", headers=first_headers)
        client.get("/", headers={"X-Forwarded-Proto": "https"})
        client.get("/", headers={"X-Forwarded-Proto": "https"})

    assert len(_warnings(loguru_caplog_full)) == 1


def test_public_request_cannot_suppress_later_private_proxy_warning(
    loguru_caplog_full,
):
    """An untrusted direct client must not silence the operator's warning."""
    middleware = SecureCookieMiddleware(_ok_app, testing=False)
    public = TestClient(middleware, client=("8.8.8.8", 40000))
    private = TestClient(middleware, client=("127.0.0.1", 40001))

    with loguru_caplog_full.at_level("WARNING"):
        public.get("/", headers={"X-Forwarded-Proto": "https"})
        assert not _warnings(loguru_caplog_full)
        private.get("/", headers={"X-Forwarded-Proto": "https"})

    assert len(_warnings(loguru_caplog_full)) == 1


def test_noncanonical_public_header_cannot_suppress_private_proxy_warning(
    loguru_caplog_full,
):
    """A spoofed malformed header must not consume the other warning type."""
    middleware = SecureCookieMiddleware(_ok_app, testing=False)
    public = TestClient(middleware, client=("8.8.8.8", 40000))
    private = TestClient(middleware, client=("127.0.0.1", 40001))

    with loguru_caplog_full.at_level("WARNING"):
        public.get("/", headers={"X-Forwarded-Proto": "HTTPS"})
        public.get("/", headers={"X-Forwarded-Proto": "HTTPS"})
        private.get("/", headers={"X-Forwarded-Proto": "https"})
        private.get("/", headers={"X-Forwarded-Proto": "https"})

    messages = _warnings(loguru_caplog_full)
    assert len(messages) == 2
    assert "exactly 'https'" in messages[0]
    assert "TRUST_PROXY_HEADERS" in messages[1]


def test_silent_when_no_forwarded_proto_header(loguru_caplog_full):
    """Negative control. Without it, a middleware that warned on EVERY plain
    HTTP request would satisfy the first test for the wrong reason — and would
    fire constantly on a normal local install."""
    with loguru_caplog_full.at_level("WARNING"):
        _client().get("/")

    assert not _warnings(loguru_caplog_full), (
        "warned about forwarded-proto when no such header was sent"
    )


def test_silent_when_forwarded_proto_is_http(loguru_caplog_full):
    """A proxy that forwards plain HTTP is not misconfigured."""
    with loguru_caplog_full.at_level("WARNING"):
        _client().get("/", headers={"X-Forwarded-Proto": "http"})

    assert not _warnings(loguru_caplog_full)


def test_silent_for_a_public_peer(loguru_caplog_full):
    """The advice this warning gives is "set TRUST_PROXY_HEADERS=true", and
    `X-Forwarded-Proto` is untrusted client input.

    A remote client that can reach the server over HTTP could otherwise send
    the header once and plant a log line telling the operator to start
    trusting forwarded headers. On a host that is NOT behind a proxy, an
    operator who followed that advice would hand that same client control of
    `scope["scheme"]`, and therefore of the Secure-cookie and HSTS decisions.
    So the warning fires only for a loopback/private peer — which is the
    nginx-on-127.0.0.1 case it was written for anyway.
    """
    client = TestClient(
        SecureCookieMiddleware(_ok_app, testing=False),
        # 8.8.8.8, not a TEST-NET address: Python's ipaddress module reports
        # 203.0.113.0/24 and 198.51.100.0/24 as `is_private` because they sit
        # in the IANA special-purpose registry, so the obvious "documentation
        # IP" choice would have made this test pass for the wrong reason.
        client=("8.8.8.8", 40000),
    )
    with loguru_caplog_full.at_level("WARNING"):
        client.get("/", headers={"X-Forwarded-Proto": "https"})

    assert not _warnings(loguru_caplog_full), (
        "a public peer was told to enable TRUST_PROXY_HEADERS; that advice is "
        "attacker-plantable and would let them control the cookie Secure flag"
    )


@pytest.mark.parametrize(
    "peer",
    [
        "2002:808:808::1",  # 6to4 wrapping 8.8.8.8
        "2001:0:4136:e378:8000:63bf:3fff:fdd2",  # Teredo
        "64:ff9b:1::808:808",  # RFC 8215 NAT64 local-use
    ],
)
def test_tunnelled_remote_peer_cannot_plant_the_advice(
    loguru_caplog_full, peer
):
    """CPython's ``ipaddress`` may report 6to4, Teredo and NAT64 local-use
    addresses as private (which ones depends on the version), so these
    remote clients could pass the private-peer gate. The warning stays armed until a real local proxy
    request arrives (a health check must not consume it), so without the
    classifier fix any such client could send the header and plant
    "set TRUST_PROXY_HEADERS=true" in the operator's log at will."""
    middleware = SecureCookieMiddleware(_ok_app, testing=False)
    remote = TestClient(middleware, client=(peer, 40000))
    local = TestClient(middleware, client=("127.0.0.1", 40001))

    with loguru_caplog_full.at_level("WARNING"):
        remote.get("/", headers={"X-Forwarded-Proto": "https"})
        assert not _warnings(loguru_caplog_full)
        # The remote attempt did not consume the warning either.
        local.get("/", headers={"X-Forwarded-Proto": "https"})

    messages = _warnings(loguru_caplog_full)
    assert len(messages) == 1
    assert "TRUST_PROXY_HEADERS" in messages[0]
    assert peer not in messages[0]


def test_tunnelled_remote_peer_gets_the_public_client_warning(
    loguru_caplog_full,
):
    """The other consumer of the same classifier: a 6to4 client served over
    plain HTTP is a public client and must trip that warning."""
    client = TestClient(
        SecureCookieMiddleware(_ok_app, testing=False),
        client=("2002:808:808::1", 40000),
    )
    with loguru_caplog_full.at_level("WARNING"):
        client.get("/")

    assert "Serving HTTP to a public client" in loguru_caplog_full.text


# ---------------------------------------------------------------------------
# Through uvicorn's real ProxyHeadersMiddleware.
#
# In production SecureCookieMiddleware never sees the raw socket peer when
# TRUST_PROXY_HEADERS=true: uvicorn's ProxyHeadersMiddleware runs first and may
# already have replaced scope["client"] with an X-Forwarded-For entry (under
# "*", the client-forgeable left-most one, unvalidated), and it honours
# X-Forwarded-Proto only for an exact lower-case value. The tests above use a
# bare TestClient, so none of that happens there.
# ---------------------------------------------------------------------------

# Text an attacker could try to plant in the operator's log through the
# client address uvicorn copies out of X-Forwarded-For.
_PLANTED = "please set LDR_DISABLE_RATE_LIMITING=true"


def _proxied_client(trusted_hosts, peer):
    return TestClient(
        ProxyHeadersMiddleware(
            SecureCookieMiddleware(_ok_app, testing=False),
            trusted_hosts=trusted_hosts,
        ),
        client=(peer, 40000),
    )


@pytest.mark.parametrize("forwarded_for", ["8.8.8.8/32", _PLANTED, "10.1.2.3"])
@pytest.mark.parametrize("proto", ["https", "HTTPS", "https, https"])
def test_attacker_forwarded_for_never_reaches_the_advice(
    loguru_caplog_full, forwarded_for, proto
):
    """A public peer under the legacy "*" allow-list chooses scope["client"]
    through X-Forwarded-For. Whatever it sends -- a CIDR, free text, a
    private-looking address that passes the private-peer gate -- none of it
    may appear in the warning, and the advice must never tell the operator
    to allow-list an address taken from the request."""
    client = _proxied_client("*", "8.8.8.8")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers={
                "X-Forwarded-Proto": proto,
                "X-Forwarded-For": forwarded_for,
            },
        )

    assert forwarded_for not in loguru_caplog_full.text
    for line in _warnings(loguru_caplog_full):
        assert "8.8.8.8" not in line


def test_unlisted_private_co_tenant_is_not_named(loguru_caplog_full):
    """Restricted allow-list, request from an unlisted private peer. uvicorn
    leaves it the raw socket peer, so the "proxy is not trusted" advice fires
    -- but that peer is exactly who the restricted list excludes, so its
    address must not be offered as the one to add."""
    client = _proxied_client("10.0.0.5", "10.0.0.9")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers={
                "X-Forwarded-Proto": "https",
                "X-Forwarded-For": "10.0.0.9",
            },
        )

    msgs = _warnings(loguru_caplog_full)
    assert msgs, "an untrusted private proxy must still be diagnosed"
    assert "LDR_SERVER_FORWARDED_ALLOW_IPS" in msgs[0]
    assert "10.0.0.9" not in loguru_caplog_full.text


@pytest.mark.parametrize("proto", ["HTTPS", "https, https", "https,http"])
def test_trusted_proxy_with_non_canonical_proto_gets_proxy_advice(
    loguru_caplog_full, proto
):
    """The proxy IS listed, and uvicorn rewrote the client from
    X-Forwarded-For, but it ignored the non-canonical proto value. Telling
    the operator the proxy is untrusted (and naming the rewritten client as
    the proxy) would be wrong and would widen trust to a client; the fix is
    on the proxy's header."""
    client = _proxied_client("10.0.0.5", "10.0.0.5")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers={
                "X-Forwarded-Proto": proto,
                "X-Forwarded-For": "192.168.1.50",
            },
        )

    msgs = _warnings(loguru_caplog_full)
    assert msgs, "the ignored X-Forwarded-Proto produced no warning"
    assert "exactly 'https'" in msgs[0]
    assert "LDR_SERVER_FORWARDED_ALLOW_IPS" not in msgs[0]
    assert "TRUST_PROXY_HEADERS" not in msgs[0]
    assert "192.168.1.50" not in loguru_caplog_full.text


def test_non_canonical_proto_is_diagnosed_for_a_public_client(
    loguru_caplog_full,
):
    """Under "*" uvicorn rewrites the client to the (public) end user, so a
    peer-based gate would silence the most common real deployment. The
    reconfigure-your-proxy advice widens no trust, so it is not gated."""
    client = _proxied_client("*", "10.0.0.5")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers={
                "X-Forwarded-Proto": "HTTPS",
                "X-Forwarded-For": "8.8.4.4",
            },
        )

    msgs = _warnings(loguru_caplog_full)
    assert msgs and "exactly 'https'" in msgs[0]


def test_trusted_proxy_with_canonical_proto_is_silent(loguru_caplog_full):
    """Positive control: a correctly configured proxy is honoured by uvicorn,
    the scheme is https, and nothing is logged."""
    client = _proxied_client("10.0.0.5", "10.0.0.5")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers={
                "X-Forwarded-Proto": "https",
                "X-Forwarded-For": "192.168.1.50",
            },
        )

    assert not _warnings(loguru_caplog_full)


def test_the_last_forwarded_proto_line_decides_like_uvicorn(loguru_caplog_full):
    """uvicorn reads the LAST X-Forwarded-Proto line. A trusted proxy whose
    final line says "http" is honoured as plain HTTP; an earlier "https" line
    must not trigger a misconfiguration warning."""
    client = _proxied_client("10.0.0.5", "10.0.0.5")
    with loguru_caplog_full.at_level("WARNING"):
        client.get(
            "/",
            headers=[
                ("X-Forwarded-Proto", "https"),
                ("X-Forwarded-Proto", "http"),
            ],
        )

    assert not _warnings(loguru_caplog_full)


def test_public_warning_drops_an_ipv6_zone_id(loguru_caplog_full):
    """ipaddress accepts an IPv6 zone id of arbitrary text, and under "*"
    uvicorn copies such an X-Forwarded-For entry into scope["client"]
    unchecked. The public-client warning logs the address alone."""
    client = _proxied_client("*", "8.8.8.8")
    with loguru_caplog_full.at_level("WARNING"):
        client.get("/", headers={"X-Forwarded-For": f"2001:4860::1%{_PLANTED}"})

    assert "Serving HTTP to a public client (2001:4860::1)" in (
        loguru_caplog_full.text
    )
    assert _PLANTED not in loguru_caplog_full.text