mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem
Resolves FE-4688.
Core types and minting for endpoint-bound FGA grants. A `Grant<E, V>` is
minted by checking the user's v2 permissions against the generated
`fgaRequirements` map for endpoint `E` (and optional variant `V`).
Nothing consumes grants yet: the fetch wrappers that require them come
in a follow-up PR.
## Solution
### What changed
- **`lib/grants.ts`** (new)
- `Grant<E, V>`: the brand is a function type, so it's invariant in both
the endpoint and the variant. A grant only satisfies its exact endpoint
and variant.
- `GrantResult` is `loading | granted | denied | error`, and `.grant` is
defined only when granted.
- `GrantFor<Method, Path>` resolves to the grant type for an annotated
endpoint, or `never` otherwise.
- Minting functions:
- `getGrant` is pure.
- `useGrant` resolves org and project from the URL unless the scope
overrides them.
- `ensureGrant` is for imperative code and prefetch.
- Self-hosted always mints. A failed permissions request returns
`error`. Grants are never part of query keys.
- **`lib/fga-refinements.ts`** (new): an empty map defining the shape
for named variants. Each entry records the base annotation it was
written against, so typecheck fails if that annotation changes.
- **`permissions-query-v2.ts`**: adds a `permissionsQueryOptionsV2`
factory. `usePermissionsQueryV2` is now a thin wrapper around it, with
the same signature and the same 5-minute `staleTime`.
- **`doPermissionsCheckV2`**:
- Now accepts OR-of-AND requirement groups. Existing single-permission
and flat-list calls behave as before.
- Empty input never grants, including an empty group.
- **`usePermissionScopeV2`**: org/project/branch-parent resolution
extracted from the V2 hook, so `useGrant` and
`useAsyncCheckPermissionsV2` share one implementation.
- **Invalidation**: the role assign, unassign, and update mutations,
`UpdateRolesConfirmationModal`, and project creation now invalidate the
v2 permissions query.
- **knip**: temporary `files` ignore for `lib/grants.ts` until its first
consumer lands.
### Behavior change
`useAsyncCheckPermissionsV2` with an explicit `projectRef` equal to the
URL's project ref now resolves branch projects to their parent before
checking. Previously, any explicit ref skipped that resolution, so users
with project-scoped roles were denied on branch projects. An explicit
ref that differs from the URL ref is still used as-is.
## Review instructions
### Tests
- Unit tests for `doPermissionsCheckV2` with OR-of-AND groups.
- `lib/grants.test.ts`:
- `getGrant`, `useGrant`, and `ensureGrant` across OR-of-AND groups,
branch projects, explicit scope, self-hosted, and loading/denied/error
states.
- Type tests: a grant can't be used for a different endpoint, a plain
object isn't a grant, `GrantFor`, and status narrowing.
The type test that a grant without a variant can't satisfy a variant
parameter moves to the api-keys refinement PR, since no endpoint has
variants yet.
1. Run `pnpm test:studio` and confirm the `doPermissionsCheckV2` and
`lib/grants` tests pass.
2. Run `pnpm typecheck` to confirm the grant type tests hold.
3. Check the branch-project behavior in `useAsyncCheckPermissionsV2`
with an explicit `projectRef` matching the URL's project ref.
## Checklist
Check all before review:
- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { doPermissionsCheckV2 } from './useCheckPermissionsV2'
|
|
import { PermissionsV2Data } from '@/data/permissions/permissions-query-v2'
|
|
|
|
function permissions(
|
|
overrides?: Partial<PermissionsV2Data['organizations'][number]>
|
|
): PermissionsV2Data {
|
|
return {
|
|
organizations: [
|
|
{
|
|
slug: 'org-slug',
|
|
role: 'developer',
|
|
permissions: ['storage_read', 'storage_write'],
|
|
projects: [],
|
|
...overrides,
|
|
},
|
|
],
|
|
}
|
|
}
|
|
|
|
describe('doPermissionsCheckV2', () => {
|
|
it('returns false when data is missing', () => {
|
|
expect(doPermissionsCheckV2(undefined, 'storage_read', 'org-slug')).toBe(false)
|
|
})
|
|
|
|
it('returns false for an unknown organization', () => {
|
|
expect(doPermissionsCheckV2(permissions(), 'storage_read', 'other-org')).toBe(false)
|
|
})
|
|
|
|
it('grants from org level permissions', () => {
|
|
expect(doPermissionsCheckV2(permissions(), 'storage_read', 'org-slug')).toBe(true)
|
|
expect(doPermissionsCheckV2(permissions(), 'custom_domain_write', 'org-slug')).toBe(false)
|
|
})
|
|
|
|
it('org level permissions apply when a projectRef is given but has no explicit entry', () => {
|
|
expect(doPermissionsCheckV2(permissions(), 'storage_read', 'org-slug', 'some-project')).toBe(
|
|
true
|
|
)
|
|
})
|
|
|
|
it('grants from a project scoped role entry', () => {
|
|
const data = permissions({
|
|
role: 'member',
|
|
permissions: ['storage_read'],
|
|
projects: [{ ref: 'project-ref', role: 'developer', permissions: ['storage_write'] }],
|
|
})
|
|
expect(doPermissionsCheckV2(data, 'storage_write', 'org-slug', 'project-ref')).toBe(true)
|
|
// project-scoped role does not leak to other projects in the org
|
|
expect(doPermissionsCheckV2(data, 'storage_write', 'org-slug', 'other-project')).toBe(false)
|
|
// ...or to org-level checks
|
|
expect(doPermissionsCheckV2(data, 'storage_write', 'org-slug')).toBe(false)
|
|
})
|
|
|
|
it('returns false when organizationSlug is undefined', () => {
|
|
expect(doPermissionsCheckV2(permissions(), 'storage_read', undefined)).toBe(false)
|
|
})
|
|
|
|
describe('requirement groups', () => {
|
|
it('keeps flat arrays as a single AND group', () => {
|
|
expect(
|
|
doPermissionsCheckV2(permissions(), ['storage_read', 'storage_write'], 'org-slug')
|
|
).toBe(true)
|
|
expect(
|
|
doPermissionsCheckV2(permissions(), ['storage_read', 'custom_domain_write'], 'org-slug')
|
|
).toBe(false)
|
|
})
|
|
|
|
it('passes when any group is fully satisfied', () => {
|
|
const groups = [['custom_domain_write'], ['storage_read', 'storage_write']] as const
|
|
expect(doPermissionsCheckV2(permissions(), groups, 'org-slug')).toBe(true)
|
|
})
|
|
|
|
it('fails when every group misses a permission', () => {
|
|
const groups = [
|
|
['storage_read', 'custom_domain_write'],
|
|
['custom_domain_write', 'storage_write'],
|
|
] as const
|
|
expect(doPermissionsCheckV2(permissions(), groups, 'org-slug')).toBe(false)
|
|
})
|
|
|
|
it('resolves each permission in a group from the project or the org', () => {
|
|
const data = permissions({
|
|
role: 'member',
|
|
permissions: ['storage_read'],
|
|
projects: [{ ref: 'project-ref', role: 'developer', permissions: ['storage_write'] }],
|
|
})
|
|
const groups = [['storage_read', 'storage_write']] as const
|
|
expect(doPermissionsCheckV2(data, groups, 'org-slug', 'project-ref')).toBe(true)
|
|
expect(doPermissionsCheckV2(data, groups, 'org-slug', 'other-project')).toBe(false)
|
|
})
|
|
|
|
it('fails closed on an empty group, even next to a satisfied one', () => {
|
|
expect(doPermissionsCheckV2(permissions(), [[]], 'org-slug')).toBe(false)
|
|
expect(doPermissionsCheckV2(permissions(), [[], ['storage_read']], 'org-slug')).toBe(true)
|
|
expect(doPermissionsCheckV2(permissions(), [[], ['custom_domain_write']], 'org-slug')).toBe(
|
|
false
|
|
)
|
|
})
|
|
|
|
it('fails closed on an empty list', () => {
|
|
expect(doPermissionsCheckV2(permissions(), [], 'org-slug')).toBe(false)
|
|
})
|
|
})
|
|
})
|