mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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.
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
import pgMeta, { type SafeSqlFragment } from '@supabase/pg-meta'
|
|
import { z } from 'zod'
|
|
|
|
import { databaseKeys } from '@/data/database/keys'
|
|
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 DatabaseFunctionsGrant = GrantFor<'post', '/platform/pg-meta/{ref}/query', 'read'>
|
|
|
|
export type DatabaseFunctionsVariables = {
|
|
projectRef?: string
|
|
grant?: DatabaseFunctionsGrant
|
|
connectionString?: string | null
|
|
schema?: string
|
|
}
|
|
|
|
export type DatabaseFunction = z.infer<typeof pgMeta.functions.pgFunctionZod>
|
|
export type SavedDatabaseFunction = Omit<
|
|
DatabaseFunction,
|
|
| 'complete_statement'
|
|
| 'argument_types'
|
|
| 'identity_argument_types'
|
|
| 'return_type'
|
|
| 'config_params'
|
|
> & {
|
|
complete_statement: SafeSqlFragment
|
|
argument_types: SafeSqlFragment
|
|
identity_argument_types: SafeSqlFragment
|
|
return_type: SafeSqlFragment
|
|
config_params: Record<string, SafeSqlFragment> | null
|
|
type: 'function' | 'procedure'
|
|
}
|
|
|
|
export async function getDatabaseFunctions(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
schema,
|
|
grant,
|
|
}: Omit<DatabaseFunctionsVariables, 'grant'> & { grant: DatabaseFunctionsGrant },
|
|
signal?: AbortSignal,
|
|
headersInit?: HeadersInit
|
|
) {
|
|
let headers = new Headers(headersInit)
|
|
|
|
const pgMetaFunctionsList = pgMeta.functions.list({
|
|
includedSchemas: schema ? [schema] : undefined,
|
|
})
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sqlOrigin: 'studio',
|
|
queryName: 'database-functions',
|
|
sql: pgMetaFunctionsList.sql,
|
|
queryKey: ['database-functions'],
|
|
grant,
|
|
},
|
|
signal,
|
|
headers
|
|
)
|
|
|
|
return result as SavedDatabaseFunction[]
|
|
}
|
|
|
|
export type DatabaseFunctionsData = Awaited<ReturnType<typeof getDatabaseFunctions>>
|
|
export type DatabaseFunctionsError = StudioError
|
|
|
|
export const databaseFunctionsQueryOptions = <TData = DatabaseFunctionsData>(
|
|
vars: DatabaseFunctionsVariables,
|
|
options?: GrantedQueryCallerOptions<
|
|
DatabaseFunctionsData,
|
|
DatabaseFunctionsError,
|
|
TData,
|
|
ReturnType<typeof databaseKeys.databaseFunctions>
|
|
>
|
|
) => {
|
|
const { projectRef, connectionString, schema, grant } = vars
|
|
return grantedQueryOptions(
|
|
{
|
|
queryKey: databaseKeys.databaseFunctions(projectRef, schema),
|
|
queryFn:
|
|
projectRef && grant
|
|
? ({ signal }) =>
|
|
getDatabaseFunctions({ projectRef, connectionString, schema, grant }, signal)
|
|
: undefined,
|
|
},
|
|
options
|
|
)
|
|
}
|