~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
"""Query-string parsing parity: Werkzeug (main) vs Starlette (this branch).
The port rewrote every ``request.args`` read as ``request.query_params``.
Both objects answer ``.get(key, default)`` and ``.getlist(key)``, so the
rename compiles everywhere — but the two are separate implementations of
the query-string grammar and they do not agree on every input.
This file is a head-to-head oracle. The "main" side is not a model of
Werkzeug: it is Werkzeug, driving a real ``werkzeug.wrappers.Request``
built from a WSGI environ. ``werkzeug~=3.1.6`` is still a declared runtime
dependency of this project (pyproject.toml; the upload-sanitisation
helpers keep it), so the comparison runs against the same library main
served on. The "port" side is ``starlette.datastructures.QueryParams``,
which is literally what ``Request.query_params`` returns.
Three findings drive the file, all measured, not assumed:
1. **Repeated keys resolve the opposite way.** ``?period=all&period=7d``
is ``"all"`` under Werkzeug's ``MultiDict.get`` (first wins) and
``"7d"`` under Starlette's ``QueryParams.get`` (last wins). Both are
silent; both return 200. This is framework-inherent — Starlette has no
first-wins mode — so it is PINNED here, not filed as something to fix.
``TestLiveRouteDuplicates`` shows it changing a real response's row
count, so the cost of the divergence is visible rather than academic.
2. **Raw (un-percent-encoded) non-ASCII bytes in the query string decode
differently.** Werkzeug decodes the query string as UTF-8; Starlette
hardcodes ``latin-1`` (``QueryParams.__init__`` does
``value.decode("latin-1")`` before ``parse_qsl``). The same wire bytes
``p=caf\\xc3\\xa9`` therefore arrive as ``"café"`` on main and
``"café"`` here. Browsers percent-encode, so the UI never hits this;
curl, proxies and hand-written clients do. Also framework-inherent,
also pinned rather than filed.
3. **Everything else in the grammar agrees**, including several rules
that a hand-rolled parser would get wrong: ``+`` is a space, a blank
value is KEPT (``?x=`` -> ``""``, not absent), a bare key is ``""``, a
nameless ``=v`` is dropped, ``;`` is NOT a separator, and a malformed
``%zz`` stays literal. Those are pinned so a future "let's just parse
the query string ourselves" or a Starlette major bump fails loudly.
Integer coercion has no framework divergence to pin — Werkzeug's
``args.get(k, d, type=int)`` calls the same builtin ``int()`` the port
calls — but it has a *shape* divergence that already produced bugs on
this branch: ``type=int`` swallows the ``ValueError`` and returns the
default, whereas a bare ``int()`` propagates it into the route's outer
``except Exception`` and answers 500. ``TestIntCoercionOnALiveRoute``
pins the fallback values against the Werkzeug oracle on a real route, and
``TestEveryIntQueryReadIsGuarded`` sweeps all 24 remaining call sites in
``web/routers/`` for the missing guard, because booting 24 routes is not
affordable and the AST answer is exact.
Negative controls actually executed while writing this file, each
against a throwaway copy under a scratch PYTHONPATH (the shipped tree was
never modified; ``git status src/`` stayed clean throughout):
* Starlette's ``ImmutableMultiDict.__init__`` rewritten to keep the FIRST
value per key (i.e. Werkzeug semantics restored) =>
``test_repeated_key_get_flipped_from_first_wins_to_last_wins`` failed
with ``assert 'all' == '7d'``. This is the mechanism the whole file
hangs on, so it is the control that matters most.
* ``context_overflow_api.py`` with the ``except (TypeError, ValueError)``
around ``per_page`` deleted => ``test_every_int_query_param_read_is_
guarded`` failed naming ``context_overflow_api.py:64``, and the
walker's own anchor test failed too.
* ``settings.py`` with a ``[]``-suffixed key read via ``.get()`` =>
``test_array_style_keys_are_read_with_getlist`` failed naming
``settings.py:1317 'category[]'``. With ``getlist("keys[]")`` removed
outright, the same test failed on its anchor assertion instead — both
halves of it are load-bearing.
The live-route tests (sections 4-6) were executed once and passed; they
were NOT additionally re-run under mutation, because booting the app was
rationed on the machine this was written on. Their guards against
vacuity are therefore in-test rather than mutation-proven: every one
sends each value on its own as a positive control before sending the
combination it pins, and every assertion reads a number that the seeded
rows make specific (5 recent rows, 1 row 300 days old).
"""
import ast
from contextlib import contextmanager
from datetime import UTC, datetime, timedelta
from pathlib import Path
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from starlette.datastructures import QueryParams
from werkzeug.test import EnvironBuilder
from werkzeug.wrappers import Request as WerkzeugRequest
from local_deep_research.database.models import Base
from local_deep_research.database.models.metrics import TokenUsage
from local_deep_research.web.routers import context_overflow_api
OVERVIEW_URL = "/api/context-overflow"
#: ``_MAX_PAGE`` is read from the shipped module rather than copied, so
#: this file cannot drift out of sync with the clamp it asserts.
MAX_PAGE = context_overflow_api._MAX_PAGE
ROUTERS_DIR = Path(context_overflow_api.__file__).resolve().parent
# ---------------------------------------------------------------------------
# The two real implementations, side by side
# ---------------------------------------------------------------------------
def werkzeug_args(query_string: str):
"""``request.args`` as main built it, from the same wire bytes.
``query_string`` is the raw QUERY_STRING as WSGI carries it: a str
whose code points are the individual bytes (latin-1), which is how a
server hands non-ASCII octets to a WSGI app.
"""
environ = EnvironBuilder().get_environ()
environ["QUERY_STRING"] = query_string
return WerkzeugRequest(environ).args
def starlette_params(query_string: str) -> QueryParams:
"""``request.query_params`` as the port builds it, same wire bytes.
Starlette constructs this from ``scope["query_string"]``, which is
``bytes`` — so the bytes are handed over exactly as encoded here.
"""
return QueryParams(query_string.encode("latin-1"))
# ---------------------------------------------------------------------------
# 1. The grammar the port did NOT change
# ---------------------------------------------------------------------------
class TestGrammarAgrees:
"""Rules both libraries implement identically. Pinned because they are
the rules a replacement parser (or a Starlette major bump) would be
most likely to change, and because several are counter-intuitive.
"""
@pytest.mark.parametrize(
("query_string", "key", "value", "values"),
[
# '+' is a space; '%20' is the same space.
("q=a+b", "q", "a b", ["a b"]),
("q=a%20b", "q", "a b", ["a b"]),
# A literal '+' has to be sent as %2B or it is lost.
("q=a%2Bb", "q", "a+b", ["a+b"]),
# Present-but-empty is KEPT, not dropped. Both libraries pass
# keep_blank_values; this is what makes `?x=` distinguishable
# from a missing `x`, and what the `x or default` port bug
# (rag.py's pdf_storage=) silently destroyed.
("period=", "period", "", [""]),
# A bare key with no '=' is also present, with value "".
("period", "period", "", [""]),
# A nameless '=value' is NOT dropped: it becomes a parameter
# whose key is the empty string. Both libraries agree, which
# is worth knowing before anyone iterates query_params
# assuming every key is a plausible identifier.
("=orphan", "", "orphan", ["orphan"]),
# Empty pairs between separators are skipped, not errors.
("&&period=7d&", "period", "7d", ["7d"]),
# ';' has not been a separator since Python 3.10 / Werkzeug 2.
# `?a=1;b=2` is ONE parameter whose value contains the ';'.
("a=1;b=2", "a", "1;b=2", ["1;b=2"]),
("a=1;b=2", "b", None, []),
# A malformed escape stays literal instead of raising.
("p=%zz", "p", "%zz", ["%zz"]),
("p=%", "p", "%", ["%"]),
# Percent-encoded NUL and multi-byte UTF-8 both survive.
("p=%00", "p", "\x00", ["\x00"]),
("p=%E2%9C%93", "p", "✓", ["✓"]),
("p=caf%C3%A9", "p", "café", ["café"]),
# Whitespace inside a value is preserved verbatim: nothing
# strips on the way in. (int() strips later - see
# TestWerkzeugTypeIntOracle - but the string is untouched.)
("limit=%20200%20", "limit", " 200 ", [" 200 "]),
# '+' inside a numeric value becomes a leading space, which
# is why `?limit=+5` still parses as 5 further down.
("limit=+5", "limit", " 5", [" 5"]),
# Non-ASCII digits are decoded, not rejected.
("limit=%EF%BC%91%EF%BC%92", "limit", "12", None),
("limit=%D9%A1%D9%A2", "limit", "١٢", None),
# Array-style sends: getlist keeps every value, in order.
("ids=1&ids=2&ids=3", "ids", None, ["1", "2", "3"]),
],
)
def test_same_answer_on_both_frameworks(
self, query_string, key, value, values
):
"""Assert the SAME literal against both libraries.
Because the expectation is a literal rather than one library's
output, this fails if EITHER side changes — it is not a
tautological "starlette equals starlette" comparison.
"""
werkzeug = werkzeug_args(query_string)
starlette = starlette_params(query_string)
if value is not None or values == []:
assert werkzeug.get(key) == value, (
f"main/Werkzeug changed: {query_string!r}[{key!r}]"
)
assert starlette.get(key) == value, (
f"port/Starlette diverged: {query_string!r}[{key!r}] is "
f"{starlette.get(key)!r}, main gives {werkzeug.get(key)!r}"
)
if values is not None:
assert werkzeug.getlist(key) == values, (
f"main/Werkzeug changed: getlist({key!r}) on {query_string!r}"
)
assert starlette.getlist(key) == values, (
f"port/Starlette diverged: getlist({key!r}) on "
f"{query_string!r} is {starlette.getlist(key)!r}"
)
def test_a_missing_key_is_the_only_thing_that_yields_the_default(self):
"""``get(k, default)`` returns the default for ABSENT only.
The distinction survived the port intact, and it is the one the
``x or default`` rewrite destroys, so pin it on both sides at the
datastructure level as well as on a route.
"""
for args in (werkzeug_args("period="), starlette_params("period=")):
assert args.get("period", "30d") == "", (
f"{type(args).__name__}: an explicitly empty value must NOT "
"collapse to the default"
)
for args in (werkzeug_args(""), starlette_params("")):
assert args.get("period", "30d") == "30d", (
f"{type(args).__name__}: an absent key must yield the default"
)
# ---------------------------------------------------------------------------
# 2. The two divergences, pinned
# ---------------------------------------------------------------------------
class TestPinnedDivergences:
def test_repeated_key_get_flipped_from_first_wins_to_last_wins(self):
"""``?period=all&period=7d`` -> ``"all"`` on main, ``"7d"`` here.
Werkzeug's ``MultiDict.get`` returns the first occurrence;
Starlette's ``QueryParams.get`` returns the last. There is no
Starlette knob for this, so the divergence is permanent and this
test documents it rather than demanding a fix. What it protects
is the *knowledge*: if someone "fixes" a route with
``getlist(k)[0]`` for Flask-compat, or if a future Starlette
changes the rule, one of these two assertions breaks and the
reader is pointed straight at the reason.
"""
query_string = "period=all&period=7d"
main = werkzeug_args(query_string)
port = starlette_params(query_string)
assert main.get("period") == "all", (
"Werkzeug MultiDict.get is first-wins; if this fails the "
"oracle itself moved and every parity claim here is suspect"
)
assert port.get("period") == "7d", (
"Starlette QueryParams.get must be last-wins; "
f"got {port.get('period')!r}"
)
assert main.get("period") != port.get("period"), (
"the frameworks are expected to DISAGREE on repeated keys"
)
# Both agree on the full list — the information is not lost, only
# the resolution rule differs. That is what makes `getlist(k)[0]`
# a viable (if ugly) compat shim, and why the divergence is
# invisible to any route that already uses getlist.
assert main.getlist("period") == ["all", "7d"]
assert port.getlist("period") == ["all", "7d"]
def test_raw_non_ascii_query_bytes_decode_as_latin1_not_utf8(self):
"""Un-percent-encoded UTF-8 in the query string now mojibakes.
Wire bytes ``p=caf\\xc3\\xa9``. Werkzeug decodes the query string
as UTF-8 and yields ``café``; Starlette's ``QueryParams`` does
``value.decode("latin-1")`` before ``parse_qsl`` and yields
``café``. Percent-encoded input (``p=caf%C3%A9``) is UTF-8 on
both — ``parse_qsl`` unquotes with ``encoding="utf-8"`` — which
is why browsers never see this and why it is easy to miss.
Framework-inherent (the latin-1 is hardcoded in Starlette), so
pinned rather than filed. Any route that stores or matches a
non-ASCII filter value verbatim — library ``?domain=``/``?search=``,
notes ``?q=`` — silently sees different text than main did when
the caller is not a browser.
"""
raw = "p=caf\xc3\xa9" # the UTF-8 bytes for 'café', as WSGI text
assert werkzeug_args(raw).get("p") == "café", (
"main decoded the raw query string as UTF-8"
)
assert starlette_params(raw).get("p") == "café", (
"Starlette decodes the query string as latin-1; got "
f"{starlette_params(raw).get('p')!r}"
)
# Control: the percent-encoded spelling of the same text does NOT
# diverge, which isolates the defect to the raw-bytes path.
encoded = "p=caf%C3%A9"
assert werkzeug_args(encoded).get("p") == "café"
assert starlette_params(encoded).get("p") == "café"
# ---------------------------------------------------------------------------
# 3. Integer coercion — the Werkzeug ``type=int`` oracle
# ---------------------------------------------------------------------------
#: ``?limit=<raw>`` -> what ``request.args.get("limit", 50, type=int)``
#: returned on main. 50 means "the value did not parse, default applied".
#: Measured against Werkzeug 3.1.8; every one of these is a value a real
#: client can send.
TYPE_INT_ORACLE = [
("50", 50),
("abc", 50), # non-numeric -> default, never an exception
("", 50), # present-but-empty -> default (int("") raises)
("0", 0), # zero parses; the ROUTE clamp decides what 0 means
("-7", -7), # negatives parse; SQLite reads LIMIT -1 as "no limit"
("%20200%20", 200), # int() strips surrounding whitespace
("+5", 5), # '+' decodes to ' ', and int(" 5") == 5
("5.0", 50), # int() rejects float syntax -> default
("0x10", 50), # no base prefixes -> default
("1_0", 10), # PEP 515 underscores ARE accepted by int()
("%EF%BC%95", 5), # fullwidth digit five -> 5
("%D9%A1%D9%A2", 12), # arabic-indic 12 -> 12
("9" * 40, int("9" * 40)), # arbitrary precision, no overflow
]
class TestWerkzeugTypeIntOracle:
"""What main actually did, so the route table below is a comparison
against measured Flask behaviour and not against my expectations.
"""
@pytest.mark.parametrize(("raw", "expected"), TYPE_INT_ORACLE)
def test_type_int_fallback(self, raw, expected):
got = werkzeug_args(f"limit={raw}").get("limit", 50, type=int)
assert got == expected, (
f"?limit={raw} gave {got!r} under Werkzeug's type=int, "
f"expected {expected!r} — the main-side oracle moved"
)
def test_type_int_never_raises_which_is_the_whole_shape_difference(self):
"""The point of ``type=int``: it cannot propagate a ValueError.
The port replaced it with a bare ``int()``, which can — and on
four routes that produced a 500 for a typo before it was caught.
``TestEveryIntQueryReadIsGuarded`` is the standing check that no
new one lands.
"""
for raw in ("abc", "", "5.0", "0x10", "nan", "%D9%A1%D9%A2abc"):
args = werkzeug_args(f"limit={raw}")
assert args.get("limit", 50, type=int) == 50, (
f"?limit={raw} must fall back to the default, not raise"
)
# And the same value through a bare int() is exactly what the
# port's try/except has to absorb.
with pytest.raises(ValueError):
int(args.get("limit"))
# ---------------------------------------------------------------------------
# Live-route fixtures
# ---------------------------------------------------------------------------
#
# ``/api/context-overflow`` is the readout route for everything below: it
# echoes ``pagination.page`` and ``pagination.per_page`` straight from the
# parsed query params, AND its ``all_requests`` row count responds to both
# of them plus ``period``. So parsing outcomes are observable as data, not
# just as a status code.
@pytest.fixture
def auth_client():
"""TestClient authenticated by overriding ``require_auth``.
Same shape as tests/web/routers/test_context_overflow_contract.py:
registering a real user would cost an encrypted-DB bootstrap per test
and the seeded in-memory DB below stands in for it.
"""
from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.fastapi_app import app
app.dependency_overrides[require_auth] = lambda: "testuser"
try:
yield TestClient(app)
finally:
app.dependency_overrides.pop(require_auth, None)
_NOW = datetime.now(UTC)
def _usage(minutes_ago: int, **overrides) -> TokenUsage:
defaults = dict(
research_id="res-1",
timestamp=_NOW - timedelta(minutes=minutes_ago),
model_provider="ollama",
model_name="llama3",
prompt_tokens=2000,
completion_tokens=300,
total_tokens=2300,
context_limit=8192,
)
defaults.update(overrides)
return TokenUsage(**defaults)
#: Five rows inside every time window, plus one 300 days old. The old row
#: is only reachable with ``period=all`` (or ``1y``), which is what makes
#: the repeated-``period`` divergence show up as a row count.
RECENT_ROWS = 5
OLD_ROW_AGE_DAYS = 300
@contextmanager
def _seeded_db():
"""Patch the router's session factory with a seeded in-memory DB.
StaticPool shares the one connection with the TestClient threadpool,
so the route runs its REAL SQL (offset/limit/date filter) against
these rows.
"""
engine = create_engine(
"sqlite://",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
seed = Session()
seed.add_all(
[_usage(minutes_ago=i + 1) for i in range(RECENT_ROWS)]
+ [_usage(minutes_ago=OLD_ROW_AGE_DAYS * 24 * 60)]
)
seed.commit()
seed.close()
@contextmanager
def _ctx(username=None, password=None, **kwargs):
with Session() as session:
yield session
with patch(
"local_deep_research.web.routers.context_overflow_api"
".get_user_db_session",
_ctx,
):
yield
def _overview(client, query=""):
resp = client.get(OVERVIEW_URL + query)
assert resp.status_code == 200, (
f"GET {OVERVIEW_URL}{query} -> {resp.status_code} {resp.text[:300]}"
)
body = resp.json()
assert body["status"] == "success", body
return body
# ---------------------------------------------------------------------------
# 4. Integer coercion, on the live route
# ---------------------------------------------------------------------------
class TestIntCoercionOnALiveRoute:
def test_seed_is_visible_and_defaults_apply(self, auth_client):
"""Positive control for every table below.
Establishes that (a) the route reaches the seeded rows, (b) the
five recent rows are inside the default 30d window and the old
one is not, and (c) the echoed pagination with NO query params is
the documented default. Without this, a table row asserting
``per_page == 50`` could be passing on an empty database.
"""
with _seeded_db():
body = _overview(auth_client)
assert body["pagination"]["page"] == 1
assert body["pagination"]["per_page"] == 50
assert body["pagination"]["total_count"] == RECENT_ROWS, (
"the default 30d window must contain exactly the recent rows; "
f"got {body['pagination']['total_count']}"
)
assert len(body["all_requests"]) == RECENT_ROWS
@pytest.mark.parametrize(
("raw", "expected_per_page"),
[
# Left column: what the client sent. Right column: what the
# route must resolve it to, = clamp(TYPE_INT_ORACLE value)
# with the route's documented [1, 500] bounds. Every one of
# these matches what Flask's `args.get("per_page", 50,
# type=int)` + the same clamp produced on main.
("abc", 50), # unparseable -> default, NOT a 500
("", 50), # present-but-empty -> default
("5.0", 50), # float syntax -> default
("0x10", 50), # base prefix -> default
("0", 1), # parses to 0, clamped up to 1
("-7", 1), # negative clamped up; LIMIT -1 = "no limit"
("99999", 500), # clamped down to the 500 ceiling
("%20200%20", 200), # int() strips whitespace
("+5", 5), # '+' -> ' ', int(" 5") == 5
("1_0", 10), # PEP 515 underscore accepted
("%EF%BC%95", 5), # fullwidth digit
("%D9%A1%D9%A2", 12), # arabic-indic digits
("9" * 40, 500), # bignum parses, then clamps
],
)
def test_per_page_matches_flask_type_int_then_clamp(
self, auth_client, raw, expected_per_page
):
with _seeded_db():
body = _overview(auth_client, f"?per_page={raw}")
assert body["pagination"]["per_page"] == expected_per_page, (
f"?per_page={raw} resolved to "
f"{body['pagination']['per_page']} but Flask's type=int plus "
f"the route's [1,500] clamp gives {expected_per_page}"
)
def test_unparseable_values_fall_back_instead_of_500ing(self, auth_client):
"""The shape difference, end to end, on both int params at once.
A bare ``int()`` without the ``except (TypeError, ValueError)``
raises inside the handler and the route's outer
``except Exception`` answers 500 — a client-side typo reported as
a backend outage. Flask's ``type=int`` could not do that.
"""
with _seeded_db():
resp = auth_client.get(
OVERVIEW_URL + "?page=abc&per_page=xyz&period=nonsense"
)
assert resp.status_code == 200, (
f"malformed pagination must not 500; got {resp.status_code} "
f"{resp.text[:300]}"
)
pagination = resp.json()["pagination"]
assert (pagination["page"], pagination["per_page"]) == (1, 50), (
f"unparseable page/per_page must fall back to (1, 50); got "
f"{(pagination['page'], pagination['per_page'])}"
)
def test_page_is_clamped_where_main_overflowed(self, auth_client):
"""``?page=10**40`` is 200 here; on main it was a 500.
main did ``max(1, request.args.get("page", 1, type=int))`` with no
ceiling, so an astronomically large (but perfectly parseable)
page reached ``.offset()`` and SQLite raised OverflowError
converting it to a signed 64-bit int. The port added ``_MAX_PAGE``
with a comment saying exactly that. This is the one intentional
behaviour change in query-param handling, so pin it: the page
echoes back clamped, and the response is a success.
"""
huge = "1" + "0" * 40
with _seeded_db():
body = _overview(auth_client, f"?page={huge}")
assert body["pagination"]["page"] == MAX_PAGE, (
f"?page={huge} must clamp to _MAX_PAGE ({MAX_PAGE}); got "
f"{body['pagination']['page']}"
)
assert body["all_requests"] == [], (
"a clamped page far past the end must return no rows"
)
def test_page_offsets_the_real_result_set(self, auth_client):
"""Positive control that ``page`` is not merely echoed.
Without this, the duplicate-``page`` test below could pass on a
route that parsed the value and then ignored it.
"""
with _seeded_db():
first = _overview(auth_client, "?per_page=2&page=1")
second = _overview(auth_client, "?per_page=2&page=2")
last = _overview(auth_client, "?per_page=2&page=3")
assert len(first["all_requests"]) == 2
assert len(second["all_requests"]) == 2
assert len(last["all_requests"]) == RECENT_ROWS - 4
ids = [r["timestamp"] for r in first["all_requests"]]
assert ids and ids != [
r["timestamp"] for r in second["all_requests"]
], "page=1 and page=2 must return different rows"
# ---------------------------------------------------------------------------
# 5. Repeated parameters, on the live route
# ---------------------------------------------------------------------------
class TestLiveRouteDuplicates:
"""The divergence from section 2, costed out on a real endpoint.
Each test sends both values alone first (so the assertion is reading a
live parameter), then sends them together and pins the LAST one.
"""
def test_repeated_per_page_takes_the_last_value(self, auth_client):
"""``?per_page=2&per_page=4`` returns 4 rows. main returned 2."""
with _seeded_db():
alone_2 = _overview(auth_client, "?per_page=2")
alone_4 = _overview(auth_client, "?per_page=4")
both = _overview(auth_client, "?per_page=2&per_page=4")
assert len(alone_2["all_requests"]) == 2, "positive control: per_page=2"
assert len(alone_4["all_requests"]) == 4, "positive control: per_page=4"
assert both["pagination"]["per_page"] == 4, (
"Starlette resolves a repeated key to its LAST value; a 2 here "
"means first-wins (Werkzeug) semantics are back"
)
assert len(both["all_requests"]) == 4, (
"and the last-wins value must be the one that reached SQL — "
f"got {len(both['all_requests'])} rows"
)
def test_repeated_page_takes_the_last_value(self, auth_client):
"""The same rule on the offset param, where it changes WHICH rows
come back rather than how many."""
with _seeded_db():
alone_1 = _overview(auth_client, "?per_page=2&page=1")
alone_3 = _overview(auth_client, "?per_page=2&page=3")
both = _overview(auth_client, "?per_page=2&page=1&page=3")
assert len(alone_1["all_requests"]) == 2, "positive control: page=1"
assert len(alone_3["all_requests"]) == RECENT_ROWS - 4
assert both["pagination"]["page"] == 3
assert both["all_requests"] == alone_3["all_requests"], (
"the LAST page value must win; matching page=1's rows would "
"mean Werkzeug first-wins semantics"
)
def test_repeated_period_silently_changes_the_time_window(
self, auth_client
):
"""The scariest instance of the divergence: a whitelisted filter.
``?period=all&period=7d`` selects the 7-day window here and
selected "all" on main. Whitelist validation does not help — both
values are valid, so nothing warns. The 300-day-old seeded row is
the detector: it is in the result set under ``all`` and absent
under ``7d``.
"""
with _seeded_db():
all_only = _overview(auth_client, "?period=all&per_page=500")
week_only = _overview(auth_client, "?period=7d&per_page=500")
both = _overview(auth_client, "?period=all&period=7d&per_page=500")
assert all_only["pagination"]["total_count"] == RECENT_ROWS + 1, (
"positive control: period=all must reach the 300-day-old row"
)
assert week_only["pagination"]["total_count"] == RECENT_ROWS, (
"positive control: period=7d must exclude the old row"
)
assert both["pagination"]["total_count"] == RECENT_ROWS, (
"last-wins means the 7d window applies; "
f"{RECENT_ROWS + 1} would mean 'all' won, i.e. main's "
"first-wins semantics"
)
# ---------------------------------------------------------------------------
# 6. Empty / whitespace / case, on the live route
# ---------------------------------------------------------------------------
class TestEmptyWhitespaceAndCase:
def test_empty_period_is_not_treated_as_absent(self, auth_client):
"""``?period=`` reaches the whitelist as ``""`` and fails it.
Both ``?period=`` and no ``period`` at all end up on the 30d
window here, but by different routes: absent -> default,
empty -> whitelist rejection. They are only indistinguishable
because the fallback happens to equal the default. The pin that
matters is that ``""`` is DELIVERED to the handler, asserted at
the datastructure level in section 1; here we pin that neither
spelling errors or widens the window.
"""
with _seeded_db():
empty = _overview(auth_client, "?period=")
absent = _overview(auth_client)
assert empty["pagination"]["total_count"] == RECENT_ROWS, (
"an empty period must land on 30d, not on 'all' — a count of "
f"{RECENT_ROWS + 1} means the old row leaked in"
)
assert (
empty["pagination"]["total_count"]
== absent["pagination"]["total_count"]
)
@pytest.mark.parametrize("raw", ["%20all%20", "ALL", "All", "+all"])
def test_period_is_matched_verbatim_with_no_implicit_normalisation(
self, auth_client, raw
):
"""Neither framework strips or case-folds a query value.
``?period=%20all%20`` and ``?period=ALL`` are NOT ``all``: they
miss the whitelist and fall back to 30d, so the 300-day-old row
stays out. Pinned because a "tidy up the input" patch
(``.strip().lower()``) would quietly widen this window for anyone
who has such a URL bookmarked — and because the sibling
``/settings/api/available-search-engines`` DOES strip+lower its
``egress_scope``, so the two conventions coexist in one codebase.
"""
with _seeded_db():
body = _overview(auth_client, f"?period={raw}&per_page=500")
exact = _overview(auth_client, "?period=all&per_page=500")
assert exact["pagination"]["total_count"] == RECENT_ROWS + 1, (
"positive control: the exact spelling 'all' widens the window"
)
assert body["pagination"]["total_count"] == RECENT_ROWS, (
f"?period={raw} must NOT be normalised into 'all'; got "
f"{body['pagination']['total_count']} rows"
)
# ---------------------------------------------------------------------------
# 7. Static sweep of the call sites a live test cannot afford to boot
# ---------------------------------------------------------------------------
def _router_sources():
"""(path, AST) for every shipped router module.
Located through the imported package, so a stale checkout cannot be
read by accident and a mutated copy on PYTHONPATH IS read — which is
what makes the negative controls in the module docstring work.
"""
for path in sorted(ROUTERS_DIR.glob("*.py")):
yield path, ast.parse(path.read_text(encoding="utf-8"))
def _caught_names(handler) -> set:
node = handler.type
if node is None:
return {"BARE"}
if isinstance(node, ast.Name):
return {node.id}
if isinstance(node, ast.Tuple):
return {e.id for e in node.elts if isinstance(e, ast.Name)}
return set()
def _numeric_query_reads(tree):
"""Yield (lineno, func_name, guards) for every ``int()``/``float()``
call whose argument reads ``request.query_params``.
``guards`` is the union of exception names caught by the enclosing
``try`` blocks.
"""
found = []
def walk(node, guards, fn):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
fn = node.name
if isinstance(node, ast.Try):
caught = set()
for handler in node.handlers:
caught |= _caught_names(handler)
for child in node.body:
walk(child, guards | caught, fn)
for handler in node.handlers:
for child in handler.body:
walk(child, guards, fn)
for child in node.orelse + node.finalbody:
walk(child, guards, fn)
return
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id in ("int", "float")
and "query_params" in ast.dump(node)
):
found.append((node.lineno, fn, guards))
for child in ast.iter_child_nodes(node):
walk(child, guards, fn)
walk(tree, frozenset(), None)
return found
#: The one deliberate exception: ``notes._clamp_limit`` documents that it
#: RAISES ValueError and that each caller wraps it. Listed by name so the
#: exemption is reviewed rather than inferred; the test below checks that
#: every call site of it really is wrapped.
UNGUARDED_BY_DESIGN = {("notes.py", "_clamp_limit")}
class TestEveryIntQueryReadIsGuarded:
"""Flask's ``type=int`` could not raise; the port's ``int()`` can.
24 call sites across 12 router modules do this coercion. Booting each
of their routes is not affordable, and the question — "is this call
lexically inside a ``try`` that catches ValueError/TypeError?" — is
answered exactly by the AST. ``/api/context-overflow`` is the worked
example in section 4; this is the sweep.
``except Exception`` alone does NOT count: on these routes it is the
outer handler that logs and returns 500, which is precisely the
behaviour that differs from Flask.
"""
def test_the_sweep_actually_finds_the_call_sites(self):
"""Control for the AST walker itself.
A walker that silently matched nothing would make the guard test
below vacuously green. Anchor it on a site verified by hand.
"""
by_file = {
path.name: _numeric_query_reads(tree)
for path, tree in _router_sources()
}
total = sum(len(v) for v in by_file.values())
assert total >= 20, (
f"the AST sweep found only {total} numeric query-param reads "
"across web/routers/; it has stopped matching the code"
)
overflow = by_file["context_overflow_api.py"]
assert len(overflow) == 2, (
"context_overflow_api.py has exactly two (page, per_page); "
f"the sweep found {overflow}"
)
assert all(
{"ValueError", "TypeError"} & guards for _, _, guards in overflow
), overflow
def test_every_int_query_param_read_is_guarded(self):
offenders = []
for path, tree in _router_sources():
for lineno, fn, guards in _numeric_query_reads(tree):
if (path.name, fn) in UNGUARDED_BY_DESIGN:
continue
if not ({"ValueError", "TypeError"} & guards):
offenders.append(f"{path.name}:{lineno} in {fn}()")
assert not offenders, (
"int()/float() on a query param without an enclosing "
"`except (TypeError, ValueError)` — Flask's type=int fell back "
"to the default here, this 500s instead:\n "
+ "\n ".join(offenders)
)
def test_the_one_exemption_is_wrapped_at_every_call_site(self):
"""``notes._clamp_limit`` raises by contract; prove the callers
catch it, so the exemption above is not a hole."""
path = ROUTERS_DIR / "notes.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
unwrapped = []
def walk(node, guards):
if isinstance(node, ast.Try):
caught = set()
for handler in node.handlers:
caught |= _caught_names(handler)
for child in node.body:
walk(child, guards | caught)
for handler in node.handlers:
for child in handler.body:
walk(child, guards)
for child in node.orelse + node.finalbody:
walk(child, guards)
return
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "_clamp_limit"
):
if not ({"ValueError", "TypeError"} & guards):
unwrapped.append(node.lineno)
for child in ast.iter_child_nodes(node):
walk(child, guards)
walk(tree, frozenset())
# Control: the call sites exist at all, so "no offenders" is not
# "nothing was inspected".
source = path.read_text(encoding="utf-8")
call_count = source.count("_clamp_limit(request")
assert call_count >= 3, (
f"expected several _clamp_limit call sites, found {call_count}"
)
assert not unwrapped, (
"notes._clamp_limit raises ValueError by contract but these "
f"call sites do not catch it (lines {unwrapped}) — a "
"non-numeric ?limit would 500 there"
)
class TestArrayStyleKeys:
def test_array_style_keys_are_read_with_getlist(self):
"""A ``[]``-suffixed key read with ``.get()`` silently keeps ONE
value — and on this branch it would keep the LAST one.
``keys[]`` is the only array-style parameter the app serves
(``/settings/api/bulk``, sent by ``static/js/services/help.js`` as
one ``keys[]=`` per key). Reading it with ``.get()`` compiles,
returns 200, and drops every key but one; under Werkzeug the
survivor was the first, here it is the last. Neither is what the
caller asked for.
"""
scalar_reads = []
getlist_reads = []
for path, tree in _router_sources():
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
if not isinstance(node.func, ast.Attribute):
continue
if node.func.attr not in ("get", "getlist"):
continue
if "query_params" not in ast.dump(node.func.value):
continue
if not node.args or not isinstance(node.args[0], ast.Constant):
continue
key = node.args[0].value
if not isinstance(key, str) or not key.endswith("[]"):
continue
target = (
getlist_reads
if node.func.attr == "getlist"
else scalar_reads
)
target.append(f"{path.name}:{node.lineno} {key!r}")
# Control: the sweep sees the one array-style key that exists, so
# an empty `scalar_reads` means "checked and clean", not
# "matched nothing".
assert getlist_reads, (
"no `[]`-suffixed query key is read with getlist() anywhere in "
"web/routers/ — either keys[] moved or this sweep is broken"
)
assert any("keys[]" in entry for entry in getlist_reads), (
f"expected the keys[] getlist read; found {getlist_reads}"
)
assert not scalar_reads, (
"array-style query keys must be read with getlist(), not "
"get() — get() keeps only the last value on Starlette:\n "
+ "\n ".join(scalar_reads)
)