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.

fix(notes): bound dict-shaped values in the log-value preview (#6191)

LearningCircuit9e439eee6df7
diff --git a/changelog.d/+notes-log-preview-dict-bound.security.md b/changelog.d/+notes-log-preview-dict-bound.security.md
new file mode 100644
index 0000000000..01e9da40fe
--- /dev/null
+++ b/changelog.d/+notes-log-preview-dict-bound.security.md
@@ -0,0 +1 @@
+Fixed a gap in the notes-API audit-log preview helper (`_log_value_preview`, #5497): a **dict**-shaped rejected value still cost O(N log N) time and touched every element, because `reprlib`'s `repr_dict` runs an internal `sorted()` over the whole container before truncating it. A wide, flat attacker-sized dict now previews in well under a millisecond instead of ~10-25 ms, and a truncated preview now states *what* was truncated: an ending of `, ...}` / `, ...]` / `, ...)` means entries were omitted from some container (the payload was oversized at the item level — flat or nested anywhere in the value), while a bare `...` ending means the value merely exceeded the 100-char preview length with every container the preview walk visits — those below the depth cap — at or under the entry limit. The two endings differ in their final character and so can never be byte-equal; previews that fit are emitted unchanged. Scope, so this is not read as broader than it is: the guarantee covers containers up to the preview depth cap (`maxlevel` 4) — a container nested at or past the cap collapses to `{...}` regardless of its item count — and the fix bounds container *breadth* only; a deeply *nested* value (e.g. a 10-way fanout five levels down, ~1 MB of JSON) is bounded by `reprlib`'s own `maxlevel` at a few milliseconds both before and after this change, and is deliberately left unchanged here because bounding it further is a design change rather than a fix. `set`/`frozenset` values are bounded by the same code, but no JSON body can deliver one (JSON has no set literal), so the reachable win is the dict case.
diff --git a/src/local_deep_research/web/routers/notes.py b/src/local_deep_research/web/routers/notes.py
index abe3f17a8b..7d42e82e63 100644
--- a/src/local_deep_research/web/routers/notes.py
+++ b/src/local_deep_research/web/routers/notes.py
@@ -21,6 +21,7 @@ Notes on the port:
"""
import asyncio
+import itertools
import json
import math
import reprlib
@@ -99,11 +100,17 @@ def _clamp_text_query(value, name, max_len=MAX_SEARCH_LEN):
# problem for a huge list/dict: ``repr()`` walks and stringifies every
# element before any truncation happens.
#
-# ``reprlib.Repr`` avoids this: its container/string methods slice or
-# ``itertools.islice`` BEFORE recursing/stringifying (see
-# ``reprlib.Repr.repr_str`` / ``repr_list`` / ``repr_dict`` in the stdlib),
-# so cost is bounded by the limits below regardless of the input's real
-# size. ``maxlevel`` also caps recursion depth for a deeply nested dict.
+# ``reprlib.Repr`` avoids this for STRINGS and LISTS/TUPLES only: its
+# ``repr_str`` slices, and ``repr_list``/``repr_tuple`` ``itertools.islice``,
+# BEFORE recursing/stringifying (see ``reprlib.Repr.repr_str`` /
+# ``repr_list`` in the stdlib), so those costs are bounded by the limits
+# below regardless of the input's real size. It does NOT hold for
+# dict/set/frozenset: ``repr_dict`` / ``repr_set`` / ``repr_frozenset``
+# call ``reprlib._possibly_sorted()`` — a plain ``sorted()`` over the FULL
+# container — and slice to ``maxdict``/``maxset``/``maxfrozenset`` only
+# afterwards. That is why values reach ``reprlib`` here only through the
+# bounded walk in ``_bound_dict_shaped_walk`` below. ``maxlevel`` also caps
+# recursion depth for a deeply nested dict.
_log_value_repr = reprlib.Repr(
maxlevel=4,
maxtuple=10,
@@ -125,12 +132,214 @@ def _log_value_preview(value: Any) -> str:
which this router allows up to ~100 MB). See ``_log_value_repr`` above
for why plain ``repr()[:100]`` is unsafe here.
- The final ``[:100]`` is a cheap backstop, not the primary defense — the
- limits on ``_log_value_repr`` already keep the *work* bounded; this just
- keeps the *output* bounded too (e.g. a container's per-element reprs can
- still sum past 100 chars before ellipsis is added).
+ ``value`` is pre-bounded by ``_bound_dict_shaped_walk`` before it
+ reaches ``reprlib`` — see that function for why reprlib's own limits do
+ not bound dict/set/frozenset cost on their own.
+
+ The final bound is a cheap backstop, not the primary defense — the
+ limits on ``_log_value_repr`` plus the pre-bounding step already keep
+ the *work* bounded; this just keeps the *output* bounded too (a
+ container's ten per-element reprs, plus reprlib's own ``...`` marker,
+ can still sum past 100 chars). But the backstop must not slice
+ truncation *markers* off with the content, and it must keep the two
+ truncation conditions distinguishable. When it truncates, the preview
+ gets one of exactly two endings, each carrying its own meaning:
+
+ * an items-omitted tail — ``', ...}'``, ``', ...]'`` or ``', ...)'``:
+ ENTRIES were dropped from some container (it held strictly more
+ than ``reprlib``'s per-container limit of 10). A repr that already
+ ends in one of ``reprlib``'s own container tails keeps it inside
+ the cap; a repr whose dropped entries are NESTED — the tail sits
+ inside the repr, followed by the outer container's closing
+ delimiters, so it is not the suffix of the whole repr and the
+ tail check cannot see it — instead gets
+ ``_PREVIEW_ITEMS_OMITTED_TAIL`` appended, guided by the
+ ``items_omitted`` flag the pre-bounding walk reports.
+ * a bare ``...`` (``_PREVIEW_ELLIPSIS``): LENGTH-only truncation —
+ no visited container lost entries; the repr simply exceeded the
+ char cap.
+
+ The two endings differ in their final character and so can never be
+ byte-equal: an 11-key (or 50,010-key) payload nested anywhere the
+ pre-bounding walk visits always previews differently from the
+ corresponding all-≤10-key payload. Reprs that fit the cap are
+ returned untouched, and the preview never exceeds the cap.
"""
- return _log_value_repr.repr(value)[:100]
+ bounded, items_omitted = _bound_dict_shaped_walk(value, 0)
+ text = _log_value_repr.repr(bounded)
+ if len(text) <= _PREVIEW_MAX_CHARS:
+ return text
+ for tail in _PREVIEW_CONTAINER_TAILS:
+ if text.endswith(tail):
+ return text[: _PREVIEW_MAX_CHARS - len(tail)] + tail
+ if items_omitted:
+ return (
+ text[: _PREVIEW_MAX_CHARS - len(_PREVIEW_ITEMS_OMITTED_TAIL)]
+ + _PREVIEW_ITEMS_OMITTED_TAIL
+ )
+ return (
+ text[: _PREVIEW_MAX_CHARS - len(_PREVIEW_ELLIPSIS)] + _PREVIEW_ELLIPSIS
+ )
+
+
+# One MORE than ``_log_value_repr``'s own maxdict/maxlist/maxset/maxtuple
+# (10), deliberately. reprlib only appends its ``...`` truncation marker
+# when the container handed to it is STRICTLY longer than that limit (``if
+# n > self.maxdict: pieces.append(self.fillvalue)``). Pre-truncating to
+# exactly 10 would make an attacker's 400,000-key body render byte-identical
+# to a legitimate 10-key one — silently erasing "there was more" from the
+# audit line, which is the one thing this log site exists to record. Keeping
+# 11 costs nothing (reprlib discards the 11th) and restores the marker.
+# ``_PREVIEW_MAX_DEPTH`` matches ``_log_value_repr``'s ``maxlevel``.
+_PREVIEW_MAX_ITEMS = 11
+_PREVIEW_MAX_DEPTH = 4
+# Final output cap for ``_log_value_preview`` (previously an inline
+# ``[:100]`` slice).
+_PREVIEW_MAX_CHARS = 100
+# Matches reprlib's ``fillvalue`` and the ``...`` marker convention used
+# throughout this module.
+_PREVIEW_ELLIPSIS = "..."
+# reprlib's "the container had more items than I showed" tails — its ``...``
+# fillvalue joined onto the container's closing bracket. When the final
+# char bound bites, preserving the tail keeps an item-bounded payload (e.g.
+# a 50,010-key dict whose first ten ~100-char values fill the cap on their
+# own) distinguishable from a merely char-bounded one, instead of slicing
+# the marker off entirely.
+_PREVIEW_CONTAINER_TAILS = (", ...}", ", ...]", ", ...)")
+# The nested counterpart to the tails above. When the pre-bounding walk
+# reports that some container it visited held more entries than
+# ``reprlib``'s per-container limit (so entries were dropped) but the
+# ``reprlib`` fill sits INSIDE the repr — followed by the outer
+# container's closing delimiters, not at the very end — the tail check
+# in ``_log_value_preview`` cannot preserve it. Appending this explicit
+# tail keeps such a payload (e.g. ``{"outer": {<11 keys with ~100-char
+# values>}}``) byte-distinguishable from the corresponding all-≤10-key
+# payload, whose only truncation is the char cap and which therefore
+# ends in a bare ``...``. Deliberately identical to the dict tail: both
+# spell "entries were omitted".
+_PREVIEW_ITEMS_OMITTED_TAIL = ", ...}"
+
+
+def _bound_dict_shaped_walk(value: Any, _depth: int) -> tuple[Any, bool]:
+ """Return a size-bounded copy of ``value``, safe to hand to
+ ``_log_value_repr.repr()``, plus an "entries were dropped" flag.
+
+ ``reprlib.Repr`` bounds *string* and *list/tuple* reprs by slicing
+ BEFORE recursing (see ``repr_str`` / ``repr_list`` in the stdlib), so
+ those are already O(1) regardless of input size — the module comment
+ above relies on that. But ``repr_dict`` / ``repr_set`` /
+ ``repr_frozenset`` call stdlib ``reprlib._possibly_sorted()`` — which is
+ a plain ``sorted(x)`` over the FULL container — and only slice to
+ ``maxdict``/``maxset``/``maxfrozenset`` afterwards. For a dict/set built
+ from a large attacker-controlled JSON body (this router allows up to
+ ~100 MB, e.g. ``{"is_collapsed": {<N keys>}}`` reaching
+ ``patch_note_research``) that reintroduces the exact O(N log N) time /
+ O(N) memory cost ``_log_value_repr`` exists to eliminate — measured at
+ ~10-22 ms for a 400k-entry dict/set on py3.14, scaling linearly, versus
+ ~0 ms for an equivalently large string or list through the same helper.
+
+ Fix: truncate every dict/set/frozenset to ``_PREVIEW_MAX_ITEMS`` entries
+ OURSELVES — via ``itertools.islice`` over the container's natural
+ iteration order (insertion order for dicts, arbitrary-but-cheap for
+ sets), never ``sorted()`` — before reprlib ever sees it. Once bounded
+ this way, reprlib's own ``repr_dict``/``repr_set`` still calls
+ ``sorted()``, but over at most ``_PREVIEW_MAX_ITEMS`` items, which is
+ negligible.
+
+ Recurses into dict values and list/tuple elements (only the first
+ ``_PREVIEW_MAX_ITEMS`` of each, mirroring reprlib's own slicing) up to
+ ``_PREVIEW_MAX_DEPTH`` — matching ``_log_value_repr``'s ``maxlevel`` —
+ so a large dict/set nested a few levels down (e.g. inside a list inside
+ a dict) is bounded too, not just one sitting at the top level. The
+ number of containers copied is the geometric sum
+ ``1 + M + M**2 + M**3`` for ``M = _PREVIEW_MAX_ITEMS`` (1,464 at the
+ current limits), NOT ``M ** _PREVIEW_MAX_DEPTH``, regardless of the real
+ structure's size. Dict/set *keys* are returned unbounded: JSON object
+ keys are always strings, and a huge individual string key is already
+ handled cheaply by reprlib's own ``repr_str`` slicing, so there is
+ nothing to pre-bound there.
+
+ Past ``_PREVIEW_MAX_DEPTH`` the walk stops and the sub-value is handed
+ to reprlib untouched. For a **dict** that costs nothing: ``repr_dict``
+ checks ``level <= 0`` *before* calling ``_possibly_sorted``, so an
+ over-deep dict collapses to ``{...}`` unsorted. It is NOT free for a
+ **set/frozenset**: ``repr_set``/``repr_frozenset`` call
+ ``_possibly_sorted`` at the top of the method, *before*
+ ``_repr_iterable`` gets to check ``level``, so a huge set nested past
+ the cap is still sorted in full and the result thrown away (measured
+ ~13 ms for ``[[[[set(range(400_000))]]]]``, output ``[[[[{...}]]]]``).
+ Likewise, set/frozenset *elements* are never recursed into, so a
+ container inside a set — a frozenset or tuple, the only hashable
+ candidates — stays unbounded at any depth (~13 ms for
+ ``{frozenset(range(400_000))}``). Neither shape is reachable from this
+ router (JSON has no set literal), but both matter if this helper is
+ reused elsewhere.
+
+ Values that are not dict/set/frozenset/list/tuple (str, int, float,
+ bool, None) pass through unchanged — reprlib's ``maxstring`` /
+ ``maxlong`` bound those. ``maxlong`` only slices an ALREADY-built
+ string, though: ``reprlib.repr_int`` calls ``builtins.repr(x)`` in full
+ before truncating, so a huge ``int`` is the one JSON-native type whose
+ repr cost this helper does not bound (measured ~2 s for a 2,000,000-
+ digit int). It is unreachable from this router only because
+ ``json.loads`` refuses an integer literal longer than
+ ``sys.get_int_max_str_digits()`` (4300 by default). Nor does the
+ pass-through extend to an arbitrary object: reprlib has no per-type
+ method for a custom class, so it falls through to ``repr_instance``,
+ which calls ``builtins.repr(x)`` in FULL and ignores ``level``
+ entirely — a hostile ``__repr__`` can allocate whatever it likes
+ (measured ~170 ms / 200 MB) before reprlib truncates the resulting
+ string. Nothing here bounds that. It is unreachable at this router's
+ call sites, whose values all come out of ``json.loads`` and so can only
+ be JSON-native types, but any reuse of this helper at a call site
+ logging a non-JSON-sourced value must account for it.
+
+ Returns ``(bounded_value, items_omitted)``. ``items_omitted`` is True
+ iff any dict/set/frozenset/list/tuple visited at a depth ``reprlib``
+ still enumerates (below ``_PREVIEW_MAX_DEPTH``, which matches its
+ ``maxlevel``) holds at least ``_PREVIEW_MAX_ITEMS`` entries — i.e.
+ strictly more than ``reprlib``'s per-container limit of one less, the
+ condition under which ``reprlib`` drops entries and appends its
+ ``...`` fill. ``_log_value_preview`` uses the flag to give over-cap
+ reprs whose item truncation is nested (fill not at the end of the
+ repr) the explicit ``_PREVIEW_ITEMS_OMITTED_TAIL`` ending.
+ Containers at or past the depth cap are handed to ``reprlib``
+ untouched and collapse there to ``{...}`` / ``[...]`` wholesale — no
+ per-entry output at any item count — so they do not set the flag.
+ """
+ if _depth >= _PREVIEW_MAX_DEPTH:
+ return value, False
+ if isinstance(value, dict):
+ bounded: dict[Any, Any] = {}
+ omitted = len(value) >= _PREVIEW_MAX_ITEMS
+ for k, v in itertools.islice(value.items(), _PREVIEW_MAX_ITEMS):
+ bounded[k], child_omitted = _bound_dict_shaped_walk(v, _depth + 1)
+ omitted = omitted or child_omitted
+ return bounded, omitted
+ if isinstance(value, (set, frozenset)):
+ # Bound the count only — set/frozenset elements are NOT recursed
+ # into (see the docstring: a frozenset or tuple nested inside a set
+ # therefore stays unbounded; unreachable from JSON input, which has
+ # no set literal). Rebuilt as a plain ``set``/``frozenset`` rather
+ # than ``type(value)(...)``, because a set SUBCLASS with a custom
+ # ``__init__`` would come back silently EMPTY or raise ``TypeError``
+ # from inside a logging call — turning a 400 into a 500.
+ bounded_items = itertools.islice(value, _PREVIEW_MAX_ITEMS)
+ if isinstance(value, frozenset):
+ return frozenset(bounded_items), len(value) >= _PREVIEW_MAX_ITEMS
+ return set(bounded_items), len(value) >= _PREVIEW_MAX_ITEMS
+ if isinstance(value, (list, tuple)):
+ bounded = []
+ omitted = len(value) >= _PREVIEW_MAX_ITEMS
+ for v in itertools.islice(value, _PREVIEW_MAX_ITEMS):
+ bounded_item, child_omitted = _bound_dict_shaped_walk(v, _depth + 1)
+ bounded.append(bounded_item)
+ omitted = omitted or child_omitted
+ # Rebuilt as a plain ``tuple``, so a namedtuple degrades to a
+ # bare tuple in the preview — acceptable for an audit line.
+ bounded_tuple = tuple(bounded) if isinstance(value, tuple) else bounded
+ return bounded_tuple, omitted
+ return value, False
# Rate limits for AI-heavy endpoints. Split into two buckets so cheap
diff --git a/tests/notes/test_log_value_preview_dict_bound.py b/tests/notes/test_log_value_preview_dict_bound.py
new file mode 100644
index 0000000000..7f41fdf643
--- /dev/null
+++ b/tests/notes/test_log_value_preview_dict_bound.py
@@ -0,0 +1,447 @@
+"""Regression tests for the dict/set-shaped-value blind spot in
+``_log_value_preview`` (``web/routers/notes.py``).
+
+Follow-up to #5497 (merged b7e0ac4a049a), which replaced the naive
+``repr(v)[:100]`` audit-log preview with a ``reprlib.Repr``-based helper to
+avoid materializing a full repr of an up-to-~100MB attacker-controlled value
+before slicing it. That fix covers strings and lists/tuples, whose
+``reprlib`` methods slice BEFORE recursing. It does NOT cover dicts, sets,
+or frozensets: CPython's ``reprlib.Repr.repr_dict`` / ``repr_set`` /
+``repr_frozenset`` call ``reprlib._possibly_sorted()`` (a plain
+``sorted(x)``) over the ENTIRE container before slicing to
+``maxdict``/``maxset``/``maxfrozenset`` — so a large dict/set reaching the
+helper (e.g. ``{"is_collapsed": {<N keys>}}`` on
+``PATCH /api/notes/<id>/research/<id>``) still costs O(N log N) time and
+touches every element, reintroducing the exact class of cost #5497 exists
+to eliminate.
+
+These tests exercise the fix directly against the pure helper functions
+(no HTTP client / DB needed) so they stay fast and deterministic.
+
+IMPORTANT for future edits: ``reprlib`` bounds the *output* even when it
+does the O(N log N) *work*, so ``isinstance(preview, str)`` and
+``len(preview) <= 100`` hold identically whether or not the fix is wired
+up — they are NOT regression guards. Every test below is instead written
+to fail under at least one of these weakenings:
+
+ (W1) un-wiring the fix: ``_log_value_repr.repr(value)[:100]``
+ (W2) replacing ``itertools.islice`` with the ``sorted()`` this PR removes
+ (W3) inflating ``_PREVIEW_MAX_ITEMS``
+ (W4) dropping (or ignoring) the ``items_omitted`` flag, which
+ re-collapses a nested 11-key payload onto its 10-key twin
+ (W5) raising ``_PREVIEW_MAX_DEPTH`` past ``reprlib``'s ``maxlevel``,
+ which inflates the container-copy bound the walk exists to cap
+ while adding nothing reprlib would still render
+ (W6) dropping the ``_PREVIEW_CONTAINER_TAILS`` branch, which mislabels
+ an over-cap list/tuple with the dict tail
+
+Each test names the weakening(s) it catches.
+"""
+
+from local_deep_research.web.routers.notes import (
+ _PREVIEW_CONTAINER_TAILS,
+ _PREVIEW_ELLIPSIS,
+ _PREVIEW_ITEMS_OMITTED_TAIL,
+ _PREVIEW_MAX_DEPTH,
+ _PREVIEW_MAX_ITEMS,
+ _bound_dict_shaped_walk,
+ _log_value_preview,
+ _log_value_repr,
+)
+
+# Deliberate literals, not the imported constants: asserting against
+# ``_PREVIEW_MAX_ITEMS`` / ``_PREVIEW_MAX_DEPTH`` would scale with any
+# change to them and so could never catch (W3) or (W5).
+EXPECTED_MAX_ITEMS = 11
+EXPECTED_MAX_DEPTH = 4
+
+
+def _bounded_copy(value):
+ """The bounded copy alone, for tests that ignore the omission flag.
+
+ Production calls ``_bound_dict_shaped_walk`` directly, because
+ ``_log_value_preview`` needs both halves of its return value.
+ """
+ return _bound_dict_shaped_walk(value, 0)[0]
+
+
+def _reverse_ordered_dict(n: int = 1000) -> dict[int, int]:
+ """A dict whose INSERTION order is the reverse of its SORTED order.
+
+ ``{i: i for i in range(n)}`` cannot distinguish ``islice`` from
+ ``sorted`` — the two orders are identical — so every order-sensitive
+ assertion here uses this instead.
+ """
+ return {i: i for i in range(n, 0, -1)}
+
+
+class CountingKey:
+ """Dict key that counts how many ``<`` comparisons it takes part in.
+
+ Gives the tests a real *work* bound with no timing/flakiness: reprlib
+ sorts whatever container it is handed, so the comparison count is a
+ direct measure of how much of the input was touched.
+ """
+
+ comparisons = 0
+
+ def __init__(self, n: int) -> None:
+ self.n = n
+
+ def __lt__(self, other: "CountingKey") -> bool:
+ type(self).comparisons += 1
+ return self.n < other.n
+
+ def __hash__(self) -> int:
+ return hash(self.n)
+
+ def __eq__(self, other: object) -> bool:
+ return isinstance(other, CountingKey) and self.n == other.n
+
+ def __repr__(self) -> str:
+ return f"K{self.n}"
+
+
+class TestPreviewBounds:
+ def test_exceeds_reprlib_limits_by_exactly_one(self):
+ """Catches (W3), and any change back to 10.
+
+ ``_PREVIEW_MAX_ITEMS`` must stay one MORE than reprlib's own
+ per-container limits: reprlib appends its ``...`` marker only when
+ ``n > maxdict`` (etc.), so pre-truncating to exactly the limit
+ deletes the marker from every oversized preview.
+ """
+ assert _PREVIEW_MAX_ITEMS == EXPECTED_MAX_ITEMS
+ for limit in (
+ _log_value_repr.maxdict,
+ _log_value_repr.maxlist,
+ _log_value_repr.maxset,
+ _log_value_repr.maxfrozenset,
+ _log_value_repr.maxtuple,
+ ):
+ assert _PREVIEW_MAX_ITEMS == limit + 1
+
+ def test_max_depth_matches_reprlib_maxlevel(self):
+ """Catches (W5).
+
+ The walk must stop exactly where ``reprlib`` stops enumerating.
+ Below ``maxlevel`` the pre-bounding is what keeps
+ ``repr_dict``/``repr_set`` off the full container; at or past it
+ ``reprlib`` collapses the container to ``{...}`` whatever the walk
+ did, so every extra level only multiplies the number of containers
+ copied (the ``1 + M + M**2 + ...`` bound documented on the walk) for
+ output that is never rendered.
+ """
+ assert _PREVIEW_MAX_DEPTH == EXPECTED_MAX_DEPTH
+ assert _PREVIEW_MAX_DEPTH == _log_value_repr.maxlevel
+
+
+class TestBoundDictShapedWalk:
+ def test_large_dict_is_truncated(self):
+ """Catches (W3) — literal bound, not the imported constant."""
+ bounded = _bounded_copy({i: i for i in range(50_000)})
+ assert isinstance(bounded, dict)
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_large_set_is_truncated(self):
+ """Catches (W3)."""
+ bounded = _bounded_copy(set(range(50_000)))
+ assert isinstance(bounded, set)
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_large_frozenset_is_truncated(self):
+ """Catches (W3)."""
+ bounded = _bounded_copy(frozenset(range(50_000)))
+ assert isinstance(bounded, frozenset)
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_large_list_is_truncated(self):
+ """Catches (W3)."""
+ bounded = _bounded_copy(list(range(50_000)))
+ assert isinstance(bounded, list)
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_bounding_keeps_iteration_order_and_never_sorts(self):
+ """Catches (W2) and (W3).
+
+ The kept keys must be the FIRST ``_PREVIEW_MAX_ITEMS`` in insertion
+ order (1000, 999, ...), not the smallest ones a ``sorted()`` would
+ select (1, 2, ...) — and not all 1000, as an inflated limit would
+ keep.
+ """
+ bounded = _bounded_copy(_reverse_ordered_dict())
+ assert list(bounded) == list(range(1000, 1000 - EXPECTED_MAX_ITEMS, -1))
+
+ def test_small_dict_passes_through_unbounded(self):
+ small = {"a": 1, "b": 2}
+ assert _bounded_copy(small) == small
+
+ def test_scalar_values_pass_through(self):
+ for value in ("hello", 42, 3.14, True, None):
+ assert _bounded_copy(value) == value
+
+ def test_set_subclass_is_rebuilt_as_a_plain_set(self):
+ """Guards the ``type(value)(...)`` footgun.
+
+ A set subclass used to be reconstructed via ``type(value)(...)``,
+ which runs the subclass's own ``__init__`` — so the "bounded" copy
+ could come back with every original element still in it.
+ """
+
+ class SelfPopulatingSet(set):
+ def __init__(self, label):
+ super().__init__(range(50_000))
+ self.label = label
+
+ bounded = _bounded_copy(SelfPopulatingSet("x"))
+ assert type(bounded) is set
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_set_subclass_with_incompatible_init_does_not_raise(self):
+ """``type(value)(...)`` used to raise ``TypeError`` here.
+
+ Raised from inside a ``logger.warning`` call, that turns an
+ intended 400 into a 500 via ``handle_api_error``.
+ """
+
+ class TwoArgSet(set):
+ def __init__(self, elements, label):
+ super().__init__(elements)
+ self.label = label
+
+ bounded = _bounded_copy(TwoArgSet(range(50_000), "x"))
+ assert type(bounded) is set
+ assert len(bounded) == EXPECTED_MAX_ITEMS
+
+ def test_large_dict_nested_in_list_is_truncated(self):
+ """Catches (W3)."""
+ # The exact reachable shape at PATCH /api/notes/<id>/research/<id>:
+ # ``{"is_collapsed": <value>}`` where <value> could itself be a
+ # large dict nested inside a list a caller constructs.
+ nested = [{"is_collapsed": {i: i for i in range(50_000)}}]
+ bounded = _bounded_copy(nested)
+ assert isinstance(bounded, list)
+ assert len(bounded) == 1
+ inner = bounded[0]["is_collapsed"]
+ assert isinstance(inner, dict)
+ assert len(inner) == EXPECTED_MAX_ITEMS
+
+ def test_large_dict_value_nested_in_dict_is_truncated(self):
+ """Catches (W2) and (W3)."""
+ nested = {"outer": _reverse_ordered_dict()}
+ bounded = _bounded_copy(nested)
+ assert len(bounded["outer"]) == EXPECTED_MAX_ITEMS
+ assert list(bounded["outer"])[0] == 1000 # islice, not sorted
+
+ def test_depth_beyond_cap_is_returned_unbounded(self):
+ # Past _PREVIEW_MAX_DEPTH the walk stops recursing and the sub-value
+ # is handed to reprlib as-is. For a DICT that is free: repr_dict
+ # checks ``level <= 0`` before calling _possibly_sorted, so it
+ # collapses to ``{...}`` without sorting. (That is NOT true of
+ # repr_set/repr_frozenset, which sort first and check level after —
+ # see the helper's docstring.)
+ deeply_nested = [[[[[{i: i for i in range(50_000)}]]]]]
+ bounded = _bounded_copy(deeply_nested)
+ # Walk down the bounded list-of-lists to the point where recursion
+ # stopped and assert the dict at the bottom was NOT truncated.
+ node = bounded
+ while isinstance(node, list):
+ node = node[0]
+ assert isinstance(node, dict)
+ assert len(node) == 50_000
+
+
+class TestLogValuePreviewDictShaped:
+ def test_preview_of_large_dict_keeps_insertion_order(self):
+ """Catches (W1), (W2) and (W3) — the core regression guard.
+
+ Under the fix, reprlib only ever sees the first 11 keys in
+ insertion order (1000..990) and sorts those, so the preview starts
+ at 990. Un-wire the bounding step, restore the ``sorted()``, or
+ inflate the limit, and reprlib sorts the whole dict instead and the
+ preview starts at ``{1: 1``.
+ """
+ preview = _log_value_preview(_reverse_ordered_dict())
+ assert preview.startswith("{990: 990, 991: 991")
+
+ def test_preview_of_large_dict_does_not_touch_every_key(self):
+ """Catches (W1), (W2) and (W3) — a *work* bound, not a timing one.
+
+ Measured: 10 comparisons with the fix (an 11-element descending
+ run), 999+ under any of the three weakenings.
+ """
+ big = {CountingKey(i): i for i in range(1000, 0, -1)}
+ CountingKey.comparisons = 0
+ _log_value_preview(big)
+ assert CountingKey.comparisons < 100
+
+ def test_preview_of_oversized_container_keeps_the_ellipsis_marker(self):
+ """Catches a return to ``_PREVIEW_MAX_ITEMS = 10``.
+
+ Without the marker a 400,000-key attacker payload logs a line
+ byte-identical to a legitimate 10-key body, erasing the one fact
+ (#5497) this log site exists to record.
+ """
+ assert _log_value_preview({i: i for i in range(400_000)}).endswith(
+ ", ...}"
+ )
+ assert _log_value_preview(list(range(50_000))).endswith(", ...]")
+ assert _log_value_preview(set(range(50_000))).endswith(", ...}")
+ assert _log_value_preview(tuple(range(50_000))).endswith(", ...)")
+
+ def test_preview_of_long_valued_dict_stays_distinguishable(self):
+ """The gate probe: values long enough that the FINAL char bound bites.
+
+ Ten ~100-char string values overflow the 100-char preview cap on
+ their own, so the final slice did the truncating — and a plain
+ ``[:100]`` sliced reprlib's ``...`` marker off with it, making an
+ 11-key (or 50,010-key) body preview byte-identical to a legitimate
+ 10-key one. The oversized previews must keep a truncation marker
+ and differ from the exactly-10-key one (which is only char-bounded,
+ and marked as such).
+ """
+ ten = {f"k{i}": "v" * 100 for i in range(10)}
+ preview_ten = _log_value_preview(ten)
+ assert preview_ten.endswith("...")
+ assert len(preview_ten) <= 100
+ for n in (11, 50_010):
+ oversized = {f"k{i}": "v" * 100 for i in range(n)}
+ preview = _log_value_preview(oversized)
+ assert preview != preview_ten
+ assert preview.endswith(", ...}")
+ assert len(preview) <= 100
+
+ def test_over_cap_list_and_tuple_keep_their_own_container_tail(self):
+ """Catches (W6) — the only test that reaches the tail branch.
+
+ The oversized containers used above preview in ~35 chars and so
+ return via the ``len(text) <= 100`` fast path, never touching
+ ``_PREVIEW_CONTAINER_TAILS``. Reaching it needs a repr that BOTH
+ overflows the char cap and ends in ``reprlib``'s own fill: a
+ top-level list/tuple of 11 ~100-char strings. Without the branch
+ these fall through to the ``items_omitted`` fallback and get the
+ dict tail ``', ...}'``, mislabelling a list as a mapping in the
+ audit line.
+ """
+ wide_values = ["v" * 100] * EXPECTED_MAX_ITEMS
+ for value, tail in (
+ (wide_values, ", ...]"),
+ (tuple(wide_values), ", ...)"),
+ ):
+ preview = _log_value_preview(value)
+ assert len(preview) == 100
+ assert preview.endswith(tail)
+ # The 10-item twin loses no entries, so it is char-bounded only and
+ # ends in a bare "..." — the pair stays distinguishable.
+ ten = _log_value_preview(["v" * 100] * 10)
+ assert ten.endswith(_PREVIEW_ELLIPSIS)
+ assert not ten.endswith(_PREVIEW_CONTAINER_TAILS)
+
+ def test_preview_of_exactly_max_items_container_has_no_marker(self):
+ """The marker must mean "there was more", not appear unconditionally."""
+ preview = _log_value_preview({i: i for i in range(10)})
+ assert (
+ preview
+ == "{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}"
+ )
+
+ def test_preview_of_large_dict_is_short(self):
+ # Output bound only — deliberately NOT a regression guard (reprlib
+ # bounds the output even when it does the full O(N log N) work).
+ preview = _log_value_preview({i: i for i in range(400_000)})
+ assert isinstance(preview, str)
+ assert len(preview) <= 100
+
+ def test_preview_of_large_set_is_short(self):
+ preview = _log_value_preview(set(range(400_000)))
+ assert isinstance(preview, str)
+ assert len(preview) <= 100
+
+ def test_preview_of_bool_is_unaffected(self):
+ # The common real-world reject value at these call sites (e.g.
+ # is_collapsed="yes", force_reindex="no") — make sure the dict/set
+ # bounding doesn't disturb plain-scalar previews.
+ assert _log_value_preview(True) == "True"
+ assert _log_value_preview("yes") == "'yes'"
+
+
+class TestNestedTruncationDistinguishability:
+ """The nested 10-vs-11 collision (gate round 2). Catches (W4).
+
+ ``reprlib``'s fill (``', ...}'``) for a NESTED truncated container is
+ followed by the outer container's closing delimiters, so it is never
+ the suffix of the whole repr and the tail-preservation branch cannot
+ keep it. Without the ``items_omitted`` flag, the nested 11-key and
+ 10-key payloads below produced byte-identical 100-char previews (both
+ sliced by the generic ``...`` fallback), erasing "entries were dropped"
+ from the audit line — the exact erasure this log site exists to record.
+ """
+
+ @staticmethod
+ def _pair(wrap):
+ """10-key and 11-key inner dicts of ~100-char values, wrapped."""
+ ten = wrap({f"k{i}": "v" * 100 for i in range(10)})
+ eleven = wrap({f"k{i}": "v" * 100 for i in range(11)})
+ return _log_value_preview(ten), _log_value_preview(eleven)
+
+ def test_nested_dict_11_keys_differs_from_10_keys(self):
+ preview_ten, preview_eleven = self._pair(lambda inner: {"outer": inner})
+ assert preview_ten != preview_eleven
+ # No container anywhere exceeded reprlib's limit: the only
+ # truncation is the char cap, marked with a bare "...".
+ assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
+ assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
+ # The inner dict lost its 11th entry: items-omitted tail.
+ assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
+ assert len(preview_ten) <= 100
+ assert len(preview_eleven) <= 100
+
+ def test_nested_dict_50_010_keys_also_carries_the_items_tail(self):
+ big = {"outer": {f"k{i}": "v" * 100 for i in range(50_010)}}
+ preview = _log_value_preview(big)
+ assert preview.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
+ assert len(preview) <= 100
+
+ def test_list_wrapped_nested_dict_11_keys_differs_from_10_keys(self):
+ preview_ten, preview_eleven = self._pair(lambda inner: [inner])
+ assert preview_ten != preview_eleven
+ assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
+ assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
+ assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
+ assert len(preview_ten) <= 100
+ assert len(preview_eleven) <= 100
+
+ def test_dict_in_list_in_dict_nested_11_keys_differs_from_10_keys(self):
+ preview_ten, preview_eleven = self._pair(
+ lambda inner: {"a": [{"b": inner}]}
+ )
+ assert preview_ten != preview_eleven
+ assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
+ assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
+ assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
+ assert len(preview_ten) <= 100
+ assert len(preview_eleven) <= 100
+
+ def test_walk_reports_whether_any_visited_container_lost_entries(self):
+ # Exactly at reprlib's limit: no fill, no flag.
+ bounded, omitted = _bound_dict_shaped_walk(
+ {"outer": {f"k{i}": "v" for i in range(10)}}, 0
+ )
+ assert omitted is False
+ assert len(bounded["outer"]) == 10
+ # One past reprlib's limit — even though nothing is pre-bounded
+ # away (11 <= _PREVIEW_MAX_ITEMS), reprlib drops the 11th entry,
+ # so the walk must already report it.
+ _, omitted = _bound_dict_shaped_walk(
+ {"outer": {f"k{i}": "v" for i in range(11)}}, 0
+ )
+ assert omitted is True
+ # Deeper nesting and a list container report it too.
+ _, omitted = _bound_dict_shaped_walk({"a": [{"b": list(range(11))}]}, 0)
+ assert omitted is True
+
+ def test_nested_container_that_fits_uses_length_only_marker(self):
+ small = {"outer": {f"k{i}": "v" for i in range(10)}}
+ preview = _log_value_preview(small)
+ assert preview.endswith(_PREVIEW_ELLIPSIS)
+ assert not preview.endswith(_PREVIEW_CONTAINER_TAILS)