fix(cli): mask sensitive values in API error output

TR
1h ago
+581-21

Why

--debug masks values the API description marks sensitive (x-speakeasy-param-sensitive, format: password) when it logs a response body. The normal error output did not: when an API error body echoed a sensitive value, it was printed as-is in the pretty Details: block, the error message line, and the -o json / agent-mode error envelope.

What changed

  • Classify now passes the error body through the SDK's redact.ResponseBody, the same masking --debug uses. The operation comes from the response's request context, or from the command when the error carries no HTTP response (for example typed error models that keep only their schema fields). Operations split per request content type generate one SDK method per variant, and the masking rules are keyed by that method's ID, so such commands now carry a speakeasy_sdk_operation annotation that the lookup prefers over the original speakeasy_operation. Other commands are unchanged.
  • The error message and the envelope error text are held to the same redaction when they fall back to the error's own text: a raw body embedded in that text is replaced by the masked body, a typed error model's JSON rendering is replaced by the masked body, and any other occurrence of a value that masking replaced is masked too (for example a typed error whose Error() returns a single sensitive message field).
  • When the error has no response body but is a generated error model, the model's own fields are checked against the operation's sensitive fields for that last step.
  • Generated only when the document has sensitive values (hasAnyRedactGraphs); other CLIs are unchanged.
  • Successful command output is unchanged and still shows decoded values.
  • Like --debug, a body that cannot be parsed for an operation with sensitive response values is masked as a whole.
  • Test fixtures (CLI-only): cliSensitiveResponseGet gains a 400 response that reuses its sensitive schema; cliSensitiveErrorMessageGet returns a 400 whose x-speakeasy-error-message field is sensitive; cliSensitiveSplitResponsePost is split into form and multipart methods and returns a sensitive 400.

Testing

  • New TestErrorOutputRedactsSensitiveResponseFields (pretty, -o json, agent mode): a 400 echoing a sensitive value never shows it, while the unmarked sibling stays readable. It failed in all three modes before the change.
  • New TestRedactedMessageDropsMaskedValues for the message fallback cases, including a message that is a masked field and values under a masked subtree.
  • New TestErrorOutputRedactsSensitiveErrorMessageField (pretty, -o json, agent mode) and TestErrorOutputRedactsSensitiveFieldsOfSplitOperation (both split commands). Each fails with its part of the fix removed.
  • TARGET=review make test-cli, TARGET=primary make test-cli, make check-template-cli, npm run format, go test ./pkg/generate/snapshots/ -run TestSnapCLI and git diff --check pass.

Public-safety check

  • This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
  • Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (.claude/skills/public-repo-communication/SKILL.md).
  • Generated fixtures and review SDK changes are public-safe.
  • I reviewed git diff --check.

Summary by cubic

Masks API-marked sensitive values in CLI error output, matching --debug. Previously, echoed values could appear in the Details: block, fallback error messages, and -o json or agent-mode envelopes; successful output is unchanged.

  • Redacts error bodies using the operation's schema, including commands for operations split by request content type.
  • Also scrubs fallback error text and machine-readable error fields, preserving numeric spelling.
  • Only generated CLIs with sensitive values are affected.