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.

8.0 KB
Raw Blame
# allow: no-sut-import — drives the real ASGI middleware classes directly
"""Header-rewriting edge cases recovered from main's deleted
``tests/web/test_app_factory_middleware.py``.

Both middlewares REWRITE the outbound header list. main tested the rewrite
against a hostile-ish header set; the FastAPI successors
(``test_secure_cookie_middleware.py``, ``test_security_headers.py``) test the
main decision each middleware makes but always with a single, well-behaved
``Set-Cookie``/``Server`` header, so the rewrite loops themselves are only
covered on their happy shape.

Specifically uncovered on this branch, and pinned here:

* ``SecureCookieMiddleware`` must not append a SECOND ``; Secure`` to a
  cookie that already carries one — the guard at ``fastapi_app.py`` reads
  ``if "; Secure" not in v_str and "; secure" not in v_str``. Nothing else
  on the branch exercises it. A duplicated attribute makes the cookie
  malformed, and a browser that rejects it is an unbreakable login loop —
  the same failure class as #3849, from the other direction.
* ``SecureCookieMiddleware`` must leave NON-cookie headers untouched while
  rewriting; the loop rebuilds the whole list, so a bug there silently
  drops or corrupts unrelated headers.
* ``SecurityHeadersMiddleware`` must strip ``Server`` regardless of the
  CASE the inner app used. The existing successor only ever sends
  lowercase ``b"server"``, which passes even if ``.lower()`` were dropped
  from the comparison — and uvicorn itself emits ``Server`` title-cased.
"""

from __future__ import annotations

import asyncio

from local_deep_research.web.fastapi_app import (
    SecureCookieMiddleware,
    SecurityHeadersMiddleware,
)


def _drive(build_middleware, scope_overrides, response_headers):
    """Run one request through the middleware and return outbound headers.

    ``build_middleware`` receives the inner ASGI app and returns the
    middleware instance wrapping it. Returns a list of
    ``(name_bytes, value_bytes)`` exactly as the wrapped app would send them.
    """

    async def inner(scope, receive, send):
        await send(
            {
                "type": "http.response.start",
                "status": 200,
                "headers": list(response_headers),
            }
        )
        await send({"type": "http.response.body", "body": b"ok"})

    middleware = build_middleware(inner)

    async def run():
        scope = {
            "type": "http",
            "scheme": "http",
            "path": "/",
            "client": ("127.0.0.1", 12345),
            "headers": [],
        }
        scope.update(scope_overrides)
        sent = []

        async def send(message):
            sent.append(message)

        async def receive():
            return {"type": "http.request", "body": b"", "more_body": False}

        await middleware(scope, receive, send)
        start = next(
            message
            for message in sent
            if message["type"] == "http.response.start"
        )
        return list(start["headers"])

    return asyncio.run(run())


def _values(headers, name: bytes):
    return [v for k, v in headers if k.lower() == name]


class TestSecureCookieRewrite:
    """``SecureCookieMiddleware.__call__``'s ``send_wrapper``."""

    def test_an_already_secure_cookie_does_not_get_a_second_flag(self):
        """The idempotence guard. Without it the cookie goes out as
        ``...; Secure; Secure``."""
        headers = _drive(
            lambda inner: SecureCookieMiddleware(inner, testing=False),
            {"scheme": "https"},
            [(b"set-cookie", b"session=abc; Path=/; HttpOnly; Secure")],
        )

        cookie = _values(headers, b"set-cookie")[0].decode("latin-1")
        assert cookie.count("Secure") == 1, cookie
        assert cookie == "session=abc; Path=/; HttpOnly; Secure"

    def test_a_lowercase_secure_attribute_also_counts_as_present(self):
        """``Secure`` is case-insensitive per RFC6265, and the guard checks
        both spellings. Dropping the ``"; secure"`` half of the condition
        appends a duplicate to any cookie written in lower case."""
        headers = _drive(
            lambda inner: SecureCookieMiddleware(inner, testing=False),
            {"scheme": "https"},
            [(b"set-cookie", b"session=abc; Path=/; httponly; secure")],
        )

        cookie = _values(headers, b"set-cookie")[0].decode("latin-1")
        assert cookie.lower().count("secure") == 1, cookie

    def test_a_cookie_without_the_flag_still_gets_it(self):
        """Positive control: the two rows above must mean "not appended
        twice", not "never appended"."""
        headers = _drive(
            lambda inner: SecureCookieMiddleware(inner, testing=False),
            {"scheme": "https"},
            [(b"set-cookie", b"session=abc; Path=/; HttpOnly")],
        )

        cookie = _values(headers, b"set-cookie")[0].decode("latin-1")
        assert cookie == "session=abc; Path=/; HttpOnly; Secure"

    def test_non_cookie_headers_survive_the_rewrite_unchanged(self):
        """The wrapper rebuilds the entire header list, so unrelated
        headers are in the blast radius of any change to that loop."""
        original = [
            (b"content-type", b"application/json"),
            (b"set-cookie", b"session=abc; Path=/"),
            (b"x-custom", b"kept"),
            (b"cache-control", b"no-store"),
        ]

        headers = _drive(
            lambda inner: SecureCookieMiddleware(inner, testing=False),
            {"scheme": "https"},
            original,
        )

        assert _values(headers, b"content-type") == [b"application/json"]
        assert _values(headers, b"x-custom") == [b"kept"]
        assert _values(headers, b"cache-control") == [b"no-store"]
        # Nothing added or dropped: same count, same order of names.
        assert [k for k, _ in headers] == [k for k, _ in original]

    def test_multiple_cookies_are_each_flagged(self):
        """Set-Cookie is a repeated header; the rewrite must handle every
        occurrence, not just the first."""
        headers = _drive(
            lambda inner: SecureCookieMiddleware(inner, testing=False),
            {"scheme": "https"},
            [
                (b"set-cookie", b"a=1; Path=/"),
                (b"set-cookie", b"b=2; Path=/"),
            ],
        )

        cookies = [v.decode("latin-1") for v in _values(headers, b"set-cookie")]
        assert cookies == ["a=1; Path=/; Secure", "b=2; Path=/; Secure"]


class TestServerHeaderStrippedRegardlessOfCase:
    """``SecurityHeadersMiddleware`` — ``k.lower() != b"server"``.

    ``tests/web/test_security_headers.py::TestServerHeaderStripped`` only
    ever emits lowercase ``b"server"``, so it passes with the ``.lower()``
    removed. uvicorn emits ``Server`` title-cased.
    """

    def _headers_after(self, server_header_name: bytes):
        return _drive(
            SecurityHeadersMiddleware,
            {},
            [
                (server_header_name, b"leaky/1.0"),
                (b"content-type", b"text/plain"),
            ],
        )

    def test_title_case_server_header_is_removed(self):
        headers = self._headers_after(b"Server")

        assert _values(headers, b"server") == []
        assert _values(headers, b"content-type") == [b"text/plain"]

    def test_upper_case_server_header_is_removed(self):
        headers = self._headers_after(b"SERVER")

        assert _values(headers, b"server") == []

    def test_lower_case_server_header_is_removed(self):
        headers = self._headers_after(b"server")

        assert _values(headers, b"server") == []

    def test_a_response_with_no_server_header_keeps_everything_else(self):
        """The strip is a filter over the whole list; it must not eat
        unrelated headers when there is nothing to remove."""
        headers = _drive(
            SecurityHeadersMiddleware,
            {},
            [(b"content-type", b"text/plain"), (b"x-custom", b"kept")],
        )

        assert _values(headers, b"content-type") == [b"text/plain"]
        assert _values(headers, b"x-custom") == [b"kept"]