Files
supabase/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.presets.ts
kemal.earth 1a2e8c679b feat(studio): handle visible project permissions for scoped pat (#53227)
## 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.
2026-11-11 09:33:57 +00:00

129 lines
4.4 KiB
TypeScript

import {
getCatalogEntry,
getEntryModes,
getVisibleEntries,
type PermissionCatalogEntry,
type PermissionMode,
type PermissionSelection,
type ResourceAccessMode,
} from './AccessToken.permissions'
/**
* Bulk presets for the permission list. Setting 42 rows one at a time is the main reason people
* abandon the scoped token flow, so a preset applies a mode to every row at once and the user
* fine-tunes from there.
*
* A preset is data, not JSX: `resolve` maps a single catalog entry to a mode, and every helper
* below takes an `entries` subset (defaulting to the whole catalog) so the same presets can later
* back per-category "set all" controls, or move server-side.
*/
export type PermissionPresetId = 'none' | 'read' | 'full'
export interface PermissionPreset {
id: PermissionPresetId
label: string
/** Optional subtext, shown beneath the label in the menu. */
description?: string
/** Surfaces the description as an inline warning once applied. */
isRisky?: boolean
/** Announced in a live region once the preset is applied. */
announcement: string
resolve: (entry: PermissionCatalogEntry) => PermissionMode
}
/**
* High-risk resources named in the Full access warning. Keyed by catalog key so the copy can only
* name scopes that actually exist — AccessToken.presets.test.ts asserts every key resolves.
*/
const FULL_ACCESS_HIGH_RISK: { key: string; noun: string }[] = [
{ key: 'project:database', noun: 'your database' },
{ key: 'project:api_gateway_keys', noun: 'API keys' },
{ key: 'organization:members', noun: 'organization members' },
]
export const getFullAccessDescription = (): string => {
const nouns = FULL_ACCESS_HIGH_RISK.filter(({ key }) => getCatalogEntry(key) !== undefined).map(
({ noun }) => noun
)
if (nouns.length === 0) return 'Grants the highest access each resource offers.'
const listed =
nouns.length === 1
? nouns[0]
: `${nouns.slice(0, -1).join(', ')}, and ${nouns[nouns.length - 1]}`
return `Grants the highest access each resource offers, including write access to ${listed}.`
}
export const PERMISSION_PRESETS: PermissionPreset[] = [
{
id: 'none',
label: 'No access',
announcement: 'All permissions set to none',
resolve: () => 'none',
},
{
id: 'read',
label: 'Read-only',
announcement: 'All permissions set to read',
resolve: () => 'read',
},
{
id: 'full',
label: 'Full access',
description: getFullAccessDescription(),
isRisky: true,
announcement: 'All permissions set to read-write',
// Five resources expose no write scopes, so read is their highest level — resolving them to
// 'readwrite' would store a mode their Select can't render.
resolve: (entry) => (entry.writable ? 'readwrite' : 'read'),
},
]
export const getPreset = (id: PermissionPresetId): PermissionPreset | undefined =>
PERMISSION_PRESETS.find((preset) => preset.id === id)
export const resolvePresetMode = (
preset: PermissionPreset,
entry: PermissionCatalogEntry,
resourceAccess: ResourceAccessMode
): PermissionMode => {
const mode = preset.resolve(entry)
return getEntryModes(entry, resourceAccess).includes(mode) ? mode : 'none'
}
/** Applies a preset over `entries`, leaving any selection outside that subset untouched. */
export const applyPreset = (
preset: PermissionPreset,
selection: PermissionSelection,
resourceAccess: ResourceAccessMode,
entries: PermissionCatalogEntry[] = getVisibleEntries(resourceAccess)
): PermissionSelection => {
const next = { ...selection }
for (const entry of entries) {
next[entry.key] = resolvePresetMode(preset, entry, resourceAccess)
}
return next
}
/**
* The preset `selection` currently matches across `entries`, or null when it matches none of them
* — the "Custom" state, which is displayed but never selectable.
*/
export const getActivePresetId = (
selection: PermissionSelection,
resourceAccess: ResourceAccessMode,
entries: PermissionCatalogEntry[] = getVisibleEntries(resourceAccess)
): PermissionPresetId | null => getActivePreset(selection, resourceAccess, entries)?.id ?? null
export const getActivePreset = (
selection: PermissionSelection,
resourceAccess: ResourceAccessMode,
entries: PermissionCatalogEntry[] = getVisibleEntries(resourceAccess)
): PermissionPreset | undefined =>
PERMISSION_PRESETS.find((preset) =>
entries.every(
(entry) =>
(selection[entry.key] ?? 'none') === resolvePresetMode(preset, entry, resourceAccess)
)
)