#12260: fix: redact secrets in tool results before persisting to session transcript
agents
stale
## Summary
- Apply `redactSensitiveText()` to tool result text blocks before writing to session JSONL files
- The read path (`session-files.ts:108`) already redacts on load, but the write path persisted secrets in plain text
- API keys, tokens, passwords, and PEM blocks from exec tool output were stored unredacted on disk
## Root Cause
In `session-tool-result-guard.ts`, the `guardedAppend` function applies `capToolResultSize()` to truncate oversized results but never calls the existing `redactSensitiveText()` function before writing. The session transcript files on disk contain verbatim exec tool output including any secrets printed to stdout/stderr.
While the read path redacts when loading transcripts for display, the raw JSONL files remain a security risk — they can be copied, backed up, or accessed by other processes without redaction.
## Fix
Added `redactToolResultSecrets()` that walks text content blocks in `toolResult` messages and applies `redactSensitiveText(text, { mode: "tools" })` — the same function and mode used by the read path. Called immediately after `capToolResultSize()` in the persist path.
## Test plan
- [x] `session-tool-result-guard.test.ts` — 10 tests pass
- [x] `session-tool-result-guard.tool-result-persist-hook.test.ts` — 2 tests pass
- [x] Existing redaction patterns cover: `sk-*`, `ghp_*`, `Bearer`, PEM blocks, `*_KEY=`, JSON `"token"`, CLI `--secret`
Closes #12182
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `src/agents/session-tool-result-guard.ts` to redact sensitive data from `toolResult` message text blocks before those messages are persisted to session JSONL transcript files. It introduces a `redactToolResultSecrets()` transform that applies the existing `redactSensitiveText(..., { mode: "tools" })` logic (already used on the read/display path) and wires it into the persistence path after the existing `capToolResultSize()` truncation.
Overall, this aligns the write path with the existing read-path redaction, reducing the chance that secrets printed by tools (stdout/stderr, env-style assignments, tokens, PEM private keys) are stored unredacted on disk.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Change is narrowly scoped: it reuses the existing redaction utility already used elsewhere in the codebase and applies it only to `toolResult` text blocks on persistence. The import path matches existing usage (`../logging/redact.js`), and the transformation preserves non-text blocks and leaves messages unchanged when no redaction is needed.
- src/agents/session-tool-result-guard.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#12296: security: persistence-only secret redaction for session transcripts
by akoscz · 2026-02-09
89.3%
#22231: fix(security): redact sensitive data in session transcripts
by novalis133 · 2026-02-20
84.8%
#23391: Logging: redact runtime secrets from skill env/apiKey in logs and t...
by bmendonca3 · 2026-02-22
82.6%
#15649: fix: sanitize tool_use IDs on session write path
by aldoeliacim · 2026-02-13
80.3%
#9011: fix(session): auto-recovery for corrupted tool responses [AI-assisted]
by cheenu1092-oss · 2026-02-04
79.5%
#22011: fix(transcript): drop empty toolCallId toolResults during persisten...
by sauerdaniel · 2026-02-20
79.3%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
79.3%
#16928: fix(security): OC-07 redact session history credentials and enforce...
by aether-ai-agent · 2026-02-15
79.0%
#3622: fix(agents): drop orphan tool results
by mickobizzle · 2026-01-28
78.6%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
78.5%