feat(api): Advertise required scopes on token-scope 403s (RFC 6750)#118612
Merged
Conversation
27a0106 to
0599669
Compare
michelletran-sentry
approved these changes
Jun 29, 2026
cvxluo
approved these changes
Jun 29, 2026
| default_detail = "The requested resource does not exist" | ||
|
|
||
|
|
||
| class InsufficientScope(PermissionDenied): |
Contributor
There was a problem hiding this comment.
not blocking, i'm curious if we will want this for any getsentry endpoints and if this will work out of the box for those
Member
Author
There was a problem hiding this comment.
i do think it works out of the box if i am to trust the agent here
0599669 to
3f7ce77
Compare
markstory
reviewed
Jun 29, 2026
Comment on lines
+38
to
+40
| return perm.has_permission(drf_request, APIView()) and perm.has_object_permission( | ||
| drf_request, APIView(), obj | ||
| ) |
Member
There was a problem hiding this comment.
Having a has_* method raise when you're denied feels a bit clunky. Usually has* methods will capture any exceptions from the checks they do and return a bool.
Member
Author
There was a problem hiding this comment.
corrected, thanks for the call out!
When a token-authorized request is denied because its scopes do not cover the endpoint's required scopes, return an RFC 6750 insufficient_scope challenge in the WWW-Authenticate header instead of a bare 403, so callers learn which scope they lack. has_permission stays a plain bool: the shared token-scope gate records the required scopes on the request, and Endpoint.permission_denied (which already raises for superuser/staff) raises InsufficientScope from them. The required scopes come from the endpoint's own scope_map, so every token-scoped endpoint benefits with no per-class edits. The response body is unchanged and the behavior is a no-op for non-token auth and for 401s, so this is non-breaking. Sentry's custom_exception_handler already forwards an exception's auth_header onto WWW-Authenticate, so the transport is reused as-is. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
3f7ce77 to
b354c93
Compare
shayna-ch
pushed a commit
that referenced
this pull request
Jun 30, 2026
…118612) ## Summary When a token-authorized request is denied for lacking the required scope, Sentry returns a bare `403 {"detail": "You do not have permission to perform this action."}` — it never says *which* scope was needed. This adopts the OAuth 2.0 standard answer (RFC 6750 `insufficient_scope`) so callers know what they're missing: ``` WWW-Authenticate: Bearer error="insufficient_scope", scope="org:admin org:write" ``` The required scopes come from the endpoint's own `scope_map`, surfaced from the single shared token-scope gate (`ScopedPermission.has_permission`), so **every** token-scoped endpoint gets it with no per-class edits. ## Why it's safe / non-breaking - The `403` **body is unchanged** — the scope info rides only in the `WWW-Authenticate` header, so clients parsing `{"detail": ...}` are unaffected. - No-op for non-token auth (session/superuser/staff) and for `401` authentication failures. - The transport already existed: `custom_exception_handler` copies an exception's `auth_header` onto `WWW-Authenticate` (it even cited RFC 6750). - Discloses only the endpoint's required scopes (already public in open-source `scope_map` and the API docs). The denial fires at the view level *before* the org/object is loaded, so it leaks no resource existence; it never enumerates the caller's held scopes. ## Non-goals - Object-level scope denials (`has_object_permission`) are not enriched yet — deliberate, to keep the blast radius small. The view-level gate covers the motivating cases. ## Tests `tests/sentry/api/test_permissions.py`: unit (the raise + exact header at the permission boundary) and end-to-end (header reaches the HTTP response; body unchanged; **no** header on `401`, on session denial, or on success). Spec: `openspec/changes/add-insufficient-scope-errors/`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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
When a token-authorized request is denied for lacking the required scope, Sentry returns a
bare
403 {"detail": "You do not have permission to perform this action."}— it never sayswhich scope was needed. This adopts the OAuth 2.0 standard answer (RFC 6750
insufficient_scope) so callers know what they're missing:The required scopes come from the endpoint's own
scope_map, surfaced from the singleshared token-scope gate (
ScopedPermission.has_permission), so every token-scopedendpoint gets it with no per-class edits.
Why it's safe / non-breaking
403body is unchanged — the scope info rides only in theWWW-Authenticateheader, so clients parsing
{"detail": ...}are unaffected.401authentication failures.custom_exception_handlercopies an exception'sauth_headerontoWWW-Authenticate(it even cited RFC 6750).scope_mapand the API docs). The denial fires at the view level before the org/object is loaded,
so it leaks no resource existence; it never enumerates the caller's held scopes.
Non-goals
has_object_permission) are not enriched yet — deliberate,to keep the blast radius small. The view-level gate covers the motivating cases.
Tests
tests/sentry/api/test_permissions.py: unit (the raise + exact header at the permissionboundary) and end-to-end (header reaches the HTTP response; body unchanged; no header
on
401, on session denial, or on success).Spec:
openspec/changes/add-insufficient-scope-errors/.🤖 Generated with Claude Code