Skip to content

feat: redact cloud credentials on paste - #34

Merged
tkc merged 1 commit into
mainfrom
redact-paste
Sep 17, 2026
Merged

tkc merged 1 commit into
mainfrom
redact-paste

Conversation

@tkc

@tkc tkc commented Sep 17, 2026

Copy link
Copy Markdown
Owner

Copying a block of logs, JSON or ~/.aws/credentials to ask an agent about it carries any live key straight into the model's context. ⌘V now redacts before the text reaches the session, keeping the surrounding text so the agent still sees what you meant to show it:

aws_secret_access_key = [redacted]
"private_key": "[redacted]"

The bottom bar says pasted with 2 secrets redacted — ⌥⌘V pastes it unchanged, so it never happens silently, and ⌥⌘V gives you the real thing when you want it (typing a key into aws configure). ⌘C and OSC 52 are untouched — rewriting a copy would silently break selecting a key on screen in order to use it.

Rules live in config, not in the binary

[paste]
mask   = true
redact = [
  '\b(?P<secret>(AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16})\b',
  '(?i)"(aws_secret_access_key|secretaccesskey|sessiontoken|private_key|client_secret)"\s*:\s*"(?P<secret>[^"]+)"',
  ...
]

The part named secret is what gets replaced, so the name and the quotes around it survive automatically. A broken pattern is refused at startup, naming its index — verified for real:

$ termit                      # with redact = ["[unclosed"]
termit: invalid config: paste.redact[0] is not a valid regex: [unclosed
  regex parse error: unclosed character class
exit=1

What iTerm2 gave, and what changed

iTerm2 puts an ordered transform pipeline behind paste (sanitizePasteEvent: in iTermPasteHelper.m: newlines → punctuation → unsafe control codes → tabs → shell escaping → regex substitution → base64) and ships the regex tool with no rules of its own. Two deliberate differences:

  • Named capture instead of pattern+substitution. iTerm2 pairs a regex with a replacement string you write with $1. Marking the span (?P<secret>…) instead means a mistyped replacement can't eat the context.
  • ⌥⌘V, not ⌘⇧V. That is iTerm2's Advanced Paste slot, and it sidesteps the Cmd+Shift combination this codebase already had to work around once (⌘I exists because ⌘⇧R doesn't always arrive).

termit ships defaults where iTerm2 ships none, because a tool nobody writes rules for protects nobody — but they stay in config, so no AWS key shape is compiled into termit. docs/references/paste.md records this.

Cost

regex was already linked via env_logger → env_filter, so making it a direct dependency costs 33 KB of binary (9,641,456 → 9,674,560) and no compile time. Rust's regex has no backtracking, so a user-supplied pattern cannot hang the terminal.

Limits, stated in the README too

  • A bare AWS secret key cannot be detected. 40 characters of base64 with no marker; a rule catching it also catches passwords, hashes and git SHAs. It is caught next to its name, which is how it arrives in a credentials file or an API response. gitleaks has the same limitation for the same reason.
  • password and token are deliberately not in the defaults — they fire on code you paste for review and damage it. There is a test asserting ordinary code passes through byte-for-byte.

Test

224 pass, 1 ignored. clippy -D warnings and fmt --check clean. Nine tests on the redactor cover a real credentials file, aws sts JSON, a GCP service-account key (the \n inside the quoted value is why the quoted rule takes everything to the closing quote), Google API keys and OAuth tokens, code that must survive untouched, empty rules, a rule with no secret group, and a broken pattern. A test pins the README's example to the shipped defaults so the two can't drift.

Not covered by a test: the wiring from clipboard through redact to the pty, and whether ⌥⌘V actually arrives on a given machine — I can't press keys from here. --keytest will show it; if it doesn't arrive, mask = false is the fallback.

🤖 Generated with Claude Code

Copying a block of logs, JSON or ~/.aws/credentials to ask an agent
about it carries any live key straight into the model's context. ⌘V now
replaces what looks like a cloud credential with [redacted] before the
text reaches the session, keeping the surrounding text so the agent
still sees what you meant to show it.

The rules are regexes in `[paste] redact`, not knowledge in the binary.
The part named `secret` is what gets replaced, so the name and quotes
around it survive and a mistyped replacement can't eat the context. A
broken regex is refused at startup, naming its index, rather than
failing silently at paste time.

Taken from iTerm2, which puts an ordered transform pipeline behind
paste (sanitizePasteEvent: in iTermPasteHelper.m) and ships regex
substitution as a tool with no rules of its own. termit ships cloud
defaults too, because a tool nobody writes rules for protects nobody.
Two things changed on the way: the redacted span is a named capture
rather than iTerm2's pattern+substitution pair, and the escape hatch is
⌥⌘V -- iTerm2's Advanced Paste slot -- which avoids the Cmd+Shift
combination this codebase already had to work around once.

regex was already linked via env_logger, so making it a direct
dependency costs 33KB of binary and no compile time. Rust's regex has no
backtracking, so a user-supplied pattern cannot hang the terminal.

Scope is the user's own paste. ⌘C and OSC 52 are untouched: rewriting a
copy would silently break selecting a key on screen in order to use it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@tkc
tkc merged commit d134805 into main Sep 17, 2026
1 check passed
@tkc
tkc deleted the redact-paste branch September 17, 2026 02:58
tkc added a commit that referenced this pull request Sep 25, 2026
* docs: note that a sleeping Mac stops an agent waiting on a reset

Claude Code resumes its task by itself when a usage limit resets, but
only while its session is alive, which makes leaving termit open the
thing that gets work done overnight. A sleeping Mac freezes every
process, so the reset passes and nothing happens -- and none of the
assertions already on this machine (coreaudiod, sharingd, powerd) hold
PreventSystemSleep, so the default state is not enough.

`caffeinate -is termit` covers it, and releases both assertions when
termit exits. Verified: PreventSystemSleep goes 0 -> 1 while it runs and
back to 0 after. The two limits are stated, because both are easy to
trip over: -s applies only on AC power, per its man page, and neither
flag stops a laptop sleeping when the lid closes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: mask credentials on screen, not on paste

The paste-time redaction shipped in #34/#35 solved the wrong problem.
The requirement is that a credential still works and simply is not
visible, not that it never reaches the program. Redacting the paste
turned `export AWS_SECRET_ACCESS_KEY=wJalr...` into
`export AWS_SECRET_ACCESS_KEY=[redacted]`, so the shell assigned the
literal string and the failure surfaced much later, in an AWS call far
from its cause.

Masking now happens while drawing. The grid keeps the real text and the
pty gets the real bytes, so a pasted export works exactly as typed and
⌘C copies the real value; what disappears is the credential being
visible in a screenshot, a screen share, or scrollback an hour later.
Redactor::spans returns byte ranges, masked_cells maps them to columns,
and the draw loop substitutes a bullet per cell so column alignment --
and any full-screen UI drawn on top of it -- does not shift.

The byte-to-column mapping is the part that can silently go wrong, so a
test masks a line with double-width characters ahead of the secret and
asserts the bullets land on the key and nowhere else.

Costs 0.041ms median for a full 47x163 screen with a credential every
eighth line, against roughly 1ms to build a frame. An ignored test
re-measures it.

`[paste]` becomes `[screen]`, since the table now describes what is
drawn. ⌥⌘V and Action::PasteRaw are gone: with paste left alone there is
nothing to escape from.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

1 participant