Files
supabase/apps/studio/data/database-triggers/database-triggers-query.ts
Charis 115a2ffeef feat(studio): gate database functions, triggers, and webhooks on sql grants (#53269)
Resolves FE-4744. Part of the grants rollout; builds on the read/write
SQL variants from #53185.

## What

- **Data layer:** the database functions, triggers, database webhooks,
and event triggers queries now take a `read` grant (exposed as
`queryOptions` factories). All 8 create/update/delete mutations take a
`write` grant and mark their SQL with `asWriteSql`.
- **Guards:** the hand-written FGA checks in the functions, triggers,
event triggers, and database webhooks lists, the functions page, the
triggers layout/route, and the webhooks integration tab now derive from
grants, with denied and error states.
- **Other consumers** updated to pass grants: `CreateFunction`,
`TriggerSheet`, `EditHookPanel`, `DeleteHookModal`,
`ChooseFunctionForm`, `FunctionSelector`, `AutoEnableRLSNotice`, and SQL
editor definition/autocomplete hooks.

## Behavior changes

- **Webhooks integration tab:** was gated on
`database_webhooks_config_read`; now gated on `database_read`, which is
what the backend checks for the pg-meta query it uses. Both permissions
are granted to the same built-in roles, so there is no change for
built-in roles. A permissions-load failure now shows an error alert
instead of the content.
- **`AutoEnableRLSNotice`:** hidden unless the user has read access,
since it can't tell whether the trigger already exists without it.

## Out of scope

- User-authored SQL execution in `QueryEditor` / `useSqlEditorExecution`
stays `unguarded.migrating` (FE-4750).

## Testing

- New tests: query factories and write mutations
(`data/database-sql-grants.test.ts`), webhooks tab granted/denied/error,
functions list read/write granted/denied/error.
- Typecheck, Prettier, lint ratchet, and Studio vitest suites for
affected areas pass.
2026-11-12 14:58:01 -05:00

106 lines
3.1 KiB
TypeScript

import pgMeta, { type PGTrigger } from '@supabase/pg-meta'
import { databaseTriggerKeys } from './keys'
import type { PostgresTrigger } from '@/components/interfaces/Database/Triggers/TriggersList/TriggerList.utils'
import { grantedQueryOptions, type GrantedQueryCallerOptions } from '@/data/granted-query-options'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { GrantFor } from '@/lib/grants'
function markSavedTriggerSafe(trigger: DatabaseTriggersData[number]): PostgresTrigger {
return trigger as PostgresTrigger
}
export type DatabaseTriggersGrant = GrantFor<'post', '/platform/pg-meta/{ref}/query', 'read'>
export type DatabaseTriggersVariables = {
projectRef?: string
grant?: DatabaseTriggersGrant
connectionString?: string | null
schemas?: string[]
}
export async function getDatabaseTriggers(
{
projectRef,
connectionString,
schemas,
grant,
}: Omit<DatabaseTriggersVariables, 'grant'> & { grant: DatabaseTriggersGrant },
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { sql } = pgMeta.triggers.list({ includedSchemas: schemas })
const { result } = await executeSql(
{
projectRef,
connectionString,
sqlOrigin: 'studio',
queryName: 'triggers',
sql,
queryKey: ['triggers'],
grant,
},
signal
)
return result as PGTrigger[]
}
export type DatabaseTriggersData = Awaited<ReturnType<typeof getDatabaseTriggers>>
export type DatabaseTriggersError = StudioError
export const databaseHooksQueryOptions = (
{ projectRef, connectionString, schemas, grant }: DatabaseTriggersVariables,
options?: GrantedQueryCallerOptions<
DatabaseTriggersData,
DatabaseTriggersError,
DatabaseTriggersData,
ReturnType<typeof databaseTriggerKeys.list>
>
) =>
grantedQueryOptions(
{
queryKey: databaseTriggerKeys.list(projectRef, schemas),
queryFn:
projectRef && grant
? ({ signal }) => getDatabaseTriggers({ projectRef, connectionString, grant }, signal)
: undefined,
},
{
select: (data): DatabaseTriggersData => {
return data.filter((trigger) => {
return (
trigger.function_schema === 'supabase_functions' &&
(trigger.schema !== 'net' || trigger.function_args.length === 0)
)
})
},
...options,
}
)
export const databaseTriggersQueryOptions = <TData = PostgresTrigger[]>(
{ projectRef, connectionString, grant }: DatabaseTriggersVariables,
options?: GrantedQueryCallerOptions<
PostgresTrigger[],
DatabaseTriggersError,
TData,
ReturnType<typeof databaseTriggerKeys.list>
>
) =>
grantedQueryOptions(
{
queryKey: databaseTriggerKeys.list(projectRef),
queryFn:
projectRef && grant
? ({ signal }) =>
getDatabaseTriggers({ projectRef, connectionString, grant }, signal).then((data) =>
data.map(markSavedTriggerSafe)
)
: undefined,
},
options
)