mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
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.
109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
|
|
import { databaseEventTriggerKeys } from './keys'
|
|
import type { EventTrigger } from '@/components/interfaces/Database/Triggers/EventTriggersList/EventTriggerList.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'
|
|
|
|
export type DatabaseEventTriggersGrant = GrantFor<'post', '/platform/pg-meta/{ref}/query', 'read'>
|
|
|
|
export type DatabaseEventTriggersVariables = {
|
|
projectRef?: string
|
|
grant?: DatabaseEventTriggersGrant
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export type DatabaseEventTrigger = {
|
|
oid: number
|
|
name: string
|
|
event: string
|
|
enabled_mode: 'ORIGIN' | 'ALWAYS' | 'REPLICA' | 'DISABLED'
|
|
tags: string[] | null
|
|
function_name: string | null
|
|
function_schema: string | null
|
|
owner: string | null
|
|
function_definition: string | null
|
|
}
|
|
|
|
const EVENT_TRIGGERS_SQL = safeSql`
|
|
select
|
|
evt.oid,
|
|
evt.evtname as name,
|
|
evt.evtevent as event,
|
|
case evt.evtenabled
|
|
when 'O' then 'ORIGIN'
|
|
when 'A' then 'ALWAYS'
|
|
when 'R' then 'REPLICA'
|
|
when 'D' then 'DISABLED'
|
|
end as enabled_mode,
|
|
evt.evttags as tags,
|
|
proc.proname as function_name,
|
|
namespace.nspname as function_schema,
|
|
owner.rolname as owner,
|
|
case
|
|
when proc.oid is not null then pg_get_functiondef(proc.oid)
|
|
else null
|
|
end as function_definition
|
|
from pg_event_trigger as evt
|
|
left join pg_proc as proc on proc.oid = evt.evtfoid
|
|
left join pg_namespace as namespace on namespace.oid = proc.pronamespace
|
|
left join pg_roles as owner on owner.oid = evt.evtowner
|
|
order by evt.evtname;
|
|
`
|
|
|
|
export async function getDatabaseEventTriggers(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
grant,
|
|
}: Omit<DatabaseEventTriggersVariables, 'grant'> & { grant: DatabaseEventTriggersGrant },
|
|
signal?: AbortSignal
|
|
) {
|
|
const { result } = await executeSql<DatabaseEventTrigger[]>(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sqlOrigin: 'studio',
|
|
queryName: 'database-event-triggers',
|
|
sql: EVENT_TRIGGERS_SQL,
|
|
queryKey: databaseEventTriggerKeys.list(projectRef),
|
|
grant,
|
|
},
|
|
signal
|
|
)
|
|
|
|
return result
|
|
}
|
|
|
|
export type DatabaseEventTriggersData = Awaited<ReturnType<typeof getDatabaseEventTriggers>>
|
|
export type DatabaseEventTriggersError = StudioError
|
|
|
|
function markSavedEventTriggerSafe(trigger: DatabaseEventTrigger): EventTrigger {
|
|
return trigger as EventTrigger
|
|
}
|
|
|
|
export const databaseEventTriggersQueryOptions = <TData = EventTrigger[]>(
|
|
{ projectRef, connectionString, grant }: DatabaseEventTriggersVariables,
|
|
options?: GrantedQueryCallerOptions<
|
|
EventTrigger[],
|
|
DatabaseEventTriggersError,
|
|
TData,
|
|
ReturnType<typeof databaseEventTriggerKeys.list>
|
|
>
|
|
) =>
|
|
grantedQueryOptions(
|
|
{
|
|
queryKey: databaseEventTriggerKeys.list(projectRef),
|
|
queryFn:
|
|
projectRef && grant
|
|
? ({ signal }) =>
|
|
getDatabaseEventTriggers({ projectRef, connectionString, grant }, signal).then(
|
|
(data) => data.map(markSavedEventTriggerSafe)
|
|
)
|
|
: undefined,
|
|
},
|
|
options
|
|
)
|