Skip to content

fix: redaction leaves words alone and covers codes and pending approvals - #161

Merged
natevick merged 4 commits into
mainfrom
natevick/cli-redact-fixes
Nov 22, 2026
Merged

natevick merged 4 commits into
mainfrom
natevick/cli-redact-fixes

Conversation

@natevick

Copy link
Copy Markdown
Contributor

Review fixes for token redaction: ordinary words stay intact, and tokens in error codes and pending approvals are now removed.

Stacked on the redaction PR. Merge the two together.

What changed

  • A token under 16 characters only matches as a whole word, so Invalid token. is untouched when the token is tok. Longer tokens match anywhere.
  • JSON is decoded and only string values and keys are cleaned. Numbers, booleans and structure stay intact.
  • The error code is cleaned, and the exit code still comes from the original code.
  • A pending approval's URL is cleaned, in JSON and terminal output. It still exits 7.
  • The test fixtures the first commit rewrote are restored unchanged.

Checks

An independent run with its own stub server compared three real binaries: before redaction, the first commit, and this one. All 8 conditions passed, and every condition that needed the bug showed it on the first commit.

  • Short tokens leave Invalid token. byte-identical to before redaction.
  • A 40-character token is removed from messages, error codes and approval URLs, with and without --timeout.
  • Errors with no token in them are byte-identical to before redaction.
  • Removing the boundary check, code cleaning or pending-path cleaning each fails tests.
  • go vet on all three platforms, go test -race, and golangci-lint 1.64.8 pass.

Not established

  • Tokens came only from SELECTIVE_TOKEN. Config, keychain and stdin weren't driven, and browser-grant and newly minted tokens aren't registered.
  • Only approvals show and suites delete were driven end to end.
  • sel test event fields, a real server's echo, and the cancelled-auth and panic paths weren't checked.
  • A short token directly after percent-encoding (%2Ftok) isn't replaced.

🤖 Generated with Claude Code

https://claude.ai/code/session_0186m4FrY9RHAiNN1uxztLCp

Nate Vick and others added 4 commits November 22, 2026 11:37
If a server error echoed the caller's token, every command except
`sel doctor` printed it on stdout and stderr, with and without
--timeout. `sel doctor` already redacted it. v0.4.0 behaves the same
way, so #149 did not cause this.

internal/redact keeps the tokens the CLI sent. api.New registers each
one, which covers SELECTIVE_TOKEN, the keychain, the config file,
`auth login --with-token` and the legacy `auth --token`, because all of
them build their client there. runWith clears the set on entry and
replaces every registered token with [redacted] in the classified
error: the message, and every field, nested ones included, in raw and
JSON-escaped form. It does that before writeErrorObject, so the 512
byte cut in BoundedError can no longer leave part of a token behind.

`sel auth logout` also prints its revoke failure through redact.String.

Only an error that held a token is rebuilt. The rebuilt error keeps the
original ErrorCode and ExitCode (api.Error derives its exit from the
HTTP status, which contract.Error would not), and a field is replaced
only when it held a token, so fields["doctor"] stays a *doctor.Report.
Numbers are decoded with UseNumber. Nothing else changes: an error
without a token is the same object, an empty token is never stored,
and success output, including `tokens create`, is not touched.

Not covered, deliberately:
- A token minted by `tokens create` and one granted by browser login
  are not registered. The CLI sends neither again in the same run, so
  no server error can echo them.
- Approval documents printed for exit 7 are not scrubbed: they are not
  errors, and whether the server puts a token in them was not checked.
- doctor keeps its own single-secret redaction (internal/doctor); it
  fails closed differently and is tested separately.
- A very short token redacts every occurrence of its characters. Real
  tokens start with sk_live_. Existing tests that sent the token `tok`
  saw it inside "token", so approvals_test.go now sends sk_live_tests and
  tokens_test.go's fixture messages no longer say "token".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186m4FrY9RHAiNN1uxztLCp
Fixes the five review findings on 080b05e.

1. Short tokens corrupted words ("Invalid token." became "Invalid
   [redacted]en." with token tok). A token under 16 characters
   (anywhereLength) now matches only with no [A-Za-z0-9_-] on either side.
   16 or more match anywhere, so x%3D<token>y is replaced.
2. tokens_test.go and approvals_test.go are restored byte for byte to
   1fe02ec and pass unedited.
3. Redaction no longer replaces inside raw JSON. Fields are decoded
   (UseNumber), walked, and only string values and map keys are cleaned.
   An unchanged value is returned as it was.
4. The error code is cleaned, keeping the original ExitCode.
5. A 202's approval is cleaned where api.pendingResponse builds it, so
   exit 7 JSON, the terminal sentence and the --wait announcement all
   read a clean document. Exit 7, id and status are kept.

Keys that clean to the same string collapse to one entry.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0186m4FrY9RHAiNN1uxztLCp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_0186m4FrY9RHAiNN1uxztLCp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_0186m4FrY9RHAiNN1uxztLCp
@natevick
natevick marked this pull request as ready for review November 22, 2026 21:56
@natevick
natevick changed the base branch from natevick/cli-redact-token to main November 22, 2026 21:56
@natevick
natevick merged commit f9f2585 into main Nov 22, 2026
1 check passed
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