Repository navigation
Studio: stop the log redactor backtracking quadratically on a cut ANSI sequence - #10721
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
Security findingsAdvisory findings (2)
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
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.
c77e25d to
6470ed1
Compare
…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.
|
@codex security review |
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.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
_ANSI_REinstudio/backend/utils/log_redaction.pysearched 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.ESC ]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_textis what guards the Settings > Logs pane, anddebug_log_reader.pyhands 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_ansianswers "the earliest terminator at or after k" with a monotonicstr.findcache 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:
api_key<cut>=value; not consuming it welded the body onto the key, soapi_keyread asapi_keyfooESCjoinedprefixtoapi_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 characterapi\x1b[36mkey=valuestopped matchingaofapi_key, theCofCookie:, theBofBearerand 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=and:from the aborted body fixed one column and broke another:\x1b^api_key=valuethen lost the key instead of the separator, 909 records in the corpusA redactor is the wrong place to smuggle a behaviour change into a performance fix.
maindoes leak on a truncated sequence (api_key\x1b]foo=valueis 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.
main, untouched here)main, masked heremain, leaked hereAlso checked: strip equality, redaction equality, second-pass equality (
mainis 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 acrosstest_debug_log_redaction.py,test_debug_log_reader.py,test_debug_log_routes.pyandtest_debug_log_sources.py.uv venvenvironments: identical counts on all four, to the case.ruff checkclean;tests/test_formatter_fixed_point.pypasses (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_linesand the exception reason inroutes/settings.py.redact_log_textkeeps 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
maintoday and has nothing to do with exporting.