Skip to content

fix(capture): keep secrets and reasoning out of capture and the store - #67

Merged
abogartz merged 5 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-secrets
Oct 24, 2026
Merged

abogartz merged 5 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-secrets

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

When auto-capture runs, it sends the flattened transcript to the capture model and stores what comes back. Separately, any write can store a credential verbatim. I found both while reviewing 0.8.0, and both are unchanged in 0.8.1. Updated after your review: redaction now also runs in appendMemory, metadata is rejected instead of rewritten, and there's a README section.

The problem and its reach. Auto-capture is off by default, so the capture half affects opt-in installs. The write half affects every install.

  • Capture (opt-in installs): an API key pasted into the chat, or a token a tool printed, is sent to the capture model. The model echoes it back, so it's stored, and later sessions get it injected into their system prompt. reasoning/thinking parts are also sent to the capture model.
  • Writes (every install): memory_remember and the other write paths store a credential verbatim, and from then on it's injected into the system prompt.

#38 covers instruction-shaped content, not credentials.

Change:

  • flattenTranscript skips reasoning/thinking parts, including the fallback to top-level text for a message that only had private parts.
  • New src/secrets.ts, a small, best-effort redactor:
    • well-known token formats: sk-…, ghp_/gho_/github_pat_, xoxb-/xoxp-, AKIA…, AIza…;
    • Bearer <token>, when the token contains a digit;
    • PEM private-key blocks;
    • high-entropy values assigned to *_KEY/*_TOKEN/*_SECRET/*_PASSWORD/password.
  • Content is redacted (matches become [redacted]):
    • on the capture prompt, before it leaves the plugin;
    • in runWriteGate, before hashing, canonical analysis, dedup, evidence and persistence. That covers memory_remember, memory_update, memory_import, auto-capture candidates and memory_init;
    • in appendMemory, the single write choke point. That also covers the viewer's /api/edit and /api/restore-entry, which bypass the gate.
  • Metadata is rejected, not rewritten. A credential-shaped scope, tags or issue is refused, so a memory is never silently re-scoped:
    • at the gate, as a credential_in_metadata block with a hint naming the field;
    • in appendMemory, as a thrown error.
      This matches fix(inject): keep every rendered memory field on its framed line #64's reject-don't-rewrite approach for control characters. The evidence records only the field names, and the auto-capture outcome record no longer logs a raw scope.
  • Unchanged: whole-snapshot /api/restore, rows already in a store, and lookups (memory_recall/memory_forget/memory_export by scope still match a legacy row, so it can still be removed).
  • README: a new "Secret redaction" section covers what runs where and which families are covered. It says this is best effort and lists the known misses: hex-only secrets, low-character-class passwords, and credentials in prose without an assignment. It also says memory_remember still replies Remembered when it redacts, so the stored text can differ from what was sent (per your ruling, no notice in the reply). Finally, it says redaction is forward-only: existing stores aren't scrubbed and memory_export writes them verbatim (Secrets already in the store are never scrubbed, and memory_export writes them verbatim #74).

Not changed, but worth a look: capture reads event.sessionID ?? event.properties.sessionID (index.ts:869). On opencode 2.x, session.idle carries it in event.data.sessionID, which your identity handler a few lines up already reads. In a fake 2.x host, capture made 0 model calls. Does it fire for you on a real 2.x host?

Tests:

  • test/capture.test.ts:
    • Each token family is redacted, and the identifier and env-reference controls are not.
    • The capture prompt contains neither the key nor the reasoning part.
    • A capture response containing a key is stored redacted.
    • A captured candidate with a credential-shaped scope is blocked, and the key appears nowhere in the evidence log.
  • test/write-caps.test.ts:
    • appendMemory called directly redacts content (your sk-abcdef… repro).
    • It rejects credential-shaped scope, tags and issue, and stores nothing.
    • The gate blocks with credential_in_metadata and no secret in evidence.
    • memory_remember replies with the unchanged Remembered: ….
    • Recall, export and forget still work on a legacy credential-shaped scope.
  • test/viewer.test.ts: a viewer edit stores the replacement redacted.

With the source change reverted, the new tests fail.

Checks (Linux, Bun 1.4.4, on 0.8.4): bun test ./test/ 310 pass, 4 skip, 0 fail. bun run test:e2e against opencode 2.0.26: 9 pass. tsc --noEmit is clean, and biome lint exits 0 with warnings unchanged from main. Nothing here is platform-specific.

Merge order: main (0.8.4) is merged in. Where #64 and this PR both validate metadata (the gate and appendMemory), the control-character check runs first, then the credential check, then content redaction. It applies cleanly alongside #62, #66 and #76 and #77; I merged all of them together on 0.8.4 to check.

🤖 Generated with Claude Code

Auto-capture sent the whole transcript, including pasted API keys and the
model's reasoning parts, to the capture model, and stored whatever came back.
A key in the transcript ended up in the store and in later system prompts.

- flattenTranscript skips reasoning/thinking parts.
- New src/secrets.ts redacts well-known token families, bearer tokens,
  PEM private keys and high-entropy values assigned to *_KEY/*_TOKEN/
  *_SECRET/*_PASSWORD names. It's applied to the capture prompt and in
  runWriteGate, so manual, imported, updated and captured writes share it.
- Env-var references and identifiers are left alone.

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

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes. The leak is real, the redactor is better than I expected, and the maintainer has decided to keep the scrub — what's missing is documentation and honest scoping of what "one rule set" actually covers.

Verified on 0.8.1 (main @ 4643ec4) and on your branch

  • flattenTranscript on main ships reasoning and thinking parts to the capture model, including the fallback case: assistant: internal chain of thought mentioning sk-… → on your branch the transcript is exactly user: please remember the flimflam widget build command.
  • buildCapturePrompt on main embeds a pasted key verbatim; on your branch it is [redacted].
  • runWriteGate redacts before hashing/analysis/persistence: a memory_remember carrying sk-… stores no key on your branch (main stores it verbatim).
  • Independent false-positive battery (my cases, not yours): redacted 5/5 secret shapes (sk-, ghp_, Bearer <token with digits>, PEM block, API_KEY="aB3dE5fG7hI9jK1lM3nO"); left untouched 10/10 realistic keepers — API_KEY=process.env.OPENAI_API_KEY, const API_TOKEN = resolveTokenName, Bearer authentication-scheme, Authorization: Bearer <your-account-slug-2024>, bun test ./test/, a 40-char action SHA pin, an issue/logfmt filename reference, ordinary prose about API keys. So "leaves ordinary code alone" holds. Known deliberate misses: hex-only secrets (SESSION_SECRET=deadbeef…, two character classes) and low-class passwords.
  • 291 pass / 4 skip / 0 fail; 3 of your tests fail against main's source; tsc clean.

Change 1 (required): README section

There is no documentation change in this PR, and it introduces a behaviour that silently rewrites what the model asked to store. Maintainer ruling: the tool reply stays as it is (no redacted= notice); disclosure belongs in the README. Please add a section covering:

  • what runs where: src/secrets.ts at the write gate over scope, content, issue, tags, and on the capture prompt;
  • the token families covered (the list above) and that matches become [redacted];
  • that it is best-effort, not a guarantee — with the concrete misses (hex-only secrets, low-character-class passwords, credentials in prose without an assignment);
  • that the tool still reports Remembered when it redacts, so the stored text may differ from what was sent;
  • that existing stores are not scrubbed and memory_export writes whatever is already stored, verbatim;
  • that auto-capture is off by default, so the capture-egress half protects opt-in installs while the gate half applies to every install (please state the severity that way in the body too).

Change 2 (required): the claim "no write path can retain a credential" is not true today

The viewer's edit and restore endpoints call appendMemory directly — that is main's own comment at src/memory.ts:165 ("the viewer's restore/edit paths do not [go through the gate]"). I confirmed it: writing through appendMemory stored sk-abcdef1234567890ABCDEFGH verbatim on your branch.

Two acceptable resolutions:

  • Preferred: scrub in appendMemory (the single write choke point) and keep the gate scrub so hashing/dedup/canonical analysis see the redacted text. This is the pattern #64 uses for control characters, and it makes the sentence true instead of qualified.
  • Acceptable: keep it gate-only, but narrow the code comment and the PR body to "every gate-fed write path", and keep the viewer gap visible in the follow-up I filed.

Change 3 (small): don't silently rewrite metadata

scope and tags are scrubbed too. If a scope ever matches a pattern it becomes [redacted], i.e. the memory is silently re-scoped — worse for a retrieval system than a rejection, because it changes where the row shows up with no signal. #64 rejects bad metadata with a reason; I'd align: rewrite content (the maintainer's call), reject scope/tags/issue that contain a credential-shaped value.

Not a request

Skipping reasoning/thinking is right regardless of provider, and also removes the largest chunk of transcript tokens from the capture call. The hadPrivatePart guard (don't fall back to top-level text for a message that only had private parts) is the correct detail and I verified it works.

WhollyMindless and others added 2 commits October 24, 2026 10:47
…ument it

Review follow-up for abogartz#67:
- appendMemory redacts content too, so the viewer's edit and
  restore-entry paths are covered, not just gate-fed writes.
- Credential-shaped scope/tags/issue are rejected (gate block
  credential_in_metadata; appendMemory throws) instead of being rewritten.
  Only content is rewritten.
- The auto-capture outcome evidence no longer records a raw scope.
- README section: what runs where, families covered, known misses,
  forward-only (see abogartz#74), and capture being opt-in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/capture.ts
#	src/gates.ts
#	src/index.ts
#	src/memory.ts
#	test/write-caps.test.ts
@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Thanks. Pushed and merged with main (0.8.4), taking your preferred route:

  1. README: a "Secret redaction" section with everything you listed: where it runs, the families covered, best effort with the concrete misses, the unchanged Remembered reply, forward-only with export writing stored text verbatim (Secrets already in the store are never scrubbed, and memory_export writes them verbatim #74), and capture being opt-in. The description states the severity that way too.
  2. appendMemory scrubs content as well, so the viewer's /api/edit and /api/restore-entry are covered. Your direct appendMemory repro is a test now. The gate scrub stays, so hashing and dedup see the redacted text.
  3. Metadata is rejected, not rewritten: a credential-shaped scope/tags/issue gives a credential_in_metadata gate block or an appendMemory throw. Since fix(inject): keep every rendered memory field on its framed line #64 merged, both checks run in the gate and in appendMemory: control characters first, then credentials, then content redaction.

I also found and fixed one leak while doing this: the auto-capture outcome evidence logged the raw candidate scope, which could hold a credential. It's redacted now, with a test.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. All three items from my request-changes review are done and verified on the combined branch (all five batch PRs merge cleanly onto 0.8.4, as you said — I checked), and your open question at the bottom turned up something real.

Verified live on my own probes against the merged source:

  • The choke point closes the viewer path: calling appendMemory directly with sk-… content — exactly the /api/edit and /api/restore-entry bypass — stores [redacted], raw key absent from the day file.
  • Reject, not rewrite: a credential-shaped scope throws from appendMemory and nothing is written; the gate blocks with credential_in_metadata on a credential-shaped tag; the evidence log contains neither the xoxb-… nor the sk-… value. Matches #64's stance.
  • README "Secret redaction" says what runs where, names the known misses (hex-only, low-entropy prose, no-assignment credentials), keeps the Remembered reply honest ("stored text can differ from what was sent"), and states redaction is forward-only with memory_export writing legacy rows verbatim (#74 stays open deliberately).

Your session.idle question — you are right, and it is worse than a test-only gap. On a real 2.x host the envelope carries the payload under event.data: our own identity handler already does ev.data ?? ev.properties precisely because session.created has no properties there. The capture handler you cite (now index.ts:869) reads only event.sessionID ?? event.properties?.sessionID, so on 2.x every session.idle hits the typeof sid !== "string" guard and capture silently never runs — in this repo's history on main, not something this PR introduces. Consistent with that: no auto_capture_* evidence row exists in the live store's entire history, though auto-capture was also off in my config, so that alone was never proof — the envelope analysis is. I am filing a follow-up issue with the one-line fix (?? event.data?.sessionID) and a fake-2.x-host test; deliberately not asking for it in this PR, which is already at the right size.

Combined-batch gates: 324 pass / 4 skip / 0 fail, tsc --noEmit clean, biome exit 0.

@abogartz
abogartz merged commit a19b2f0 into abogartz:main Oct 24, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.8.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants