mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## What kind of change does this PR introduce? Bug fix. ## What is the current behavior? On a project whose database server statically checks `DO` blocks before running them (Multigres does), several Studio actions fail: - Creating a table shows "Failed to update API access privileges for new table", although the table is created. - Toggling Realtime for a table fails with `EXECUTE of a runtime-built statement inside a PL/pgSQL body is not supported`. - The same error blocks editing a role or schema (rename, owner, drop) and changing column privileges. All of these come from SQL that `@supabase/pg-meta` generates. Such a server only accepts a dynamic `EXECUTE` when it can prove the statement is fixed, and two shapes in our SQL prevent that: - parentheses around the payload: `execute(format(...))` - `%s` conversions in `format()` strings, which substitute raw text (for example `%s` fed by `<oid>::regclass`, by a `string_agg(...)` subselect, or by a variable holding a table list) ## What is the new behavior? `tablePrivileges.grant/revoke`, `columnPrivileges.grant/revoke`, `publications.update`, `roles.update/remove` and `schemas.update/remove` generate SQL with no parentheses around the payload and only `%I`/`%L` conversions: - Table and column privileges name the table with `%I.%I`, taking the schema and name from `pg_class`/`pg_namespace`. - The publication update loops over the tables to drop and to add, with the same `tables` semantics as before (`undefined` keeps the tables, `null` publishes all tables, a list replaces them, bare names resolve through the search path). - `schemas.remove` picks between `drop schema … cascade` and `… restrict` with two plain statements instead of a `%s` fed by a `CASE`. - The grant and revoke format strings are built with `literal()`, so a quote in a role name can't end the string early. The statements are equivalent on regular Postgres. ## Additional context - New tests: replacing, clearing and keeping a publication's tables, switching between a table list and all tables, and a test (no database needed) that fails if the generated SQL of any of these builders goes back to `execute(...)` or `%s`. - I ran the pg-meta tests against a local Postgres 17. A set of existing snapshot tests were recorded on Postgres 14 and fail there with and without this change (for example the new `MAINTAIN` privilege in table privileges), so CI is the real check for those. The tests covering the changed SQL pass. - Some other Studio SQL is blocked on such servers for different reasons (for example creating foreign servers, and `SET` inside a `DO` block). It isn't addressed here. --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
90 lines
3.8 KiB
TypeScript
90 lines
3.8 KiB
TypeScript
import { expect, test } from 'vitest'
|
|
|
|
import pgMeta from '../src/index'
|
|
|
|
// Servers that statically check DO blocks (such as Multigres) only accept a
|
|
// dynamic EXECUTE whose statement they can prove is fixed. Without that proof
|
|
// they reject the whole block with "EXECUTE of a runtime-built statement …".
|
|
// Two shapes break the proof, and the generated SQL must avoid both:
|
|
// - parentheses around the payload: `execute(format(…))`
|
|
// - a `%s` conversion in a format string, which substitutes raw text
|
|
// `%I`, `%L` and `%%` are fine.
|
|
const expectAnalyzableExecutes = (sql: string) => {
|
|
expect(sql).not.toMatch(/\bexecute\s*\(/i)
|
|
|
|
const formatStrings = [...sql.matchAll(/\bexecute\s+format\(\s*(E?'(?:[^']|'')*')/gi)].map(
|
|
(m) => m[1]
|
|
)
|
|
for (const formatString of formatStrings) {
|
|
// Strip the allowed conversions; any `%` left over is a `%s` or another unchecked conversion.
|
|
expect(formatString.replace(/%(?:\d+\$)?[IL]|%%/g, ''), formatString).not.toContain('%')
|
|
}
|
|
return formatStrings.length
|
|
}
|
|
|
|
test('table privilege grant and revoke SQL has analyzable EXECUTEs', () => {
|
|
const grant = pgMeta.tablePrivileges.grant([
|
|
{ relationId: 1, grantee: 'anon', privilegeType: 'SELECT' },
|
|
{ relationId: 1, grantee: 'authenticated', privilegeType: 'ALL', isGrantable: true },
|
|
{ relationId: 2, grantee: 'public', privilegeType: 'INSERT' },
|
|
{ relationId: 2, grantee: "it's a role", privilegeType: 'UPDATE' },
|
|
]).sql
|
|
const revoke = pgMeta.tablePrivileges.revoke([
|
|
{ relationId: 1, grantee: 'anon', privilegeType: 'DELETE' },
|
|
{ relationId: 2, grantee: 'public', privilegeType: 'SELECT' },
|
|
]).sql
|
|
|
|
expect(expectAnalyzableExecutes(grant)).toBe(4)
|
|
expect(expectAnalyzableExecutes(revoke)).toBe(2)
|
|
// A quote in the grantee can't break out of the format string.
|
|
expect(grant).toContain(`"it''s a role"`)
|
|
})
|
|
|
|
test('publication update SQL has analyzable EXECUTEs', () => {
|
|
const variants = [
|
|
{},
|
|
{ tables: null },
|
|
{ tables: [] },
|
|
{ tables: ['public.t', 'bare', 'a.b.c'] },
|
|
{ tables: ['My Schema.Odd Table', `it's."q"`] },
|
|
{ name: 'renamed', owner: 'postgres', publish_insert: true },
|
|
]
|
|
for (const params of variants) {
|
|
const { sql } = pgMeta.publications.update(1, params)
|
|
expect(expectAnalyzableExecutes(sql), JSON.stringify(params)).toBeGreaterThan(0)
|
|
}
|
|
})
|
|
|
|
test('column privilege grant and revoke SQL has analyzable EXECUTEs', () => {
|
|
const grant = pgMeta.columnPrivileges.grant([
|
|
{ columnId: '1.1', grantee: 'anon', privilegeType: 'SELECT' },
|
|
{ columnId: '1.2', grantee: 'public', privilegeType: 'UPDATE', isGrantable: true },
|
|
]).sql
|
|
const revoke = pgMeta.columnPrivileges.revoke([
|
|
{ columnId: '1.1', grantee: "it's a role", privilegeType: 'SELECT' },
|
|
]).sql
|
|
|
|
expect(expectAnalyzableExecutes(grant)).toBe(2)
|
|
expect(expectAnalyzableExecutes(revoke)).toBe(1)
|
|
expect(revoke).toContain(`"it''s a role"`)
|
|
})
|
|
|
|
test('schema update and remove SQL has analyzable EXECUTEs', () => {
|
|
expect(
|
|
expectAnalyzableExecutes(pgMeta.schemas.update({ id: 1 }, { name: 'a', owner: 'b' }).sql)
|
|
).toBe(2)
|
|
// Both branches of the cascade choice are present, each with a fixed statement.
|
|
expect(expectAnalyzableExecutes(pgMeta.schemas.remove({ id: 1 }, { cascade: true }).sql)).toBe(2)
|
|
expect(expectAnalyzableExecutes(pgMeta.schemas.remove({ name: 's' }).sql)).toBe(2)
|
|
})
|
|
|
|
test('role update and remove SQL has analyzable EXECUTEs', () => {
|
|
const update = pgMeta.roles.update(
|
|
{ id: 1 },
|
|
{ name: 'renamed', canLogin: true, connectionLimit: 5, validUntil: '2030-01-01' }
|
|
).sql
|
|
expect(expectAnalyzableExecutes(update)).toBe(2)
|
|
expect(expectAnalyzableExecutes(pgMeta.roles.update({ id: 1 }, { canLogin: false }).sql)).toBe(1)
|
|
expect(expectAnalyzableExecutes(pgMeta.roles.remove({ id: 1 }, { ifExists: true }).sql)).toBe(1)
|
|
})
|