Files
supabase/apps/studio/components/interfaces/Account/AccessTokens/AccessToken.presets.test.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

151 lines
5.8 KiB
TypeScript

import { describe, expect, test } from 'vitest'
import {
getCatalogEntry,
getVisibleEntries,
isEntryVisible,
PERMISSION_CATALOG,
PERMISSION_CATALOG_BY_CATEGORY,
sanitizeSelection,
type PermissionSelection,
} from './AccessToken.permissions'
import {
applyPreset,
getActivePresetId,
getFullAccessDescription,
getPreset,
PERMISSION_PRESETS,
} from './AccessToken.presets'
const NONE = getPreset('none')!
const READ = getPreset('read')!
const FULL = getPreset('full')!
describe('PERMISSION_PRESETS', () => {
test('offers no access, read-only and full access in that order', () => {
expect(PERMISSION_PRESETS.map((preset) => preset.id)).toEqual(['none', 'read', 'full'])
})
test('resolves every catalog entry to a mode its row can render, capping full access at read for read-only resources', () => {
expect(PERMISSION_CATALOG.some((entry) => !entry.writable)).toBe(true)
for (const entry of PERMISSION_CATALOG) {
expect(NONE.resolve(entry)).toBe('none')
expect(READ.resolve(entry)).toBe('read')
expect(FULL.resolve(entry)).toBe(entry.writable ? 'readwrite' : 'read')
}
})
test('only marks full access as risky, and only it carries a description', () => {
expect(
PERMISSION_PRESETS.filter((preset) => preset.isRisky).map((preset) => preset.id)
).toEqual(['full'])
expect(
PERMISSION_PRESETS.filter((preset) => preset.description !== undefined).map(
(preset) => preset.id
)
).toEqual(['full'])
})
})
describe('getFullAccessDescription', () => {
test('names high-risk resources that exist in the catalog', () => {
const description = getFullAccessDescription()
expect(description).toBe(
'Grants the highest access each resource offers, including write access to your database, API keys, and organization members.'
)
for (const key of ['project:database', 'project:api_gateway_keys', 'organization:members']) {
expect(getCatalogEntry(key)).toBeDefined()
expect(getCatalogEntry(key)!.risk).toBe('high')
// The copy claims write access on these specifically, so they have to be writable
expect(getCatalogEntry(key)!.writable).toBe(true)
}
})
})
describe('applyPreset', () => {
test('sets every catalog entry by default', () => {
const selection = applyPreset(READ, {}, 'organization')
expect(Object.keys(selection)).toHaveLength(PERMISSION_CATALOG.length)
expect(Object.values(selection).every((mode) => mode === 'read')).toBe(true)
})
test('overwrites existing manual choices', () => {
const selection = applyPreset(NONE, { 'project:database': 'readwrite' }, 'organization')
expect(selection['project:database']).toBe('none')
})
test('leaves entries outside the given subset untouched', () => {
const database = PERMISSION_CATALOG_BY_CATEGORY.find((category) => category.key === 'database')!
const before: PermissionSelection = { 'project:advisors': 'read' }
const selection = applyPreset(FULL, before, 'organization', database.entries)
expect(selection['project:advisors']).toBe('read')
expect(selection['project:database']).toBe('readwrite')
expect(Object.keys(selection)).toHaveLength(database.entries.length + 1)
})
})
describe('getActivePresetId', () => {
test('reads an empty selection as no access', () => {
expect(getActivePresetId({}, 'organization')).toBe('none')
})
test('identifies a selection produced by each preset', () => {
for (const preset of PERMISSION_PRESETS) {
expect(getActivePresetId(applyPreset(preset, {}, 'organization'), 'organization')).toBe(
preset.id
)
}
})
test('returns null once a single row diverges', () => {
const selection = applyPreset(READ, {}, 'organization')
selection['project:storage'] = 'readwrite'
expect(getActivePresetId(selection, 'organization')).toBeNull()
})
test('ignores rows outside the given subset', () => {
const database = PERMISSION_CATALOG_BY_CATEGORY.find((category) => category.key === 'database')!
const selection = applyPreset(READ, {}, 'organization', database.entries)
selection['project:storage'] = 'readwrite'
expect(getActivePresetId(selection, 'organization')).toBeNull()
expect(getActivePresetId(selection, 'organization', database.entries)).toBe('read')
})
})
describe('presets on project scope', () => {
test('sets only the visible rows, leaving hidden ones absent', () => {
for (const preset of PERMISSION_PRESETS) {
const selection = applyPreset(preset, {}, 'project')
const keys = Object.keys(selection)
expect(keys).toEqual(getVisibleEntries('project').map((entry) => entry.key))
expect(keys.every((key) => isEntryVisible(key, 'project'))).toBe(true)
}
})
test('never sets Organization Settings to Read', () => {
for (const preset of PERMISSION_PRESETS) {
expect(applyPreset(preset, {}, 'project')['organization:admin']).not.toBe('read')
}
expect(applyPreset(READ, {}, 'project')['organization:admin']).toBe('none')
expect(applyPreset(FULL, {}, 'project')['organization:admin']).toBe('readwrite')
expect(applyPreset(READ, {}, 'organization')['organization:admin']).toBe('read')
})
test('leaves a hidden row at none after a scope switch reset it', () => {
const configuredOnOrgScope: PermissionSelection = { 'organization:members': 'readwrite' }
const afterSwitch = sanitizeSelection(configuredOnOrgScope, 'project')
expect(applyPreset(FULL, afterSwitch, 'project')['organization:members']).toBe('none')
expect(applyPreset(READ, afterSwitch, 'project')['organization:members']).toBe('none')
})
test('still reports the applied preset as active on project scope', () => {
for (const preset of PERMISSION_PRESETS) {
expect(getActivePresetId(applyPreset(preset, {}, 'project'), 'project')).toBe(preset.id)
}
})
})