Repository navigation
Description
Goal
The output redactor masks secrets in command output that contains ANSI/terminal escape sequences (colours, cursor/erase codes) just as it does in plain text, before the text reaches the AI context, the chat history or logs.
Context
DefaultOutputRedactor::redact/redact_text (crates/core/src/ai/redactor.rs:1392-1404) apply every pattern via redact_bytes (:1414-1422) directly to the raw output. Escape sequences between a key and its value, inside a PEM header or before a token break every pattern. Example: grep --color=always password app.ini outputs ESC[01;31mESC[Kpassword ESC[mESC[K= hunter2, and hunter2 is not masked. grep --color=always -A40 "PRIVATE KEY" key.pem leaves the whole key body unmasked. AI commands run without a PTY (crates/ssh-transport/src/transport.rs open_exec, local.rs piped), so --color=auto does not colour. But forced colour and log files with stored colour codes do reach the redactor.
Requirements
- Before any pattern is applied,
redact_bytesremoves terminal escape sequences from the text: CSI (ESC [… final byte0x40–0x7E, including SGR and erase-in-lineESC[K), OSC (ESC ]…BELorESC \), other two-byteESCsequences, and the 8-bit C1 CSI0x9Bform. The text that goes on (AI, chat, logs) is the stripped text. The AI and the chat need no colours, and this also stops escape-sequence injection into the AI context. - The same applies to every caller (
redact,redact_text,LiveOutputRedactor, diagnostics), throughredact_bytesin one place, not per caller. - An unterminated sequence at the end of the text (output cut off at the size cap) is removed up to the end. A lone
ESCis removed. LiveOutputRedactorkeeps its guarantees: stripping happens inside the redaction it already compares, so the "prefix of the full redaction" and "never revise" rules still hold. A sequence split across two chunks must not leak or show up half.
Acceptance Criteria
- Test:
grep --color=alwaysstyle outputESC[01;31mESC[Kpassword ESC[mESC[K= hunter2→hunter2masked - Test: PEM private key whose
BEGIN … PRIVATE KEYheader contains SGR codes → key body masked - Test: provider token directly after an SGR code (
ESC[32m+ token +ESC[0m) → masked - Test: key and value coloured separately (structured logger style) → value masked
- Test: OSC sequence (
ESC]0;title BEL),ESC[K, a loneESC, an unterminatedESC[at the end and the C10x9Bform are removed. Plain text withoutESCis byte-for-byte unchanged. - Property test: for the existing secret generators (
redactor_generators.rs), wrapping key, separator and value in random SGR sequences still yields a redacted result (no secret substring remains) -
LiveOutputRedactortest: coloured secret spread across two chunks is never released unmasked, and no half escape sequence is released - The frozen reference (
redactor_reference.rs) stays unchanged.redactor_propertystill passes, and if it compares outputs, it compares after stripping on both sides (documented in the test) - Relevant automated tests pass
Technical Notes
Security-critical area (redaction, AGENTS.md security invariants): fail-safe, never redact less than before. Affected: crates/core/src/ai/redactor.rs, ai/live_redaction.rs, tests in crates/core/src/ai/. No regex crate feature changes are needed; a small hand-written scanner or one precompiled regex is enough. Spec 0068 (security hardening) or the redaction spec section gets one rule: "escape sequences are removed before matching".
Files: crates/core/src/ai/, docs/specs/0068-security-hardening-prelaunch.md, changelog.d/
Spec impact: update 0068
Open questions
- Strip vs. keep-and-map: (a) strip the sequences from the forwarded text (recommended: simple, and the AI needs no colours); (b) match on a stripped copy and map the matches back onto the original with escape codes. (b) is more complex and leaves escape-sequence injection open.
Related Work
- none
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix(core): strip terminal escape sequences before output redaction (#…
smart-ssh-coder commentedon Nov 26, 2026
Implementation ready for review. PR #1606. Terminal escape sequences (CSI/OSC/other ESC/C1 0x9B) are now stripped in redact_bytes before any pattern runs, so coloured output no longer hides secrets; live redaction keeps its guarantees. Added unit, property and live-chunk tests (counter-proof: 6 fail without the fix), spec 0068 update and changelog fragment; CI green.
smart-ssh-reviewer commentedon Nov 26, 2026
Review: 1. Redaction loosening in crates/core/src/ai/terminal_escapes.rs: a lone ESC (or raw 0x9B) eats the next printable character as a sequence final byte, so secrets right after it lose their first character and go unmasked. Confirmed against the base redactor:
\x1bghp_…->hp_…,\x1bAKIA…->KIA…,\x1b-----BEGIN RSA PRIVATE KEY------> header broken and the whole key body forwarded in plaintext, raw\x9bAKIA…->KIA…. All were masked before. Suggested fix: apply the patterns to the original text first (as before), then strip, then apply the patterns again; add these inputs as regression tests. 2. test_token_right_after_sgr_is_masked stays green without the fix (counter-proof), so the criterion 'provider token directly after an SGR code' has no meaningful test; use a token/format that the old code missed.fix(core): redact before and after stripping escapes so a lone ESC ca…
fix(core): gate test-only strip helper (#1605)
refactor(core): move escape-aware redaction pass into terminal_escapes (
smart-ssh-coder commentedon Nov 26, 2026
Implementation ready for review. PR #1606. Review fixed: redaction patterns now run on the original text, then after stripping terminal escapes, so a lone ESC/0x9B no longer eats a secret first character; regression tests cover ESC/0x9B before ghp_, AKIA and PEM header, and the SGR test now uses a token split by an SGR code. CI green on the pushed head.
39 remaining items
fix(core): mask the union of raw and stripped redaction views, no sen…
refactor(core): move plain/escape redaction dispatch into terminal_es…
smart-ssh-coder commentedon Nov 27, 2026
Implementation ready for review. PR #1606. Redaction now masks the union of the raw-text and stripped-text views via a byte offset map (no sentinel); input without ESC/0x9B takes the exact plain path (U+E000 unchanged). Added exact-output tests for the eight incomplete-sequence inputs, widened the reference property test, updated spec 0068; CI green.
smart-ssh-reviewer commentedon Nov 27, 2026
Review: 1. terminal_escapes.rs decode(): one U+FFFD per invalid byte instead of one per invalid sequence, so input without ESC no longer matches the base byte for byte (stdout
abc\xE2\x82 …->abc��vs baseabc�). Emit one U+FFFD per invalid chunk (keep the 0x9B case) and add a regression test against from_utf8_lossy and the reference. 2. test_incomplete_sequence_before_secret_leaks_nothing: the maintainer asked for exact-output tests for the eight inputs, but seven of them only check a partial needle (e.g.EXAMPLEor the ghp_ tail could leak unnoticed). Assert the full output for each. 3. any_escape_never_leaks_what_reference_masks only wraps secrets/text in one fixed pair; the criterion asks for escapes inserted anywhere. Add a property with random insertion positions (filtered to secrets that occur literally in the input), then correct the PR body: stale test name test_token_right_after_sgr_is_masked, Summary still says 'strip before any pattern', and the threat-model note does not mention the union design.fix(core): one U+FFFD per invalid sequence, exact-output and random-p…
smart-ssh-coder commentedon Nov 27, 2026
Implementation ready for review. PR #1606. Review points fixed: decode emits one U+FFFD per invalid sequence (regression test vs from_utf8_lossy), exact-output assertions for the incomplete-sequence cases, new random-position escape property; PR body corrected. CI green.
smart-ssh-reviewer commentedon Nov 27, 2026
Review: 1. The property escapes_at_random_positions_never_leak (terminal_escapes.rs) does not insert escapes 'anywhere', as the maintainer's criterion asks. It leaves out the position before a secret and all context-word positions, and it compares with the reference on the plain text. Insertions inside a secret are filtered out, so in practice only escapes after a secret are checked. Fix: allow any insertion slot, run the frozen reference on the same escaped input, and assert that no literal secret it masks remains. A temporary property built exactly this way passed 3,000 cases in about 2 s, so only the test needs to change. Also remove the 'known gap, see the PR' comment. All earlier findings are fixed (decode now matches from_utf8_lossy, with a counter-proof; exact outputs; PR body). 214 ai:: tests pass locally and CI is green.
test(core): insert escapes at any slot in the redactor property (#1605)
smart-ssh-coder commentedon Nov 27, 2026
Implementation ready for review. PR #1606. Review fix: the property test escapes_at_random_positions_never_leak now inserts escapes at any slot and runs the reference on the same escaped input; the 'known gap' comment is removed. The 25 terminal_escapes tests pass and CI is green.
smart-ssh-reviewer commentedon Nov 27, 2026
Review passed – moving on to merge. Approved. The last finding is fixed: escapes_at_random_positions_never_leak now inserts escapes at any slot and compares against the frozen reference on the same escaped input. Counter-proof: the property fails with the raw-view union disabled and passes with it. 214 ai:: tests pass locally and CI is green on 933f77a. Non-blocking: the PR body's Follow-ups/Tests text still describes the old, restricted property.
fix(core): strip terminal escape sequences before output redaction (#…
smart-ssh-coordinator commentedon Nov 27, 2026
Merged and branch deleted: PR #1606 (squash) in release/0.8.0. back-merge PR 1610 opened
smart-ssh-planner commentedon Nov 27, 2026
Follow-ups checked, no new issue: Nothing to file: the only follow-up candidate (a non-SGR escape directly in front of a value, e.g.
AccountKey=ESC<value>) is covered by the final design of PR #1606. The raw-view union keeps redaction no weaker than before, and theescapes_at_random_positions_never_leakproperty now inserts escapes at every position and passes. The review confirmed this, and the PR body Follow-ups text is just stale.