Files
TREK/server/tests/unit/db/engine/postgres/plugin-sql-guard.test.ts
T
Maurice 636fc7d0a6 fix(db): refuse the column notation without parentheses and aclitem in plugin SQL
Postgres reads x.f as the call f(x) when x has no column f. The guard counted
that spelling only after ")." or "].", so over a function in FROM that
returns a single value, or a parameter, the allowlist was bypassed:
"select g.obj_description from generate_series(...) g" returned the comments
on every object of the database, and g.current_schemas, g.txid_status and
$1.ts_token_type passed as well. A refused catalog function after a dot is
now refused too, except the names in COLUMN_NAMES (money, name, oid, version,
xid, xml), which take no argument or only convert it to their type, so t.name
keeps working. PGPLUG-021 holds that list to pg_proc on the lane's server.

The type aclitem names roles: '=r'::aclitem printed the cluster's first
superuser and 'x=r/x'::aclitem told whether a role exists. It is refused in
every spelling like the reg* types.

The allowlist itself moves to assets/postgres/plugin-functions.txt, beside
the catalog list it is checked against. As TypeScript it spelled 'like' and
'json_each', names of Postgres functions that the SQLite dialect gate reads
as SQLite SQL.
2026-11-17 23:30:29 +00:00

347 lines
19 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The guard that keeps a plugin's own SQL inside its schema on Postgres
* (src/db/engine/postgres/plugin-sql-guard.ts), without a database: what it
* lets through, what it refuses and why, how it reads quoting and comments
* the way Postgres does, and what it tells the store about a statement. The
* same rules against a real database are PGPLUG-003/004 in the Postgres lane.
*/
import {
checkPlaceholders,
checkQualifiers,
parsePluginSql,
PluginSqlRefused,
tokenize,
type PluginTarget,
} from '../../../../../src/db/engine/postgres/plugin-sql-guard';
import { describe, expect, it } from 'vitest';
const OWN = 'trek_plugin_notes';
const SCHEMAS = new Set(['public', 'auth', 'pg_catalog', 'information_schema', OWN, 'trek_plugin_other']);
/** Parses `sql` and checks its qualified names as the store does. */
function check(sql: string): void {
for (const statement of parsePluginSql(sql)) checkQualifiers(statement, OWN, SCHEMAS);
}
describe('plugin SQL guard', () => {
it.each([
'SELECT * FROM notes WHERE id = $1',
'select n.body, t.name from notes n join tags t on t.note_id = n.id',
'INSERT INTO notes (body) VALUES ($1) RETURNING id',
"UPDATE notes SET body = 'it''s' WHERE id = 1",
'DELETE FROM notes WHERE id IN (SELECT note_id FROM trash)',
'WITH recent AS (SELECT * FROM notes ORDER BY id DESC LIMIT 5) SELECT count(*) FROM recent',
'VALUES (1), (2)',
'TABLE notes',
'(SELECT 1) UNION (SELECT 2)',
'CREATE TABLE notes (id bigserial PRIMARY KEY, body text, "temp" real, schema text)',
'CREATE UNIQUE INDEX notes_body ON notes (body)',
'CREATE UNLOGGED TABLE cache (k text)',
'CREATE OR REPLACE VIEW v AS SELECT body FROM notes',
'CREATE MATERIALIZED VIEW mv AS SELECT 1',
'CREATE SEQUENCE s',
"CREATE TYPE mood AS ENUM ('ok', 'sad')",
'ALTER TABLE notes ADD COLUMN tag text',
'DROP TABLE IF EXISTS notes CASCADE',
'TRUNCATE notes',
'REFRESH MATERIALIZED VIEW mv',
'SELECT body FROM trek_plugin_notes.notes',
'SELECT "notes"."body" FROM "notes"',
"SELECT 'public.users -- not code' AS text",
'SELECT 1 -- public.users\n',
'SELECT /* public.users */ 1',
'SELECT 1.5e3, .5, 1e-3, 10',
'INSERT INTO kv VALUES ($1, $2) ON CONFLICT (k) DO NOTHING',
// A line comment ends at CR as well as LF, as in Postgres; what follows is read as SQL.
'SELECT 1 -- public.users\r',
'SELECT 1 -- public.users\r\nFROM notes',
'SELECT\t1\f',
// Characters JavaScript reads differently are fine inside strings, quoted names and comments.
"SELECT 'a b
c\u000bd'",
'SELECT 1 AS "a b"',
'SELECT 1 /*   */',
'WITH gone AS (DELETE FROM notes RETURNING id) INSERT INTO trash SELECT id FROM gone',
'MERGE INTO notes n USING incoming i ON n.id = i.id WHEN NOT MATCHED THEN INSERT (body) VALUES (i.body)',
'SELECT body AS "into", "temporary" FROM notes',
// TEMP is refused only where it makes a temporary object; a column of that name is fine.
'CREATE TABLE readings (day text, temp real, temporary bool)',
'SELECT day, temp FROM readings WHERE temp > 1 ORDER BY temp',
'select 1 from notes as temporary',
'INSERT INTO temp (x) VALUES (1)',
// INSERT INTO in a WITH query's body or after a WITH clause is the statement's own verb.
'WITH n AS (INSERT INTO notes (body) VALUES ($1) RETURNING id) SELECT id FROM n',
'WITH n AS NOT MATERIALIZED (SELECT 1) INSERT INTO notes (body) SELECT * FROM n',
'WITH n AS MATERIALIZED (INSERT INTO notes (body) VALUES (1) RETURNING id) SELECT 1',
// Names that only start like a refused one.
'SELECT region, regexp_replace(body, $1, $2), regr_slope(1, 2) FROM notes',
'CREATE TYPE span AS RANGE (subtype = float8)',
'CREATE TABLE index (x int)',
'CREATE INDEX ON notes (body)',
])('lets through %s', (sql) => {
expect(() => check(sql)).not.toThrow();
});
it.each([
['SELECT * FROM public.users', /another schema/],
['SELECT * FROM "public"."users"', /another schema/],
['SELECT * FROM PUBLIC . users', /another schema/],
['SELECT * FROM public/**/.users', /another schema/],
['SELECT * FROM auth.secret', /another schema/],
['SELECT * FROM trek_plugin_other.notes', /another schema/],
['SELECT * FROM trek_plugin_not_yet.notes', /another schema/],
['SELECT * FROM trek.public.users', /another schema/],
['SELECT * FROM pg_stats', /"pg_stats" may not be named/],
['SELECT * FROM "pg_catalog".pg_authid', /"pg_catalog" may not be named/],
['SELECT * FROM information_schema.tables', /"information_schema" may not be named/],
["SELECT query_to_xml('select 1', true, true, '')", /"query_to_xml" may not be named/],
["SELECT database_to_xml(true, true, '')", /"database_to_xml" may not be named/],
["SELECT ts_stat('select 1')", /"ts_stat" may not be named/],
["SELECT \"set_config\"('search_path', 'public', true)", /"set_config" may not be named/],
["SELECT current_setting('search_path')", /"current_setting" may not be named/],
["SELECT nextval('users_id_seq')", /"nextval" may not be named/],
["SELECT 'users'::regclass", /"regclass" may not be named/],
["SELECT to_regclass('users')", /"to_regclass" may not be named/],
['SELECT lo_get(1)', /"lo_get" may not be named/],
["SELECT dblink('x', 'y')", /"dblink" may not be named/],
['WITH RECURSIVE r(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM r) SELECT * FROM r', /RECURSIVE/],
['SELECT 1 AS x INTO copy_of', /SELECT \.\.\. INTO/],
['CREATE TEMP TABLE t (x int)', /CREATE TEMP is not allowed/],
['CREATE TEMPORARY TABLE t (x int)', /CREATE TEMPORARY is not allowed/],
["CREATE FUNCTION f() RETURNS int LANGUAGE sql AS 'select 1'", /CREATE FUNCTION is not allowed/],
['CREATE OR REPLACE FUNCTION f() RETURNS int', /CREATE FUNCTION is not allowed/],
['CREATE SCHEMA mine', /CREATE SCHEMA is not allowed/],
['CREATE EXTENSION dblink', /CREATE EXTENSION is not allowed/],
['DROP SCHEMA trek_plugin_notes', /DROP SCHEMA is not allowed/],
['DROP OWNED BY current_user', /DROP OWNED is not allowed/],
['ALTER TABLE notes SET SCHEMA x', /may not change the schema/],
['ALTER TABLE notes OWNER TO someone', /may not change the owner/],
['ALTER ROLE trek SUPERUSER', /ALTER ROLE is not allowed/],
['CREATE TABLE t (x int) TABLESPACE pg_default', /tablespace/],
['SET search_path TO public', /may not start with SET/],
['RESET ROLE', /may not start with RESET/],
['COPY notes TO STDOUT', /may not start with COPY/],
['GRANT SELECT ON notes TO PUBLIC', /may not start with GRANT/],
['DO $$ BEGIN END $$', /dollar-quoted/],
['LISTEN x', /may not start with LISTEN/],
['PREPARE p AS SELECT 1', /may not start with PREPARE/],
['EXPLAIN ANALYZE SELECT 1', /may not start with EXPLAIN/],
['BEGIN', /transaction-control/],
['COMMIT', /transaction-control/],
['SAVEPOINT a', /transaction-control/],
["SELECT E'\\x'", /escape strings/],
["SELECT U&'\\0041'", /Unicode escapes/],
['SELECT * FROM U&"\\0070ublic".users', /Unicode escapes/],
['SELECT $tag$x$tag$', /dollar-quoted/],
['SELECT 1.public', /a number may not run straight into a name/],
["SELECT 'open", /a string is not closed/],
['SELECT "open', /a quoted name is not closed/],
['SELECT /* open', /a comment is not closed/],
['SELECT ""', /a quoted name may not be empty/],
['SELECT `x`', /unexpected character/],
['SELECT 1; SELECT * FROM public.users', /another schema/],
['SELECT 1 FROM notes INTO TEMP copy', /SELECT \.\.\. INTO/],
['SELECT 0x1F', /hexadecimal/],
['SELECT 0o17, 0b1', /hexadecimal/],
['SELECT 1_000', /digit separators/],
])('refuses %s', (sql, reason) => {
expect(() => check(sql)).toThrow(PluginSqlRefused);
expect(() => check(sql)).toThrow(reason);
});
// The two bypasses of the final review, with their variants: each was let through, and Postgres ran it.
it.each([
// A `--` comment ended by a bare CR hid the rest from the guard, which Postgres then ran.
['select 1 --\rfrom public.users', /another schema/],
['select 1 --x\rfrom public.users', /another schema/],
['select 1 --\r, email, password_hash from public.users', /another schema/],
["select 1 --\r; reset search_path; update users set role='admin'", /may not start with RESET/],
['select 1 --\r; create function f() returns int', /CREATE FUNCTION is not allowed/],
['insert into notes (body) values (1) --x\r; update public.users set role = 1', /another schema/],
['select 1 -- x\r\u000b; select 1', /U\+000B/],
// A parenthesized SELECT ... INTO, at any depth, made a temporary table that shadowed TREK's own.
["(select 1 as id, 'admin' as role into temp users)", /TEMP|SELECT \.\.\. INTO/],
["(select 99 as id, 'forged'::text as name into temp users)", /TEMP|SELECT \.\.\. INTO/],
['(select 1 into temp t)', /TEMP|SELECT \.\.\. INTO/],
['((select 1 into t))', /SELECT \.\.\. INTO/],
['(select 1) into t', /SELECT \.\.\. INTO/],
['(select 1 into trek_r_tmp_x)', /SELECT \.\.\. INTO/],
['select * from (select 1 into t) s', /SELECT \.\.\. INTO/],
['WITH x AS (SELECT 1) SELECT * INTO t FROM x', /SELECT \.\.\. INTO/],
['WITH x AS (SELECT 1 INTO t) SELECT 1', /SELECT \.\.\. INTO/],
['VALUES (1) INTO t', /SELECT \.\.\. INTO/],
['TABLE notes INTO t', /SELECT \.\.\. INTO/],
['select 1 into temporary t', /TEMPORARY|SELECT \.\.\. INTO/],
['select 1 into unlogged t', /SELECT \.\.\. INTO/],
['CREATE TABLE t AS SELECT 1 INTO u', /SELECT \.\.\. INTO/],
['CREATE GLOBAL TEMP TABLE t (x int)', /CREATE GLOBAL is not allowed|TEMP/],
['CREATE LOCAL TEMPORARY TABLE t (x int)', /CREATE LOCAL is not allowed|TEMPORARY/],
['CREATE TEMP VIEW v AS SELECT 1', /CREATE TEMP is not allowed/],
['CREATE OR REPLACE TEMP VIEW v AS SELECT 1', /CREATE TEMP is not allowed/],
['CREATE TEMPORARY SEQUENCE s', /CREATE TEMPORARY is not allowed/],
// The re-review's bypasses. A column alias named insert or merge in front of INTO made a table.
['select 1 insert into t1', /SELECT \.\.\. INTO/],
['select 1 merge into t9', /SELECT \.\.\. INTO/],
['select 1 insert into unlogged t10', /SELECT \.\.\. INTO/],
['select x as insert into t from notes', /SELECT \.\.\. INTO/],
['WITH x AS (SELECT 1) SELECT 1 insert INTO t', /SELECT \.\.\. INTO/],
['insert into notes select 1 insert into t', /SELECT \.\.\. INTO/],
['select 1 insert into temp t', /SELECT \.\.\. INTO TEMP/],
// The reg* types and functions that look an object up by a name in a string, in every spelling.
["select 'public.users'::regtype::oid", /"regtype" may not be named/],
["select cast('int4' as regtype)", /"regtype" may not be named/],
["select regtype 'text'", /"regtype" may not be named/],
[`select 'x'::"regtype"`, /"regtype" may not be named/],
["select 'english'::regconfig", /"regconfig" may not be named/],
["select 'simple'::regdictionary", /"regdictionary" may not be named/],
["select '{public.users}'::_regclass", /"_regclass" may not be named/],
["select 'x'::pg_catalog.regtype", /"pg_catalog" may not be named/],
["select has_table_privilege('public.users', 'select')", /"has_table_privilege" may not be named/],
["select currtid2('public.users', '(0,1)')", /"currtid2" may not be named/],
["select gin_clean_pending_list('public.users_idx')", /"gin_clean_pending_list" may not be named/],
["select to_regtype('int4')", /"to_regtype" may not be named/],
// aclitem looks its roles up by name and names them in its output.
["select '=r'::aclitem", /"aclitem" may not be named/],
["select cast('x=r/x' as aclitem)", /"aclitem" may not be named/],
[`select array['=r']::"aclitem"[]`, /"aclitem" may not be named/],
["select '{=r}'::_aclitem", /"_aclitem" may not be named/],
['CREATE TABLE grants (a aclitem)', /"aclitem" may not be named/],
// A base type is built from C functions, which only a superuser may do.
['CREATE TYPE fake (INPUT = int4in, OUTPUT = int4out, INTERNALLENGTH = 1000)', /composite, enum or range/],
// The protocol ends a statement's text at a NUL, in a string as anywhere else.
["select 'a\u0000' -- then\nfrom public.users", /U\+0000/],
['CREATE INDEX i (x)', /must name its table with ON/],
['ALTER TABLE', /could not be read/],
])('refuses the bypass %j', (sql, reason) => {
expect(() => check(sql)).toThrow(PluginSqlRefused);
expect(() => check(sql)).toThrow(reason);
});
it.each([
['NO-BREAK SPACE', ' ', 'U+00A0'],
['BYTE ORDER MARK', '', 'U+FEFF'],
['LINE SEPARATOR', '
', 'U+2028'],
['PARAGRAPH SEPARATOR', '
', 'U+2029'],
['IDEOGRAPHIC SPACE', ' ', 'U+3000'],
['vertical tab', '\u000b', 'U+000B'],
['NUL', '\u0000', 'U+0000'],
['escape', '\u001b', 'U+001B'],
['DEL', '\u007f', 'U+007F'],
])('refuses a %s outside a string, which JavaScript and Postgres read differently', (_name, ch, code) => {
for (const sql of [
`SELECT 1${ch}FROM public.users`,
`SELECT x${ch}FROM notes`,
`${ch}SELECT 1`,
`SELECT public${ch}.users`,
]) {
expect(() => check(sql), JSON.stringify(sql)).toThrow(PluginSqlRefused);
expect(() => check(sql), JSON.stringify(sql)).toThrow(code);
}
});
it('reads nested block comments as Postgres does, so code after the inner close is still a comment', () => {
expect(() => check('SELECT 1 /* outer /* inner */ public.users */')).not.toThrow();
expect(() => check('SELECT 1 /* one */ FROM public.users')).toThrow(/another schema/);
});
it('compares a long name the way Postgres keeps it: cut to 63 bytes', () => {
const own = `trek_plugin_${'a'.repeat(51)}`;
expect(own).toHaveLength(63);
const statement = parsePluginSql(`SELECT * FROM ${own}zzzz.notes`)[0];
expect(() => checkQualifiers(statement, own, new Set([own]))).not.toThrow();
const other = `public${'x'.repeat(70)}`;
const cut = other.slice(0, 63);
expect(() => checkQualifiers(parsePluginSql(`SELECT * FROM ${other}.t`)[0], own, new Set([cut]))).toThrow(
/another schema/,
);
});
it('splits a script at its semicolons, keeps each statement text and drops empty ones', () => {
const statements = parsePluginSql("CREATE TABLE a (x text);;\n-- note\nINSERT INTO a VALUES ('a;b');");
expect(statements.map((s) => s.text)).toEqual(['CREATE TABLE a (x text)', "INSERT INTO a VALUES ('a;b')"]);
expect(parsePluginSql('-- nothing\n;')).toEqual([]);
// What follows a CR is a statement of its own, as Postgres reads it, and is checked like any other.
expect(parsePluginSql('select 1 --x\r; select 42').map((s) => s.text)).toEqual(['select 1 --x', 'select 42']);
});
it('says whether a statement returns rows, may write, qualifies a name and how many placeholders it has', () => {
const [select] = parsePluginSql('SELECT * FROM notes WHERE id = $1 AND tag = $2');
expect(select).toMatchObject({ returnsRows: true, mayWrite: false, qualifies: false, params: 2 });
const [returning] = parsePluginSql('UPDATE notes SET body = $1 RETURNING id');
expect(returning).toMatchObject({ returnsRows: true, mayWrite: true });
const [insert] = parsePluginSql('INSERT INTO notes (body) VALUES ($1)');
expect(insert).toMatchObject({ returnsRows: false, mayWrite: true });
const [cte] = parsePluginSql('WITH gone AS (DELETE FROM notes RETURNING id) SELECT count(*) FROM gone');
expect(cte).toMatchObject({ returnsRows: true, mayWrite: true });
const [ddl] = parsePluginSql('CREATE TABLE t (x int)');
expect(ddl).toMatchObject({ returnsRows: false, mayWrite: true });
const [qualified] = parsePluginSql('SELECT n.body FROM notes n');
expect(qualified.qualifies).toBe(true);
});
it('refuses TEMP where it makes a temporary object, and says so', () => {
for (const sql of [
'CREATE TEMP TABLE t (x int)',
'CREATE TEMPORARY VIEW v AS SELECT 1',
'CREATE OR REPLACE TEMP VIEW v AS SELECT 1',
'CREATE LOCAL TEMP TABLE t (x int)',
'CREATE GLOBAL TEMPORARY TABLE t (x int)',
]) {
expect(() => check(sql), sql).toThrow(/is not allowed: a temporary object outlives the call/);
}
expect(() => check('select 1 into temp t')).toThrow(/SELECT \.\.\. INTO TEMP is not allowed: no temporary tables/);
expect(() => check('CREATE LOCAL TABLE t (x int)')).toThrow(/CREATE LOCAL is not allowed$/);
});
it('names the existing objects a statement changes, for the store to resolve in its transaction', () => {
const targets = (sql: string) => parsePluginSql(sql)[0].targets;
const rel = (...name: string[]): PluginTarget => ({ kind: 'relation', name });
expect(targets('ALTER TYPE int4 RENAME TO int4x')).toEqual([{ kind: 'type', name: ['int4'] }]);
expect(targets('DROP TYPE IF EXISTS mood, "Text" CASCADE')).toEqual([
{ kind: 'type', name: ['mood'] },
{ kind: 'type', name: ['Text'] },
]);
expect(targets('DROP TABLE IF EXISTS a, "B", trek_plugin_notes.c CASCADE')).toEqual([
rel('a'),
rel('B'),
rel('trek_plugin_notes', 'c'),
]);
expect(targets('DROP INDEX CONCURRENTLY IF EXISTS i')).toEqual([rel('i')]);
expect(targets('DROP MATERIALIZED VIEW mv')).toEqual([rel('mv')]);
expect(targets('REFRESH MATERIALIZED VIEW CONCURRENTLY mv WITH DATA')).toEqual([rel('mv')]);
expect(targets('TRUNCATE TABLE ONLY a, b * RESTART IDENTITY')).toEqual([rel('a'), rel('b')]);
expect(targets('TRUNCATE a')).toEqual([rel('a')]);
expect(targets('CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS i ON ONLY t USING btree (x)')).toEqual([rel('t')]);
expect(targets('CREATE INDEX ON t (x)')).toEqual([rel('t')]);
expect(targets('ALTER MATERIALIZED VIEW IF EXISTS mv RENAME TO mv2')).toEqual([rel('mv')]);
expect(targets('ALTER TABLE IF EXISTS ONLY t ADD CONSTRAINT f FOREIGN KEY (x) REFERENCES p (id)')).toEqual([
rel('t'),
rel('p'),
]);
expect(targets('CREATE TABLE c (x int REFERENCES p) INHERITS (q, r)')).toEqual([rel('p'), rel('q'), rel('r')]);
expect(targets('CREATE TABLE c PARTITION OF p FOR VALUES IN (1)')).toEqual([rel('p')]);
expect(targets('ALTER TABLE p ATTACH PARTITION c FOR VALUES IN (1)')).toEqual([rel('p'), rel('c')]);
expect(targets('ALTER TABLE c NO INHERIT p')).toEqual([rel('c'), rel('p')]);
expect(targets('CREATE TABLE t (x int)')).toEqual([]);
expect(targets('SELECT 1')).toEqual([]);
expect(() => parsePluginSql('DROP TABLE a.b.c.d')).toThrow(/at most three parts/);
});
it('turns a SQLite ? placeholder into a message about $1 when args are bound', () => {
const [sqliteStyle] = parsePluginSql('SELECT * FROM notes WHERE id = ?');
expect(() => checkPlaceholders(sqliteStyle, [1])).toThrow(/\$1, \$2/);
expect(() => checkPlaceholders(sqliteStyle, [])).not.toThrow();
const [pgStyle] = parsePluginSql('SELECT * FROM notes WHERE id = $1');
expect(() => checkPlaceholders(pgStyle, [1])).not.toThrow();
});
it('folds unquoted names to lower case and keeps quoted ones as written', () => {
expect(tokenize('Select "MiXed", Plain')).toEqual([
{ kind: 'word', value: 'select', quoted: false, at: 0 },
{ kind: 'word', value: 'MiXed', quoted: true, at: 7 },
{ kind: 'punct', value: ',', at: 14 },
{ kind: 'word', value: 'plain', quoted: false, at: 16 },
]);
});
});