mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
636fc7d0a6
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.
129 lines
6.0 KiB
TypeScript
129 lines
6.0 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)',
|
|
// 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([
|
|
"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])',
|
|
"select to_tsvector('english', body) @@ plainto_tsquery('english', $1) from notes",
|
|
'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.xid, 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();
|
|
});
|
|
});
|