Skip to content

fix(cli): redact secret-like argv echoes in INVALID_ARGS error lines - #4851

Draft
strzhao wants to merge 4 commits into
callstack:mainfrom
strzhao:community-arg-echo-redact
Draft

strzhao wants to merge 4 commits into
callstack:mainfrom
strzhao:community-arg-echo-redact

Conversation

@strzhao

@strzhao strzhao commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An INVALID_ARGS message embeds the raw argv the parse refused, and a rejected positional can be a credential the caller typed into the wrong slot, so every route that publishes the message scrubs secret-like tokens (#4778). Round-2 fixes:

  • The path exemption in kernel/redaction no longer covers a token whose separator sits next to a non-path character (+, =, ?): those are the base64 and query shapes of a credential. Real config paths keep their text.
  • The MCP error result now scrubs structuredContent.message like its text line.
  • The cli_parse_failed diagnostic scrubs the same way. exitOnParseFailure moves to cli/failure-output, keeping cli.ts under the 1000-line guard.

Before / after

$ agent-device fill a "aB3dEfGh1jKlMn/oPqRsTuVw+xYz==" c   # base faa3c82ec
Error (INVALID_ARGS): fill accepts at most 2 positional argument(s), received 3: a aB3dEfGh1jKlMn/oPqRsTuVw+xYz== c.
$ agent-device fill a "aB3dEfGh1jKlMn/oPqRsTuVw+xYz==" c   # head
Error (INVALID_ARGS): fill accepts at most 2 positional argument(s), received 3: a <redacted len=30> c.

--debug diagnostic data.error: raw value at base, Invalid platform: <redacted len=20> at head. MCP structuredContent.message likewise.

Validation

Tested at 135bcaf. tsc -b tsconfig.typecheck.json clean. check:affected: format, lint, typecheck green; the layering ratchet reads the fork mirror's origin/main (the known blocker recorded earlier — all 12 violations in files this diff does not touch), CI runs the same gate green. Green suites: redaction, command-tools, cli-exit-paths, cli-device-pin, json view, command-output, both budget ratchets. Each new assertion fails at c91670897 with the fix reverted per site. Not device-facing: display formatting only.

Related: #4778, #4014, #3261.

🤖 Generated with Claude Code

An INVALID_ARGS message embeds the raw argv the parse refused, and a
rejected positional can be a credential the caller typed into the wrong
slot, so the error line echoed it verbatim into transcripts. The scrub
lives at the display line, per the callstack#4778 principle that values stay raw
until a display line is built.

- kernel/redaction: redactSecretLikeText replaces each whitespace token
  whose word is past 12 characters and mixes upper and lower case with a
  digit or a symbol by <redacted len=N>. Edge punctuation never counts,
  so an instruction identifier like deviceTarget. keeps its text.
- command-output: formatHumanError scrubs the INVALID_ARGS message. The
  Next step stays as the producer wrote it, because a hint can carry a
  machine-parsable example the reader is meant to run.
- cli json view: the --json error message scrubs the same way. The hint
  and the typed details keep their existing owners (hint examples, the
  structured redaction normalizeError already applied).

Co-Authored-By: Claude Code <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member

Thanks for tackling this leak. At c1ec2c4 the approach needs a rethink before the details are worth fixing, so this review asks one question first.

Would it be simpler to stop echoing the rejected values where they start? For example, assertCommandPositionalArity (command-schema.ts:42) could say fill accepts at most 2 positional argument(s), received 3 and not print the extras. The other argv echoes are a short list of producer sites: the Invalid <flag>: <value> messages in src/cli/parser/args.ts:309-324 and the unknown command and flag messages. That closes every route at once (human, --json, MCP text and structured content, --debug diagnostics, the Node client), needs no heuristic and no second placeholder, and cannot touch paths the system looked up.

Two problems with the display-layer scrub as written make that worth deciding now:

  1. It hides ordinary values, which is a regression. isSecretLikeToken (redaction.ts:188) treats any word longer than 12 characters with a lowercase letter, an uppercase letter and a non-letter as a credential, and it runs over every INVALID_ARGS message. Config file paths, app binary paths and bundle ids such as com.apple.MobileSMS become <redacted len=N>, so users cannot see which path or id an error is about. The Coverage failure on this head is three assertions that lost a config path for this reason (cli-config-retired-record.test.ts:58 and :75, cli-device-pin.test.ts:42).
  2. It does not hold on every route. A second independent pass found the raw value still published in MCP structuredContent.message (command-tools.ts:305), in the cli_parse_failed diagnostic under --debug (cli.ts:338), in fetched daemon diagnostics, and in error details such as conflicts[].requested. It also found shapes the heuristic misses that the kernel redactor already knows (a lowercase key like adc_cli_…, a Bearer span) and a credential split across two positionals.

If the scrub stays instead, it needs to apply only to values that were argv of the failed parse, through one view used by all surfaces, with the existing kernel patterns reused. That is a larger change than the producer-side fix.

Also on this head: Repo Guards fails because isSecretLikeToken is exported with no consumer; dropping export fixes it.

Not verified here: the head was not checked out or run. The redactor behavior was checked by running its redaction.ts on sample strings; the MCP finding comes from reading the code.

strzhao and others added 2 commits November 21, 2026 05:41
The Fallow audit flags isSecretLikeToken as an exported symbol with no
known consumers: redactSecretLikeText is its only caller. The helper
keeps its role and its JSDoc; it no longer widens the kernel surface.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The scrub treated any long mixed-case token as a credential, so the
project config path in a device-pin conflict came out as
<redacted len=101>: the reader lost the file the message names for
them to fix. A token that carries a path separator is a location, not
a credential, and keeps its text. The conservative-path table row
moves to the same rule, and the conflict message shape gets its own
row.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@strzhao
strzhao marked this pull request as ready for review November 20, 2026 22:02
@thymikee

Copy link
Copy Markdown
Member

Thanks for the quick follow-up.

The ordinary-value regression from the earlier review (#4851 (comment)) is fixed at c916708, and Coverage now passes. Two high-priority problems remain, so the PR is not ready to merge.

The path exemption at packages/kernel/src/redaction.ts:190 skips any token that contains / or \. A credential typed into the wrong slot that contains a slash is now echoed unchanged by received 3: ... and Invalid --flag: <value>. Standard base64 does this, for example aB3dEfGh1jKlMn/oPqRsTuVw+xYz==, and so does a URL with key=. Running the head's function on these inputs printed them verbatim. This breaks the rule that rejected values must not be echoed, on the human and --json routes. The rule should hold for every rejected argv value, so please do not widen the shape heuristic. Remove the echo where the message is built, in command-schema.ts:42-51 and args.ts:182-183 and 309-324. Those sites already enumerate where the value enters the message.

The scrub still covers only the two display writers, formatHumanError and printJson. The MCP error result sets structuredContent: normalized unscrubbed at src/mcp/command-tools.ts:305. exitOnParseFailure writes the raw error.message into the cli_parse_failed diagnostic at src/cli.ts:335-339, which is flushed under --debug. An extra positional that holds a password still reaches the transcript or the diagnostics file on those routes, and the earlier review named both of them. Fixing the producers closes every route at once: human, --json, MCP text and structured content, diagnostics and the Node client.

Could the heuristic and both call sites be deleted instead? assertCommandPositionalArity would print only received N, and args.ts would name the flag or command without the value, or print the value only after the existing kernel structured redaction. That needs no heuristic, no placeholder and no path exemption, and it looks smaller than the current +69 production lines. It first needs the maintainer to confirm that losing the echoed values from these messages is acceptable. The issue's own requirement points that way, and the earlier question is still unanswered.

Not blocking, and fine to take or leave: the heuristic at redaction.ts:181 now has three special cases that each followed a failing test, and removing the echo at the producers makes it unnecessary; the Body rules job also fails because the PR body has no ## Before / after section and is over 250 words, so a Summary, Before / after and Validation body under 250 words should pass it.

The Body rules failure is a PR-body format gate. Runner Build (tvOS + visionOS + watchOS) builds the Swift runner, and this diff touches only TypeScript, so that failure looks unrelated. Not verified here: a live CLI or MCP run of this head, the Runner Build logs, and the Body rules job log. The base64 bypass came from running the head's function on sample strings, and the MCP and diagnostics routes were read in code.

Before merge, decide between removing the echo at the producers and scoping a scrub to the failed parse's argv on every route. Then fix the PR body sections.

…d-argv echo route

The path exemption skipped any token that carries a path separator, so a
credential with a slash (standard base64, a URL query) was echoed
verbatim by the INVALID_ARGS display lines. The exemption now holds only
when every character is a path character; a separator next to any other
character falls through to the word test, and the existing path rows
keep their text.

Two routes still published the unscrubbed message: the MCP error result
set structuredContent unredacted beside its already-scrubbed text line,
and the cli_parse_failed diagnostic carried the raw message into the
--debug transcript. Both apply the same kernel display scrub now; every
other code keeps its typed message. exitOnParseFailure moves to
cli/failure-output beside the other failure writers, which also keeps
cli.ts under the 1000-line guard.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@strzhao

strzhao commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Round-2 items, pushed at 135bcaf (kept in draft for your re-review):

  1. Path exemption hole: the exemption now holds only when every character of the token is a path character (letters, digits, ., :, /, \, -, ~). A separator next to any other character (+, =, ? — standard base64, a URL query) falls through to the word test, so aB3dEfGh1jKlMn/oPqRsTuVw+xYz== and https://…?key=… redact. The three existing path rows (lowercase app path, capitalized media path, mixed-case config path) keep their text unchanged.
  2. Remaining echo routes: buildErrorToolResult now scrubs structuredContent.message (the text line was already scrubbed through formatHumanError), and the cli_parse_failed diagnostic scrubs before emitDiagnostic writes it. Both reuse redactSecretLikeText, the same owner the human and --json views use; non-INVALID_ARGS codes keep their typed message. exitOnParseFailure moved to cli/failure-output.ts so cli.ts stays under the 1000-line guard.
  3. Each new assertion was verified to fail at c91670897 with the fix reverted per site.

On the open question (remove the echo at the producers vs. scoped scrub): this round implements the scoped-scrub option with the shape heuristic unchanged in strictness (only the exemption narrowed). If you confirm that dropping the echoed values from assertCommandPositionalArity and the args.ts flag messages is acceptable, the producer-side removal supersedes all of this and the heuristic can go — say the word and I will rework it that way.

Gates: tsc -b tsconfig.typecheck.json clean; check:affected green on format/lint/typecheck, layering ratchet blocked locally only by the fork-mirror origin/main merge-base (12 violations, none in touched files) as recorded in the earlier round.

🤖 Generated with Claude Code

@strzhao
strzhao marked this pull request as draft November 21, 2026 03:00
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update.

The earlier review (#4851 (comment)) is partly addressed, but the review of 135bcaf found that credential-shaped values and some real config paths are still handled wrongly.

The path exemption in redaction.ts#L193 passes any token with a slash or backslash whose characters are all in [\w.:/\\~-]. Base64 credentials use / and only sometimes + or =, so wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY is still treated as a location. agent-device fill @e1 user wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY prints received 3: @e1 user wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY on the human, --json, MCP text, MCP structuredContent and --debug routes. The rule should be that a rejected argv value never appears in an INVALID_ARGS message, whatever its shape. The sites that enumerate this are the producers: assertCommandPositionalArity (command-schema.ts#L42) and the Unknown/Invalid messages in args.ts (lines 105, 184, 309-324). Print the count or the flag name there, or scope the scrub to the exact argv tokens of the failed parse. Either way, the shape heuristic should go, and no new exemption should replace it.

The same line also breaks real paths. A path token with any character outside that set, such as @, +, (, ) or ,, now goes through the word test. The device_pin_conflict message (device-selection-resolver.ts#L406) names the project config file. When the project directory name contains @, the whole path prints as <redacted len=41>. When it contains a space and parentheses, as in common Dropbox and iCloud folder names, the path is cut mid-way. Base printed both in full, so the message no longer names the config file for these users. A shape heuristic cannot tell wJalr…/K7MDENG/… from app@2/agent-device.json. Removing the echo at the producers, or scrubbing only the failed parse's argv tokens, never touches system-supplied paths.

A second independent pass found that agent-device 'Sup3rSecretPass!2026' --debug still publishes the rejected command. The unknown-command refusal at args.ts#L179-L184 reaches failure-output.ts#L79-L87, and data.error is scrubbed. But the scope command stays Sup3rSecretPass!2026, and the diagnostics writer copies it into the cli_parse_failed event (diagnostics.ts#L182-L205). The diagnostic command should come from a validated command name. If the candidate is unrecognized, omit it or scrub it before the event is emitted.

A second independent pass found that the 24-character token aB3dEfGh1jKlMn/oPqRsTuVw needs no = padding and so passes the path exemption. As the value of fill --platform, it reaches the enum refusal at args.ts#L309 and appears verbatim in the stderr line, the JSON error.message and cli_parse_failed. As an unknown MCP tool name it reaches command-tools.ts#L207 and router.ts#L167-L168, so MCP text and structuredContent carry it too. Rejected command names and non-path values such as an invalid enum should not use the path exemption. Paths should be preserved only when they come from a field known to hold a path.

A second independent pass found that adc_cli_samplecredential2026 as an invalid --platform value is echoed in the CLI stderr line and JSON message, and as an unknown MCP tool name in MCP text and structuredContent. It matches the existing adc_cli_ pattern at redaction.ts#L3-L4, and the same parse failure's diagnostic replaces it with [REDACTED] (redaction.ts#L80). The display scrub at line 196 requires uppercase, so the routes disagree. The shared scrub should reuse the kernel credential match for rejected values, including known credentials outside the mixed-case test.

A second independent pass found that a project config with malformed JSON at a path containing + (for example a Client+Preview directory) loses its path. cli-config.ts#L64-L67 and L92-L98 build a message that names the file, and at base all three CLI routes show it. At 135bcaf the message reads Invalid JSON in config file: <redacted len=37>. The same config loads for MCP tools (tool-input-config.ts#L22-L27), so both tool error views lose the path as well. The config loader's resolved path should travel as a typed message argument, with the scrub applied only to rejected values. That keeps +, = and ? in rejected credentials going through the token test.

A second independent pass found that the remote diagnostics tail republishes a scrubbed refusal. With a supported remote app session and a readable request record, agent-device trigger-app-event 'Sup3rSecretPass!2026' --debug is accepted by push.ts#L116-L119. The daemon then rejects the ! in app-events.ts#L28-L36 and emits the raw INVALID_ARGS message as request_failed (request-router.ts#L739-L752). The CLI prints that tail (cli.ts#L671-L680), so the value reaches the human transcript although the summary hides it. The rejected event name should be scrubbed at the argument-error producer, before the message can enter the request record or response.

Not blocking, and fine to take or leave: the guard code === 'INVALID_ARGS' ? { ...x, message: redactSecretLikeText(x.message) } : x now appears at four sites (error.ts#L61-L64, json.ts#L19-L25, command-tools.ts#L308-L313 and failure-output.ts#L83-L86). Producer-side removal makes all four disappear. If the scrub stays, apply it once where the parse error is built, or once in normalizeError.

Would deleting redactSecretLikeText, isSecretLikeToken, PATH_TOKEN_CHARACTERS and the four guards be simpler? assertCommandPositionalArity could print <subject> accepts at most N positional argument(s), received M, and args.ts could name the flag or the command without the rejected value, or pass it through the existing structured redaction. That would close the human, --json, MCP text, structuredContent, diagnostics and Node client routes at once, with no path exemption, and it is likely smaller than the current +131/-44 production lines. The author offered this rework in the 2026-11-21 comment and is waiting for the word, so the maintainer needs to confirm that INVALID_ARGS messages may drop the echoed argv values.

The required evidence is present. The Body rules job failed on the earlier PR body (no ## Before / after section and 383 words). The current body has that section and about 220 words outside code blocks, so a re-run should pass, and the code diff does not cause that failure. Smoke Tests, Smoke Tests (ios) and Smoke Tests (macos) were cancelled, not failed, so they give no signal about this diff, which only changes CLI and MCP error formatting. No conflicts are known.

Not verified here: a live CLI or MCP run of this head; the redaction results come from running the head's redaction.ts on sample strings, with the routes read in code. The new tests were not run, and regression validity was judged by reading the earlier code paths. The reason for the cancelled Smoke Tests jobs was not checked. The Node client and fetched daemon diagnostics were not traced at this head, though the argv echo producers run only on the CLI parse path, so they are unlikely to reach those routes.

Before merge, the slash-containing base64 leak and the config-path redaction need to be closed, and the maintainer needs to approve removing the argv echo at the producers, which closes both and deletes the heuristic.

This branch has not been deployed

No deployments
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.

2 participants