Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.

feat(api-client): Carry the scopes a 403 insufficient_scope challenge accepts - #146

Merged
BYK merged 1 commit into
mainfrom
feat/insufficient-scope-challenge
Nov 12, 2026
Merged

BYK merged 1 commit into
mainfrom
feat/insufficient-scope-challenge

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

When a token lacks every scope an endpoint accepts, Sentry returns a 403 with DRF's generic {"detail":"You do not have permission to perform this action."} and names the accepted scopes only in WWW-Authenticate: Bearer error="insufficient_scope", scope="..." (getsentry/sentry#118612). The request core dropped that header, so the MCP told agents to "address the concern and try again", which can't work with the same token (getsentry/toolkit#2208). The CLI parsed the header itself, but only on its own request path.

  • readErrorResponse reads the challenge into acceptedScopes (403s only).
  • The core passes it through createApiError (new optional last argument) to ApiPermissionError.acceptedScopes, with isInsufficientScopeError().
  • ApiPermissionError.toUserMessage() names the scopes and says retrying with the same token will not help. Other 403s keep the old message.
  • parseInsufficientScopeChallenge moves here from the Sentry CLI, so the CLI and MCP share one parser. It keeps only well-formed namespace:action scope names, so no other header text reaches users or agents.

Everything is additive; existing callers don't change. The toolkit change that uses this is on toolkit branch fix/insufficient-scope-403.

via @BYK.

--

View Session [Sentry]

… accepts

Sentry answers a token that lacks every scope an endpoint accepts with a
403, DRF's generic detail, and the accepted scopes only in
WWW-Authenticate: Bearer error="insufficient_scope", scope="...".
readErrorResponse now reads them into acceptedScopes, the core passes
them to ApiPermissionError, and its toUserMessage names them and says
retrying with the same token will not help. parseInsufficientScopeChallenge
moves here from the Sentry CLI so the CLI and MCP share it.

Refs getsentry/toolkit#2208

Co-Authored-By: BYK <[email protected]>
@BYK
BYK enabled auto-merge (squash) November 12, 2026 22:44
@BYK
BYK merged commit 18c2d5f into main Nov 12, 2026
11 checks passed
@BYK
BYK deleted the feat/insufficient-scope-challenge branch November 12, 2026 22:47
sentry-junior Bot added a commit to getsentry/toolkit that referenced this pull request Nov 12, 2026
1.83.0 carries the insufficient_scope challenge's scopes on
ApiPermissionError.acceptedScopes (getsentry/sentry-api-schema#146).

Co-Authored-By: Claude <[email protected]>
BYK pushed a commit to getsentry/toolkit that referenced this pull request Nov 13, 2026
…t_scope (#2222)

Fixes #2208.

When a token lacks the scope an endpoint needs, Sentry returns a 403
with DRF's generic detail and lists the accepted scopes only in
`WWW-Authenticate: Bearer error="insufficient_scope", scope="..."`. The
MCP dropped that header and showed the generic **Input Error** template
("…address the concern and try again"), which can't work with the same
token. For example, starting autofix in `analyze_issue_with_seer` needs
`event:write`, `event:admin`, or `seer:write`. Sentry already sends the
challenge for that endpoint (getsentry/sentry#118612), so no backend
change is needed.

`@sentry/api` 1.83.0 (getsentry/sentry-api-schema#146) reads the header
in the shared request core: `ApiPermissionError.acceptedScopes` holds
the scopes, and its `toUserMessage()` names them. This PR bumps to
1.83.0 and uses that:

- **MCP**: `formatErrorForUser` returns a **Missing Permission** message
that names the accepted scopes and says retrying with the same token
won't help. It also finds the error when a tool rewrapped it as `cause`
(e.g. `withApiErrorHandling`'s `UserInputError`). Embedded agent tools
get the same message, without the "try again" advice.
- **CLI**: the CLI already turned this challenge into scope guidance on
its own request path. It now uses the shared
`parseInsufficientScopeChallenge` instead of a local copy, so behavior
is the same. `sentry mcp` gets the MCP fix through mcp-core.

The message only names the scopes the endpoint accepts, which are the
same for every caller. Nothing about the user, role, org, or token is
added. A role-based 403 has no challenge and keeps the old message.

Out of scope: `analyze_issue_with_seer` still declares `requiredScopes:
[]`. Gating the tool on `event:write` would hide it from grants that can
read Seer results, so that's a product call for a separate change.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A0e1dc56124311021b965584c -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3A0e1dc56124311021b965584c)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A0e1dc56124311021b965584c/?project=4510944073809921)

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

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Claude <[email protected]>
sentry-junior Bot added a commit to getsentry/sentry that referenced this pull request Nov 14, 2026
Brings static/packages/api level with getsentry/sentry-api-schema main
(2.3.1):

- @sentry/api/docs exports the API reference data (getsentry/sentry-api-schema#150), with the
  no-typescript-source-strings lint rule, which the api client CI job
  now runs.
- listPage keeps per_page stable across offset cursors (getsentry/sentry-api-schema#149).
- ApiPermissionError carries the scopes from a 403 insufficient_scope
  challenge (getsentry/sentry-api-schema#146).
- Union fields and annotated single-member allOf fields keep their own
  descriptions.

The version moves to 2.3.1, so craft's first api@ release bumps from
sentry-api-schema's latest npm version. The release check no longer cuts
a patch for API ownership stats alone: the asset stays on each release
for ad-hoc use, but nothing reads it on a schedule.

Co-Authored-By: BYK <[email protected]>
BYK added a commit to getsentry/sentry that referenced this pull request Nov 14, 2026
…0472)

Moves `@sentry/api`, the generated TypeScript client for Sentry's public
API, from getsentry/sentry-api-schema to `static/packages/api`. It
builds from `tests/apidocs/openapi-derefed.json`, the spec this
repository generates. The npm name stays `@sentry/api`, so
getsentry/toolkit, sentry-docs and other consumers don't change. This PR
replaces the pre-merge check from #130450, which was closed.

This PR doesn't publish anything. Releases from here stay off until a
person cuts the first `api@` tag (see the cutover section).

## Package

- The sources match getsentry/sentry-api-schema `main` at 2.3.1,
including the `@sentry/api/docs` API reference export
(getsentry/sentry-api-schema#150), the `listPage` `per_page` fix
(getsentry/sentry-api-schema#149) and `insufficient_scope` handling on
`ApiPermissionError` (getsentry/sentry-api-schema#146).
- One deliberate difference from sentry-api-schema `main`: the generated
`buildUrl` trims trailing slashes from the base URL before appending
`/api/0` (`withApiBasePath` in `lib/api-client/api-base-url.ts`), so
`https://sentry.io/` no longer requests `https://sentry.io//api/0`.
Published 2.x versions keep that bug; the fix ships with the first
`api@` release from here.
- `package.json` is at 2.3.1, the latest npm version. Craft bumps from
it while there is no `api@` tag, so the first release from here lands
after sentry-api-schema's.
- Differences from sentry-api-schema, all because of where the package
now lives:
- TypeScript 6 through the repo's `@typescript/typescript6` alias, and
zod 4.6.7, the version `static/app`, gsApp, gsAdmin and scraps already
pin. The other dev tools are pinned to exact versions, and oxlint
matches the repo's 1.90.0.
- The build reads the spec files through `lib/spec-files.mjs` and
collects them under `spec/`.
- `source-analysis` uses a path-parameter pattern that can't backtrack
on runs of `{`, for a CodeQL alert the move raised.
- The scripts are `test:vitest` and `lint:js`, not `test` and `lint`.
The repo's `test-ci` and `test-all` pass Jest flags to every workspace's
`test` script, and the repo's `lint:js` runs each workspace's `lint:js`.

## Spec files on GitHub releases

Each `[email protected]` GitHub release gets the files that version was built
from as assets. The pack job adds them to the release artifact; craft's
github target attaches every file in it, and its npm target publishes
only the tarball:

- `openapi-derefed.json`, the dereferenced OpenAPI spec.
- `rpc_method_schema.json`, for the openapi-diff/RPC check. It comes
from a new `make build-rpc-schema` target.
- `api_ownership_stats_dont_modify.json`, which `make build-api-docs`
still generates. No known consumer reads it; it is kept for ad-hoc and
future use.

For example:
`https://github.com/getsentry/sentry/releases/download/[email protected]/openapi-derefed.json`.
The npm package doesn't include them.

## CI: `api client` is a required check

The new `api-client` job in `backend.yml` builds, typechecks, lints and
tests the client against the PR's own spec. It is part of
`backend-required-check`. A spec change that broke the client once
stalled releases for 2 days with nothing catching it before merge; this
check catches that class of change.

- It reuses the spec from the `api-docs` job (shared as a workflow
artifact), so the spec isn't built twice.
- Both jobs run only when the new `api_client` filter matches: the
existing `api_docs` paths plus `static/packages/api/**`. On other PRs
both jobs are skipped, and `backend-required-check` counts skipped jobs
as passing.

## Releases

- **Pack:** `package-pack.yml` sets up the Python env for `api`, then
builds the spec, the RPC schema and the package before `pnpm pack`.
- **Release on change:** a `release-api-package` job in `openapi.yml`
runs after the master spec build whenever the spec or the package might
have changed. It runs
`.github/workflows/scripts/api-package-release-preflight.sh`, then
`craft prepare` with `getsentry/publish`. The preflight skips the
release when:
- there is no `api@` tag yet, so the job stays inert until the first
release is cut by hand;
- an `api/release/*` branch is still pending. When that release merges
back, its own run releases whatever landed in the meantime;
  - HEAD predates the newest `api@` tag;
- the spec, the RPC schema and the package are unchanged since the last
release. It compares the files against the last release's assets.
- **Version bump:** a spec change is a minor release, as in
sentry-api-schema. A package change uses craft's automatic bump. An
RPC-schema-only change is a patch. An ownership-stats-only change
doesn't release; the file refreshes with the next release.
- **Concurrency:** releases share a concurrency group with
`cancel-in-progress: false`, so a burst of merges produces one release.
- There is no cron. Manual releases go through the Release workflow,
which gets a `static/packages/api` option.
- **sentry-docs:** `bump-sentry-docs.yml` runs on each published `api@`
release. `.craft.yml` lists the npm target before github for
`static/packages/*`, so the version is already on npm when the release
is published (the same order applies to icons, scraps and search-syntax,
and nothing in them needs the tag before the npm publish). It dispatches
sentry-docs' `bump-api-schema-sha.yml` by file name with the existing
`SENTRY_API_SCHEMA_UPDATER` app. sentry-docs' bump reads the version
from npm, so it works the same after the move.
- The mirror push to getsentry/sentry-api-schema still runs, on the same
`api_client` filter, and the release doesn't wait for it.

## Cutover (in this order)

1. Merge #130541 and getsentry/eng-pipes#1116, which retires the `apis`
command. #130541 falls back to sentry-api-schema `main` until an `api@`
release exists.
2. Merge this PR.
3. In getsentry/publish `auto-approve-repos.txt`, replace the
`getsentry/sentry-api-schema` line with this repo's
`static/packages/api` entry.
4. Disable sentry-api-schema's Release workflow and its sentry-docs
dispatch workflow (getsentry/sentry-api-schema#155), so only one repo
publishes.
5. Cut the first `api@` release by hand: run the Release workflow with
workspace `static/packages/api`. Check that the version it picks is
above sentry-api-schema's last npm release. From then on, `openapi.yml`
releases on change.
6. Remove the openapi-diff fallback, the mirror push in `openapi.yml`,
and the sentry-api-schema links in the getsentry/toolkit docs,
getsentry/skills `document-api-endpoint` and devinfra-mcp
`docs/sentry/ci.md`.
7. Archive getsentry/sentry-api-schema. Its copies of the
`SENTRY_API_SCHEMA_UPDATER_*` credentials can go with it.

`static/packages/api/` is owned by @getsentry/owners-api and
@getsentry/ocpo in CODEOWNERS.

This PR touches `static/` as well as backend CI files. The package is a
standalone npm package and isn't part of either deploy, so splitting it
wouldn't change what gets deployed.

No UI change.

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

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

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A69da19b385017e68b7f9eace56e9477f)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A69da19b385017e68b7f9eace56e9477f/?project=4510944073809921)

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

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant