Files
TREK/server/tests/unit/db/engine/postgres/plugin-sql-functions.test.ts
T
Maurice c970245e0d fix(plugins): hold plugin data on Postgres to its own schema and to one contract on both engines
The plugin SQL guard now refuses what still reached outside a plugin's
schema or depended on the server:
- reads of the system columns (xmin, xmax, cmin, cmax, ctid, tableoid),
  which show transaction ids and storage layout and do not exist on
  SQLite; a value of the plugin's own may still be named xmin with AS
- the xid and xid8 types wherever the word stands for the type, since
  age() of an xid is the cluster's transaction counter
- the text search functions that coerce a text argument to regconfig, a
  catalog lookup by name, which leave the allowlist
- collations and access methods outside the set every server has, which
  could tell whether a locale or an extension exists

Every plugin transaction pins, with SET LOCAL, the settings that decide
how values print and parse (TimeZone UTC, DateStyle, the C locales, float,
bytea and xml output, the text search configuration), so a plugin's output
neither depends on nor reveals the server's configuration.

Both stores now keep one contract. Its texts and caps live once, in
plugin-data-contract.ts, and both stores throw only those: NUL in SQL,
an argument or a migration id is refused on SQLite as on Postgres,
transaction control is refused in every call on both engines (tx() is
how a batch becomes atomic), a closed store refuses every call, and the
Postgres store answers the cases better-sqlite3 names with its words.
The SQLite store moves beside the Postgres one as SqlitePluginDataStore
in db/engine/sqlite, takes its file from the backend so src/db never
imports src/nest, and no longer needs the driver exemption. A contract
test runs every violation on both stores and fails when they answer
differently. The Postgres lane holds the allowlist, the pinned settings
and the SQL plugins commonly write against a real database.
2026-11-18 13:20:07 +01:00

146 lines
6.9 KiB
TypeScript

/**
* Which of Postgres's own functions a plugin's SQL may call
* (src/db/engine/postgres/plugin-sql-functions.ts), as the guard applies it
* without a database: the catalog's functions outside the allowlist are
* refused in every spelling of a call, the allowed ones and a plugin's own
* names pass. PGPLUG-021 in the Postgres lane holds the catalog list to the
* real `pg_proc` and runs the same payloads against a database.
*/
import {
catalogFunctionNames,
CATALOG_FUNCTIONS_FILE,
pluginFunctionNames,
PLUGIN_FUNCTIONS_FILE,
} from '../../../../../src/db/engine/postgres/plugin-sql-functions';
import { parsePluginSql } from '../../../../../src/db/engine/postgres/plugin-sql-guard';
import fs from 'node:fs';
import { describe, expect, it } from 'vitest';
describe('functions a plugin may call on Postgres', () => {
it('lists the catalog functions one per line, sorted and without duplicates', () => {
const lines = fs.readFileSync(CATALOG_FUNCTIONS_FILE, 'utf8').trimEnd().split(/\r?\n/);
expect(lines.length).toBeGreaterThan(2000);
expect(lines).toEqual([...new Set(lines)].sort());
expect(lines.every((line) => /^[A-Za-z0-9_]+$/.test(line))).toBe(true);
});
it('lists the allowed functions one per line without duplicates, comments aside', () => {
const names = fs
.readFileSync(PLUGIN_FUNCTIONS_FILE, 'utf8')
.split(/\r?\n/)
.filter((line) => line !== '' && !line.startsWith('#'));
expect(names.length).toBeGreaterThan(300);
expect(names.every((line) => /^[a-z0-9_]+$/.test(line))).toBe(true);
expect(new Set(names).size).toBe(names.length);
expect(pluginFunctionNames()).toEqual(new Set(names));
});
it('allows only functions the catalog has, so a typo cannot stand in for one', () => {
const missing = [...pluginFunctionNames()].filter((name) => !catalogFunctionNames().has(name));
expect(missing).toEqual([]);
});
it.each([
// The catalog lookups by OID the review found, and their siblings.
'select obj_description(16386)',
"select obj_description(16386, 'pg_class')",
'select col_description(16386, 2)',
"select shobj_description(16384, 'pg_authid')",
'select format_type(16388, null)',
// Server and session information.
'select version()',
'select current_database()',
'select inet_server_addr(), inet_server_port()',
'select inet_client_addr()',
'select current_query()',
'select current_schemas(true)',
'select txid_current()',
'select getdatabaseencoding()',
"select has_largeobject_privilege(1, 'select')",
'select acldefault(1, 10)',
'select setseed(0.5)',
// A text search configuration is a regconfig, looked up by a name in a string.
"select to_tsvector('trek_plugin_other.cfg', 'a')",
"select to_tsvector('english', body) @@ plainto_tsquery('english', $1) from notes",
"select to_tsquery('a'), phraseto_tsquery('a'), websearch_to_tsquery('a')",
"select ts_headline('a', 'a'::tsquery)",
"select jsonb_to_tsvector('{}'::jsonb, '[\"all\"]')",
// Every spelling of the call.
'SELECT OBJ_DESCRIPTION(16386)',
'select "obj_description"(16386)',
'select obj_description /* x */ (16386)',
'select (16386::oid).obj_description',
'select (16386::oid)."obj_description"',
'select (array[16386::oid])[1].obj_description',
// The column notation after an alias or a parameter: a function in FROM
// that returns a single value, or the parameter, is the argument.
'select u.obj_description from unnest(array[16386::oid]) u',
'select g.obj_description from generate_series(1, 20000) g',
'select o.obj_description from rows from (unnest(array[16386::oid])) o',
'select $1.obj_description',
'select g."current_schemas" from unnest(array[true]) g',
'select g.txid_status from generate_series(1, 3) g',
'select n.x from notes n, unnest(array[1]) g where g.mxid_age > 0',
'select n.x from notes n where n.body = (select version())',
'create table t (v text default version())',
'insert into notes (body) values (current_database())',
// The keywords that call one without parentheses.
'select current_user',
'select session_user',
'select current_role',
'select user',
'select system_user',
'select current_catalog',
'select current_schema',
'select CURRENT_USER',
])('refuses %s', (sql) => {
expect(() => parsePluginSql(sql)).toThrow(/a plugin may not (call|read)/);
});
it.each([
// xid is the transaction id type age() reads the server's counter with; after a dot it is a cast.
"select age('3'::xid)",
'select n.xid from notes n',
'select $1.xid',
])('refuses %s as a transaction id', (sql) => {
expect(() => parsePluginSql(sql)).toThrow(/stands for a transaction id type/);
});
it.each([
"select lower(body), substring(body from 2 for 3), regexp_replace(body, 'a', 'b') from notes",
"select now(), date_trunc('day', now()), extract(epoch from now()), to_char(now(), 'YYYY')",
"select jsonb_build_object('a', 1), jsonb_set('{}'::jsonb, '{a}', '1'), json_agg(body) from notes",
'select count(*), sum(n), string_agg(body, $1), row_number() over (order by id) from notes',
'select gen_random_uuid(), md5(body), sha256(body::bytea) from notes',
'select x from generate_series(1, 5) as g(x)',
'select unnest(array[1, 2]), cardinality(array[1])',
// Text search on vectors a plugin builds itself, with no configuration to look up.
"select ts_rank(array_to_tsvector(tags), 'a & b'::tsquery), setweight(array_to_tsvector(tags), 'A') from notes",
'select body from notes where array_to_tsvector(tags) @@ $1::tsquery',
'select coalesce(a, b), nullif(a, 0), greatest(a, b), least(a, b) from notes',
"select cast(n as numeric(10, 2)), body::varchar(20), 'now'::timestamp(3) from notes",
'select current_date, current_timestamp, localtimestamp',
"select a from notes where body like ('%' || $1)",
'select * from notes tablesample system (10)',
// A plugin's own names, even where they share a refused function's name.
'CREATE TABLE version (id bigserial PRIMARY KEY, format_type text)',
'CREATE TABLE IF NOT EXISTS version (id bigint)',
'INSERT INTO version (id) VALUES (1)',
'INSERT INTO trek_plugin_notes.version (id) VALUES (1)',
'CREATE TABLE notes (id bigint, v bigint REFERENCES version (id))',
'CREATE VIEW version (a) AS SELECT 1',
'select n.version, n.user_id, "user", "current_user" from notes n',
// The refused names a table may well have as a column, after a dot.
'select n.name, n.money, n.oid, n.xml, n.version from notes n',
'select (n).name, $1.name from notes n',
"insert into notes (id, name) values (1, 'a') on conflict (id) do update set name = excluded.name",
'select n.body from trek_plugin_notes.notes n join trek_plugin_notes.version v on v.id = n.id',
'select version from notes',
'select mine(1, 2)',
"select '[1,2)'::span",
])('allows %s', (sql) => {
expect(() => parsePluginSql(sql)).not.toThrow();
});
});