Skip to content

Studio: stop the log redactor backtracking quadratically on a cut ANSI sequence - #10721

Merged
danielhanchen merged 7 commits into
mainfrom
fix/log-redaction-ansi-redos
Sep 13, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
fix/log-redaction-ansi-redos

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

_ANSI_RE in studio/backend/utils/log_redaction.py searched for its string terminators with a lazy [\s\S]*?. An introducer with no terminator scanned to the end of the record, failed, fell through to the single character Fe branch, and the engine retried from the next position. Every introducer therefore paid a full scan of everything after it, so the cost grew with the square of the record length.

Input main this PR
5k of U+009D 0.268 s 0.0043 s
10k of U+009D 1.077 s 0.0089 s
20k of U+009D 4.175 s 0.0158 s
40k of U+009D 16.889 s 0.0334 s
80k of U+009D 72.9 s 0.0475 s
40k of ESC ] 7.431 s 0.0179 s
40k of ordinary text 0.0046 s 0.0046 s
2k colorized log lines 0.0004 s 0.0004 s

Doubling the input quadruples the time, which is the shape rather than the constant.

Why this is reachable

An unterminated introducer is not an exotic input. A log rotated in the middle of a sequence leaves one behind, as does any writer whose output is cut, and U+009D is ordinary UTF-8 (\xc2\x9d). redact_log_text is what guards the Settings > Logs pane, and debug_log_reader.py hands it whole lines once a second while the tab is open, so a log carrying C1 characters costs the pane seconds of CPU per poll. No attacker is required.

The fix

The semantics were never the problem. The retry was.

A lazy scan that FINDS its terminator already succeeds in one pass; only the failing one rescans. So _strip_ansi answers "the earliest terminator at or after k" with a monotonic str.find cache instead of a rescan, which is O(n) for the record, and is otherwise the same machine: same alternatives, same order, same lazy shortest match, same fallthrough to the Fe branch when a control string never terminates. It skips from introducer to introducer rather than character to character, so ordinary text is still walked in C.

The output is identical to the pattern it replaces on every input. That is the whole contract, and it is what the tests assert.

Why it does not also fix the leaks

The first version of this PR did try to improve the truncated cases, and review found four separate P1s in it. Each attempted fix moved a leak rather than removing one, because the strip cannot tell a cut sequence's payload from log text:

  • consuming an aborted body ate the separator out of api_key<cut>=value; not consuming it welded the body onto the key, so api_key read as api_keyfoo
  • dropping a lone ESC joined prefix to api_key; keeping one as a space broke _COOKIE_PAIR_RE (^[A-Za-z0-9_.\-]+=\S) and the presigned URL rule, which read the space as the value's first character
  • a boundary across a whole removed run fires for a run of ordinary colour codes too, so api\x1b[36mkey=value stopped matching
  • a charset designator read the next character as its own final byte, taking the a of api_key, the C of Cookie:, the B of Bearer and the ? of a presigned URL. Every plausible final byte is also a plausible first character of a key, so narrowing the range does not separate them
  • excluding = and : from the aborted body fixed one column and broke another: \x1b^api_key=value then lost the key instead of the separator, 909 records in the corpus

A redactor is the wrong place to smuggle a behaviour change into a performance fix. main does leak on a truncated sequence (api_key\x1b]foo=value is unmasked today), and that is worth fixing, but on its own merits and with its own tests, not folded into a ReDoS fix where every regression is a credential.

Verification

The contract is differential, so the old regex is kept verbatim in the test file as the oracle.

  • test_the_strip_is_unchanged_by_the_rewrite: every sequence shape (well formed in all six forms, truncated, cut-then-terminated, interleaved introducers, stray terminators) crossed with seven credential and benign lines at four positions each, over 1000 comparisons.
  • test_the_strip_is_unchanged_on_random_records: 20000 random records over a mostly-control alphabet.
  • test_an_unterminated_ansi_introducer_does_not_cost_quadratic_time: 14 pathological shapes at 40k, asserting a shape rather than a number, since quadratic here is seconds and linear is milliseconds.

Out of tree, a differential harness loads both implementations side by side and compares them over 172150 cases: the hand corpus, structured credential fuzz, and 50000 random records.

masked by both 44102
leaked by both (pre-existing on main, untouched here) 27226
leaked by main, masked here 0
masked by main, leaked here 0

Also checked: strip equality, redaction equality, second-pass equality (main is not idempotent on a record holding an aborted CSI, and neither is this, identically), CRLF and BOM and astral and RTL and NUL input, the per-line reader path, and linear time with a superlinearity guard at 10k/20k/40k/80k.

  • studio/backend: 207 tests pass across test_debug_log_redaction.py, test_debug_log_reader.py, test_debug_log_routes.py and test_debug_log_sources.py.
  • Python 3.10, 3.11, 3.12 and 3.13 in separate uv venv environments: identical counts on all four, to the case.
  • ruff check clean; tests/test_formatter_fixed_point.py passes (the repo pins ruff 0.6.9 through its pre-commit hook).

Scope

Backend only: one function and its tests. No frontend, Rust or Tauri file is touched, and the frontend does no ANSI handling of its own, so there is nothing browser-specific to differ between Chrome, Firefox, Safari and Edge. No model, GPU, CUDA, ROCm or Metal path is touched. The only two callers are debug_log_reader.list_recent_lines and the exception reason in routes/settings.py. redact_log_text keeps its signature, so an existing install picks this up as an ordinary source update with no migration.

Found while working on #10719 and kept separate from it, since this one affects the live viewer on main today and has nothing to do with exporting.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-13T12:41:09.181833Z ed4ba2a Manual request
🔒 Security Review ✅ Completed 2026-09-13T11:56:49.458541Z 907f19b Manual request

Security findings

Advisory findings (2)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed the lazy terminator scan is still in log_redaction.py on main, so an introducer with no terminator makes the Logs pane rescan the whole record each poll, and the negated classes keep terminated sequences stripped. Will get this reviewed.

…I sequence

The three string terminated branches of _ANSI_RE scanned for their
terminator with a lazy [\s\S]*?. An introducer with no terminator scans
to the end of the record, fails, and falls through to the single
character Fe branch, so every introducer pays a full scan of what
follows it and the cost grows with the square of the record length.

Measured on this tree, in characters of U+009D: 10k took 1.16s, 20k took
4.99s, 40k took 15.82s. The same 40k of ordinary text takes 0.005s.
After this change 40k takes 0.013s.

An unterminated introducer is not an exotic input. A log rotated in the
middle of a sequence leaves one, as does any writer whose output is cut,
and U+009D is ordinary UTF-8. The viewer hands whole lines to
redact_log_text once a second while the tab is open, so this is reachable
from the Settings > Logs pane on any host whose logs carry C1 characters.

Each body class now excludes the introducers as well as the terminators.
The second part is what does the work: excluding only the terminators
still lets a run of introducers be consumed to end of string from every
starting position, which is the same quadratic shape. A control sequence
cannot nest inside another, so an introducer inside a body means the
earlier one was never terminated.

Behaviour is unchanged for terminated sequences, which is what the
anchored rules below depend on: a colorized writer puts an escape between
a key and its value, and without stripping it every rule stops matching.
The existing 203 debug log tests pass untouched.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…signator eating the next character

A writer cut after a charset introducer had its final byte read off the
following text, so ESC ( in front of api_key took the a and left pi_key.
The designator final byte now stops at 0x5F; no registered 94-character
set uses a lowercase final, so nothing real is given up.

A bare ESC is itself the non-alphanumeric boundary _KEY_START looks
behind for, so deleting it joined prefix to api_key and the key stopped
matching. It now leaves a space, but only where that weld would happen:
elsewhere the space is the damage, since the cookie and presigned URL
rules read the character after the separator as the value's first.

The keypad pair ESC = and ESC > drops out of the Fe class. A full screen
app writes those and a log almost never does, while claiming ESC = eats
the separator out of api_key<ESC>=value and takes the mask with it.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

tests/test_formatter_fixed_point.py asserts every tracked file is already
a fixed point of ruff-format-with-kwargs, and the repo pins ruff 0.6.9
rather than whatever is installed.
…t a C1 introducer

A designator's final byte is chosen from a range that also holds every
plausible first character of a key, so a writer cut after ESC ( read the
next character as its own. It ate the C of Cookie:, the B of Bearer and
the ? of a presigned URL, and each one took a mask with it. Narrowing
the range does not separate the two cases, so the branch goes and ESC (
B is left in the viewer as (B, exactly as on main.

The 7 bit OSC and DCS bodies excluded ESC but not the 8 bit introducers,
so a cut ESC ] swallowed the CSI after it and every visible character
with it. They now exclude the same C1 introducers the 8 bit branch does,
which is the same no-nesting rule applied across both forms.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

Four rounds of review found the same shape four times: the strip was
guessing where a cut control sequence ended, and every guess moved a
leak rather than removing one. Consuming an aborted body ate the
separator out of api_key<cut>=value; not consuming it welded the body
onto the key. Dropping a lone escape joined prefix to api_key; keeping
one broke the cookie and presigned URL rules, which read the character
after the separator as the value's first. A charset designator read the
next character as its own final byte, and no narrowing of that range
separates a designator from the first letter of a key.

None of that was the bug. The lazy alternation was quadratic only on
FAILURE: a scan that finds its terminator already succeeds in one pass,
and only the failing one rescans from the next position. So the walk
answers the earliest terminator at or after k with a monotonic str.find
cache, and is otherwise the same machine -- same alternatives, same
order, same lazy shortest match, same fallthrough.

The output is therefore identical to the pattern it replaces on every
input, which is the contract the new tests assert: the old regex is kept
verbatim as the oracle, over every sequence shape crossed with every
credential form at four positions, plus 20000 random records. The
differential harness agrees across 172150 cases, with zero records where
the two disagree in either direction, on Python 3.10 through 3.13.

A redactor is the wrong place to smuggle a behaviour change into a
performance fix. The leaks that main has on a truncated sequence are
still there, and are worth their own change with its own tests.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 520a8cccd0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The measurements and the history of what was tried belong in the pull
request, not in the file. What stays is the ordering constraint on the
Fe branch, the invariant that keeps the scan linear, and the one thing a
future change must not undo: the output matches the pattern it replaced.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: ed4ba2aa7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit d7e5c6b into main Sep 13, 2026
32 of 34 checks passed
@danielhanchen
danielhanchen deleted the fix/log-redaction-ansi-redos branch September 13, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant