Skip to content

fix(cli): Say which scope or org is wrong when CI commands get 401/403 - #2160

Closed
sentry-junior[bot] wants to merge 7 commits into
mainfrom
fix/cli-ci-auth-failure-hints
Closed

sentry-junior[bot] wants to merge 7 commits into
mainfrom
fix/cli-ci-auth-failure-hints

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Summary

The CI commands (release create / set-commits / finalize / deploy, sourcemap upload) fail on 401/403 with "may lack the required scope" or "not recognized or has been revoked" and nothing else. These errors are silenced as 4xx and their detail comes through as [Filtered] on spans, so we can't tell the cases apart either.

Over the last 14 days, cli.command spans for these commands recorded about 217k 401/403 failures on 0.45.0 (the version the bundler plugins pin) from about 1.5k orgs. On 0.48/0.49 there were about 470. The failure rate is about the same: roughly 1% of set-commits runs hit a 403 on the repos list, and about 1% of release create and sourcemap upload runs hit a 401. The split:

  • set-commits: 403 on GET organizations/{org}/repos/ (about 41k on 0.45, 126 on 0.49)
  • release create: 401 (about 75k on 0.45, about 100 on 0.49) and 403 (about 18k on 0.45)
  • sourcemap upload: 401 (about 59k on 0.45, about 110 on 0.49) and 403 (about 19k on 0.45), on chunk-upload options, assemble and chunk POSTs

Changes

  • Name the missing scope. Since feat(api): Advertise required scopes on token-scope 403s (RFC 6750) sentry#118612, Sentry answers a token-scope 403 with WWW-Authenticate: Bearer error="insufficient_scope", scope="...", listing every scope the endpoint accepts. I checked this against SaaS. The shared 401/403 enrichment now reads that header and says "Any one of these works: …". If one of those scopes is org:ci, a personal token also gets the hint that an org auth token has it. That's the set-commits repos case: the repos list accepts org:ci but not project:releases. If the token is an org auth token, the message says it only has org:ci and that a personal token with one of the listed scopes is needed.
  • Org mismatch. An org auth token works only in its own org, so the token's org is compared with the org in the request path. Sentry answers that mismatch with a 403 when both orgs are on the same server, and with a 401 "Invalid org token" when the token is unknown on the target's server. In either case, when the sntrys_ claim names a different org, the error now names both orgs. Numeric org IDs are skipped.
  • 401s for org auth tokens point at the org's Settings > Auth Tokens instead of the personal token page.
  • Chunk-upload POSTs used to build their own ApiError and skipped all of this. They now go through throwRawApiError.
  • Triage signal. ApiError.denialReason is a fixed category: insufficient_scope, org_mismatch, access_denied, expired, unrecognized, member_policy or seat_limit. It's recorded as api_error.denial_reason on the command span, as api_denial_reason on cli.error.silenced, and as denial_reason on the cli.api_error_silenced log. For 401/403s it replaces the detail on the span and the log: that detail is our own guidance, it can now name orgs, and most of it is scrubbed to [Filtered] anyway. The values avoid words such as "auth" and "token" on purpose: messages with "Unauthorized" in them already come through as [Filtered].

Self-hosted servers older than getsentry/sentry#118612 don't send the challenge. They keep today's generic message.

Scopes (checked in getsentry/sentry and getsentry/getsentry)

Endpoint Permission Accepted scopes
GET organizations/{org}/ OrganizationDetailsPermission (getsentry) org:read, org:write, org:admin, org:billing
chunk-upload GET / POST ChunkUploadPermission → OrganizationReleasePermission project:read (GET only), project:write, project:admin, project:releases, org:ci
artifact bundle assemble, releases POST / PUT, deploys POST OrganizationReleasePermission project:write, project:admin, project:releases, org:ci
GET organizations/{org}/repos/ OrganizationIntegrationsLoosePermission org:read, org:write, org:admin, org:integrations, org:ci

Org auth tokens only ever have org:ci (ORG_AUTH_TOKEN_ALLOWED_SCOPES).

Not in this PR

  • Org auth tokens always get a 403 on the region lookup (GET organizations/{org}/). The CLI swallows it and falls back, but it accounts for most of the 403 http.client spans on 0.49 (about 20k a week). Reading region_url from the token's claim would skip that request.
  • A clear error when the org value is a URL, a DSN or a pasted command. Those cause some of the 404s.
  • normalizeEndpoint still leaves org slugs in cli_error.api_endpoint and in span descriptions.
  • The bundler plugins pin 0.45.0, so most users only get this once the plugins move to a release that has it.

via @betegon.

--

View Session in Sentry

Sentry now advertises the scopes an endpoint accepts on a token-scope 403
(`WWW-Authenticate: Bearer error="insufficient_scope", scope="..."`). Read
it in the shared 401/403 enrichment and name those scopes, with an org:ci
hint for personal tokens and a "personal token needed" hint for org auth
tokens.

When the org auth token's claim names a different org than the request
path, say so on both the 403 (same server) and the 401 ("Invalid org
token") paths. Org auth tokens also get pointed at the org's token
settings on a 401 instead of the personal token page.

Record a fixed `denial_reason` category on the ApiError, the command span
(`api_error.denial_reason`) and the `cli.error.silenced` metric, in place
of the guidance text, which can name orgs and is mostly scrubbed to
[Filtered] anyway. Chunk upload POSTs now go through the same enrichment.

Co-Authored-By: bete <[email protected]>
@vercel

vercel Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 11, 2026 8:22am UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 11, 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.

Stale Bugbot comment from a previous run.

`getAuthToken()` skips an expired stored login that can still refresh and
falls back to the env token, but requests still send that login. With an
org auth token for another org in SENTRY_AUTH_TOKEN, a 401/403 from the
OAuth request was reported as an org mismatch. Read the token from
`getCredentialContext()` instead, and ignore it when it is an OAuth login.

@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.

Stale Bugbot comment from a previous run.

`auth login --token` stores a `sntrys_` token with the "oauth" source, so
skipping that source missed org mismatches for stored org auth tokens.
Parse the claim of whatever token the request sent; OAuth access tokens
have none.
For a stored login, the insufficient_scope guidance said `auth login`,
which refuses to run for a logged-in user in CI and offers to add another
account interactively. Suggest `auth refresh --scope <scope>` instead,
naming the first accepted scope the CLI's login requests by default.

@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.

Stale Bugbot comment from a previous run.

`auth refresh --scope X` grants exactly X, so the hint would have
narrowed the login to one scope. Follow the scope-recovery elevation
rule instead: when a default scope is accepted, point at
`auth login --force` (the defaults); otherwise list the defaults plus
the accepted scope.
The scope-challenge and 401 guidance keyed off whether an env token
exists, so a stored `sntrys_` token got OAuth re-login steps and a
refreshable login with a leftover env token was blamed on
SENTRY_AUTH_TOKEN. Branch on `getCredentialContext()` instead: a
refreshable login gets login steps, an org auth token (env or stored)
gets org token advice, and the message names the env var only when the
env token is what was sent.

@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 c737518. Configure here.

The 401 path still checked whether an env token exists, so a stored
login with a leftover SENTRY_AUTH_TOKEN got env-token revocation advice.
Give env-token advice only when the env token is what was sent.
@sentry-junior

sentry-junior Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of one PR per fix, as requested. Every Bugbot/Warden fix from this PR, and its regression tests, moved into the PR that owns that code.

# Fix PR Base
5 Triage signal (denialReason on span, metric and log) #2163 main
4 Chunk-upload POSTs through throwRawApiError #2164 main
1 Missing scope from the insufficient_scope challenge #2165 stacked on #2163
3 401s for org auth tokens point at the org's Settings > Auth Tokens #2167 stacked on #2165
2 Org mismatch between the token's claim and the request path #2168 stacked on #2165

Merged together, the five branches combine cleanly on today's main.

@sentry-junior sentry-junior Bot closed this Nov 11, 2026
betegon added a commit that referenced this pull request Nov 11, 2026
#2164)

Split out of #2160.

`sourcemap upload` chunk POSTs built their own error from the raw
response body. A 401/403 there skipped the guidance every other request
gets, and `enriched403` stayed false. Over 14 days, `sourcemap upload`
recorded about 78k 401/403 failures on 0.45.0, some of them on chunk
POSTs. This PR sends those failures through the same path as every other
request.

### Before

```
Error: Chunk upload failed: 403 Forbidden
  Endpoint: <region-url>/api/0/organizations/my-org/chunk-upload/
  {"detail":"You do not have permission to perform this action."}
```

### After

```
Error: Chunk upload failed: 403 Forbidden
  Endpoint: <region-url>/api/0/organizations/my-org/chunk-upload/
  You do not have permission to perform this action.

  Your SENTRY_AUTH_TOKEN token may lack the required scope for this operation.
  Check token scopes at: <sentry-url>/settings/account/api/auth-tokens/
```

Stands alone on `main`.

<!-- junior-request-attribution:start -->
via @betegon.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A28e783cd48c13b214d69caf6d8977a0c -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A28e783cd48c13b214d69caf6d8977a0c/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: bete <[email protected]>
betegon added a commit that referenced this pull request Nov 11, 2026
Split out of #2160.

401/403s from the CI commands (`release *`, `sourcemap upload`) are
silenced as user errors, and the detail on their spans is mostly
scrubbed to `[Filtered]`, so we can't tell them apart. Over 14 days that
was about 217k failures from about 1.5k orgs. This PR records a fixed
reason category for each one. The CLI's own output doesn't change.

### Before

```
# cli.command span
api_error.status:  401
api_error.message: Failed to create release: 401 Unauthorized
api_error.detail:  [Filtered]

# cli.error.silenced metric
error_class: ApiError   reason: api_user_error   api_status: 401

# cli.api_error_silenced log
status: 401   endpoint: …   detail: [Filtered]
```

### After

```
# cli.command span
api_error.status:        401
api_error.message:       Failed to create release: 401 Unauthorized
api_error.denial_reason: unrecognized

# cli.error.silenced metric
error_class: ApiError   reason: api_user_error   api_status: 401   api_denial_reason: unrecognized

# cli.api_error_silenced log
status: 401   endpoint: …   denial_reason: unrecognized
```

The values are `insufficient_scope`, `access_denied`, `expired`,
`unrecognized`, `member_policy` and `seat_limit`; #2168 adds
`org_mismatch`. On 401/403s, `denial_reason` replaces `detail`. The
values avoid words such as "auth" and "token", because scrubbing turns
values containing them into `[Filtered]`.

<!-- junior-request-attribution:start -->
via @betegon.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A28e783cd48c13b214d69caf6d8977a0c -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A28e783cd48c13b214d69caf6d8977a0c/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: bete <[email protected]>
betegon added a commit that referenced this pull request Nov 11, 2026
…ssing (#2165)

Stacked on #2163.

Split out of #2160.

About 1% of `release set-commits` runs hit a 403 on the repos list: over
14 days, about 41k on 0.45 and 126 on 0.49. The repos list accepts
`org:ci` but not `project:releases`, so a personal token made for
releases fails there with no hint why. Since getsentry/sentry#118612,
Sentry lists the accepted scopes in `WWW-Authenticate: Bearer
error="insufficient_scope", scope="..."`. The CLI now shows them.

### Before

`release set-commits`, with a personal token in `SENTRY_AUTH_TOKEN` that
lacks `org:ci`:

```
Error: Failed to list repositories: 403 Forbidden
  You do not have permission to perform this action.

  Your SENTRY_AUTH_TOKEN token may lack the required scope for this operation.
  Check token scopes at: <sentry-url>/settings/account/api/auth-tokens/
```

### After

```
Error: Failed to list repositories: 403 Forbidden
  You do not have permission to perform this action.

  Your SENTRY_AUTH_TOKEN token lacks the scope this request needs. Any one of these works: org:admin, org:ci, org:integrations, org:read, org:write.
  An org auth token has org:ci. Create one in your organization's Settings > Auth Tokens.
  Check token scopes at: <sentry-url>/settings/account/api/auth-tokens/
```

The second line depends on the credential that sent the request:
- **Org auth token, on an endpoint `org:ci` can't satisfy:** `Org auth
tokens only have org:ci. Use a personal token with one of these scopes
instead.`
- **`sentry auth login` session:** `Your login lacks the scope this
request needs. Any one of these works: …`, followed by `auth login
--force`, or `auth refresh --scope <default scopes>,<needed scope>` when
none of the default scopes is accepted.

Self-hosted servers older than getsentry/sentry#118612 don't send the
header, so they keep the "may lack the required scope" message.

<!-- junior-request-attribution:start -->
via @betegon.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A28e783cd48c13b214d69caf6d8977a0c -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A28e783cd48c13b214d69caf6d8977a0c/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: bete <[email protected]>
betegon added a commit that referenced this pull request Nov 11, 2026
…2167)

Stacked on #2165.

Split out of #2160.

About 1% of `release create` and `sourcemap upload` runs hit a 401: over
14 days, about 75k and 59k on 0.45 (about 100 each on 0.49). Most CI
setups use org auth tokens, but the 401 message sends every env token to
the personal token page.

### Before

`release create`, with an org auth token (`sntrys_…`) in
`SENTRY_AUTH_TOKEN`:

```
Error: Failed to create release: 401 Unauthorized
  Invalid org token

  Your SENTRY_AUTH_TOKEN token is not recognized or has been revoked.
  Create a new token at: <sentry-url>/settings/account/api/auth-tokens/
```

### After

```
Error: Failed to create release: 401 Unauthorized
  Invalid org token

  Your SENTRY_AUTH_TOKEN org auth token is not recognized or has been revoked.
  Create a new one in your organization's Settings > Auth Tokens.
```

On `Token expired`, the third line reads `Your SENTRY_AUTH_TOKEN org
auth token has expired.` instead. A stored `sntrys_…` token reads `Your
stored org auth token …`. A `sentry auth login` session with a leftover
`SENTRY_AUTH_TOKEN` now gets `Re-authenticate with: sentry auth login`
instead of advice about the env token.

<!-- junior-request-attribution:start -->
via @betegon.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A28e783cd48c13b214d69caf6d8977a0c -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A28e783cd48c13b214d69caf6d8977a0c/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: bete <[email protected]>
betegon added a commit that referenced this pull request Nov 11, 2026
…2168)

Stacked on #2165.

Split out of #2160.

An org auth token works only in its own org. When it targets another
org, Sentry answers 403 if both orgs are on the same server, or 401
"Invalid org token" if they aren't, and neither response says why. The
CLI now compares the org in the token (`sntrys_…`) with the org in the
request path and names both orgs when they differ.

### Before

`release create` for `other-org`, with an org auth token for `my-org` in
`SENTRY_AUTH_TOKEN`:

```
Error: Failed to create release: 403 Forbidden
  You do not have permission to perform this action.

  Your SENTRY_AUTH_TOKEN token may lack the required scope for this operation.
  Check token scopes at: <sentry-url>/settings/account/api/auth-tokens/
```

```
Error: Failed to create release: 401 Unauthorized
  Invalid org token

  Your SENTRY_AUTH_TOKEN token is not recognized or has been revoked.
  Create a new token at: <sentry-url>/settings/account/api/auth-tokens/
```

### After

```
Error: Failed to create release: 403 Forbidden
  You do not have permission to perform this action.

  This org auth token belongs to organization 'my-org', but the request is for 'other-org'.
  Use an auth token created in 'other-org', or target 'my-org' instead.
```

```
Error: Failed to create release: 401 Unauthorized
  Invalid org token

  This org auth token belongs to organization 'my-org', but the request is for 'other-org'.
  Use an auth token created in 'other-org', or target 'my-org' instead.
```

These errors are recorded with `denial_reason: org_mismatch` (#2163).
Numeric org IDs are skipped, and the org comparison ignores case.

<!-- junior-request-attribution:start -->
via @betegon.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A28e783cd48c13b214d69caf6d8977a0c -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A28e783cd48c13b214d69caf6d8977a0c/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: bete <[email protected]>

This branch was previously deployed

1 inactive deployment
Preview — 39f7ab04 Deployed Nov 11, 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.

1 participant