Quadratic compression steps (HYBRID TOC fitting, repeated-heading keys, tail anomaly) let a crafted response stall the proxy event loop
Description
Three steps in memtomem-stm's compression tiers do quadratic or worse work in the size of an upstream tool response. These are algorithmic loops, not regexes. Compression runs synchronously on the proxy's event loop, so while one response is being compressed, every other call through the proxy waits.
The worst of them is reached under the default AUTO strategy by markdown with many headings: an 86 KB response stalls the proxy for about 37 s, and 118 KB of one repeated heading for about 73 s. max_upstream_chars defaults to 10,000,000, so it does not bound the input in practice.
Severity: High (availability). No data is exposed. The stall ends when compression finishes. No CVSS vector or CVE is asserted yet.
This is separate from GHSA-7jqx-w58w-8h4p, which covers quadratic regex scans on the same text path. Its fix does not touch these loops.
Affected steps (measured on main 5897eb75, Python 3.13, max_chars=8000)
| step | what it does | crafted input | time |
|---|---|---|---|
HYBRID TOC fitting (HybridCompressor, the TOC tail) | to fit the budget, drops TOC entries one at a time and binary-searches each preview, re-serializing the whole TOC with json.dumps at every step | markdown with distinct headings: 2,000 / 4,000 / 8,000 headings (20 / 42 / 86 KB) | 1.4 s / 7.9 s / 36.7 s |
| same, via repeated headings | same | 4,000 / 8,000 copies of one heading with distinct bodies (58 / 118 KB) | 16.3 s / 73.1 s |
SELECTIVE _unique_key | for each repeated heading, probes the #2, #3, … suffixes from 2 upward | 4,000 / 8,000 / 16,000 copies of one heading (58 / 118 / 244 KB), parse alone | 0.5 s / 2.2 s / 9.8 s |
| TRUNCATE tail-anomaly path | builds the anomalous tail with list.insert(0, …), which shifts every line already collected | "prefix\n" * 2n + "tail\n" * n, 1.2 / 2.4 / 4.8 MB | 0.44 s / 1.44 s / 4.8 s |
AUTO selects HYBRID for markdown with headings above the budget, and TRUNCATE for the repetitive plain text, so every row is reachable without configuration. SELECTIVE _unique_key is reached both through HYBRID and when SELECTIVE is configured directly.
Prerequisites and reach
- The default configuration is affected.
- The content must come from an upstream MCP server the operator configured. The realistic attacker is whoever controls content that such an upstream relays (a fetched web page, an issue body, a repository file), not the upstream's operator.
Fix
All three steps now run in linear time and return byte-identical output.
- HYBRID measures candidate TOCs instead of serializing them. Each entry and the envelope are serialized once, the preview search keeps its probe sequence, and dropped entries are counted with prefix sums.
- SELECTIVE keeps the next suffix per heading.
- TRUNCATE collects the tail with
appendand reverses it once.
The fix is checked against the old code, used as an oracle, on random and shaped TOCs at every budget, and against seven mutations. On the inputs above the step now takes at most 0.08 s (it took 36.7 s / 73.1 s / 8.7 s before), and the tail path grows 4.0x for 4x input (11.5x before).
Discovery
Found during the review of the GHSA-7jqx-w58w-8h4p fix: the HYBRID and _unique_key steps by the plan review, the tail-anomaly path by the implementation review. All three were reproduced and measured independently.