mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem We had some org permissions lingering when the permission form was scoped to project only. ## Solution We narrow the possible permissions you can pass, here's what changed with some guards. - On Project scope, 11 permissions that can’t work no longer appear. These are the org-level ones (Members, Projects, Billing and the like) plus a few rows with nothing behind them. Organization scope and full-access tokens look exactly as before. - Organization Settings lost its “Read” option on Project scope. You can pick None or Read-write. Read-write is still needed for one thing: claiming a project through OAuth. - Hidden permissions can’t slip through. Switching scope clears them, presets skip them, the counters ignore them, and the request sent to the API drops them as a last check. - A shared package was updated, which wasn’t in the plan. Six of those rows weren’t showing in the form at all because shared-data was on an older version of @supabase/shared-types than Studio. Claude Code updated it so the form matches the full permission list. **That makes the PR touch package.json and the lockfile, so call it out in review.** - One loose end: the Full access preset description still mentions “organization members”, which is now hidden on Project scope. Fixing it would mean changing copy, so it was left for a follow-up. ## Review instructions 1. Check out the branch and run `pnpm install` — `packages/shared-data` picks up a new `@supabase/shared-types`, so this isn't optional. 2. Run `pnpm dev:studio` and open http://localhost:8082/account/tokens. 3. Open the dev toolbar (the floating Supabase button, or the user dropdown), go to **FLAGS → ConfigCat**, and turn on `scopedPAT`. 4. Click **Generate new token**. **Resource access** defaults to **Project**. 5. Expand all five permission categories. See that 11 rows are gone: Organizations, Organization Audit Logs, Organization Billing And Invoices, Organization Configuration, Organization Members, Organization Projects, Platform Webhooks (organization), Project Authentication Hooks, Project Authentication Users, Perform Developer Level Operations, and the project-level SQL Snippets. 6. See that SQL Snippets (account-wide), Projects (account-wide), Organization Settings, and every other row are still there. 7. Open the **Organization Settings** dropdown. See that it offers only **None** and **Read-write**. 8. Switch **Resource access** to **Organization**. See that all 11 rows come back, and Organization Settings offers **Read** again. 9. Set **Organization Members** to Read-write and **Organization Settings** to Read. Switch to Project, then back to Organization. See that both are now **None**. 10. Switch back to Project and apply **Preset · Read-only**. See that Organization Settings stays at None and no hidden row is set. 11. Fill in a name, organization, and project, then create the token. See that the `POST /platform/profile/scoped-access-tokens` request body contains none of the hidden scope IDs.
168 lines
5.8 KiB
TypeScript
168 lines
5.8 KiB
TypeScript
import {
|
|
getCatalogEntry,
|
|
PERMISSION_CATALOG,
|
|
PERMISSION_CATALOG_BY_CATEGORY,
|
|
PERMISSION_MODE_LABEL,
|
|
type CategoryWithEntries,
|
|
type PermissionCatalogEntry,
|
|
type PermissionCategoryKey,
|
|
type PermissionMode,
|
|
type RiskLevel,
|
|
} from 'shared-data/scoped-access-token-permissions'
|
|
|
|
import type { ScopedAccessTokenPermission } from './AccessToken.constants'
|
|
|
|
export {
|
|
getCatalogEntry,
|
|
PERMISSION_CATALOG,
|
|
PERMISSION_CATALOG_BY_CATEGORY,
|
|
PERMISSION_MODE_LABEL,
|
|
}
|
|
export type { CategoryWithEntries, PermissionCatalogEntry, PermissionMode, RiskLevel }
|
|
|
|
/**
|
|
* Selection model for the scoped personal access token creation flow: none/read/readwrite modes per
|
|
* catalog entry, and the conversions between a selection and concrete FGA scope ids.
|
|
*
|
|
* The catalog itself (resources, categories, risk metadata, scopes) lives in
|
|
* shared-data/scoped-access-token-permissions.ts so the docs generator can consume it too.
|
|
*/
|
|
|
|
/** Map of resource key -> selected mode. Absent keys are treated as 'none'. */
|
|
export type PermissionSelection = Record<string, PermissionMode>
|
|
|
|
/** FGA scope ids a catalog entry grants at the given mode. */
|
|
export const getEntryScopes = (
|
|
entry: PermissionCatalogEntry,
|
|
mode: PermissionMode
|
|
): ScopedAccessTokenPermission[] => {
|
|
if (mode === 'none') return []
|
|
if (mode === 'readwrite') return [...entry.readScopes, ...entry.writeScopes]
|
|
return entry.readScopes
|
|
}
|
|
|
|
/** Flattens a selection into the concrete FGA scope ids to send to the API. */
|
|
export const selectionToScopes = (
|
|
selection: PermissionSelection
|
|
): ScopedAccessTokenPermission[] => {
|
|
const scopes: ScopedAccessTokenPermission[] = []
|
|
for (const [key, mode] of Object.entries(selection)) {
|
|
const entry = getCatalogEntry(key)
|
|
if (!entry) continue
|
|
scopes.push(...getEntryScopes(entry, mode))
|
|
}
|
|
return Array.from(new Set(scopes))
|
|
}
|
|
|
|
/**
|
|
* Reverses `selectionToScopes`: derives a selection from a token's granted FGA scope ids.
|
|
*
|
|
* Tokens created through the Management API can hold arbitrary scope subsets that the
|
|
* none/read/readwrite modes cannot represent exactly (e.g. a lone branching_development_create).
|
|
* Any granted scope of an entry marks it at the corresponding mode, so a partial grant is never
|
|
* dropped. The mode is an upper bound and may name specific operations the token lacks, but it
|
|
* never understates the token's authority or risk. The endpoint and MCP-tool lists, computed
|
|
* from the actual granted scopes, remain the precise view.
|
|
*/
|
|
export const scopesToSelection = (grantedScopes: string[]): PermissionSelection => {
|
|
const granted = new Set(grantedScopes)
|
|
const selection: PermissionSelection = {}
|
|
for (const entry of PERMISSION_CATALOG) {
|
|
const hasWrite = entry.writeScopes.some((scope) => granted.has(scope))
|
|
const hasRead = entry.readScopes.some((scope) => granted.has(scope))
|
|
if (hasWrite) selection[entry.key] = 'readwrite'
|
|
else if (hasRead) selection[entry.key] = 'read'
|
|
}
|
|
return selection
|
|
}
|
|
|
|
const PROJECT_SCOPE_HIDDEN_KEYS = new Set<string>([
|
|
'user:organizations',
|
|
'organization:audit_logs',
|
|
'organization:billing',
|
|
'organization:config',
|
|
'organization:members',
|
|
'organization:projects',
|
|
'organization:platform_webhooks',
|
|
'project:auth_hooks',
|
|
'project:auth_users',
|
|
'project:operations',
|
|
'project:snippets',
|
|
])
|
|
|
|
const PROJECT_SCOPE_MODES: Record<string, PermissionMode[]> = {
|
|
'organization:admin': ['none', 'readwrite'],
|
|
}
|
|
|
|
export const isEntryVisible = (key: string, resourceAccess: ResourceAccessMode): boolean =>
|
|
resourceAccess !== 'project' || !PROJECT_SCOPE_HIDDEN_KEYS.has(key)
|
|
|
|
export const getEntryModes = (
|
|
entry: PermissionCatalogEntry,
|
|
resourceAccess: ResourceAccessMode
|
|
): PermissionMode[] => {
|
|
const restricted = resourceAccess === 'project' ? PROJECT_SCOPE_MODES[entry.key] : undefined
|
|
const modes = restricted ?? ['none', 'read', 'readwrite']
|
|
return modes.filter((mode) => mode !== 'readwrite' || entry.writable)
|
|
}
|
|
|
|
export const getVisibleEntries = (resourceAccess: ResourceAccessMode): PermissionCatalogEntry[] =>
|
|
PERMISSION_CATALOG.filter((entry) => isEntryVisible(entry.key, resourceAccess))
|
|
|
|
export const getVisibleCategories = (resourceAccess: ResourceAccessMode): CategoryWithEntries[] =>
|
|
PERMISSION_CATALOG_BY_CATEGORY.map((category) => ({
|
|
...category,
|
|
entries: category.entries.filter((entry) => isEntryVisible(entry.key, resourceAccess)),
|
|
})).filter((category) => category.entries.length > 0)
|
|
|
|
export const sanitizeSelection = (
|
|
selection: PermissionSelection,
|
|
resourceAccess: ResourceAccessMode
|
|
): PermissionSelection => {
|
|
const next: PermissionSelection = {}
|
|
for (const [key, mode] of Object.entries(selection)) {
|
|
const entry = getCatalogEntry(key)
|
|
const isAllowed =
|
|
entry !== undefined &&
|
|
isEntryVisible(key, resourceAccess) &&
|
|
getEntryModes(entry, resourceAccess).includes(mode)
|
|
next[key] = isAllowed ? mode : 'none'
|
|
}
|
|
return next
|
|
}
|
|
|
|
export const countConfiguredInCategory = (
|
|
selection: PermissionSelection,
|
|
categoryKey: PermissionCategoryKey,
|
|
resourceAccess: ResourceAccessMode
|
|
): number =>
|
|
PERMISSION_CATALOG.filter(
|
|
(entry) =>
|
|
entry.category === categoryKey &&
|
|
isEntryVisible(entry.key, resourceAccess) &&
|
|
(selection[entry.key] ?? 'none') !== 'none'
|
|
).length
|
|
|
|
export const countConfigured = (
|
|
selection: PermissionSelection,
|
|
resourceAccess: ResourceAccessMode
|
|
): number =>
|
|
PERMISSION_CATALOG.filter(
|
|
(entry) =>
|
|
isEntryVisible(entry.key, resourceAccess) && (selection[entry.key] ?? 'none') !== 'none'
|
|
).length
|
|
|
|
export const RISK_LEVEL_LABEL: Record<RiskLevel, string> = {
|
|
low: 'Low risk',
|
|
medium: 'Medium risk',
|
|
high: 'High risk',
|
|
}
|
|
|
|
export type ResourceAccessMode = 'project' | 'organization' | 'account'
|
|
|
|
export const RISK_TONE_VARIANT: Record<RiskLevel, 'success' | 'warning' | 'destructive'> = {
|
|
low: 'success',
|
|
medium: 'warning',
|
|
high: 'destructive',
|
|
}
|