Skip to content

fix(errors): redact direct stream and normalized overflow messages - #155

Merged
patlux merged 1 commit into
patlux:mainfrom
karaaslanz:fix/redact-direct-error-helpers-20261014
Oct 15, 2026
Merged

patlux merged 1 commit into
patlux:mainfrom
karaaslanz:fix/redact-direct-error-helpers-20261014

Conversation

@karaaslanz

@karaaslanz karaaslanz commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Two exported Command Code error helpers apply inconsistent credential redaction:

  • commandCodeErrorMessage(...) redacts extracted object fields, but returns a direct string unmodified.
  • normalizeCommandCodeErrorMessage(...) adds the context_length_exceeded prefix to raw upstream error text without redacting embedded credentials. normalizeCommandCodeMessage(...) inherits that behavior.

This is a defense-in-depth consistency issue in helper outputs; existing production callers also perform downstream redaction in some paths. This PR does not claim an observed external credential leak.

Fix

Reuse the existing redactCommandCodeErrorText() helper at those two return boundaries; preserve existing overflow classification and non-overflow rejection logic. No regex or broader API changes.

Tests

Two new focused tests using synthetic strings only:

  1. direct-string stream error with api_key=direct_secret_value is redacted;
  2. normalized overflow and finalized assistant error with api_key=overflow_secret are redacted.

RED: new tests fail on unchanged upstream b61093d (8/10 passing).
GREEN: all 10/10 overflow tests pass on b78d21a; npm run typecheck PASS, Prettier check for both edited files PASS, git diff --check PASS.

Exactly two files changed: src/overflow.ts and tests/test-overflow.ts. Full suite not run locally. Exact-head upstream GitHub Actions CI completed SUCCESS (9/9 CI jobs: format, typecheck, Semgrep, CodeQL, Gitleaks, dependency review, npm audit, lifecycle and Oh My Pi). The separate memory benchmark is pending and excluded from this CI result.

@karaaslanz
karaaslanz marked this pull request as ready for review October 14, 2026 09:04

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at exact head b78d21ab993f70e5b352716858f7074d68a065d4 against base b61093de3f866fc533f827a25a27bc342e9d678d.

Verification (isolated worktree, npm ci)

  • npm run typecheck PASS, npm run test:overflow 10/10 PASS, npm run test:unit exit 0 (392 checks, 0 failures), npm run test:pi-local PASS (the e2e that asserts context_length_exceeded: prefixing and the no-secrets remnant), Prettier check on both changed files PASS, git diff --check PASS.
  • RED proven on base: the two new tests applied to b61093d fail (8/10, exactly the two new ones), GREEN at this head.
  • An independent fresh-context review confirms: the guards in normalizeCommandCodeErrorMessage all run on the raw text before redaction, so classification cannot change; the context_length_exceeded: prefix is prepended after redaction; and the redactor is a fixed point (redact(redact(x)) === redact(x)), which matters because production stacks it (commandCodeErrorMessage → src/core.ts detail assembly).
  • The genuinely reachable case fixed here is the normalize boundary: message_end can receive an errorMessage produced by the native/compat path that was never redacted (src/transport.ts builds its terminal error from the raw message), and base prefixing exposed it verbatim. The string branch of commandCodeErrorMessage is defense-in-depth for its current callers, matching the author's framing.

Notes

  • (P2, tradeoff) The shared redactor masks some non-secret diagnostics on these two surfaces: USER_TOKEN_PATTERN catches user_*/cc_* snake_case codes (user_not_found, cc_request_timeout), and CREDENTIAL_PATTERN catches token: 5000-style values. Classification is unaffected, and the dominant final-error path already redacted on base, so this is an acceptable defense-in-depth tradeoff — worth a conscious sign-off (and a line in the release changelog).
  • (P3, pre-existing) Redactor format gaps unchanged by this PR, now reachable at two more boundaries: BEARER_PATTERN's character class omits _ (Bearer user_secret), and CREDENTIAL_PATTERN does not match JSON-quoted keys ("api_key":"value"). Optional follow-up.
  • (P3, tests) A classify-before-redact assertion (e.g. a message whose keywords would be destroyed by redaction still detected) and an idempotence test would pin the two properties production relies on.

No P0/P1 findings; residual risk is low. Approving.

@patlux
patlux merged commit 640e931 into patlux:main Oct 15, 2026
13 of 15 checks 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.

3 participants