Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(secret-scan): catch OpenRouter, Groq, xAI, Perplexity and Replicate keys (#7959)

garrytane2cb6c8f1954
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 76033c6e07..c975f41d97 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,27 @@ credits are retained; no result has been reassigned to another provider. Origina
identifiers and attribution are available in the pre-removal Git revision
`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).
+## [Unreleased]
+
+**The secret scanner now catches OpenRouter, Groq, xAI, Perplexity and Replicate API keys.**
+
+A raw OpenRouter key (`sk-or-v1-` plus 64 hex) slipped past every lane: the bare OpenAI rule stops at the dash after `sk-or`, so the key didn't block `gbrain sources push`, wasn't replaced with `<REDACTED:…>` in search, recall or transcript import, and wasn't counted by `gbrain transcripts audit-secrets`, which left doctor's `transcript_secret_exposure` reading clean. Groq (`gsk_`), xAI (`xai-`), Perplexity (`pplx-`) and Replicate (`r8_`) keys had the same gap. Each now has its own rule with the provider's published fixed length and a hard right edge, so a longer run of the same characters is never cut into a false key. Anthropic (`sk-ant-`), project-scoped OpenAI (`sk-proj-`) and Google (`AIza`) keys were already caught and are unchanged.
+
+### What to do after upgrading
+
+- A push that worked before can now stop with `rule [openrouter] blocks pushes since gbrain v0.60.499.0` (or groq, xai, perplexity, replicate). Rotate the key with the provider and remove it from the page. Don't allowlist a live key.
+- `gbrain doctor` asks for a fresh transcript audit once, because the audit's detectors changed. Run `gbrain transcripts audit-secrets --json` to list conversation pages that still hold such a key (slugs and line numbers, never values).
+
+### Itemized changes
+
+- `src/core/secret-scan.ts`: five vendor rules (`openrouter`, `groq`, `xai`, `perplexity`, `replicate`) with `since` `PROVIDER_KEYS_SINCE`, placed with the vendor rules ahead of the catch-alls, so `Bearer <key>` keeps the vendor name. No existing rule changed or moved.
+- `src/core/transcripts/secret-audit.ts`: `TRANSCRIPT_SECRET_AUDIT_VERSION` 1 → 2, so a clean audit cached by the old detectors no longer reads ok.
+- Tests build every key at runtime (`test/helpers/provider-keys.ts`) and pin each rule through the scanner, retrieval redaction, transcript import, the audit and the push gate, plus near misses (one character short or long, wrong case or separator, the prefix inside a longer word) that fire no rule.
+
+Reported by @lelle-bot (#7959). Contributed by @Masashi-Ono0611 (#7961), who proposed the OpenRouter rule.
+
+Supersedes #7961
+
## [0.60.498.0] - 2026-10-29
**The Claude Code and Codex plugins now put gbrain's brain-first rule beside every turn, the same way `gbrain setup` does.**
diff --git a/docs/architecture/key-files/secret-scan.md b/docs/architecture/key-files/secret-scan.md
index 4eb3ceb57d..a6997cd9d2 100644
--- a/docs/architecture/key-files/secret-scan.md
+++ b/docs/architecture/key-files/secret-scan.md
@@ -6,6 +6,6 @@ The format-based secret detector shared by `sources push`, bootstrap verify, the
compiled-context sensitivity scan and the transcripts redactor, and the composed
sensitivity detector built on it.
-- `src/core/secret-scan.ts` — pattern-based secret detection shared by `sources push`, bootstrap verify, the compiled-context sensitivity scan and the transcripts redactor. Detection is FORMAT-based, not prefix-only: vendor shapes (anthropic, openai, voyage, github, gitlab, slack, aws AKIA/ASIA, google, stripe, sendgrid, twilio, supabase sb_secret_/sbp_, npm, huggingface, `digitalocean` `do[opr]_v1_` + 64 hex, gbrain), JWTs, private keys (whole-text claims, below), plus four catch-alls appended LAST (`bearer` — prebuilt two-group layout so only the token is the value; `db_url_credentials` — the `scheme://user:pass@` span of database URLs, host/db kept; `url_credentials` — the same span for `http(s)` (scheme case-insensitive, `gi`), user 0–128 stopping at `:`/`/`, password 1–256 excluding whitespace, `@`, `"`, `'`, `/`, `?`, `#`, with a `validate` hook that rejects placeholder passwords (`<...>`, `${...}`, `$VAR`, all `*`, all `x`); `basic_auth` — two prebuilt case-insensitive forms, `Authorization: Basic` with a 4–2048 char value and bare `Basic` with 16–2048, both with a hard right edge and a `validate` that requires the value to base64-decode to printable ASCII containing `:`; it is NOT in the bearer family, so it joins neither the echo dictionary nor `secret:bearer`). The pattern contract: `CorePattern` carries optional `flags` (default `'g'`), `precheck`, `validate(value)` (O(value), run on the whole match before the claim and the union split; a rejected match is not re-tried from inside) and `since` (the gbrain version that added or last changed the rule; `SECURITY_WAVE_SINCE` = `0.60.31.0` on `digitalocean`, `url_credentials`, `basic_auth` and `private_key_pem`, kept in step with the release that ships a rule change). Compiled patterns keep group 1 = anchor, group 2 = value. `patternSince(pattern)` reads the map; every finding of such a pattern carries `SecretFinding.since`, which the push and bootstrap refusals render. Fixed-length shapes carry a trailing negative lookahead. Per-line claimed-span dedupe is first-wins for ATTRIBUTION (order matters: `Bearer <vendor key>` keeps the vendor name, a fully covered later span is skipped) but coverage is the UNION: every unclaimed slice of a later overlapping span is claimed as a hit of the later pattern (own value, own fingerprint), so a database URL whose username is a vendor-shaped id still loses its password. The transcript-only `labeled_credential` rule (`ScanOpts.labeledCredentials`) lives in `secret-scan-labeled.ts` (Security). The opt-in `high_entropy_assignment` rule (`ScanOpts.highEntropy`) requires a 12+ char value that contains a digit and a non-digit AND clears 3.5 bits/char on the bias-corrected (Miller-Madow) estimate `correctedEntropy`, since raw per-character entropy is capped at log2(n) and rejected most random 12-16 char secrets. It is two compiled patterns sharing the name: QUOTED (after `"` or `'`, any run of non-quote, non-whitespace characters) and UNQUOTED (the legacy `[A-Za-z0-9+/_=-]` class plus `!#$%*@^~.?<>`; never `&`, `()[]{}`, `:`, `,` or `;`, so query strings, calls and SQL `SET` lists stay intact, and a password carrying `,`/`;` matches only quoted; the last character excludes `.?!`, so a sentence's full stop is not claimed). Its `validate` rejects values that start with `scheme://`, `./`, `~/`, `${`, `$(` or `<REDACTED:`, dotted identifier paths (`process.env.X`) and `/`-paths holding a character outside the legacy class. Consumers with it on: retrieval output, transcript ingest, hooks and the hook heartbeat, the atoms drain, `embed-facts.ts` error text and the context-engine relay re-scan. Off: the push gate, bootstrap verify/repo and the compiled-context sensitivity scan. Own minimal allowlist + `<ws>/.gbrain-scan-allow` per-finding overrides — deliberately NOT the repo's `.gitleaks.toml` (a public-repo CI fixture policy); the allowlist block (`loadWorkspaceAllowlist`, the glob dialect `globToRegExp`/`matchesGlob`, `valueAllowlisted`/`pathAllowlisted`, `SCAN_ALLOW_FILENAME`, the 16-hex fingerprint floor) lives in `src/core/secret-scan-allowlist.ts` since wave 16 (G4.1, the 1500-line module cap) and is re-exported here unchanged (`test/secret-scan-allowlist.test.ts`). `correctedEntropy` is one histogram pass (distinct count = histogram size; byte-identical to the former `shannonEntropy + Set` form, `test/secret-scan-entropy-equivalence.test.ts`; ~80 → ~25 ms per 250 × 4096-char segment). `ScanOpts.work` / `RedactOpts.work` takes a `ScanWork` accumulator (`src/core/secret-scan-work.ts`: lines, regex execs, candidates per pattern, chars handed to the entropy gate counted inside `correctedEntropy`, validate calls, claims, union splits, echo sweeps) pinned exactly on the perf fixture by `test/secret-scan-work-counters.test.ts`; the perf file's absolute bounds stay the only guard on per-character cost. Deliberately NOT patterns: `sb_publishable_`, Supabase project refs (identifiers, not credentials). Never spell a literal database or `http(s)` URL with userinfo, or any credential, in this file's comments or in fixtures (assemble test values at runtime; `scripts/check-pg-url-redaction.sh` greps `src/` for every `db_url_credentials` scheme). Long lines are scanned in full (no length cap — a secret on a 1 MB minified line must still redact); what keeps that linear is bounded quantifiers on every shape whose value class could otherwise run to end-of-line — `db_url_credentials` (user ≤128 stopping at `:`/`/`; password 1–256 stopping only at whitespace, `@` and the string delimiters `"`/`'`, so unescaped `/ { } < > | ^ \` and backtick passwords redact), `jwt` (prebuilt so its boundary excludes `-` as well as `_`; segments ≤4096), `high_entropy_assignment` (≤64 identifier chars after the keyword, value ≤4096), `url_credentials` (user ≤128, password ≤256), `basic_auth` (value ≤2048, so its decode is constant work) and the private-key walks (below) — plus a per-pattern `precheck` substring gate on the catch-alls (`@` for both URL patterns; `earer`/`EARER`, since an all-caps `BEARER` scheme is accepted; a case-insensitive `basic`), a carried next-newline position in `scanPemBlocks` (sticky -1, so the header-line count is O(text) even when thousands of hits sit ahead of a newline-free multi-megabyte tail), a lazily-allocated per-line claimed-char bitmap behind the first-wins dedupe (O(value) per hit), and a windowed `buildPreview` (whole line ≤160 chars; otherwise hit−40…+80, edges snapped outward to whole spans, every claimed span in the window spliced by position, so a preview never carries a sibling secret or a fragment). `redactFindings` (= `planRedaction` + `applyRedaction`) rebuilds the text by SPAN-SPLICE in one O(text + hits) pass over the claimed spans, and fills the UNCLAIMED gaps with a bounded echo pass: the values claimed by `bearer` and `high_entropy_assignment` form an echo dictionary (value → pattern; floors `ECHO_MIN_CHARS_BEARER` = 20 / `ECHO_MIN_CHARS_ENTROPY` = 12, values ≤ `ECHO_MAX_VALUE_CHARS` = 512, the first `ECHO_MAX_UNIQUE` = 64 unique values in claim order across BOTH patterns — a value past either cap is redacted at its claim, its echoes are the accepted miss), matched by a streaming merge over the ORIGINAL text — per value, the next occurrence at or beyond the emit cursor (`indexOf` resuming at the cursor, re-sought only once the cursor passes it), leftmost-longest at each step, emitted incrementally into the unclaimed gaps — so retained state is O(values) however long the text is (occurrences are never collected into a list) (never an alternation RegExp, never over emitted tokens). Echoes add no findings. `planRedaction` feeds a caller-supplied shared map (`RedactOpts.echoValues`) and `applyRedaction` reads it at apply time, so a multi-field document scrubs an echo in one field of a value claimed in another. Every other pattern redacts exactly what `scanText` reports (a value embedded past its boundary is left as-is; vendor shapes re-match on their own). PRIVATE KEYS: `pemClaims` finds `BEGIN`/`END … PRIVATE KEY` fences in two global passes and merges them in text order. BLOCK: a BEGIN whose next fence is an END within `PEM_BODY_MAX_CHARS` (16384) is claimed header through END; a block never swallows a later BEGIN. FORWARD (BEGIN with no END in view): the header plus the body lines that follow; a header followed by no body line (a marker quoted in prose) is claimed alone. BACKWARD (END with no BEGIN in view): the body lines before the fence plus the fence; an END with no body line before it is no claim. A body line is a whole-line base64 run (`A-Za-z0-9+/=`) indented by up to 16 spaces/tabs; line ends are `\n`, `\r\n` or the literal JSON escapes; interior lines are 16–128 chars, the line next to a fence and a partial line at a cut 1–128; optional RFC 1421 `Proc-Type:`/`DEK-Info:` lines and one blank line may follow BEGIN; a line with anything else, or a run over 128 chars, ends the body. Total claimed body stays within `PEM_BODY_MAX_CHARS`. Each walk stops at the neighbouring fence or the previous claim, so the pass is linear (20k unterminated BEGINs, 20k END-only fences and 1 MB of base64 lines are perf-pinned). Per-line hits that fall wholly inside a key claim are dropped (one key, one finding). A FORWARD claim's finding also carries `legacyFingerprint`, the header-only fingerprint earlier releases reported, so `workspace-push.ts` can name a stale allowlist entry; a truncated key's fingerprint covers its body and therefore depends on where the text was cut, so it is not a stable allowlist key. `privateKeySpans(text)` exports the same claims as sorted, disjoint `{start, end}` spans. Accepted misses: body on the header's own line, space-joined single-line keys, blockquoted (`> `) keys, base64 runs over 128 chars, body past 16384 chars, `http(s)` passwords containing a literal `/`, `?` or `#`, Basic values over 2048 chars, digitless assignment values, dotted passwords whose every segment starts with a letter, and unquoted passwords containing `,` or `;`. False-positive budget: `scripts/secret-scan-fp-budget.ts` diffs findings against a base ref's scanner (`--base`, default `origin/master`) (`--dir <brain>` reports per-pattern counts only, never content) and `test/secret-scan-fp-budget.test.ts` pins a synthetic brain-like fixture (zero findings on its non-secret part in both modes, every planted secret redacted). Shape tests: `test/secret-scan-credential-shapes.test.ts`, `test/secret-scan-private-key-spans.test.ts`. Perf pins: `test/secret-scan-perf.test.ts`. Retrieval output is another consumer of its canonical scanner; that output boundary does not sanitize internal evaluation/provider inputs.
+- `src/core/secret-scan.ts` — pattern-based secret detection shared by `sources push`, bootstrap verify, the compiled-context sensitivity scan and the transcripts redactor. Detection is FORMAT-based, not prefix-only: vendor shapes (anthropic, openai, voyage, github, gitlab, slack, aws AKIA/ASIA, google, stripe, sendgrid, twilio, supabase sb_secret_/sbp_, npm, huggingface, `digitalocean` `do[opr]_v1_` + 64 hex, the AI-provider keys `openrouter` `sk-or-v1-` + 64 lowercase hex, `groq` `gsk_` + 52, `xai` `xai-` + 80, `perplexity` `pplx-` + 48 and `replicate` `r8_` + 37, all fixed-length with a hard right edge, gbrain), JWTs, private keys (whole-text claims, below), plus four catch-alls appended LAST (`bearer` — prebuilt two-group layout so only the token is the value; `db_url_credentials` — the `scheme://user:pass@` span of database URLs, host/db kept; `url_credentials` — the same span for `http(s)` (scheme case-insensitive, `gi`), user 0–128 stopping at `:`/`/`, password 1–256 excluding whitespace, `@`, `"`, `'`, `/`, `?`, `#`, with a `validate` hook that rejects placeholder passwords (`<...>`, `${...}`, `$VAR`, all `*`, all `x`); `basic_auth` — two prebuilt case-insensitive forms, `Authorization: Basic` with a 4–2048 char value and bare `Basic` with 16–2048, both with a hard right edge and a `validate` that requires the value to base64-decode to printable ASCII containing `:`; it is NOT in the bearer family, so it joins neither the echo dictionary nor `secret:bearer`). The pattern contract: `CorePattern` carries optional `flags` (default `'g'`), `precheck`, `validate(value)` (O(value), run on the whole match before the claim and the union split; a rejected match is not re-tried from inside) and `since` (the gbrain version that added or last changed the rule; `SECURITY_WAVE_SINCE` = `0.60.31.0` on `digitalocean`, `url_credentials`, `basic_auth` and `private_key_pem`, `PROVIDER_KEYS_SINCE` on the five AI-provider rules (#7959), kept in step with the release that ships a rule change). Compiled patterns keep group 1 = anchor, group 2 = value. `patternSince(pattern)` reads the map; every finding of such a pattern carries `SecretFinding.since`, which the push and bootstrap refusals render. Fixed-length shapes carry a trailing negative lookahead. Per-line claimed-span dedupe is first-wins for ATTRIBUTION (order matters: `Bearer <vendor key>` keeps the vendor name, a fully covered later span is skipped) but coverage is the UNION: every unclaimed slice of a later overlapping span is claimed as a hit of the later pattern (own value, own fingerprint), so a database URL whose username is a vendor-shaped id still loses its password. The transcript-only `labeled_credential` rule (`ScanOpts.labeledCredentials`) lives in `secret-scan-labeled.ts` (Security). The opt-in `high_entropy_assignment` rule (`ScanOpts.highEntropy`) requires a 12+ char value that contains a digit and a non-digit AND clears 3.5 bits/char on the bias-corrected (Miller-Madow) estimate `correctedEntropy`, since raw per-character entropy is capped at log2(n) and rejected most random 12-16 char secrets. It is two compiled patterns sharing the name: QUOTED (after `"` or `'`, any run of non-quote, non-whitespace characters) and UNQUOTED (the legacy `[A-Za-z0-9+/_=-]` class plus `!#$%*@^~.?<>`; never `&`, `()[]{}`, `:`, `,` or `;`, so query strings, calls and SQL `SET` lists stay intact, and a password carrying `,`/`;` matches only quoted; the last character excludes `.?!`, so a sentence's full stop is not claimed). Its `validate` rejects values that start with `scheme://`, `./`, `~/`, `${`, `$(` or `<REDACTED:`, dotted identifier paths (`process.env.X`) and `/`-paths holding a character outside the legacy class. Consumers with it on: retrieval output, transcript ingest, hooks and the hook heartbeat, the atoms drain, `embed-facts.ts` error text and the context-engine relay re-scan. Off: the push gate, bootstrap verify/repo and the compiled-context sensitivity scan. Own minimal allowlist + `<ws>/.gbrain-scan-allow` per-finding overrides — deliberately NOT the repo's `.gitleaks.toml` (a public-repo CI fixture policy); the allowlist block (`loadWorkspaceAllowlist`, the glob dialect `globToRegExp`/`matchesGlob`, `valueAllowlisted`/`pathAllowlisted`, `SCAN_ALLOW_FILENAME`, the 16-hex fingerprint floor) lives in `src/core/secret-scan-allowlist.ts` since wave 16 (G4.1, the 1500-line module cap) and is re-exported here unchanged (`test/secret-scan-allowlist.test.ts`). `correctedEntropy` is one histogram pass (distinct count = histogram size; byte-identical to the former `shannonEntropy + Set` form, `test/secret-scan-entropy-equivalence.test.ts`; ~80 → ~25 ms per 250 × 4096-char segment). `ScanOpts.work` / `RedactOpts.work` takes a `ScanWork` accumulator (`src/core/secret-scan-work.ts`: lines, regex execs, candidates per pattern, chars handed to the entropy gate counted inside `correctedEntropy`, validate calls, claims, union splits, echo sweeps) pinned exactly on the perf fixture by `test/secret-scan-work-counters.test.ts`; the perf file's absolute bounds stay the only guard on per-character cost. Deliberately NOT patterns: `sb_publishable_`, Supabase project refs (identifiers, not credentials). Never spell a literal database or `http(s)` URL with userinfo, or any credential, in this file's comments or in fixtures (assemble test values at runtime; `scripts/check-pg-url-redaction.sh` greps `src/` for every `db_url_credentials` scheme). Long lines are scanned in full (no length cap — a secret on a 1 MB minified line must still redact); what keeps that linear is bounded quantifiers on every shape whose value class could otherwise run to end-of-line — `db_url_credentials` (user ≤128 stopping at `:`/`/`; password 1–256 stopping only at whitespace, `@` and the string delimiters `"`/`'`, so unescaped `/ { } < > | ^ \` and backtick passwords redact), `jwt` (prebuilt so its boundary excludes `-` as well as `_`; segments ≤4096), `high_entropy_assignment` (≤64 identifier chars after the keyword, value ≤4096), `url_credentials` (user ≤128, password ≤256), `basic_auth` (value ≤2048, so its decode is constant work) and the private-key walks (below) — plus a per-pattern `precheck` substring gate on the catch-alls (`@` for both URL patterns; `earer`/`EARER`, since an all-caps `BEARER` scheme is accepted; a case-insensitive `basic`), a carried next-newline position in `scanPemBlocks` (sticky -1, so the header-line count is O(text) even when thousands of hits sit ahead of a newline-free multi-megabyte tail), a lazily-allocated per-line claimed-char bitmap behind the first-wins dedupe (O(value) per hit), and a windowed `buildPreview` (whole line ≤160 chars; otherwise hit−40…+80, edges snapped outward to whole spans, every claimed span in the window spliced by position, so a preview never carries a sibling secret or a fragment). `redactFindings` (= `planRedaction` + `applyRedaction`) rebuilds the text by SPAN-SPLICE in one O(text + hits) pass over the claimed spans, and fills the UNCLAIMED gaps with a bounded echo pass: the values claimed by `bearer` and `high_entropy_assignment` form an echo dictionary (value → pattern; floors `ECHO_MIN_CHARS_BEARER` = 20 / `ECHO_MIN_CHARS_ENTROPY` = 12, values ≤ `ECHO_MAX_VALUE_CHARS` = 512, the first `ECHO_MAX_UNIQUE` = 64 unique values in claim order across BOTH patterns — a value past either cap is redacted at its claim, its echoes are the accepted miss), matched by a streaming merge over the ORIGINAL text — per value, the next occurrence at or beyond the emit cursor (`indexOf` resuming at the cursor, re-sought only once the cursor passes it), leftmost-longest at each step, emitted incrementally into the unclaimed gaps — so retained state is O(values) however long the text is (occurrences are never collected into a list) (never an alternation RegExp, never over emitted tokens). Echoes add no findings. `planRedaction` feeds a caller-supplied shared map (`RedactOpts.echoValues`) and `applyRedaction` reads it at apply time, so a multi-field document scrubs an echo in one field of a value claimed in another. Every other pattern redacts exactly what `scanText` reports (a value embedded past its boundary is left as-is; vendor shapes re-match on their own). PRIVATE KEYS: `pemClaims` finds `BEGIN`/`END … PRIVATE KEY` fences in two global passes and merges them in text order. BLOCK: a BEGIN whose next fence is an END within `PEM_BODY_MAX_CHARS` (16384) is claimed header through END; a block never swallows a later BEGIN. FORWARD (BEGIN with no END in view): the header plus the body lines that follow; a header followed by no body line (a marker quoted in prose) is claimed alone. BACKWARD (END with no BEGIN in view): the body lines before the fence plus the fence; an END with no body line before it is no claim. A body line is a whole-line base64 run (`A-Za-z0-9+/=`) indented by up to 16 spaces/tabs; line ends are `\n`, `\r\n` or the literal JSON escapes; interior lines are 16–128 chars, the line next to a fence and a partial line at a cut 1–128; optional RFC 1421 `Proc-Type:`/`DEK-Info:` lines and one blank line may follow BEGIN; a line with anything else, or a run over 128 chars, ends the body. Total claimed body stays within `PEM_BODY_MAX_CHARS`. Each walk stops at the neighbouring fence or the previous claim, so the pass is linear (20k unterminated BEGINs, 20k END-only fences and 1 MB of base64 lines are perf-pinned). Per-line hits that fall wholly inside a key claim are dropped (one key, one finding). A FORWARD claim's finding also carries `legacyFingerprint`, the header-only fingerprint earlier releases reported, so `workspace-push.ts` can name a stale allowlist entry; a truncated key's fingerprint covers its body and therefore depends on where the text was cut, so it is not a stable allowlist key. `privateKeySpans(text)` exports the same claims as sorted, disjoint `{start, end}` spans. Accepted misses: body on the header's own line, space-joined single-line keys, blockquoted (`> `) keys, base64 runs over 128 chars, body past 16384 chars, `http(s)` passwords containing a literal `/`, `?` or `#`, Basic values over 2048 chars, digitless assignment values, dotted passwords whose every segment starts with a letter, and unquoted passwords containing `,` or `;`. False-positive budget: `scripts/secret-scan-fp-budget.ts` diffs findings against a base ref's scanner (`--base`, default `origin/master`) (`--dir <brain>` reports per-pattern counts only, never content) and `test/secret-scan-fp-budget.test.ts` pins a synthetic brain-like fixture (zero findings on its non-secret part in both modes, every planted secret redacted). Shape tests: `test/secret-scan-credential-shapes.test.ts`, `test/secret-scan-private-key-spans.test.ts`. Perf pins: `test/secret-scan-perf.test.ts`. Retrieval output is another consumer of its canonical scanner; that output boundary does not sanitize internal evaluation/provider inputs.
- `src/core/secret-scan-stream.ts` — `scanTextStream(chunks, opts)`, the windowed scan of a byte stream too large to hold as one string (the push gate's staged blobs over 25 MiB). Bytes are decoded by a streaming `TextDecoder` (`ignoreBOM`, so a split multi-byte character joins and invalid bytes become U+FFFD as `Buffer.toString` makes them) and handed to `scanTextSpans` (`secret-scan.ts`: `scanText` plus each claim's absolute span, allowlisted claims kept with `finding: null`) in windows of at most `STREAM_SCAN_WINDOW_CHARS` (8 Mi UTF-16 units). Each next window starts at a line start at least `STREAM_SCAN_OVERLAP_CHARS` (64 Ki, above the longest claim: a private key's 16384-char body plus fences, a jwt's 3 × 4096) before the previous end, or mid-line at exactly the overlap (never inside a surrogate pair) when the line is too long. A window keeps the claims that start before the next window's start, the last window keeps the rest, and a claim wholly inside a span an earlier window kept (a seam's partial view, or an exact duplicate) is dropped; lines are 1-based over the whole stream. `streamScanWork` (@internal) counts windows, bytes, scanned chars and the largest window. `test/secret-scan-stream.test.ts` pins equality with `scanText` across both seams of a window, mid-line splits, split multi-byte characters, invalid UTF-8 and a BOM.
- `src/core/context/sensitivity-scan.ts` — composed detector for compiled-context entries (secret-scan → PII → path shapes → operator blocklist → operator pattern file). secret-scan OWNS credential-shaped families it also matches: a PII finding whose value (raw, or with the `Bearer ` keyword stripped) matches a secret finding's fingerprint is not reported twice; short bearer tokens under secret-scan's floor still surface as `pii:bearer`. Every family honors the same `.gbrain-scan-allow` fingerprint escape hatch. `formatSensitivityDrop(drop, workspaceRoot)` renders the one content-free stderr line `gbrain compile-context` prints per dropped entry: reason `sensitivity_scan`, the pattern family, the fingerprint and the absolute `.gbrain-scan-allow` path, never the value (`test/sensitivity-scan.test.ts`).
diff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.md
index 3126507620..5164be2349 100644
--- a/docs/guides/write-refusals.md
+++ b/docs/guides/write-refusals.md
@@ -735,8 +735,8 @@ replace an allowlisted value.
### Shapes this scanner catches
Vendor key prefixes (OpenAI, Anthropic, Voyage, GitHub, GitLab, Slack, AWS, Google,
-Stripe, SendGrid, Twilio, Supabase, npm, Hugging Face, DigitalOcean, gbrain
-tokens), JWTs, `Bearer` tokens, private keys, database URLs carrying a
+Stripe, SendGrid, Twilio, Supabase, npm, Hugging Face, DigitalOcean, OpenRouter,
+Groq, xAI, Perplexity, Replicate, gbrain tokens), JWTs, `Bearer` tokens, private keys, database URLs carrying a
password, `http(s)` URLs carrying a password (`url_credentials`) and
`Authorization: Basic` credentials (`basic_auth`). A private key is caught
even when a snippet or chunk cut off its `BEGIN` or `END` line: the key body
@@ -787,7 +787,8 @@ Each finding carries:
value's hash. It identifies the value without revealing it.
- **rule ... since**: the gbrain version that added or changed the rule
(v0.60.31.0 for `url_credentials`, `basic_auth`, `digitalocean` and
- private keys whose `BEGIN` or `END` line is missing). A push that worked before an upgrade and fails after it names
+ private keys whose `BEGIN` or `END` line is missing; v0.60.499.0 for
+ `openrouter`, `groq`, `xai`, `perplexity` and `replicate`). A push that worked before an upgrade and fails after it names
the version here.
- **allow this finding**: the exact command that appends the fingerprint to
the `.gbrain-scan-allow` file at the repository root. The path is absolute
diff --git a/scripts/module-size-limits.tsv b/scripts/module-size-limits.tsv
index d033a92051..d93348961f 100644
--- a/scripts/module-size-limits.tsv
+++ b/scripts/module-size-limits.tsv
@@ -47,4 +47,4 @@ src/commands/doctor/checks/extraction-sync.ts 1500 ratchet +7 GBRA-130 A-1: link
src/core/search/evidence-delivery.ts 1740 ratchet +5 W5: EvidencePlan.rankCut resolved with the plan (logic in search/page-rank-cut.ts); +20 W5 whole_pages: wholeCost prices a block as delivery would emit it whole (needs the private Block/enrichBlock internals) and the cut is wired before allocate (cutWholePages lives in search/page-rank-cut.ts); new row: crossed the 1500 unlisted cap at the #6367 merge (master 1479, its explicit-budget cap) by the C1 date header threaded through every block path, cap included; pagePlanHits (search/page-plan-hits.ts) and the header loader (evidence-date.ts blockDateHeaders) were peeled first; peel candidate: allocateCapped; +8 at the #6396 merge: page evidence text cut through the fence-chunk trust overlay and the caller minTrust floor (re-summed: our 1504 + master +8); +119 Cat 40 next wave F2: the search.long_page_unit policy (withLongPagePolicy, selectLongPages size preflight with page/byte/deadline limits) and the long-page fetch and fallback in deliverEvidence, kept in the shared seam the plan names (gbrain-evals docs/plans/2026-10-11-cat40-next-wave, F2); +17 F2 rework to the frozen-comparison policy: search.long_page_min_chars (rendered-body characters, default 20,000) read in withLongPagePolicy, a character count in the preflight, the rendered-body confirmation in deliverEvidence, and the long-page row exemption in capDeliveredSnippets (re-summed at the 0.60.225.0 merge: master 1537 + F2 136); +39 Wave B PR 2 lane 2b: planSegmentedBlock joins the windows search/segmented-evidence.ts reads around a segmented page's hits into one run-marked document (needs the private Doc/Piece/buildDoc/locateAnchor internals), planBlock's unit cut split into blockFromDoc for both, sections bounded by their run, selectionCost counts a gap between runs, and the fetch skips the whole-body fence overlay for a segmented page; +4 segmented pages read at once before planning (segmentedReads; the read is search/segmented-evidence.ts, the statements page-body/read-range.ts); +17 GBRA-156: tail trim plan flag, DeliverOptions.query, DeliveryMeta.tail_trim, the trim call and the frozen-hit score/query library fields (logic in search/evidence-tail-trim.ts; interface fields cannot move); +7 GBRA-205: EvidencePlan.titleHeader and DeliveryMeta.title_header (interface fields cannot move) and the titles option on the header loader (logic in search/evidence-date.ts; the query budget key lives in search/evidence-packing.ts)
src/commands/config.ts 1524 ratchet new row: crossed the 1500 unlisted cap in wave 15 P7.4 #5461: set-time validation of think.pages_budget_chars / think.excerpt_ceiling_chars (range + parser live in core/think/pages-budget.ts), mirroring the cycle.consolidate.cluster_threshold hook; +1 wave 16 G5.1 (#6495): set-time refusal hook for search.keyword_statement_timeout_ms (check lives in commands/config/keyword-timeout-value.ts); +1 wave 18b lane 5 (#5012 item 6): search.contextual_retrieval prints the existing-pages hint after the write (text lives in commands/config/contextual-mode-hint.ts); +1 #6712 PR 2: retired/rollback sync key dispatch line (logic in core/sync-config-keys.ts handleSyncKeyConfig); +1 #6865: sync.slug_conflicts.auto_remove boolean validation at set time (refusal text in repair/slug-autoheal.ts); +1 #6948: sync.origin_twins.auto_resolve boolean validation at set time (refusal text in repair/origin-twins-heal.ts); +2 #6795: config set chat_model/expansion_model help line and the model-pin import (shadow report and file-pin unset live in commands/config/model-pin-shadow.ts); +1 #6712 PR 4b: sync.admission set-time hook (value logic in commands/config/sync-admission-value.ts); +4 #7130: set-time refusal hook for sync.max_deletes / sync.max_delete_ratio (and .source.<id>) / purge.retention_hours (check lives in core/sync-reconcile.ts deleteSafetyConfigRefusal); +1 GBRA-150: sync.metafile_pages set-time route (body in config/sync-metafile-pages-value.ts); re-summed at the v0.60.330.0 merge (1520 measured); +2 #7242 minions.terminal_hook file-plane key and its set branch (logic in minions/terminal-hook.ts); +1 GBRA-161: generations.* set-time route (refusal in commands/config/generations-values.ts); +1 GBRA-207 #7630: sync.repo_path set-time refusal hook (refusal in core/sync-config-keys.ts syncRepoPathRefusal)
src/commands/mcp-expose.ts 1512 ratchet hosted_option: Session carries the gbrain.io coaching notice on the first-receipt run and Tailscale/service friction exits (copy, refs and gating live in core/onboard/hosted-option.ts)
-src/core/behavior-change-notice.ts 1552 ratchet the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +2 GBRA-205 saved_facts_stems row and its comment (re-summed at the #7901 0.60.492.0 merge: 1541 + 2); +2 GBRA-205 search.query_facts_block default-on row and its approval comment (re-summed at the #7610 0.60.490.0 merge: 1539 + 2); +2 the #7900 content checkpoint host-move row (since TBD until the merge slot), +2 the #7897 private-hold sweep row (since TBD until the merge slot), +2 the #7880 ingest_capture and company-brain private-hold row (since TBD until the merge slot), +5 the frozen-source wave comment and four rows (#7868, #7850, #7869/#7870, #7871), +2 the #7848 visibility_noncanonical hold row (since TBD until the merge slot), +2 the #7846 extract_atoms session-source row (re-summed at the 0.60.481.0 merge: master 1524 + 2), +2 the #7805 synthesize session-source and #7841 visibility_not_applied rows (re-summed at the 0.60.480.0 merge: master 1522 + 2), +2 strict door planned-state row (#7705, re-summed at the 0.60.470.0 merge: master 1514 + 2), +2 Bun floor 1.4.4 row (#7644), the GBRA-95 RLS backfill row (+2), +3 rows for #7596 pass_interrupted, #7599 read-only serve and the --access=read-only ceiling, +1 G5 lane C trust_release row, +1 #7619 cli_flag_equals, +3 GBRA-207 #7630 relative-anchor and stalled-write rows, +2 the #7632 function search_path disclosure row and +2 the #7679 orphaned worktree refresh row (re-summed at the 0.60.471.0 merge: master 1516 + 2), +2 the #7814 retired v0.13.1 grandfather row, +2 the #7618 conversion withhold/progress/push row (master 1520 + 2); the behavior-change table split (#7607) retires this row; +4 GBRA-188 #7489: the wave comment and three rows (brain-first instructions, starter surface default, per-turn rule); +1 GBRA-188 plugin_turn_hook_gbra188 row
+src/core/behavior-change-notice.ts 1553 ratchet the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +2 GBRA-205 saved_facts_stems row and its comment (re-summed at the #7901 0.60.492.0 merge: 1541 + 2); +2 GBRA-205 search.query_facts_block default-on row and its approval comment (re-summed at the #7610 0.60.490.0 merge: 1539 + 2); +2 the #7900 content checkpoint host-move row (since TBD until the merge slot), +2 the #7897 private-hold sweep row (since TBD until the merge slot), +2 the #7880 ingest_capture and company-brain private-hold row (since TBD until the merge slot), +5 the frozen-source wave comment and four rows (#7868, #7850, #7869/#7870, #7871), +2 the #7848 visibility_noncanonical hold row (since TBD until the merge slot), +2 the #7846 extract_atoms session-source row (re-summed at the 0.60.481.0 merge: master 1524 + 2), +2 the #7805 synthesize session-source and #7841 visibility_not_applied rows (re-summed at the 0.60.480.0 merge: master 1522 + 2), +2 strict door planned-state row (#7705, re-summed at the 0.60.470.0 merge: master 1514 + 2), +2 Bun floor 1.4.4 row (#7644), the GBRA-95 RLS backfill row (+2), +3 rows for #7596 pass_interrupted, #7599 read-only serve and the --access=read-only ceiling, +1 G5 lane C trust_release row, +1 #7619 cli_flag_equals, +3 GBRA-207 #7630 relative-anchor and stalled-write rows, +2 the #7632 function search_path disclosure row and +2 the #7679 orphaned worktree refresh row (re-summed at the 0.60.471.0 merge: master 1516 + 2), +2 the #7814 retired v0.13.1 grandfather row, +2 the #7618 conversion withhold/progress/push row (master 1520 + 2); the behavior-change table split (#7607) retires this row; +4 GBRA-188 #7489: the wave comment and three rows (brain-first instructions, starter surface default, per-turn rule); +1 GBRA-188 plugin_turn_hook_gbra188 row; +1 GBRA-242 provider_key_rules_7959 row (#7959)
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index ef57bdec77..54f399401f 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1354,6 +1354,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
// #7917 / #7918 maintenance lowerings and unreadable derivation declarations. `since` is stamped at the merge slot.
{ since: '0.60.496.0', id: 'maintenance_lowering_7917_7918', text: 'A maintenance publication that lowers a page\'s trust tier (a dream or patterns write over a page the owner wrote, such as a grounding repair outside the output prefix) now files a `lower_page` proposal, so the page shows up in `gbrain trust review`; before, only agent content writes did. A maintenance write whose derivation declaration is present but unreadable now publishes at `unknown` and stamps an `unknown` trust marker into the file, instead of taking the write channel\'s tier and leaving the file unmarked. Both only lower trust. `gbrain trust backfill --repair-unknown-markers` and `gbrain repair provenance` on a classic (unmanaged) checkout now also write the repaired page to its file in the same step, and leave a page whose file holds an edit sync has not imported untouched (run `gbrain sync` first).' },
{ since: '0.60.498.0', id: 'plugin_turn_hook_gbra188', text: 'The Claude Code and Codex plugins (gbrain, gbrain-coding, gbrain-daily) now declare the UserPromptSubmit hook `gbrain setup` writes, run through the plugin launcher with GBRAIN_HOOK_LANE=plugin (Claude Code `hook user-prompt`, 3 s; Codex `hook user-prompt --harness codex`, 15 s, skipped until trusted in /hooks). While a brain-first block is installed it adds the per-turn rule, and with a serve running the read-context block; it exits without output when setup, bootstrap or a workspace install already wires the event, so a turn gets it once; a recorded `gbrain setup <harness> --no-hooks` turns it off too. doctor `harness_wiring` and `gbrain post-upgrade` now name `gbrain setup <harness> --instructions-only` for a plugin-only install with no brain-first block.' },
+ { since: '0.60.498.0', id: 'provider_key_rules_7959', text: 'The secret scanner now catches OpenRouter (`sk-or-v1-`), Groq (`gsk_`), xAI (`xai-`), Perplexity (`pplx-`) and Replicate (`r8_`) API keys: `gbrain sources push` refuses a page that holds one, and search, recall and transcript import redact it. Rotate such a key with its provider rather than allowlisting it. `gbrain doctor` asks once for a fresh `gbrain transcripts audit-secrets` run.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/secret-scan.ts b/src/core/secret-scan.ts
index 44d3cce68f..b98a883724 100644
--- a/src/core/secret-scan.ts
+++ b/src/core/secret-scan.ts
@@ -173,6 +173,12 @@ interface CorePattern {
*/
const SECURITY_WAVE_SINCE = '0.60.31.0';
+/**
+ * `since` for the AI-provider key rules added by #7959 (`openrouter`, `groq`,
+ * `xai`, `perplexity`, `replicate`).
+ */
+const PROVIDER_KEYS_SINCE = '0.60.499.0';
+
/**
* Placeholder passwords in documentation URLs: `<password>`, `${VAR}`,
* `$VAR`, all-`*` and all-`x` masks. Never a live credential, so
@@ -242,6 +248,15 @@ const CORE_PATTERNS: ReadonlyArray<CorePattern> = [
// DigitalOcean personal access (dop_), OAuth (doo_) and refresh (dor_)
// tokens: v1 + 64 lowercase hex, hard right edge.
{ name: 'digitalocean', source: 'do[opr]_v1_[a-f0-9]{64}(?![A-Za-z0-9])', since: SECURITY_WAVE_SINCE },
+ // AI-provider keys (#7959): each a fixed-length body with a hard right edge,
+ // so a longer run of the same characters is never cut into a false key.
+ // OpenRouter: v1 + 64 lowercase hex. The dashes after `sk-or` and `v1` stop
+ // the bare sk- rule above, which is why it needs its own entry.
+ { name: 'openrouter', source: 'sk-or-v1-[a-f0-9]{64}(?![A-Za-z0-9])', since: PROVIDER_KEYS_SINCE },
+ { name: 'groq', source: 'gsk_[A-Za-z0-9]{52}(?![A-Za-z0-9])', since: PROVIDER_KEYS_SINCE },
+ { name: 'xai', source: 'xai-[A-Za-z0-9_]{80}(?![A-Za-z0-9_])', since: PROVIDER_KEYS_SINCE },
+ { name: 'perplexity', source: 'pplx-[A-Za-z0-9]{48}(?![A-Za-z0-9])', since: PROVIDER_KEYS_SINCE },
+ { name: 'replicate', source: 'r8_[A-Za-z0-9_-]{37}(?![A-Za-z0-9_-])', since: PROVIDER_KEYS_SINCE },
// gbrain's own tokens: generateToken (core/utils.ts) mints 'gbrain_' plus an
// optional OAuth infix (at_ access / rt_ refresh / cs_ client secret /
// code_ auth code) plus 32 random bytes hex. Without this entry the scanner
diff --git a/src/core/transcripts/render.ts b/src/core/transcripts/render.ts
index ddfc99c18f..a12401f818 100644
--- a/src/core/transcripts/render.ts
+++ b/src/core/transcripts/render.ts
@@ -133,7 +133,8 @@ export function loadImportRedactionPatterns(userPatternsPath?: string): ImportRe
*
* Detector classes (all from `secret-scan.ts`, format-based — not just vendor
* prefixes): vendor key shapes (anthropic/openai/voyage/github/gitlab/slack/
- * aws/google/stripe/sendgrid/twilio/supabase/npm/huggingface/gbrain), JWTs,
+ * aws/google/stripe/sendgrid/twilio/supabase/npm/huggingface/digitalocean/
+ * openrouter/groq/xai/perplexity/replicate/gbrain), JWTs,
* whole-block PEM private keys, `Bearer <token>` headers, connection strings
* carrying inline credentials, and — opted in for THIS lane — the
* high-entropy `KEY=`/`TOKEN=`/`PASSWORD=` assignment heuristic (value must
diff --git a/src/core/transcripts/secret-audit.ts b/src/core/transcripts/secret-audit.ts
index cb98586271..d514c6f27e 100644
--- a/src/core/transcripts/secret-audit.ts
+++ b/src/core/transcripts/secret-audit.ts
@@ -17,7 +17,7 @@ import type { BrainEngine } from '../engine.ts';
import { scanText } from '../secret-scan.ts';
/** Bump when the transcript-lane detectors change, so doctor asks for a fresh audit. */
-export const TRANSCRIPT_SECRET_AUDIT_VERSION = 1;
+export const TRANSCRIPT_SECRET_AUDIT_VERSION = 2;
export const TRANSCRIPT_SECRET_AUDIT_KEY = 'transcripts.secret_audit';
const BATCH = 25;
const LINES_PER_PAGE = 20;
diff --git a/test/helpers/provider-keys.ts b/test/helpers/provider-keys.ts
new file mode 100644
index 0000000000..d96ff63ee7
--- /dev/null
+++ b/test/helpers/provider-keys.ts
@@ -0,0 +1,87 @@
+/**
+ * Synthetic AI-provider API keys for the #7959 secret-scan rules (openrouter,
+ * groq, xai, perplexity, replicate), each built at runtime from random parts
+ * so no committed line carries a key-shaped literal (gitleaks, CEO-10).
+ *
+ * `nearMisses` are strings one step off each published shape (a body one
+ * character short or long, the wrong case or separator, the prefix embedded
+ * in a longer word). No rule may match them.
+ */
+import { randomBytes } from 'node:crypto';
+
+const ALNUM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+
+/** Random alphanumeric run that always carries a digit and a letter. */
+export function alnum(n: number): string {
+ let s = '';
+ for (const b of randomBytes(n)) s += ALNUM[b % ALNUM.length];
+ return 'k' + s.slice(2) + String(randomBytes(1)[0]! % 10);
+}
+
+export const hex = (n: number): string => randomBytes(Math.ceil(n / 2)).toString('hex').slice(0, n);
+
+export interface ProviderKeyShape {
+ pattern: 'openrouter' | 'groq' | 'xai' | 'perplexity' | 'replicate';
+ make(): string;
+ nearMisses(): string[];
+}
+
+const join = (sep: string, ...parts: string[]) => parts.join(sep);
+
+export const PROVIDER_KEY_SHAPES: ReadonlyArray<ProviderKeyShape> = [
+ {
+ pattern: 'openrouter',
+ make: () => join('-', 'sk', 'or', 'v1', hex(64)),
+ nearMisses: () => [
+ join('-', 'sk', 'or', 'v1', hex(63)),
+ join('-', 'sk', 'or', 'v1', hex(65)),
+ join('-', 'sk', 'or', 'v1', hex(64).toUpperCase().replace(/[0-9]/g, 'A')),
+ join('-', 'xsk', 'or', 'v1', hex(64)),
+ join('_', 'sk', 'or', 'v1', hex(64)),
+ ],
+ },
+ {
+ pattern: 'groq',
+ make: () => join('_', 'gsk', alnum(52)),
+ nearMisses: () => [
+ join('_', 'gsk', alnum(51)),
+ join('_', 'gsk', alnum(53)),
+ join('-', 'gsk', alnum(52)),
+ join('_', 'agsk', alnum(52)),
+ ],
+ },
+ {
+ pattern: 'xai',
+ make: () => join('-', 'xai', alnum(40) + '_' + alnum(39)),
+ nearMisses: () => [
+ join('-', 'xai', alnum(79)),
+ join('-', 'xai', alnum(81)),
+ join('_', 'xai', alnum(80)),
+ join('-', 'foxai', alnum(80)),
+ ],
+ },
+ {
+ pattern: 'perplexity',
+ make: () => join('-', 'pplx', alnum(48)),
+ nearMisses: () => [
+ join('-', 'pplx', alnum(47)),
+ join('-', 'pplx', alnum(49)),
+ join('_', 'pplx', alnum(48)),
+ join('-', 'applx', alnum(48)),
+ ],
+ },
+ {
+ pattern: 'replicate',
+ make: () => join('_', 'r8', alnum(20) + '-' + alnum(16)),
+ nearMisses: () => [
+ join('_', 'r8', alnum(36)),
+ join('_', 'r8', alnum(38)),
+ join('-', 'r8', alnum(37)),
+ join('_', 'ar8', alnum(37)),
+ ],
+ },
+];
+
+/** Prose that names every prefix without carrying a key. */
+export const PROVIDER_PREFIX_PROSE =
+ 'route through the sk-or-v1 router, a gsk_ prefix, the xai-sdk, pplx-api docs and r8_ tokens are rotated monthly';
diff --git a/test/search/output-redaction.test.ts b/test/search/output-redaction.test.ts
index 0c3f4730e1..3663cf6d33 100644
--- a/test/search/output-redaction.test.ts
+++ b/test/search/output-redaction.test.ts
@@ -7,6 +7,7 @@ import {
import type { OperationContext } from '../../src/core/ops/contract.ts';
import { encodeDeepResearchId } from '../../src/core/deep-research-id.ts';
import { cpuMs } from '../helpers/cpu-time.ts';
+import { PROVIDER_KEY_SHAPES } from '../helpers/provider-keys.ts';
const key = 'sk-proj-' + 'syntheticfixture19'.repeat(3);
const bearer = 'syntheticbareecho19'.repeat(3);
@@ -300,6 +301,16 @@ describe('retrieval output: shapes that need the S1 scanner patterns', () => {
}
});
+ test('AI-provider keys (#7959) are redacted under their own rule, nested fields included', () => {
+ for (const shape of PROVIDER_KEY_SHAPES) {
+ const key = shape.make();
+ expect(redact(`router key ${key} end`)).toBe(`router key <REDACTED:${shape.pattern}> end`);
+ const out = redactRetrievalOutput([{ title: key, chunk_text: `OPENAI_BASE=x KEY=${key}` }], { nested: [{ detail: key }] });
+ expect(JSON.stringify(out).includes(key)).toBe(false);
+ expect(out.results[0]!.title).toBe(`<REDACTED:${shape.pattern}>`);
+ }
+ });
+
test('a private key cut before its END fence loses its body, and preceding prose survives', () => {
const lines = [b64Line(64), b64Line(64), b64Line(40)];
const out = redact(`deploy notes:\n${begin('RSA PRIVATE KEY')}\n${lines.join('\n')}`);
diff --git a/test/secret-scan-credential-shapes.test.ts b/test/secret-scan-credential-shapes.test.ts
index 03680d1fb3..ea8cb7c179 100644
--- a/test/secret-scan-credential-shapes.test.ts
+++ b/test/secret-scan-credential-shapes.test.ts
@@ -13,6 +13,7 @@
import { describe, expect, test } from 'bun:test';
import { randomBytes } from 'crypto';
import { correctedEntropy, patternSince, redactFindings, scanText, shannonEntropy } from '../src/core/secret-scan.ts';
+import { PROVIDER_KEY_SHAPES, PROVIDER_PREFIX_PROSE } from './helpers/provider-keys.ts';
const ALNUM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/** Random alphanumeric string that always carries a digit (entropy rule input). */
@@ -296,6 +297,47 @@ describe('A5 high_entropy_assignment: punctuated values', () => {
});
});
+describe('A6 AI-provider keys (#7959)', () => {
+ test('each key fires under its own rule in prose, KEY=value, JSON and after Bearer, and redacts to its token', () => {
+ for (const shape of PROVIDER_KEY_SHAPES) {
+ const key = shape.make();
+ for (const line of [
+ `router key ${key} end`,
+ `API_KEY=${key}`,
+ JSON.stringify({ api_key: key }),
+ `Authorization: Bearer ${key}`,
+ ]) {
+ for (const opts of [{}, { highEntropy: true, labeledCredentials: true }]) {
+ const findings = scanText(line, opts);
+ expect(findings.map((f) => f.pattern), line).toEqual([shape.pattern]);
+ expect(JSON.stringify(findings).includes(key)).toBe(false);
+ expect(redactFindings(line, opts).text.includes(key)).toBe(false);
+ }
+ }
+ expect(redactFindings(`k ${key} end`).text).toBe(`k ${REDACTED(shape.pattern)} end`);
+ }
+ });
+
+ test('near misses fire no rule at all: wrong length, case or separator, or the prefix inside a longer word', () => {
+ for (const shape of PROVIDER_KEY_SHAPES) {
+ for (const miss of shape.nearMisses()) {
+ expect(scanText(`see ${miss} end`, { highEntropy: true, labeledCredentials: true }), miss).toEqual([]);
+ }
+ }
+ expect(scanText(PROVIDER_PREFIX_PROSE, { highEntropy: true, labeledCredentials: true })).toEqual([]);
+ });
+
+ test('the rules carry one since version, which every finding reports', () => {
+ const since = patternSince('openrouter');
+ expect(since).toMatch(/^\d+\.\d+\.\d+\.\d+$/);
+ expect(since).not.toBe(patternSince('digitalocean'));
+ for (const shape of PROVIDER_KEY_SHAPES) {
+ expect(patternSince(shape.pattern)).toBe(since);
+ expect(scanText(`k ${shape.make()}`)[0]!.since).toBe(since);
+ }
+ });
+});
+
describe('CEO-12 ordering and DX-5 since', () => {
test('vendor shapes win attribution over the appended catch-alls', () => {
const tok = ['dop', 'v1', hex(64)].join('_');
diff --git a/test/sources-push-cli.serial.test.ts b/test/sources-push-cli.serial.test.ts
index 9ca9cf9af4..688e80f1c4 100644
--- a/test/sources-push-cli.serial.test.ts
+++ b/test/sources-push-cli.serial.test.ts
@@ -23,8 +23,9 @@ import { execFileSync, spawnSync } from 'node:child_process';
import { runSources } from '../src/commands/sources.ts';
import { acquirePushLock, resolveWorkspaceRoot, PUSH_STREAM_SCAN_MAX_BYTES } from '../src/core/workspace-push.ts';
-import { SCAN_ALLOW_FILENAME } from '../src/core/secret-scan.ts';
+import { SCAN_ALLOW_FILENAME, patternSince } from '../src/core/secret-scan.ts';
import type { BrainEngine } from '../src/core/engine.ts';
+import { PROVIDER_KEY_SHAPES } from './helpers/provider-keys.ts';
const T = 60_000; // explicit per-test timeout — bun ignores bunfig.toml's key
const OPENAI = 'sk-' + 'A1b2C3d4E5f6G7h8I9j0K1l2M3n4';
@@ -312,6 +313,25 @@ describe('--json output', () => {
// ── refusal guidance (security wave DX-3/ENG-11/CEO-21) ─────────────────────
+describe('AI-provider keys (#7959) block the push', () => {
+ test('each bare key is one finding under its own rule with its since; no key reaches stdout or stderr', async () => {
+ const keys = PROVIDER_KEY_SHAPES.map((s) => s.make());
+ writeFileSync(join(work, 'keys.md'), keys.map((k, i) => `line ${i}: ${k}`).join('\n') + '\n');
+ const r = await runPushCli(untouchableEngine, ['--path', work, '--allow-unverified-remote', '--json']);
+ expect(r.exitCode).toBe(5);
+ const res = JSON.parse(r.stdout[0]!);
+ expect(res.status).toBe('blocked_secrets');
+ expect(res.findings.map((f: { pattern: string }) => f.pattern).sort()).toEqual(PROVIDER_KEY_SHAPES.map((s) => s.pattern).sort());
+ for (const f of res.findings) {
+ expect(f.file).toBe('keys.md');
+ expect(f.since).toBe(patternSince(f.pattern));
+ }
+ const out = [...r.stdout, ...r.stderr].join('\n');
+ for (const k of keys) expect(out.includes(k)).toBe(false);
+ expect(originHead(bare)).toBe(git(work, 'rev-parse', 'origin/main'));
+ }, T);
+});
+
describe('blocked_secrets guidance — runnable from outside the source dir', () => {
function cloneAt(dir: string): void {
execFileSync('git', ['-c', 'protocol.file.allow=always', 'clone', '-q', bare, dir], {
diff --git a/test/transcripts-ingest-pglite.test.ts b/test/transcripts-ingest-pglite.test.ts
index 9639a6ae51..4be3a4e896 100644
--- a/test/transcripts-ingest-pglite.test.ts
+++ b/test/transcripts-ingest-pglite.test.ts
@@ -29,6 +29,7 @@ import {
import type { HarnessRoot } from '../src/core/transcripts/detect.ts';
import { buildTranscriptSlug } from '../src/core/transcripts/types.ts';
import { buildHermesFixture } from './fixtures/transcripts/hermes-fixture-builder.ts';
+import { PROVIDER_KEY_SHAPES } from './helpers/provider-keys.ts';
const CODEX_SLUG = buildTranscriptSlug('codex', '2026-08-02T09:00:00.000Z', {
sessionId: 'rollout-1', // payload.id — the per-thread id (#4981)
@@ -752,6 +753,30 @@ describe('format-based redaction before write (unprefixed credential shapes)', (
});
});
+describe('AI-provider keys (#7959) are redacted before write', () => {
+ test('a bare key in a message lands as its own placeholder; neither body nor raw metadata keeps it', async () => {
+ const keys = PROVIDER_KEY_SHAPES.map((s) => ({ pattern: s.pattern, key: s.make() }));
+ const p = join(tmp, 'provider-keys-session.jsonl');
+ writeFileSync(
+ p,
+ keys.map(({ pattern, key }, i) =>
+ claudeCodeLine(i % 2 ? 'assistant' : 'user', `pk-000${i}`, `2026-08-26T10:00:0${i}.000Z`, `the ${pattern} key is ${key} for now`),
+ ).join('\n') + '\n',
+ );
+ const r = await runTranscriptsIngest(engine, baseOpts([p]));
+ expect(r.pages.imported).toBe(1);
+ expect(r.redactions).toBeGreaterThanOrEqual(keys.length);
+ const slug = r.slugsTouched[0];
+ const body = (await engine.getPage(slug, { sourceId: 'default' }))!.compiled_truth;
+ const stored = JSON.stringify(await engine.getRawData(slug, undefined, { sourceId: 'default' }));
+ for (const { pattern, key } of keys) {
+ expect(body).toContain(`the ${pattern} key is <REDACTED:${pattern}> for now`);
+ expect(body).not.toContain(key);
+ expect(stored).not.toContain(key);
+ }
+ });
+});
+
/** Count calls to an engine method for one block; restores the original. */
async function spyEngine<K extends 'getRawData' | 'putRawData'>(
method: K,
diff --git a/test/transcripts-secret-audit.test.ts b/test/transcripts-secret-audit.test.ts
index 9ae4a5a024..9451e82d4b 100644
--- a/test/transcripts-secret-audit.test.ts
+++ b/test/transcripts-secret-audit.test.ts
@@ -11,12 +11,14 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { resetPgliteState } from './helpers/reset-pglite.ts';
import {
TRANSCRIPT_SECRET_AUDIT_KEY,
+ TRANSCRIPT_SECRET_AUDIT_VERSION,
auditTranscriptSecrets,
loadTranscriptSecretAudit,
saveTranscriptSecretAudit,
} from '../src/core/transcripts/secret-audit.ts';
import { transcriptSecretExposureCheck } from '../src/commands/doctor/checks/transcript-secrets.ts';
import type { DoctorContext } from '../src/commands/doctor/context.ts';
+import { PROVIDER_KEY_SHAPES } from './helpers/provider-keys.ts';
const PW = ['hunter', '2'].join('');
let engine: PGLiteEngine;
@@ -60,6 +62,16 @@ describe('transcripts audit-secrets', () => {
expect(JSON.stringify(result)).not.toContain(PW);
});
+ test('AI-provider keys (#7959) are counted under their own rule, never returned', async () => {
+ const keys = PROVIDER_KEY_SHAPES.map((s) => s.make());
+ await conversation('conversations/sessions/2026-01-04-codex-cccccccccccc', keys.map((k, i) => `line ${i}: key ${k}`).join('\n'));
+ const result = await auditTranscriptSecrets(engine);
+ expect(result.pages_affected).toBe(1);
+ expect(result.by_pattern).toEqual(Object.fromEntries(PROVIDER_KEY_SHAPES.map((s) => [s.pattern, 1])));
+ expect(result.pages[0]!.lines).toEqual(keys.map((_, i) => i + 1));
+ for (const k of keys) expect(JSON.stringify(result)).not.toContain(k);
+ });
+
test('the cached summary carries no slugs and no values', async () => {
await conversation('conversations/sessions/2026-01-02-codex-aaaaaaaaaaaa', `password: ${PW}`);
expect(await saveTranscriptSecretAudit(engine, await auditTranscriptSecrets(engine))).toBe(true);
@@ -101,6 +113,19 @@ describe('transcript_secret_exposure doctor check', () => {
expect(ok?.status).toBe('ok');
});
+ test('a clean audit cached by older detectors no longer reads ok: the #7959 rules ask for a fresh audit', async () => {
+ await conversation('conversations/sessions/2026-01-02-codex-aaaaaaaaaaaa', `key ${PROVIDER_KEY_SHAPES[0]!.make()}`);
+ const clean = { ...(await auditTranscriptSecrets(engine)), pages_affected: 0, hits_total: 0, by_pattern: {} };
+ await saveTranscriptSecretAudit(engine, { ...clean, detector_version: TRANSCRIPT_SECRET_AUDIT_VERSION - 1 });
+ const stale = await transcriptSecretExposureCheck(ctx());
+ expect(stale?.status).toBe('warn');
+ expect(stale?.message).toContain('the last audit used older detectors');
+ await saveTranscriptSecretAudit(engine, await auditTranscriptSecrets(engine));
+ const fresh = await transcriptSecretExposureCheck(ctx());
+ expect(fresh?.status).toBe('warn');
+ expect(fresh?.message).toContain('found 1 credential hit(s)');
+ });
+
test('a source-scoped audit does not clear the brain-wide warning', async () => {
await conversation('conversations/sessions/2026-01-02-codex-aaaaaaaaaaaa', 'clean');
await saveTranscriptSecretAudit(engine, await auditTranscriptSecrets(engine, { sourceId: 'default' }));