Skip to content

TD-5454: Scrubber: the upper-case secret-name rule reads a name once - #312

Open
captcrock wants to merge 1 commit into
mainfrom
td/5454-upper-name-once
Open

captcrock wants to merge 1 commit into
mainfrom
td/5454-upper-name-once

Conversation

@captcrock

Copy link
Copy Markdown
Contributor

The upper-case secret-name rule (DB_PASSWORD=…, API_TOKEN: "…") was quadratic on a long upper-case name with many credential words and no =: "TOKEN" * 8000 took about 5 s in every reply's scrub. It now reads a name once and finds exactly the same matches.

What changes

  • core/tstd/logging.py: _SECRET_ASSIGNMENT was \b([A-Z][A-Z0-9_]*WORD[A-Z0-9_]*\s*[=:]…. On a run with many credential words and no =, it tried every word against every place the name could end. A name's characters can never be a space, = or :, so the name always runs to the end of its run. A lazy lookahead now finds a credential word after the name's first letter, and a possessive [A-Z][A-Z0-9_]*+ reads the run once. The rest of the pattern, groups included, is unchanged. _ESCAPED_SECRET_ASSIGNMENT (TD-5450) is built from the same pattern, so it gets the same fix.
  • Tests in core/tests/test_security_suite.py, beside TD-5449's:
    • The regex before this change is kept in the tests. Both must give the same finditer spans (group 1 and group 2) on 20,000 seeded texts, after \b and after an escaped line break.
    • 128,000-unit runs of TOKEN, PASSWORD, SECRET_, API_KEY, an escaped TOKEN and TOKEN followed by a space must each be read in under 5 s.
    • A long name with a value is still masked.
  • docs/tst-desk-backlog.md: TD-5454 entry.

Before / after

redact_secrets and secret_spans on 40,000 characters of each run:

run main cb20b685 (redact / spans) this branch
TOKEN 4.91 s / 4.63 s 0.01 s / 0.01 s
PASSWORD 2.97 s / 3.42 s 0.01 s / 0.01 s
SECRET_ 3.89 s / 3.85 s 0.01 s / 0.01 s
API_KEY 3.84 s / 3.65 s 0.01 s / 0.01 s
lower-case and mixed-case runs (token, x_token, apiToken, …), 12 shapes 0.00 to 0.02 s 0.00 to 0.01 s

The regex alone on "TOKEN" * 8000: 5.07 s on main, under 1 ms here. The new long-name test (128,000 TOKEN) was still running when a 120 s limit stopped it on main; here it takes 0.35 s.

Same matches: 0 differences from the regex before this change:

  • 300,000 random texts (the prototype);
  • 1,000,000 longer ones, both rules (an e2e fuzz outside the repo);
  • 20,000 in the tests;
  • the review's own 200,000, which include Unicode word characters and spaces, and every text of up to 6 symbols from a 14-symbol alphabet.

Independent review

A separate reviewer, given the diff, main's regex and the ticket, read and probed it. Verdict: MERGE, no defects.

  • Why the matches are the same: [A-Z0-9_] can never match \s or [=:], so main's name always ends at the end of its run. The lookahead stays inside that run, because every credential word is made of [A-Z_]. The tail of the pattern is unchanged, and the lookahead is zero-width and outside group 1. The escaped variant can only start after \n, \r or \t, so it always starts at a run's start.
  • Equivalence fuzz: whole match, group 1 and group 2, both regexes, also with finditer starting partway into the text. 0 differences.
  • Speed: 40 shapes at 160k and 320k characters, both regexes and redact_secrets plus secret_spans. All doubled in time when the input doubled; the worst was 0.31 s at 320k of TOKEN=".
  • Tests: the timing cases fail on main's regex (measured quadratic: TOKEN ×1000 → ×2000 went from 0.048 s to 0.198 s). The equivalence test guards future changes; it passes trivially on main, where both sides are the same regex.
  • Two notes, both fixed: the test's pieces lacked an escaped \r (added), and the backlog's 1,000,000-text figure came from a fuzz outside the repo (now said so).

Tests

  • tests/test_security_suite.py -k upper_name: 8 passed.
  • Scrubber-related test files (53 files): 2,243 passed.
  • tests/test_security_suite.py + tests/test_pem_redaction.py (an earlier run): 962 passed, 1 failed. The failure was test_connection_scoped_reply_scrubs_memory_content (the WebSocket server's port was 0), in a run made while other test processes were running on the machine. Run alone it passed 2 of 2 on this branch and 1 of 1 on main's logging.py, and it passed in the 2,243 above.

Checks

  • ruff, ruff format and mypy: clean.
  • release_scan --git: 0 hits.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

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