fix(auth): short-circuit whoami for org auth tokens (CLI-1AZ)#841
Merged
Conversation
) GET /api/0/auth/ historically returned 400 Bad Request for valid Bearer tokens because Sentry's AuthIndexEndpoint excluded UserAuthTokenAuthentication, so tokens were silently ignored and the handler returned 400 with an empty body. Fixed server-side by getsentry/sentry#112853 (shipped 26.4.1), but CLIs in the wild must still degrade gracefully during rollout — and org auth tokens (sntrys_...) continue to hit this 400 forever because the fix only added UserAuthTokenAuthentication (not OrgAuthTokenAuthentication) and UserAuthTokenAuthentication.accepts_auth explicitly rejects the sntrys_ prefix. - Short-circuit whoami when the active token is an org auth token — whoami is semantically meaningless for org-scoped tokens (no user to return). Emit a clear CliError pointing to auth status / org list before any API call. - Translate ApiError(400) from /auth/ into AuthError('invalid') with skipAutoAuth: true and a helpful message (sentry auth login, sentry auth status). Silent refresh wouldn't help (token is valid, endpoint is refusing to parse it); triggering auto-login on whoami itself would loop. - Add allowlisted response-header capture in apiRequestToRegion 4xx/5xx path (content-type, content-length, server, cf-ray, x-sentry-error, www-authenticate) so future empty-detail 400s are triageable without a user-side repro. - New src/lib/token-type.ts helper with classifySentryToken() for prefix-based classification (org-auth-token / user-auth-token / oauth-or-legacy). Drives Sentry issue CLI-1AZ down to zero once rollout completes and keeps org-token users on a clear path forever.
Contributor
|
Contributor
Codecov Results 📊✅ 5933 passed | Total: 5933 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
All tests are passing successfully. ❌ Patch coverage is 79.49%. Project has 13047 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 75.16% 75.14% -0.02%
==========================================
Files 284 285 +1
Lines 52437 52490 +53
Branches 0 0 —
==========================================
+ Hits 39412 39443 +31
- Misses 13025 13047 +22
- Partials 0 0 —Generated by Codecov Action |
CI's check:errors linter rejects CliError with ad-hoc 'Try:' strings — switch to ResolutionError (which already has structured headline/hint/ suggestions semantics) per AGENTS.md error hierarchy guidance.
The server-side fix (getsentry/sentry#112853) is rolled out — OAuth and user PAT 400s on /auth/ are no longer happening. The only persistent 400 case is org auth tokens, which we already short-circuit before the API call. The 400 → AuthError translation was both unhelpful (the 'rolling out' claim is no longer accurate) and unreachable in normal operation. Anomalous future 400s remain diagnosable via the response-header telemetry context added to apiRequestToRegion in this PR.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix CLI-1AZ:
sentry auth whoamithrows a confusing empty-detailApiError: API request failed: 400 Bad Requestwhen the active token is an organization auth token (sntrys_...).The Sentry backend's
/auth/endpoint (AuthIndexEndpoint) doesn't accept org auth tokens —UserAuthTokenAuthentication.accepts_authexplicitly rejects thesntrys_prefix, andOrgAuthTokenAuthenticationwas never added to this endpoint (getsentry/sentry#112853 added user-token auth only). Even if it did,whoamiis semantically meaningless for org tokens — they aren't tied to a single user. The CLI should detect this client-side and tell the user clearly, not let them hit a 400.What this PR does
Org-token short-circuit (
src/commands/auth/whoami.ts): before any API call, inspect the effective token viagetAuthToken(). If it'ssntrys_..., throw aResolutionErrorpointing tosentry auth statusandsentry org list. No request, no confusing 400, no auto-login loop.Response-header telemetry (
src/lib/api/infrastructure.ts): on every 4xx/5xx inapiRequestToRegion, attach an allow-listed subset of response headers (content-type,content-length,server,cf-ray,x-sentry-error,www-authenticate) to the Sentry scope asapi_response_headerscontext. Lets future empty-detail4xx events be triaged without user-side repro — Sentry-app 4xx (JSON body) vs CDN / WAF / proxy 4xx (HTML or empty body) is visible at a glance. Useful diagnostic for any future auth-class anomaly.New
classifySentryToken()helper (src/lib/token-type.ts): literal-prefix classification returning"org-auth-token"/"user-auth-token"/"oauth-or-legacy". Tiny, reusable, property-tested.Background
Most events on CLI-1AZ pre-date the server-side rollout of getsentry/sentry#112853 (merged Apr 20, shipped 26.4.1). That PR added
UserAuthTokenAuthenticationto the/auth/endpoint, fixing 400s for OAuth and user PATs (sntryu_...). Now that the rollout is complete, the only persistent 400 cause on/auth/is org auth tokens — addressed by the short-circuit above.Verification
bun x tsc --noEmit— clean.bun run script/check-error-patterns.ts— no anti-patterns.bun test test/commands/auth/whoami.test.ts— 13/13 pass (org-token short-circuit + error message +sntryu_passes through, plus existing happy-path coverage).bun test test/lib/token-type.property.test.ts— 10/10 pass (fast-check properties + case sensitivity + boundary cases).bun test test/lib/api/infrastructure.test.ts— 5/5 pass (telemetry addition doesn't regress).Manual repro
Refs
6c28450, merged Apr 20 2026, shipped 26.4.1)