Skip to content

fix(memory-ingest): --scan-secrets scans the rendered page and fails closed - #2945

Closed
brucek wants to merge 2 commits into
garrytan:mainfrom
brucek:fix/memory-ingest-scan-rendered-page
Closed

brucek wants to merge 2 commits into
garrytan:mainfrom
brucek:fix/memory-ingest-scan-rendered-page

Conversation

@brucek

@brucek brucek commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Why (in your own words)

I ran /setup-gbrain with --scan-secrets expecting nothing secret to reach the brain. It skipped 30 files, but gitleaks still flags 4 of the 159 pages it imported. The scan ran on the raw .jsonl, where JSON escapes the quotes around a value (KEY=\"value\"), and gitleaks' assignment rules don't match that. The page that actually gets imported has the unescaped KEY="value", which they do match. Separately, when gitleaks can't produce a result (non-zero exit, a 16 MB maxBuffer overflow on a file with many findings, missing binary, slow-probe cooldown), the file was imported unscanned. This PR scans the exact bytes that get staged and, under --scan-secrets, skips any file it could not scan. Fixes #2944.

Live evidence

Standalone: the escaping gap (gitleaks 8.30.1, random value):

$ V=$(LC_ALL=C tr -dc 'bcdfghjklmnpqrstvwxz0-9' </dev/urandom | head -c 16)
$ printf '{"text":"LINKEDIN_CLIENT_SECRET=\\"%s\\""}\n' "$V" > raw.jsonl
$ printf 'LINKEDIN_CLIENT_SECRET="%s"\n' "$V" > page.md
$ gitleaks detect --no-git --no-banner --source raw.jsonl
INF no leaks found
$ gitleaks detect --no-git --no-banner --source page.md
WRN leaks found: 1

End-to-end: bin/gstack-memory-ingest.ts --bulk --scan-secrets with the real gitleaks 8.30.1, a throwaway HOME, one synthetic Codex rollout per scenario (random fake values), and a gbrain shim that snapshots the staging dir. The last column is gitleaks run over that snapshot, i.e. what would have been imported.

Before (main, 636175d):

-- clean-control    written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 0
-- escaped-secret   written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 2 ["generic-api-key","linkedin-client-secret"]
     raw .jsonl (scanned today)      scanner=gitleaks findings=0
     rendered page body (imported)   scanner=gitleaks findings=2
-- many-findings    written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 4875   (raw scan: ENOBUFS -> scanner "error")
-- scanner-error    written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 2     (GITLEAKS_CONFIG -> malformed TOML -> FTL)
-- scanner-missing  written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 2
     stderr: gitleaks not in PATH; secret scanning disabled.

After (this branch):

-- clean-control    written: 1 | skipped (secret-scan): 0 | pages imported: 1 | findings in them: 0
-- escaped-secret   written: 0 | skipped (secret-scan): 1 | pages imported: 0
     stderr: [secret-scan match] $HOME/.codex/sessions/2026/09/22/rollout-…-synthetic.jsonl (2 findings); skipped
-- many-findings    written: 0 | skipped (secret-scan): 1 | pages imported: 0
     stderr: [secret-scan match] … (4874 findings); skipped
-- scanner-error    written: 0 | skipped (secret-scan): 1 | pages imported: 0
     stderr: [secret-scan error] … (gitleaks could not scan it); skipped
-- scanner-missing  written: 0 | skipped (secret-scan): 1 | pages imported: 0
     stderr: gitleaks not in PATH; files cannot be secret-scanned. …
     stderr: [secret-scan missing] … (gitleaks could not scan it); skipped

Tests. bun test test/gstack-memory-ingest.test.ts test/gstack-memory-helpers.test.ts: 80 pass, 0 fail. Against main's source with these tests: the 3 new ingest tests fail, and the helpers file fails to load (no secretScanText export).

What changed

  • lib/gstack-memory-helpers.ts: new secretScanText(text). It writes the text to a private temp file (mkdtemp 0700, file 0600), runs the unchanged secretScanFile() on it, and removes it in finally. A temp-file failure returns scanner: "error", never an empty "clean" result. The absent/slow-probe warnings no longer say files "go unscanned" / "secret scanning disabled", since the ingest now skips them.
  • bin/gstack-memory-ingest.ts preparePages(): the scan moves from before parsing to after renderPageBody(), on rendered_body, the string writeStaged() writes verbatim (disambiguateSlugs() only renames). Under --scan-secrets a file is skipped on any finding or when scanner !== "gitleaks". Skips are counted in the existing skipped (secret-scan) line, so BulkResult is unchanged. The stderr line says which: [secret-scan match] vs [secret-scan error|missing] … (gitleaks could not scan it). As before, skipped files aren't recorded in state, so the next run retries them.
  • setup-gbrain/memory.md: --scan-secrets now scans the rendered page, and without gitleaks files are skipped, not imported unscanned.
  • Tests: a shared installFakeGitleaks() whose detect keys off the content of the scanned file. The existing --scan-secrets test keyed off the source path containing "dirty", which no longer reaches gitleaks. New tests:
    • a Codex session whose secret is only visible unescaped. The test asserts the raw file lacks the marker, then that the page is skipped and never staged.
    • fail-closed for scanner error and missing
    • secretScanText scans the exact text, cleans up its temp file, and reports missing rather than clean

Deliberate behavior change for review: the raw-file scan is replaced, not kept alongside. Every staged byte is in rendered_body. Keeping both would double the per-file gitleaks cost (~256 ms/file) and keep skipping files whose only findings are in content the page never includes, e.g. tool results and function_call_output, which parseTranscriptJsonl() drops. Happy to keep the source scan as an extra gate if you'd rather.

Scope

  • Changed: lib/gstack-memory-helpers.ts, bin/gstack-memory-ingest.ts, setup-gbrain/memory.md, test/gstack-memory-helpers.test.ts, test/gstack-memory-ingest.test.ts
  • Verified live by: the sandboxed before/after ingest runs above (macOS, gitleaks 8.30.1, bun 1.4.2); the two affected test files; the full bun run test on this branch and on main, same machine and session: identical failing-test sets (389 each). All of those are in PTY/CSO/Codex/autoplan suites that need a less locked-down environment than mine; none are in memory-ingest, helpers or setup-gbrain.
  • Did NOT test:
    • Linux or Windows
    • gitleaks versions other than 8.30.1
    • remote-http MCP mode: the scan runs in preparePages() before either staging branch, but I didn't exercise it
    • the GSTACK_INGEST_RESUME_DIR path: a dir staged earlier without --scan-secrets is still imported as-is on resume
    • re-running on my real corpus: the repro is synthetic on purpose, so no real secrets were handled

Liveness proof (required for external contributors)

To be attached by @brucek (screenshot with GSTACK PR typed live) before this PR leaves draft.

Checklist

🤖 Generated with Claude Code

…closed

--scan-secrets ran gitleaks on the raw transcript .jsonl, then imported a
page rendered from it. gitleaks' assignment rules don't match across a
JSON-escaped quote (KEY=\"v\" on disk), so a secret the rendered page
shows as KEY="v" was imported unflagged. And the gate skipped a file only
on scanner "gitleaks" with findings, so a scan that errored (non-zero
exit, 16MB maxBuffer overflow on a file with many findings, unparseable
report) or could not run (gitleaks missing, slow-probe cooldown) imported
the file unscanned.

Scan the rendered page body, the exact bytes writeStaged() writes, via a
new secretScanText() helper, and skip the file whenever the scan did not
complete. Skipped files stay out of the state file, so the next run
retries them. Reword the helper warnings and setup-gbrain/memory.md,
which described the fail-open as intended.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trunk-io

trunk-io Bot commented Sep 23, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@brucek

brucek commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

Hopefully this is right..?

Screenshot 2026-09-22 at 10 33 36 PM

garrytan#2959 (v1.91.2.0) merged this branch's commit verbatim as the
first item of its reliability wave, plus follow-up hardening (scan-mode-aware
dedup, snapshot-bound scans at the import boundary). Every conflict resolves
to main's side. git's clean text merge of lib/gstack-memory-helpers.ts had
kept both copies of secretScanText (a duplicate export), so that file is
main's too. The merged tree is identical to main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@brucek

brucek commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2959, which merged this change (plus follow-up hardening) in v1.91.2.0. Thanks for folding it in!

@brucek brucek closed this Sep 26, 2026
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.

memory-ingest --scan-secrets misses JSON-escaped secrets and fails open when gitleaks can't scan

1 participant