-
-
Notifications
You must be signed in to change notification settings - Fork 7
fix(errors): surface real API errors and granular scopes in 403s (#785) #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+472
−43
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
823803e
fix(errors): surface real API errors and granular scopes in 403s (#785)
BYK d3f88f5
refactor(api-scope): align regex with Sentry's real scope list
BYK 9931385
fix(api-scope): drop no-op `:`->`:` substitution in scope regex
BYK 8a970af
refactor(errors): trim #789 patch per review
BYK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * Extract Sentry scope identifiers from a 403 response, so we can hint | ||
| * at the specific missing scope instead of a hardcoded default | ||
| * (getsentry/cli#785 #9). | ||
| * | ||
| * Sentry's standard 403 path is a DRF `PermissionDenied` with no | ||
| * structured scope info, but some endpoints include the scope in the | ||
| * free-text `detail`. We also peek at a few plausible structured field | ||
| * names (`required` / `requiredScopes` / `scopes`) in case they're | ||
| * added later. Empty result → callers fall back to their defaults. | ||
| */ | ||
|
|
||
| /** | ||
| * Canonical Sentry scopes, mirrored from getsentry/sentry | ||
| * `src/sentry/conf/server.py` SENTRY_SCOPES. Excludes OIDC scopes | ||
| * (`openid`/`profile`/`email`) and internal-only `org:superuser`. | ||
| */ | ||
| const SENTRY_SCOPES = [ | ||
| "org:read", | ||
| "org:write", | ||
| "org:admin", | ||
| "org:integrations", | ||
| "org:ci", | ||
| "member:invite", | ||
| "member:read", | ||
| "member:write", | ||
| "member:admin", | ||
| "team:read", | ||
| "team:write", | ||
| "team:admin", | ||
| "project:read", | ||
| "project:write", | ||
| "project:admin", | ||
| "project:releases", | ||
| "project:distribution", | ||
| "event:read", | ||
| "event:write", | ||
| "event:admin", | ||
| "alerts:read", | ||
| "alerts:write", | ||
| ] as const; | ||
|
|
||
| // Explicit alternation (not `<ns>:<action>` product) rejects nonexistent | ||
| // combinations like `release:write` or `alerts:admin`. `:` is not a | ||
| // regex metachar so no escaping needed. | ||
| const KNOWN_SCOPE_RE = new RegExp(`\\b(?:${SENTRY_SCOPES.join("|")})\\b`, "gi"); | ||
|
|
||
| const SCOPE_FIELD_NAMES = ["required", "requiredScopes", "scopes"] as const; | ||
|
|
||
| /** | ||
| * Extract Sentry scope identifiers from a 403 response detail. | ||
| * | ||
| * @param detail - ApiError.detail value; string, object, or undefined | ||
| * @returns Deduplicated, source-ordered scope identifiers. Empty when none found. | ||
| */ | ||
| export function extractRequiredScopes(detail: unknown): string[] { | ||
| if (!detail) { | ||
| return []; | ||
| } | ||
| if (typeof detail === "object") { | ||
| const fromFields = extractFromRecord(detail as Record<string, unknown>); | ||
| if (fromFields.length > 0) { | ||
| return fromFields; | ||
| } | ||
| // Fall back to scanning the serialized form to catch non-standard keys. | ||
| return extractFromText(JSON.stringify(detail)); | ||
| } | ||
| if (typeof detail === "string") { | ||
| return extractFromText(detail); | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| function extractFromRecord(record: Record<string, unknown>): string[] { | ||
| for (const field of SCOPE_FIELD_NAMES) { | ||
| const value = record[field]; | ||
| if (!Array.isArray(value)) { | ||
| continue; | ||
| } | ||
| const scopes = collectScopesFromArray(value); | ||
| if (scopes.length > 0) { | ||
| return [...new Set(scopes)]; | ||
| } | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| /** Accepts both bare strings and `{scope: "..."}` objects. */ | ||
| function collectScopesFromArray(entries: unknown[]): string[] { | ||
| const out: string[] = []; | ||
| for (const entry of entries) { | ||
| const scope = extractScopeCandidate(entry); | ||
| if (scope && matchesKnownScope(scope)) { | ||
| out.push(scope.toLowerCase()); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function extractScopeCandidate(entry: unknown): string | undefined { | ||
| if (typeof entry === "string") { | ||
| return entry; | ||
| } | ||
| if ( | ||
| entry && | ||
| typeof entry === "object" && | ||
| "scope" in entry && | ||
| typeof (entry as { scope: unknown }).scope === "string" | ||
| ) { | ||
| return (entry as { scope: string }).scope; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| /** Tests + resets the shared `g`-flagged regex. */ | ||
| function matchesKnownScope(scope: string): boolean { | ||
| const matched = KNOWN_SCOPE_RE.test(scope); | ||
| KNOWN_SCOPE_RE.lastIndex = 0; | ||
| return matched; | ||
| } | ||
|
|
||
| function extractFromText(text: string): string[] { | ||
| const matches = text.match(KNOWN_SCOPE_RE); | ||
| if (!matches) { | ||
| return []; | ||
| } | ||
| return [...new Set(matches.map((m) => m.toLowerCase()))]; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.