feat(api): Advertise required scopes on token-scope 403s (RFC 6750)#118612
Draft
gricha wants to merge 1 commit into
Draft
feat(api): Advertise required scopes on token-scope 403s (RFC 6750)#118612gricha wants to merge 1 commit into
gricha wants to merge 1 commit into
Conversation
8ce455b to
623301f
Compare
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. 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 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>
623301f to
27a0106
Compare
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