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.

13.5 KB
Raw Blame
"""Direct pins for ``web.dependencies.csrf._tokens_match``.

This is the single comparison primitive under every CSRF check in the
FastAPI port: header tokens, urlencoded form-field tokens, both compared
against the session token through this function. Its behavioral envelope
was only ever pinned INDIRECTLY — through full-middleware 403 tests
(``tests/web/test_csrf_middleware_edges.py`` covers non-ASCII operands
arriving over header/form) — meaning a refactor of the helper itself had
no direct net. The properties worth pinning at unit level:

1. ASCII equality and inequality are decided by ``secrets.compare_digest``
   — the timing-safe comparator, not ``==`` and not a hand-rolled loop.
   A swap to either is invisible to result assertions (they agree with
   the comparator on every operand pair), so the delegation itself is
   pinned directly, in two shapes:
   ``TestDelegatesToCompareDigest.test_operand_pairs_reach_the_comparator``
   wraps the real comparator in a pass-through spy and requires the
   helper's own two operands, in order, to reach it, over pairs that
   differ at index 0, at the last index, and in length only. What
   actually catches a reimplementation short-circuiting on the first
   differing byte (the classic timing oracle) is the delegation
   assertion itself (``calls == [(session_token, provided_token)]``,
   which comes back ``[]`` for anything that never calls the comparator)
   — that holds regardless of which pair is used; the pair variety
   instead exercises that the helper's own operands, in that order,
   reach the spy under each shape of mismatch; and
   ``test_the_comparators_return_value_is_handed_back`` swaps in a
   sentinel verdict, which pins that the comparator's answer is what the
   helper returns rather than merely something it consulted.
2. Non-string operands fail closed instead of raising:
   ``compare_digest`` raises on non-ASCII strings and mismatched operand
   types, and both operands here are untrusted runtime state (header
   bytes latin-1-decoded, form values possibly containing U+FFFD, stale
   or forged session payloads possibly non-string). A 500 from the CSRF
   check would be a denial-of-service primitive on every mutating route.
3. A wholly non-ASCII operand — 64 repetitions of Cyrillic 'а' (U+0430,
   the visual lookalike for ASCII 'a'), not an ASCII token with one
   character substituted in — fails CLOSED on the same ``isascii`` gate
   as point 2. No comparator is fooled by anything here; nothing in
   this helper compares glyph shapes. What the gate buys is the
   fail-closed 403 rather than the ``TypeError``-turned-500 that point 2
   already describes.

One deliberate oddity is pinned as a fact, not endorsed: two EMPTY
strings compare equal. That result is unreachable from ``CSRFMiddleware``,
which never reaches ``_tokens_match`` with an empty operand on either
side. Both guards are ordering properties of the middleware source, read
at the lines cited, not something a status-code test can distinguish:

* ``csrf.py:276`` — ``if not session_token:`` returns 403 before the
  token is read off the request at all, so an absent OR empty session
  token never reaches the comparison.
* ``csrf.py:374`` — ``if not provided or not _tokens_match(session_token,
  provided):`` short-circuits on the left operand, so an empty provided
  token is a 403 without ``_tokens_match`` being called.

``tests/security/test_csrf_protection.py`` covers the OUTCOME of the
first guard from the request side, not this ordering:
``test_post_request_without_session_token_rejected`` (:147) posts with no
session at all and ``test_post_request_without_csrf_token_rejected``
(:167) posts with a stamped session but no token; each asserts a bare
403. Neither sends an EMPTY token, and neither can tell "403 before
comparing" apart from "403 after comparing two empties and getting True,
then rejecting for some other reason" — which is exactly why the
helper-level fact is pinned here rather than inferred there. It is
pinned so that any refactor making it REACHABLE (e.g. moving either
presence check out of the middleware) trips a named test and forces the
empties question to be answered consciously.
"""

import pytest

from local_deep_research.web.dependencies import csrf as csrf_mod
from local_deep_research.web.dependencies.csrf import _tokens_match


ASCII_TOKEN = "a" * 64
# Operand pairs that a first-differing-byte short circuit would rank
# differently from a constant-time comparison. Position 0 and
# length-only are the two shapes the rest of this file never produced.
DIFFERS_AT_INDEX_0 = "b" + ASCII_TOKEN[1:]
DIFFERS_AT_LAST_INDEX = ASCII_TOKEN[:-1] + "b"
SHORTER_PREFIX = ASCII_TOKEN[:-1]


class TestAsciiOperands:
    def test_equal_ascii_tokens_match(self):
        assert _tokens_match(ASCII_TOKEN, ASCII_TOKEN)

    def test_single_character_difference_fails(self):
        assert not _tokens_match(ASCII_TOKEN, DIFFERS_AT_LAST_INDEX)

    def test_first_character_difference_fails(self):
        # Differing at index 0 rather than the last index: the pair a
        # prefix-comparing reimplementation rejects fastest.
        assert not _tokens_match(ASCII_TOKEN, DIFFERS_AT_INDEX_0)

    def test_different_lengths_fail(self):
        assert not _tokens_match(ASCII_TOKEN, ASCII_TOKEN + "x")
        assert not _tokens_match(ASCII_TOKEN, SHORTER_PREFIX)
        assert not _tokens_match(SHORTER_PREFIX, ASCII_TOKEN)

    def test_both_empty_strings_match_but_are_unreachable_from_middleware(
        self,
    ):
        # Inert today: the middleware 403s on a falsy session token
        # (csrf.py:276) and short-circuits on a falsy provided token
        # (csrf.py:374, ``if not provided or not _tokens_match(...)``),
        # so neither operand can be empty by the time this helper is
        # called. Named test so a refactor that makes this reachable is a
        # conscious decision.
        #
        # INVERTED PIN: this test FAILS if the code is hardened to reject
        # empty operands (e.g. an explicit "both non-empty" precondition
        # added to ``_tokens_match`` itself). That is deliberate — update
        # this test then, it is not a regression.
        assert _tokens_match("", "")


class TestNonStringOperands:
    def test_none_session_token_fails_closed(self):
        assert not _tokens_match(None, ASCII_TOKEN)

    def test_none_provided_token_fails_closed(self):
        assert not _tokens_match(ASCII_TOKEN, None)

    def test_bytes_operands_fail_closed(self):
        # Header/form bytes that never got decoded must not raise from
        # compare_digest's type check.
        assert not _tokens_match(ASCII_TOKEN, ASCII_TOKEN.encode())
        assert not _tokens_match(ASCII_TOKEN.encode(), ASCII_TOKEN)

    def test_int_operands_fail_closed(self):
        assert not _tokens_match(12345, ASCII_TOKEN)
        assert not _tokens_match(ASCII_TOKEN, 12345)


class TestNonAsciiOperands:
    def test_non_ascii_session_token_fails_closed(self):
        # e.g. a stale/forge-resistant payload corruption: any non-ASCII
        # session-side string must not raise from compare_digest.
        assert not _tokens_match("tökén-" + "a" * 58, ASCII_TOKEN)

    def test_non_ascii_provided_token_fails_closed(self):
        # Form values may contain U+FFFD from lossy decoding.
        assert not _tokens_match(ASCII_TOKEN, "token\ufffd" + "a" * 58)

    def test_cyrillic_homoglyph_substitution_fails_closed(self):
        # 'а' here is U+0430, not ASCII 'a'. The isascii gate rejects the
        # whole string regardless of how similar it looks.
        lookalike = "а" * 64
        assert not _tokens_match(ASCII_TOKEN, lookalike)

    def test_matching_non_ascii_pair_still_fails_closed(self):
        # Even a "successful" match on non-ASCII operands must not pass:
        # the ASCII gate is a precondition, not a best-effort filter.
        non_ascii = "é" * 64
        assert not _tokens_match(non_ascii, non_ascii)


class TestDelegatesToCompareDigest:
    """The delegation pin: the comparator itself must decide.

    Every result assertion in the classes above survives rewriting the
    helper's last line as ``session_token == provided_token``, or as a
    hand-rolled length-then-byte loop — all three agree on every operand
    pair, so no black-box result can tell them apart. What distinguishes
    them is WHO decides, so both tests here patch ``compare_digest`` on
    the ``secrets`` module object the production module bound at import
    time (the helper resolves ``secrets.compare_digest`` at call time).

    ``test_operand_pairs_reach_the_comparator`` uses a pass-through spy —
    the real comparator still answers, so the helper's verdict stays
    honest — and asserts the spy saw exactly ``(session_token,
    provided_token)``, over pairs differing at index 0, at the last
    index, and in length only. A ``==`` rewrite or a byte loop never
    consults the spy at all, so ``calls`` stays empty and the pin trips.

    ``test_the_comparators_return_value_is_handed_back`` goes further: it
    substitutes a sentinel object as the verdict and requires the helper
    to return that exact object, which pins that the comparator's answer
    IS the helper's answer rather than something the helper consulted and
    then re-derived (``compare_digest(a, b); return a == b`` passes the
    spy test and fails this one).

    DISCLOSED NARROWNESS of the sentinel test, deliberate on both counts:

    * it FAILS on ``return bool(secrets.compare_digest(...))``, because a
      sentinel object is not the object ``bool()`` returns. That rewrite
      is behaviourally equivalent today (``compare_digest`` already
      returns ``bool``), so this is a strictness the pin buys on purpose:
      wrapping the comparator's result is how a "just normalise it"
      refactor starts, and the next step — normalising with something
      that is not ``bool`` — would not be equivalent. Update this test
      consciously if the wrapper is ever wanted; it is not a regression.
    * it FAILS on ``hmac.compare_digest(...)``, which is the same
      function object (``secrets`` imports it from ``hmac``) reached
      through a different module attribute, so patching ``secrets`` does
      not intercept it. Also deliberate: the spy in
      ``tests/security/test_csrf_coverage.py``
      (``test_constant_time_compare_is_on_the_enforcement_path``) patches
      the same attribute, so keeping the helper on ``secrets`` is what
      keeps that middleware-level pin observing anything at all.
    """

    OPERAND_PAIRS = [
        pytest.param(ASCII_TOKEN, ASCII_TOKEN, True, id="identical"),
        pytest.param(
            ASCII_TOKEN, DIFFERS_AT_INDEX_0, False, id="differs-at-index-0"
        ),
        pytest.param(
            ASCII_TOKEN, DIFFERS_AT_LAST_INDEX, False, id="differs-at-last"
        ),
        pytest.param(
            ASCII_TOKEN, SHORTER_PREFIX, False, id="provided-is-a-prefix"
        ),
        pytest.param(
            SHORTER_PREFIX, ASCII_TOKEN, False, id="session-is-a-prefix"
        ),
    ]

    @pytest.mark.parametrize(
        ("session_token", "provided_token", "expected"), OPERAND_PAIRS
    )
    def test_operand_pairs_reach_the_comparator(
        self, session_token, provided_token, expected, monkeypatch
    ):
        calls = []
        real = csrf_mod.secrets.compare_digest

        def spy(a, b):
            calls.append((a, b))
            return real(a, b)

        monkeypatch.setattr(csrf_mod.secrets, "compare_digest", spy)

        result = _tokens_match(session_token, provided_token)

        # `is` on purpose: the helper is annotated `-> bool`, and a
        # truthy non-bool (e.g. returning the operand itself) would
        # satisfy a bare assert while breaking any caller that
        # serialises or identity-checks the verdict.
        assert result is expected, (
            f"expected {expected!r}, got {result!r} — the verdict must be "
            "the comparator's own bool, not a truthy stand-in"
        )
        assert calls == [(session_token, provided_token)], (
            "the decision must go through secrets.compare_digest with the "
            "helper's operands in (session_token, provided_token) order — "
            f"the spy saw {calls!r}; a plain '==' or a hand-rolled "
            "byte-by-byte loop never consults the timing-safe comparator "
            "and leaks the session token one position at a time"
        )

    @pytest.mark.parametrize(
        ("session_token", "provided_token"),
        [
            pytest.param(ASCII_TOKEN, ASCII_TOKEN, id="equal-operands"),
            pytest.param(
                ASCII_TOKEN, DIFFERS_AT_INDEX_0, id="unequal-operands"
            ),
        ],
    )
    def test_the_comparators_return_value_is_handed_back(
        self, session_token, provided_token, monkeypatch
    ):
        verdict = object()
        calls = []

        def fixed_verdict(a, b):
            calls.append((a, b))
            return verdict

        monkeypatch.setattr(csrf_mod.secrets, "compare_digest", fixed_verdict)

        # Sentinel verdict: no comparison operator can produce this
        # object, in either direction (``==`` would answer True on the
        # equal pair and False on the unequal one).
        assert _tokens_match(session_token, provided_token) is verdict, (
            "the helper must return the comparator's own result; see this "
            "class's docstring for the two equivalent rewrites this pin "
            "deliberately rejects"
        )
        assert calls == [(session_token, provided_token)], (
            "the comparator must be called with the helper's operands in "
            f"(session_token, provided_token) order — the sentinel saw "
            f"{calls!r}"
        )