Skip to content

fix(sanitizer): the key test was still quadratic — one call below the #1670 fix (#2398) - #2405

Closed
dolho wants to merge 1 commit into
devfrom
fix/2398-sanitizer-key-redos
Closed

dolho wants to merge 1 commit into
devfrom
fix/2398-sanitizer-key-redos

Conversation

@dolho

@dolho dolho commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

This is my regression. #1670 fixed the catastrophic backtracking in the credential sanitizer's line scan (#1661) and left the identical shape one call deeper, in _is_sensitive_kv_key. Every one of the 14 py-spy dumps is on that stack.

<genexpr> (credential_sanitizer.py)      <- the any(...) in this function
_is_sensitive_kv_key
_redact_kv_match  ->  sanitize_text  ->  sanitize_dict (recursive)
sanitize_subprocess_line  ->  read_stdout  (headless_executor)

The cost

re.search(r'(?:.*TOKEN.*)\Z', key) retries the leading .* from every offset — O(n²) in key length. Measured on 3.13:

key length old new
4 KB 0.32 s —
16 KB 5.31 s —
44 KB 39.8 s 0.00035 s 112,000×
44 KB (agent copy, 30 patterns) 43.9 s 0.00066 s 67,000×

Forty seconds of CPU for one key, per match, per line, on a thread holding the GIL. That is the pegged core, and it is why the event loop stopped accepting.

Why a quadratic test looked harmless

A wrong docstring. It described key as "a short KEY= name". It is not — the key is whatever preceded an = in _KV_LINE_RE's unbounded ([^\s"'=]+). And the stack above reaches it from stream-json tool results through recursive sanitize_dict, so multi-KB "keys" are the normal case, not an adversarial one. That single wrong assumption is what let the quadratic survive the #1670 review.

The fix

Every pattern is .*LITERAL.* or LITERAL.*. Under .search() with a trailing \Z, both mean exactly "the key contains this literal" — so the test is substring containment: linear, C-level, no backtracking.

Verdicts are unchanged, and that was the part to get right — a faster check that redacts less is a credential leak. Pinned against the old implementation as an oracle: 5,420 keys on the backend copy and 9,607 on the agent copy, zero disagreements, including the deliberate #1670 suffix semantics (DB_ still redacts MY_DB_PASSWORD).

The literals are derived from the patterns and the derivation refuses anything that is not a plain literal — so a future pattern needing real regex fails loudly at import instead of being silently reduced to a check that redacts less. That is the guard #1670 should have had.

Both copies — and the agent one is the one that matters

Invariant #5. The two files are vendored in shape, not byte-identically: the agent list is SENSITIVE_VAR_PATTERNS with 30 entries to the backend's 13. Every py-spy dump was taken in the agent copy, so a backend-only fix would have left all 14 burns exactly where they were. A test asserts the agent copy carries the fix.

What this does and does not close

  • Closes the burn: the hot path is now linear, so the arrival of a large tool result cannot peg a core.
  • Does not address the zombie schedule_executions rows or the missing HEALTHCHECK on the agent image (no auto-recovery). Those are real and separate; the burn was making them visible.
  • The mitigations in place (max_parallel_tasks 10→5, execution_timeout 3600→1800) cap blast radius and are still worth keeping until this ships to the fleet — note the agent copy means a base-image rebuild is required for existing agents.

Test Plan

Related to #2398 · follow-up to #1670 / #1661

…1670 fix (#2398)

py-spy caught this 14 times across three agents (cmo, copywriter, cornelius),
every dump on the same stack:

    <genexpr> (credential_sanitizer.py)      <- the any(...) in this function
    _is_sensitive_kv_key
    _redact_kv_match -> sanitize_text -> sanitize_dict (recursive)
    sanitize_subprocess_line -> read_stdout (headless_executor)

#1670 made the LINE scan linear (#1661) and left the KEY test quadratic:
re.search(r'(?:.*TOKEN.*)\Z', key) retries the leading .* from every offset,
O(n^2) in key length. Measured on 3.13: 4 KB = 0.32s, 44 KB = 40s — for ONE
key, per match, per line, on a thread holding the GIL. That is the pegged core.

What made a quadratic test look harmless was a wrong docstring. It called the
key "a short KEY= name". It is not: the key is whatever preceded an "=" in
_KV_LINE_RE's unbounded capture — and the stack above reaches it from
stream-json tool RESULTS via recursive sanitize_dict, so multi-KB "keys" are
the normal case rather than an adversarial one.

Every pattern is ".*LITERAL.*" or "LITERAL.*", and under .search() with a
trailing \Z both mean exactly "the key CONTAINS this literal" — so the test is
substring containment. Linear, C-level, no backtracking.

VERDICTS ARE UNCHANGED, and that is the part that mattered to get right: a
faster check that redacts LESS is a credential leak. Pinned against the old
implementation as an oracle — 5,420 keys on the backend copy, 9,607 on the
agent copy, zero disagreements — including the deliberate #1670 suffix
semantics (DB_ still redacts MY_DB_PASSWORD).

The literals are DERIVED from the patterns, and the derivation REFUSES anything
that is not a plain literal, so a future pattern needing real regex fails loudly
at import rather than being silently reduced to a check that redacts less.

BOTH copies (Invariant #5). The two are vendored in shape, not byte-identically
— the agent list is SENSITIVE_VAR_PATTERNS with 30 entries to the backend's 13
— and every py-spy dump was taken in the AGENT copy, so a backend-only fix
would have left all 14 burns exactly where they were.

  backend copy:      44 KB key  40s -> 0.00035s  (112,000x)
  agent-server copy: 44 KB key  44s -> 0.00066s  ( 67,000x)

727 passed across the sanitizer, #1661 and #1670 suites.

Related to #2398

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@dolho

dolho commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2406, which targets main.

The burn is on client instances and they run main. Both sanitizer copies on main carry the vulnerable code, and the agent copy's _is_sensitive_kv_key is at line 150 there — exactly the line in every py-spy dump, which confirms which tree the dumps came from.

The two files have diverged between main and dev, so #2406 is written against main's tree and measured there (7,566 + 9,606 keys, 0 disagreements; 704 tests) rather than cherry-picked from this branch.

dev has the identical bug and gets it via the main→dev back-merge, which will likely need a manual resolution on these two files. Branch kept in case we'd rather have a separate dev PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant