Skip to content

fix(auth): reject malformed bearer credentials - #1638

Merged
BYK merged 4 commits into
mainfrom
bt/fix-auth-token-leaks
Sep 28, 2026
Merged

BYK merged 4 commits into
mainfrom
bt/fix-auth-token-leaks

Conversation

@betegon

@betegon betegon commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Malformed bearer credentials can be echoed by runtime header-validation errors. Normalize access tokens and reject invalid formatting before constructing authenticated API, Docs, init, and artifact-download requests. Failures use a fixed AUTH_INVALID message without the credential and preserve exit code 12.

Share one helper for trimming surrounding whitespace and ASCII controls. Internal whitespace, controls, and non-ASCII characters remain invalid; split lines are never joined. Environment selection, token host claims, and cache Vary metadata use the same trimming rules. Cache metadata only trims a candidate: validation waits until OAuth refresh and credential selection determine the token actually used.

Validate access tokens before SQLite writes, including refreshed credentials. Reject a malformed explicit auth login --token before changing the host or clearing existing authentication. Legacy JSON migration skips malformed auth, migrates other settings, and retains the original file with a fixed recovery warning, so help, login, and logout remain usable.

Malformed-token failures remain visible as safe MalformedAuthTokenError events without retaining the credential or original cause. They do not trigger auto-login or mark a session crashed; ordinary authentication failures keep their existing reporting policy.

Validation

  • Full validation at e754fb328: 467 unit files, 10,050 passed / 14 skipped (TZ=UTC); 34 bundled E2E passed across auth, library, and migration; lint, TypeScript, and bundle build passed.
  • After test-only cleanup: all 102 affected unit tests and 14 auth E2E passed, along with Biome and git diff --check. Production code is unchanged; the full suite and build were not repeated locally.
  • Property and regression coverage for edge padding, internal invalid characters, auth precedence, OAuth refresh, cache metadata, and persistence.

Separate work

General output and telemetry redaction remains in #1640. SDK invocation isolation (#1645) and rc tokens truncated when copied into process.env (#1646) are separate fixes; this PR does not address those earlier input/lifecycle boundaries.

Bundler consumers pinned to sentry ^0.44.0 need a 0.44.x backport or a dependency update after release.

@vercel

vercel Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cli Ready Ready Preview Sep 28, 2026 5:50pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Sep 28, 2026 5:50pm UTC

Request Review

@betegon
betegon marked this pull request as ready for review September 28, 2026 10:31
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Sep 28, 2026
Comment thread packages/cli/src/lib/sentry-client.ts Outdated
@betegon
betegon force-pushed the bt/fix-auth-token-leaks branch from bc20791 to 9a22d3c Compare September 28, 2026 10:44
@vercel
vercel Bot temporarily deployed to Preview – sentry-local September 28, 2026 10:44 Inactive
@betegon betegon changed the title fix(auth): validate bearer tokens and redact diagnostics fix(auth): reject malformed bearer credentials Sep 28, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9a22d3c. Configure here.

Comment thread packages/cli/src/lib/sentry-client.ts
@betegon
betegon requested a review from BYK September 28, 2026 10:55
Comment thread packages/cli/src/lib/auth-header.ts Outdated
Comment thread packages/cli/src/lib/sentry-client.ts
Comment thread packages/cli/src/lib/sentry-client.ts
@betegon
betegon requested a review from BYK September 28, 2026 11:55

betegon commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

both comments addressed! @BYK

Comment thread packages/cli/src/lib/auth-header.ts Outdated
Comment thread packages/cli/src/lib/sentry-client.ts Outdated
@BYK
BYK merged commit 980fa50 into main Sep 28, 2026
37 of 38 checks passed
@BYK
BYK deleted the bt/fix-auth-token-leaks branch September 28, 2026 21:26
betegon added a commit that referenced this pull request Sep 29, 2026
## Summary

Runtime errors can echo credentials into command diagnostics. Redact
recognizable Sentry tokens and Bearer values in CLI/Stricli errors, SDK
errors, and outgoing telemetry envelopes, including credentials split by
control characters or escaped in JSON.

Telemetry redaction runs at the final transport boundary, after scope
attributes and internal errors are resolved. A detached JSON copy
preserves SDK serialization behavior without mutating caller objects or
live SDK state. Text redaction has no SDK dependencies, preserving the
completion startup path. Fatal diagnostics retain error names while
redacting credentials.

This complements the merged auth validation in #1638: diagnostics can
still contain credentials from custom headers and other error paths.

## Validation

- Full unit suite: 10,143 passed, 14 skipped (`TZ=UTC`).
- 35 auth/library/completion E2E tests passed against a fresh bundle.
- Lint, typecheck, bundle build, and diff checks passed.
- Regression tests failed before these fixes and passed afterward: both
CLI/library entry points avoid eager SDK imports, and the actual fatal
handler preserves the error name and redacts split credentials.
- Factored regex sources and flags match the previous implementation
exactly; envelope redaction was moved without behavior changes.

## Limits

Redaction recognizes Sentry token prefixes and Bearer context; it does
not detect arbitrary opaque secrets without that context. Binary
attachment bytes are preserved. Successful API response formatting is
unchanged.

Runtime header errors can contain ambiguous delimiters inside the
credential itself. Redaction conservatively uses the final matching
delimiter, which can also hide intervening text when multiple
diagnostics are concatenated into one string.

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – cli — 0e530330 Deployed Sep 28, 2026 by vercel[bot]
Preview – sentry-local — 0e530330 Deployed Sep 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants