Skip to content

fix(cli): Record why Sentry refused a request on 401/403 errors - #2163

Merged
betegon merged 2 commits into
mainfrom
fix/cli-api-denial-reason
Nov 11, 2026
Merged

betegon merged 2 commits into
mainfrom
fix/cli-api-denial-reason

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

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

via @betegon.

--

View Session in Sentry

401/403s are silenced as user errors, and the detail on their spans is
mostly scrubbed to [Filtered], so the failures can't be told apart.
Give ApiError a fixed `denialReason` category from the 401/403
enrichment and record it as `api_error.denial_reason` on the command
span, `api_denial_reason` on `cli.error.silenced` and `denial_reason`
on the `cli.api_error_silenced` log, in place of the detail.

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 9:05am UTC

Request Review

Per review, this PR doesn't add tests for telemetry.

Co-Authored-By: bete <[email protected]>
@betegon
betegon merged commit 263382d into main Nov 11, 2026
26 checks passed
@betegon
betegon deleted the fix/cli-api-denial-reason branch November 11, 2026 10:07
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
…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 — afc99d46 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