Skip to content

fix(api): Allow Bearer token auth for GET /api/0/auth/ - #112853

Merged
BYK merged 2 commits into
masterfrom
BYK/fix/auth-index-bearer-token
Apr 20, 2026
Merged

BYK merged 2 commits into
masterfrom
BYK/fix/auth-index-bearer-token

Conversation

@BYK

@BYK BYK commented Apr 13, 2026

Copy link
Copy Markdown
Member

GET /api/0/auth/ returns 400 for valid Authorization: Bearer <token> requests
because BaseAuthIndexEndpoint overrides authentication_classes to only
(QuietBasicAuthentication, SessionAuthentication), excluding UserAuthTokenAuthentication.

This adds an initialize_request override on AuthIndexEndpoint that conditionally
prepends UserAuthTokenAuthentication for GET requests only. POST (login), PUT (sudo),
and DELETE (logout) retain session-only auth since they are session-management operations
where Bearer token auth is inappropriate.

Fixes #112778

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 13, 2026
@BYK
BYK marked this pull request as ready for review April 13, 2026 20:55
@BYK
BYK force-pushed the BYK/fix/auth-index-bearer-token branch from fb6c27e to 08792d1 Compare April 13, 2026 20:55
@BYK
BYK requested a review from a team as a code owner April 13, 2026 20:55
BaseAuthIndexEndpoint restricts authentication_classes to only
QuietBasicAuthentication and SessionAuthentication, which excludes
UserAuthTokenAuthentication. This causes GET /api/0/auth/ to return
400 for valid Bearer tokens since DRF treats the request as anonymous.

Override initialize_request on AuthIndexEndpoint to conditionally add
UserAuthTokenAuthentication for GET requests only. POST/PUT/DELETE
retain the original session-only auth since they are session management
operations where Bearer token auth is inappropriate.

Fixes GH-112778
Co-Authored-By: Claude Sonnet 4 <[email protected]>
@BYK
BYK merged commit 6c28450 into master Apr 20, 2026
56 checks passed
@BYK
BYK deleted the BYK/fix/auth-index-bearer-token branch April 20, 2026 20:18
BYK added a commit to getsentry/cli that referenced this pull request Apr 25, 2026
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.
BYK added a commit to getsentry/cli that referenced this pull request Apr 25, 2026
## Summary

Fix CLI-1AZ: `sentry auth whoami` throws a confusing empty-detail
`ApiError: API request failed: 400 Bad Request` when the active token is
an organization auth token (`sntrys_...`).

The Sentry backend's `/auth/` endpoint (`AuthIndexEndpoint`) doesn't
accept org auth tokens — `UserAuthTokenAuthentication.accepts_auth`
explicitly rejects the `sntrys_` prefix, and
`OrgAuthTokenAuthentication` was never added to this endpoint
(getsentry/sentry#112853 added user-token auth only). Even if it did,
`whoami` is 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

1. **Org-token short-circuit** (`src/commands/auth/whoami.ts`): before
any API call, inspect the effective token via `getAuthToken()`. If it's
`sntrys_...`, throw a `ResolutionError` pointing to `sentry auth status`
and `sentry org list`. No request, no confusing 400, no auto-login loop.

2. **Response-header telemetry** (`src/lib/api/infrastructure.ts`): on
every 4xx/5xx in `apiRequestToRegion`, attach an allow-listed subset of
response headers (`content-type`, `content-length`, `server`, `cf-ray`,
`x-sentry-error`, `www-authenticate`) to the Sentry scope as
`api_response_headers` context. Lets future empty-`detail` 4xx 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.

3. **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
`UserAuthTokenAuthentication` to 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

```
# Org-token short-circuit (no API call, clear message)
SENTRY_AUTH_TOKEN='sntrys_fake' bun run src/bin.ts auth whoami

# Normal happy path (unchanged)
bun run src/bin.ts auth whoami
```

## Refs

- Sentry issue: [CLI-1AZ](https://sentry.sentry.io/issues/7425666334/)
- Server-side context: getsentry/sentry#112853 (`6c28450`, merged Apr 20
2026, shipped 26.4.1)
@github-actions github-actions Bot locked and limited conversation to collaborators May 6, 2026

This branch was successfully deployed

1 active deployment
Preview — aef679eb Deployed Apr 13, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AuthIndexEndpoint returns 400 for valid Bearer token auth (missing UserAuthTokenAuthentication)

2 participants