~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(security): pin that encoded request targets cannot dodge the CSRF exemptions (#6723)
LearningCircuitb11557ab5762
diff --git a/tests/security/test_csrf_encoded_path_consistency.py b/tests/security/test_csrf_encoded_path_consistency.pynew file mode 100644index 0000000000..d9dc6b5369--- /dev/null+++ b/tests/security/test_csrf_encoded_path_consistency.py@@ -0,0 +1,297 @@+"""Encoded request targets cannot dodge the path-based CSRF exemptions.++# allow: no-sut-import — the system under test is the app's middleware ++# routing stack, driven end-to-end through the ``app`` fixture at the raw+# ASGI boundary; there is no single module to import.++The decoding chain these pins guard (verified live against uvicorn/h11,+and against a minimal starlette app for the router half):++* The ASGI server percent-decodes the wire request target ONCE:+ wire ``/%61pi/clear_history`` -> ``scope["path"] == "/api/clear_history"``+ wire ``/%2561pi/clear_history`` -> ``scope["path"] == "/%61pi/clear_history"``+ (``scope["raw_path"]`` keeps the undecoded wire bytes; nothing in+ ``web/`` reads it).+* Every app middleware — ``CSRFMiddleware`` included — judges+ ``scope["path"]`` verbatim: there is no ``unquote`` anywhere between+ the server and the exemption decision.+* Starlette's router ALSO matches ``scope["path"]`` verbatim (routing+ does not percent-decode), so a double-encoded wire target — scope+ path ``/%61pi/...`` — matches NO route and dies as a 404. It never+ reaches a FastAPI handler in production.++The security property holding it together: the middleware and the+router judge the SAME string. A path-prefix or exact-match exemption+therefore cannot be manufactured by encoding (``/%77s/...`` is not the+``/ws`` exemption), and an encoded mutating path cannot be smuggled+past the CSRF layer into a decoded route match. The same verbatim+judgement keeps the body-size middleware prefixes safe: an encoded+upload path misses the large-upload prefix match and falls to the+STRICTER default cap, never the other way around.++Known harness artifact these tests deliberately route around: the+transport underneath ``authenticated_client`` — Starlette's own+``TestClient`` (``starlette/testclient.py`` calls ``unquote`` on the+path when it builds the ASGI scope) layered on top of httpx's URL+parsing — decodes the target twice before the app sees it. That is a+property of Starlette's TestClient, not of the suite's Flask-compat+wrapper (``_FlaskCompatClient`` only adds Flask-shaped response+accessors such as ``.get_data()``/``.get_json()``; it never touches+the request path). So a wire ``/%2561pi/...`` posted through+``authenticated_client`` arrives at the app as a plain ``/api/...`` —+the app never sees an encoded path through that transport. Production+(uvicorn) decodes the wire target exactly once, and Starlette's+router matches ``scope["path"]`` verbatim afterwards (no further+decode), so this double decode has no production analogue. Every pin+below therefore drives the ASGI callable directly with+``scope["path"]``/``scope["raw_path"]`` set explicitly — the boundary+uvicorn actually hands the app. ``raw_path`` is set to the wire target+that decodes (once) to the pinned ``path``, the same invariant uvicorn+maintains.++Pins:++1. Encoded mutating scope paths (``/%61pi/...``, ``/%2561pi/...``) ++ bogus token -> 403 from the CSRF layer: CSRF is enforced before+ routing, on paths that match no exemption. Neither spelling is+ exempt whether or not it is decoded first, so this pin cannot by+ itself catch a decode regression being inserted upstream — that is+ pin 2's job.+2. Encoded exempt-prefix shape (``/%77s/api/...``) + bogus token ->+ 403: percent-encoding cannot MANUFACTURE the ``/ws`` exemption.+ This is the pin that goes red if a percent-decode is ever inserted+ anywhere between the server and the CSRF judgement.+3. Double-encoded scope path (``/%61pi/...``) + VALID token -> 404:+ the router does not decode, so the request never reaches the+ handler. Tripwire for a second decode appearing anywhere between+ the server and route matching — it would flip to a handler response.+4. A ``/ws``-prefixed path with an encoded traversal tail cannot reach+ a mutating route: the exemption only ever lands in the socket+ mount and dies there as a 404 — this is the one pin driven through+ the real client transport, whose double decode is harmless here+ because the traversal tail lands inside the exempt mount either+ way.+"""++import asyncio+import json++import pytest+++def _wire_raw_path(scope_path: str) -> bytes:+ """The wire target uvicorn received for this scope path.++ uvicorn decodes the request target exactly once, so the invariant is+ ``unquote(raw_path) == scope_path``: re-encode every literal ``%``+ as ``%25`` and that single decode undoes exactly that.+ """+ return scope_path.replace("%", "%25").encode("ascii")+++def _drive(app, method, path, headers):+ """Invoke the real ASGI app with a uvicorn-shaped scope.++ Mirrors the raw-scope idiom of tests/web/test_teardown_cleanup_asgi.py+ and tests/web/dependencies/test_csrf_streaming_replay.py: no HTTP+ client in the loop, so ``scope["path"]`` arrives at the app+ byte-for-byte as set here — the exact string the middleware judges+ and the router matches.+ """+ sent = []++ async def receive():+ return {"type": "http.request", "body": b"", "more_body": False}++ async def send(message):+ sent.append(message)++ scope = {+ "type": "http",+ "asgi": {"version": "3.0", "spec_version": "2.3"},+ "http_version": "1.1",+ "method": method,+ "scheme": "http",+ "path": path,+ # The wire target that the server's single decode turned into+ # ``path`` — the invariant uvicorn hands every app.+ "raw_path": _wire_raw_path(path),+ "query_string": b"",+ "root_path": "",+ "headers": headers,+ "client": ("127.0.0.1", 12345),+ "server": ("testserver", 80),+ }++ asyncio.run(app(scope, receive, send))++ start = next(m for m in sent if m["type"] == "http.response.start")+ body = b"".join(+ m.get("body", b"") for m in sent if m["type"] == "http.response.body"+ )+ # Raw (name, value) pairs, not a dict: a response can carry more than+ # one Set-Cookie header, and collapsing them into a dict keeps only+ # the last one — see `_session_cookie_and_csrf_token`, which needs+ # the specific one starting with ``session=``.+ return start["status"], start.get("headers", []), body+++def _session_cookie_and_csrf_token(app):+ """Mint a real signed session carrying a CSRF token.++ Drives ``GET /auth/csrf-token`` through the same raw-scope boundary+ (a safe method, so the CSRF layer passes it through), then returns+ the session cookie plus the token it stamped. The POST pins replay+ that cookie, so the CSRF layer sees a session-bound token exactly+ as a logged-in browser's request would — pins 1/2 land on the+ token-MISMATCH branch, not the empty-session branch.+ """+ status, headers, body = _drive(+ app, "GET", "/auth/csrf-token", [(b"host", b"testserver")]+ )+ assert status == 200, f"csrf-token bootstrap failed: {status} {body!r}"+ # List comprehension over every header, not a dict lookup: a dict+ # keyed on header name would silently keep only the LAST Set-Cookie+ # if the response ever carries more than one.+ session_cookies = [+ v+ for k, v in headers+ if k == b"set-cookie" and v.startswith(b"session=")+ ]+ assert len(session_cookies) == 1, (+ f"expected exactly one session Set-Cookie, got {session_cookies!r} "+ f"in {headers!r}"+ )+ cookie_pair = session_cookies[0].split(b";", 1)[0]+ return cookie_pair, json.loads(body)["csrf_token"]+++def _post_with_token(app, cookie, path, token):+ return _drive(+ app,+ "POST",+ path,+ [+ (b"host", b"testserver"),+ (b"cookie", cookie),+ (b"content-length", b"0"),+ (b"x-csrftoken", token),+ ],+ )+++ "encoded_scope_path",+ [+ # Wire /%2561pi/clear_history: uvicorn's single decode leaves+ # the path still encoded at the app boundary.+ "/%61pi/clear_history",+ # Wire /%252561pi/clear_history: even one more layer of encoding+ # does not change the judgement — the layer never decodes.+ "/%2561pi/clear_history",+ ],+)+def test_encoded_mutating_path_is_csrf_enforced(app, encoded_scope_path):+ """CSRF is enforced before routing, on paths that match no exemption.++ Neither spelling of this path is ever in ``_SKIP_EXACT_PATHS`` /+ ``_SKIP_PATH_PREFIXES``, decoded or not, so this pin holds even under+ a decode regression without detecting one — pin 2+ (``test_encoded_ws_prefix_cannot_manufacture_the_exemption``) is the+ one that goes red when a decode is inserted before the CSRF+ judgement.+ """+ cookie, _token = _session_cookie_and_csrf_token(app)+ status, _headers, body = _post_with_token(+ app, cookie, encoded_scope_path, b"bogus-token-override"+ )++ assert status == 403, (+ f"the CSRF layer did not enforce on scope path {encoded_scope_path}"+ )+ # The session bootstrap above stamps a real session-bound token, so a+ # bogus header lands on the token-MISMATCH branch (csrf.py's final+ # check) rather than the differently worded empty-session branch —+ # assert the mismatch branch's exact message to pin that distinction.+ assert b"CSRF token missing or invalid" in body, (+ f"the 403 did not come from the token-mismatch branch: {body[:200]!r}"+ )+++def test_encoded_ws_prefix_cannot_manufacture_the_exemption(app):+ """``/%77s/api/...`` decodes to ``/ws/api/...`` — exempt — but the+ middleware judges the encoded string verbatim, so enforcement holds.++ This is the pin that goes red if a percent-decode is ever inserted+ anywhere before the CSRF judgement (a normalizing middleware, an+ ``unquote`` in the exemption check): the path would arrive as+ ``/ws/...``, sail past CSRF, and the answer would stop being a CSRF+ 403 — the socket mount or the router would speak instead.+ """+ cookie, _token = _session_cookie_and_csrf_token(app)+ status, _headers, body = _post_with_token(+ app, cookie, "/%77s/api/clear_history", b"bogus-token-override"+ )++ assert status == 403, (+ "an encoded /ws prefix dodged CSRF enforcement — a decode now "+ "sits between the server and the exemption judgement"+ )+ # Same reasoning as pin 1: the session bootstrap stamps a real token,+ # so a bogus header must land on the token-MISMATCH branch, not the+ # empty-session branch — the two are distinguishable by message.+ assert b"CSRF token missing or invalid" in body, (+ f"the 403 did not come from the token-mismatch branch: {body[:200]!r}"+ )+++def test_double_encoded_target_with_a_valid_token_never_reaches_the_handler(+ app,+):+ """The router matches ``scope["path"]`` verbatim — no unquote.++ The scope form of a double-encoded wire target+ (``/%61pi/clear_history``) PASSES CSRF with a valid token (the+ previous pins prove the layer judged and enforced that same string+ first) and then matches no route: 404. A handler response here+ (200, 401, anything but 404) would mean a second decode has+ appeared somewhere between the server and route matching — the+ exact smuggling seam this suite watches.+ """+ cookie, token = _session_cookie_and_csrf_token(app)+ status, _headers, _body = _post_with_token(+ app, cookie, "/%61pi/clear_history", token.encode()+ )++ assert status == 404, (+ f"double-encoded target produced a {status} — something decoded "+ "scope['path'] before the router matched it"+ )+++def test_ws_exemption_cannot_reach_a_mutating_route(authenticated_client):+ """A request the CSRF layer exempts (scope path under ``/ws``) with an+ encoded traversal tail lands in the socket mount and dies there as a+ 404 "Not Found" — it never reaches the FastAPI router, so it cannot+ reach the ``clear_history`` handler either.++ Verified against a scratch app carrying the real CSRF exempt-prefix+ constants and the real socket mount shape (``app.mount("/ws", ...)``+ wrapping ``socketio.ASGIApp(sio, socketio_path="/ws/socket.io")``):+ this pin goes red — 200 with the handler's success body — the moment+ a decode-and-normalise step is inserted between the CSRF middleware+ and the router, which collapses the traversal tail onto+ ``/api/clear_history`` and lets it match the real route.+ """+ response = authenticated_client.post(+ "/ws/%2e%2e/api/clear_history",+ headers={"X-CSRFToken": "bogus-token-override"},+ )++ assert response.status_code == 404, (+ f"expected the socket mount's 404, got {response.status_code}: "+ f"{response.content[:200]!r}"+ )+ assert b"Not Found" in response.content, (+ f"the 404 did not come from the socket mount: {response.content[:200]!r}"+ )