Skip to content

perf(security): gate the credential scan behind a cheap superset pre-filter - #7056

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
rnoack1:perf/redact-credentials-single-pass
Sep 1, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
rnoack1:perf/redact-credentials-single-pass

Conversation

@rnoack1

@rnoack1 rnoack1 commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

redact_credentials is the single most expensive thing the gateway's event loop
does. A py-spy capture of a live gateway (2,945 samples, --rate 10 --nonblocking, window containing a 168-second API request) attributes:

frame % of all samples
redact_credentials 60.1%
_flush_dirty_slots → flush_slot_now 43.9%
_save_slot_to_history 43.4%
_build_message_entry 40.4%
MainThread (the event loop) total 35.2%

It is reached per message, per flush, via _flush_dirty_slots →
_save_slot_to_history → _build_message_entry → _redact_meta_for_role →
_redact_meta → _redact_value. On a host injecting 240K-char contexts that
rescans megabytes on every dirty-slot flush.

The function ran three separate full-text regex scans over every string, and
the first of them was the hottest line in the whole profile:

line % of all samples
_CREDENTIAL_PATTERNS.finditer(result) 38.2%
_B64_CHUNK_RE.finditer(text) 14.5%
_BARE_SECRET_RUN_RE.finditer(text) 6.8%

_CREDENTIAL_PATTERNS is a 23-branch alternation, so re retries every branch
at essentially every position — measured 117 ns/char. That cost is paid in
full even though real text almost never contains a credential: across 1,804 live
session-history messages (1.47 MB) the pattern matched zero times.

Why it matters

This is event-loop time on the request path, so it is latency users feel, not
background cost. It also scales the wrong way — with history length times flush
frequency, both of which grow during a session.

#7031 added a 3s memo to /api/skills so that one endpoint stops paying this
cost. That removes a caller; it does not make the redaction cheaper. This does.

What changed

Three changes, all pure control flow. Output is byte-identical — this is the
redaction boundary, so a regression here writes live credentials into persisted
chat history.

1. Gate pass 1 on a cheap superset pre-filter. _might_contain_credential
answers "could any branch match at all?" and pass 1 is skipped when the answer is
no. When it returns False, finditer would have yielded nothing and the loop body
would not have run, so skipping cannot change the output.

It is built from each branch's necessary condition, never a restatement of the
branch — 21 literal anchors, plus five leading-shape regexes for the branches with
no usable literal (GitHub PAT, Telegram, Discord, URI userinfo, and the
case-insensitive Authorization: Bearer). The superset direction is the security
property: a false positive costs a scan we would have run anyway, a false negative
would leak.

Two anchors were tightened after measuring them against real content, because the
obvious form was far too broad:

  • sk- matched "task-" → replaced with sk-proj- / sk-ant-
  • [0-9]{6,}: matched an epoch timestamp followed by a colon (29 of 614 real
    messages) → tightened to [0-9]{6,}:[A-Za-z0-9_-]{30}

The pre-filter fires on 5 of 1,804 real messages (0.3%) and costs 32
ns/char
against pass 1's 117.

2. Share one base64-run scan between passes 2 and 3. They scanned the same
text twice for the same spans. [A-Za-z0-9+/]{40,} is greedy and leftmost so it
yields exactly the maximal runs of length ≥ 40, which is also precisely what
_BARE_SECRET_RUN_RE's (?<![A-Za-z0-9+/]) / (?![A-Za-z0-9+/]) boundaries
select. The only difference is the trailing ={0,2} that _B64_CHUNK_RE
additionally consumes, and = is not in the run's character class, so
rstrip("=") recovers the bare run exactly.

The two loops stay separate and in their original order. Fusing them into one
per-run loop would interleave the passes, which changes the order of warnings
and — because each pass mutates result via str.replace(…, 1) — which
occurrence each replacement lands on, and whether pass 3's run not in result
guard sees pass 2's edits. Sharing the scan while keeping the loops ordered is
what makes this byte-identical.

3. Stop re-scanning a chunk that is already a chunk. Pass 2 called
_decode_b64_safe(chunk), which re-ran the chunk regex over a string that is
itself one _B64_CHUNK_RE match. New _decode_b64_chunk skips that. The generic
helper is unchanged for its other callers.

Both security properties called out in the existing comments are preserved and
now asserted: warnings carry length only, never a slice of the matched secret,
and passes 2 and 3 still scan the original text rather than the mutated
result so match offsets stay stable.

The Authorization anchor must use the regex engine, not str.lower()

An earlier revision of this PR anchored the case-insensitive branch with
"authorization" in text.lower(). That was a redaction bypass, and it is
fixed here.

str.lower() and re.IGNORECASE are two different case-folding
implementations, so they disagree. re folds via sre_compile._equivalences,
which treats U+0131 (LATIN SMALL LETTER DOTLESS I) and U+0130 (LATIN CAPITAL
LETTER I WITH DOT ABOVE) as equivalent to i/I; str.lower() leaves U+0131
unchanged and expands U+0130 to two code points. So:

Authorızation: Bearer opaque-token-123456     (U+0131 for the i)

was matched by the (?i:Authorization) branch but missed by the
.lower() anchor — the gate returned False, pass 1 was skipped, and the bearer
token was persisted verbatim with no warning. The same disagreement exists for
U+017F/s and U+212A/k, so this is a class of defect rather than one
homoglyph.

The anchor is now _CREDENTIAL_PREFILTER_AUTHORIZATION_RE = re.compile(r"(?i:Authorization)") — the branch's own sub-pattern, so the superset
property holds by construction: same engine, same folding rules. The rule this
establishes is that a case-insensitive branch may only be anchored by the same
regex engine, never by a hand-rolled fold.

A widened branch is now caught, not just an added one

The branch-count assertion catches an added branch. It structurally cannot
catch a widened one, because widening does not change the count — and the
bypass above is exactly a branch accepting more than its anchor.
test_widening_a_branch_cannot_outgrow_its_anchor closes that: it compiles each
branch standalone, mutates that branch's registered sample (case flips and the
Unicode homoglyphs that break hand-rolled folds), and for every mutation the
branch still accepts, asserts the gate accepts it too. It fails on the pre-fix
implementation, naming branch 22.

_CREDENTIAL_PATTERNS is also now marked at its definition site as having a
dependent pre-filter, spelling out that adding, widening, or case-relaxing a
branch is a two-site change and which test catches each case. That missing signal
is what let the drift ship.

Deliberately not changed

result = result.replace(matched, tag, 1) inside pass 1 stays as-is. Rebuilding
from collected spans would not be byte-identical: branches carrying a left
look-behind (Discord, the 2-segment link token) can match text that also occurs
earlier in the string at a position the look-behind rejects, and replace-first
then lands on that earlier occurrence — span-replacement would not. The corpus
pins that case ("x" + token + " " + token). At 40 matches in a 240K string the
str.replace loop costs 1.4 ms, so the risk buys almost nothing.

_B64_CHUNK_RE itself is also untouched — adding a capture group would have
changed findall semantics for the external consumer at
dashboard/handlers/artifacts.py:477.

Benchmarks

Best of 5, Python 3.12.14, measured on two worktrees at the same base commit
(baseline has no pre-filter at all):

input before after speedup
synthetic 24K, no credential 4.09 ms 1.61 ms 2.54×
synthetic 240K, no credential 40.74 ms 15.95 ms 2.55×
real session corpus, 1.47 MB 227.09 ms 89.54 ms 2.54×
240K + 1 real credential 41.65 ms 35.58 ms 1.17×
240K + anchor present, no credential 40.99 ms 36.98 ms 1.11×

The last row is the pre-filter's worst case — an anchor is present, so the
pre-filter fires and the full scan runs anyway — and it is still faster, because
the shared base64 scan more than covers the pre-filter's cost. No input shape
regresses.

Switching the Authorization anchor from str.lower() to the (?i:…) regex
costs about 1.4 ms on a 240K input (14.55 → 15.95 ms), taking the clean-text
speedup from 2.93× to 2.55×. That is the correct trade: the .lower() form was
faster and wrong.

Tests

test/test_credential_prefilter.py (new, 181 cases) pins the original
three-pass implementation as a reference oracle
and asserts the live function is
byte-identical to it on both the returned text and the warnings list. The
oracle reuses the module's own compiled patterns and gate helpers, so what it
isolates is exactly the control-flow change.

Corpus covers: every one of the 23 branches (alone and embedded in prose), the
Unicode case-folding shapes, base64-encoded credentials, bare 40-char AWS secret
keys, the glued-secret sliding-window cases the existing comment calls out
(X+key, key+A, SECRET=+key+ABC, key+X+key), adjacent and repeated
matches, the look-behind-shadowed occurrence described above, = padding
variants, hex digests and long base64 blobs, PEM complete/truncated/inline-in-prose,
empty and whitespace-only strings, and 108K-char inputs.

test_corpus_actually_exercises_every_pass asserts all three warning kinds
appear, so the differential corpus cannot be vacuously all-clean.
test_warnings_never_carry_secret_material asserts every warning matches
Redacted … (N chars) and contains no secret substring.
test_prefilter_still_skips_credential_free_text pins the negative direction, so
a "fix" that made the gate fire on everything — correct but worthless — fails.

Supporting equivalences are asserted rather than assumed:
test_b64_chunk_spans_match_bare_secret_run_spans proves the shared scan is
legitimate, and test_decode_b64_chunk_matches_generic_helper_on_chunks proves
the new decode helper agrees with the one it replaces.

Negative controls — a test that cannot fail proves nothing on a redaction
boundary. Run against the pre-fix tree, the new tests produce 22 failures,
including the differential oracle test itself:

  1. test_unicode_case_folding_cannot_bypass_the_prefilter — all four bypass
    shapes fail pre-fix, each asserting the branch matches (positive control, so
    the test cannot go vacuous) and then that the gate does not skip it.
  2. test_prefilter_is_a_superset_under_every_re_ignorecase_equivalence —
    generalises past the two reported homoglyphs by enumerating every code
    point in U+0100–U+24FF that re.IGNORECASE folds into a letter of
    "Authorization" but str.lower() does not, and asserting the property over all
    of them. Independently rediscovers U+0130.
  3. test_widening_a_branch_cannot_outgrow_its_anchor — fails pre-fix naming
    branch 22.
  4. test_output_is_byte_identical_to_reference — the bypass shapes are now in the
    corpus, so the buggy gate makes the live function diverge from the oracle.
    Their absence from the corpus is what let the bypass ship.
  5. test_differential_test_detects_a_too_narrow_prefilter disarms every anchor via
    monkeypatch and asserts the comparison differs, proving the byte-identity
    assertion can fail rather than passing because both sides share a defect.
  6. test_each_sample_is_specific_to_its_own_branch removes branch i and asserts
    sample i stops matching, so no sample is vacuously covered by another branch.
  7. test_every_pattern_branch_has_a_prefilter_anchor asserts branch count ==
    registered sample count, so a 24th branch fails loudly at the point of the edit.
  8. test_case_insensitive_branches_must_be_anchored_by_the_same_engine pins which
    branches are (?i:…), so a new one cannot quietly be gated by a literal.

Also verified by sabotaging the source (dropping the eyJ literal and disarming
the GitHub-PAT shape): the differential test fails on exactly the
credential-bearing cases, then reverted byte-identically.

test_prefilter_soundness_under_fuzz runs 4,000 seeded random strings built from
the real branch samples plus mutations and asserts both differential equality and
the superset property, with a coverage floor (checked >= 500) so a fuzz run that
produced almost no real matches fails instead of silently asserting nothing — that
floor already caught a first draft which produced only 5.

Independently of pytest, a cross-tree differential ran a baseline worktree
(no pre-filter at all) and this tree over 9,804 inputs (real session content
plus seeded fuzz), 5,031 of which produce warnings, and both produced an
identical aggregate output digest:

cases=9804
inputs_producing_warnings=5031
aggregate_output_digest=f0f36484180622f5bf868419049a1e0b8f65fdadce8058380d9b36e9fd90d13a5ff51fb4af62f61a4026c3d4bb64cc75430f433a6fade730c28b47c081709d04

Gate results: every step of Backend Lint & Type Check run locally — black
(baselined), subprocess-encoding gate, lockdown-before-publish, isort, flake8,
mypy — all pass. Security and redaction suites (test_credential_prefilter,
test_security, test_log_redaction, test_redaction_mirror_parity,
test_snapshot_redaction, test_streaming_meta_redaction,
test_redact_meta_snapshot, test_handlers_artifacts_coverage) → 1,378 passed,
1 skipped
.

The full suite shows 96 failures; all 96 reproduce identically on an unmodified
baseline worktree at the same commit — same 31 files, same per-file counts — and
none is a security or redaction test. They are environmental (uid/ownership,
xdist host budget, install-path checks).

security.py fails raw black, identically on baseline (448-line diff both
sides) and black wants nothing in the added lines; the repo's own gate passes it
via the known-unformatted baseline. The new test file is black-clean.

@rnoack1
rnoack1 requested review from a team and smeyffret August 30, 2026 17:08
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Aug 30, 2026
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 0235df7 to 550c6e9 Compare August 30, 2026 17:47
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Aug 30, 2026
@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

Reviewed eb347a3a3ccf37270249571c017d1f0b19103adb via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] eb347a3

@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — ✅ PASS

Design-level review of eb347a3a3ccf37270249571c017d1f0b19103adb via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Both hunks and the surrounding base code are verified: the pre-filter anchors are genuine necessary conditions of the branches at security.py:9278-9388 (e.g. sk-proj-/sk-ant- are the branches' literal prefixes, the Telegram {30} anchor is a subset condition of the branch's {30,}), the shared-scan equivalence between _B64_CHUNK_RE and _BARE_SECRET_RUN_RE (security.py:9494,9511) holds since = is outside the run class, and redact_credentials is a shared primitive with ~40 call sites — so fixing the function rather than one caller (as #7031 did) is the right layer. The two-site-edit hazard the pre-filter creates on the redaction boundary is mitigated by the branch-count, per-branch-specificity, mutation, structure-derived-generation, differential-oracle, and fuzz tests, each with negative controls proving it can fail.

Design-Verdict: PASS

A profiled hot-path fixed at the shared primitive, byte-identical by differential oracle, with the pre-filter's silent-bypass hazard guarded from every direction.

Suggestions

  • The flush path still re-redacts unchanged history on every dirty-slot flush, so the O(history × flush-frequency) scaling the description names survives this constant-factor win; a per-message redaction cache on that path is the natural follow-up PR.

[DESIGN-REVIEWED] eb347a3

@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ✅ PASS

Premise-level review of eb347a3a3ccf37270249571c017d1f0b19103adb via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

I've verified everything I need from the base tree. The claims in the diff check out against the base: the three-pass structure exists as described, _decode_b64_safe and _BARE_SECRET_RUN_RE retain independent production consumers (_contains_fixed_credential at security.py:9796, _text_contains_bare_secret at security.py:9806) so nothing goes dead, no prior credential pre-filter exists anywhere (the only other "pre-filter" is the unrelated byte gate in mcp_gateway/image_budget.py), and redact_credentials has ~25 production call sites across 18 files, so fixing the primitive is the general fix rather than a point patch at one caller.

First-Principles-Verdict: PASS

A measured 60%-of-event-loop cost removed by pure control flow, byte-identity pinned by a differential oracle — every item traces to a profile line or a demonstrated bypass.

What this change ships

Intent: make persisted-chat redaction stop costing event-loop latency on credential-free text — a FIX.

  1. Credential-free text skips the 23-branch scan (0.3% fire rate, measured) — justified
  2. One base64-run scan feeds passes 2 and 3 instead of two scans — justified, same measured cause
  3. Chunk decode no longer re-scans a string that is already one chunk — justified
  4. Two anchors deliberately narrower than the obvious literal (sk-proj-/sk-ant-, Telegram tail) — justified, measured against real messages
  5. New private helpers/constants, each with exactly 1 production consumer inside redact_credentials — justified, internal only
  6. _CREDENTIAL_PATTERNS now carries a two-site-change maintenance contract in comments — declared
  7. 730-line test module: differential oracle, superset/branch-count/widening guards, negative controls — declared

Watch

  • The description itself names the scaling cause — "history length times flush frequency" — and this fix only shrinks the constant (117→32 ns/char); the re-redaction of unchanged messages per flush lives in the chat-flush path, genuinely out of a security.py change's scope, and remains the cause-level fix left on the table.
  • test_a_widened_branch_cannot_outgrow_its_anchor couples the suite to re._parser (private API) to guard a widening mode not yet observed; the observed class (case-fold homoglyphs) is already covered by the perturbation guard. Permanent test-maintenance cost taken for a hypothesized edit — defensible given the silent-leak severity, but it is the one element whose provenance is a hypothesis rather than a defect.

[FIRST-PRINCIPLES-REVIEWED] eb347a3

@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed eb347a3a3ccf37270249571c017d1f0b19103adb via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] eb347a3

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Aug 30, 2026
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 550c6e9 to 0517c9a Compare August 30, 2026 19:03
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Aug 30, 2026
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 0517c9a to 5cbc819 Compare August 30, 2026 19:15
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Aug 30, 2026
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 5cbc819 to 4bf73bc Compare August 30, 2026 19:50
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Aug 30, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Aug 30, 2026
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 4bf73bc to 6e3eb77 Compare August 30, 2026 21:49
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision readiness: action required A blocking check or review needs attention labels Aug 30, 2026
…filter

Skips a 23-branch alternation scan that cannot match, shares one base64 run
scan between passes 2 and 3, and drops a redundant chunk re-scan. 2.6-2.9x.
@rnoack1
rnoack1 force-pushed the perf/redact-credentials-single-pass branch from 6e3eb77 to eb347a3 Compare August 30, 2026 22:27
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Aug 30, 2026
@iamwhatever
iamwhatever merged commit 4bc1927 into kirodotdev:main Sep 1, 2026
72 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 1, 2026
@rnoack1
rnoack1 deleted the perf/redact-credentials-single-pass branch September 1, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants