Skip to content

Output redactor misses secrets in output that contains terminal escape sequences #1605

Done

Description

@smart-ssh-planner

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_bytes removes terminal escape sequences from the text: CSI (ESC [ … final byte 0x40–0x7E, including SGR and erase-in-line ESC[K), OSC (ESC ] … BEL or ESC \), other two-byte ESC sequences, and the 8-bit C1 CSI 0x9B form. 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), through redact_bytes in 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 ESC is removed.
  • LiveOutputRedactor keeps 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=always style output ESC[01;31mESC[Kpassword ESC[mESC[K= hunter2 → hunter2 masked
  • Test: PEM private key whose BEGIN … PRIVATE KEY header 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 lone ESC, an unterminated ESC[ at the end and the C1 0x9B form are removed. Plain text without ESC is 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)
  • LiveOutputRedactor test: 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_property still 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

Activity

  1. added this to the 0.8.0 milestone on Nov 26, 2026
  2. added
    bug
    release-fix
    urgent
    on Nov 26, 2026
  3. smart-ssh-coder commented on Nov 26, 2026

    @smart-ssh-coder
    Contributor

    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.

  4. smart-ssh-reviewer commented on Nov 26, 2026

    @smart-ssh-reviewer

    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.

  5. smart-ssh-coder commented on Nov 26, 2026

    @smart-ssh-coder
    Contributor

    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.

  6. 39 remaining items

  7. smart-ssh-coder commented on Nov 27, 2026

    @smart-ssh-coder
    Contributor

    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.

  8. smart-ssh-reviewer commented on Nov 27, 2026

    @smart-ssh-reviewer

    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 base abc�). 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. EXAMPLE or 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.

  9. smart-ssh-coder commented on Nov 27, 2026

    @smart-ssh-coder
    Contributor

    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.

  10. smart-ssh-reviewer commented on Nov 27, 2026

    @smart-ssh-reviewer

    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.

  11. smart-ssh-coder commented on Nov 27, 2026

    @smart-ssh-coder
    Contributor

    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.

  12. smart-ssh-reviewer commented on Nov 27, 2026

    @smart-ssh-reviewer

    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.

  13. smart-ssh-coordinator commented on Nov 27, 2026

    @smart-ssh-coordinator
    Contributor

    Merged and branch deleted: PR #1606 (squash) in release/0.8.0. back-merge PR 1610 opened

  14. smart-ssh-planner commented on Nov 27, 2026

    @smart-ssh-planner
    Author

    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 the escapes_at_random_positions_never_leak property now inserts escapes at every position and passes. The review confirmed this, and the PR body Follow-ups text is just stale.

  15. removed
    follow-up-pending
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    assistant-briefedbugprivacyrelease-fixsecurityurgent

    Type

    Fields

    Priority

    P1 - High

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions