From f1ea716305d5e6b4ece45cf9a300d5712e44d353 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 22:28:38 +0100 Subject: [PATCH] fix(db): hold plugin SQL on Postgres in layers the database enforces The token guard kept leaking: on a superuser connection an unqualified `alter type int4 rename to int4x` renamed pg_catalog's type for the whole server, sequences and catalog-only objects grew the database past the quota, regtype/regconfig/regdictionary looked TREK's objects up by name, and a column alias named insert or merge in front of INTO made a table. Instead of a longer word list, the store now asks the database: - No plugin SQL while TREK's role is privileged in the sense of the restore check (PRIVILEGED_ROLE_SQL, imported from server-checks.ts): every db:own call fails with PRIVILEGED_ROLE_PLUGIN_REFUSAL, read once per pool connection, and the boot log says why. - The objects an ALTER, DROP, REFRESH, TRUNCATE or CREATE INDEX ... ON changes, and the tables a CREATE or ALTER TABLE ties to its own, are resolved with to_regclass/to_regtype in the transaction right before the statement, and it is refused unless each one found lies in the plugin's schema. An unqualified int4, text or box is pg_catalog's and refused even with the role check off. - The quota counts every relation of the schema with its TOAST table, and a schema holds at most PLUGIN_SCHEMA_MAX_OBJECTS catalog objects (relations, types, constraints, enum labels). - The guard refuses every reg* type and its array in every spelling, the has_*_privilege functions, currtid2 and the BRIN/GIN maintenance functions; INTO only after the INSERT or MERGE that is the statement's verb or a WITH body's; TEMP only where it makes a temporary object (CREATE TEMP/TEMPORARY, SELECT ... INTO TEMP), so a column named temp works again; a base type; and a NUL anywhere in the SQL text. - A bound value or a migration id holding NUL is refused with the reason instead of being stripped, so the plugin's keys never change meaning. Every bypass from the re-review is a test on the real database, on the role the plugin data is meant to run as and on a superuser connection with the role check off. --- .../db/engine/postgres/plugin-data-checks.ts | 161 ++++++++ .../db/engine/postgres/plugin-data-store.ts | 102 +++-- .../db/engine/postgres/plugin-sql-guard.ts | 312 +++++++++++++-- server/src/db/nul-free.ts | 9 +- .../nest/plugins/host/plugin-data.backend.ts | 31 +- server/tests/postgres/plugin-data.test.ts | 373 +++++++++++++++--- server/tests/postgres/sqlite-move.test.ts | 9 +- .../engine/postgres/plugin-data-store.test.ts | 101 ++++- .../engine/postgres/plugin-sql-guard.test.ts | 93 ++++- .../unit/plugins/plugin-data-backend.test.ts | 57 ++- 10 files changed, 1097 insertions(+), 151 deletions(-) create mode 100644 server/src/db/engine/postgres/plugin-data-checks.ts diff --git a/server/src/db/engine/postgres/plugin-data-checks.ts b/server/src/db/engine/postgres/plugin-data-checks.ts new file mode 100644 index 000000000..2e6a09651 --- /dev/null +++ b/server/src/db/engine/postgres/plugin-data-checks.ts @@ -0,0 +1,161 @@ +import type { PluginSqlClient } from './plugin-data-store'; +import { PluginSqlRefused, type PluginStatement } from './plugin-sql-guard'; +import { PRIVILEGED_ROLE_DESCRIPTION, PRIVILEGED_ROLE_SQL } from './server-checks'; + +/** + * The checks the Postgres plugin store (`plugin-data-store.ts`) asks the + * database itself, behind the token guard (`plugin-sql-guard.ts`): whether + * TREK's role may run plugin SQL at all, which schema the objects a statement + * changes really live in, and how much the plugin's schema holds. + */ + +/** + * Why plugin databases are refused while TREK connects as a privileged role: + * every statement a plugin sends runs as that role, and such a role may + * change the system catalogs or reach the database host, which no check on + * the SQL can rule out. + */ +export const PRIVILEGED_ROLE_PLUGIN_REFUSAL = + `Plugin databases (db:own) are refused on Postgres while TREK_DB_URL connects as ${PRIVILEGED_ROLE_DESCRIPTION}: ` + + "a plugin's SQL runs as TREK's role. Connect TREK as a role that owns its database and is not a superuser " + + '(see the wiki page "Database: Postgres").'; + +/** Asks whether TREK's role is privileged before a plugin call runs on a connection. */ +export interface PluginRoleGate { + /** Throws {@link PRIVILEGED_ROLE_PLUGIN_REFUSAL} when the role `client` is connected as is privileged. */ + refuseIfPrivileged(client: PluginSqlClient): Promise; +} + +/** + * The role check of {@link PRIVILEGED_ROLE_SQL}, read once per pool + * connection and kept for as long as that connection lives. A new connection, + * after a restart, a reconnect or the pool replacing one, reads it again, so a + * role that gained or lost a privilege is seen without a restart once the pool + * has turned over; the check costs one query per connection, not per call. + */ +export class PrivilegedRoleGate implements PluginRoleGate { + private readonly answers = new WeakMap>(); + + async refuseIfPrivileged(client: PluginSqlClient): Promise { + if (await this.privileged(client)) throw new Error(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + } + + /** Whether the role `client` is connected as is privileged; a failed read is not kept. */ + privileged(client: PluginSqlClient): Promise { + let answer = this.answers.get(client); + if (answer === undefined) { + answer = client + .query(`select (${PRIVILEGED_ROLE_SQL}) as privileged`) + .then((result) => (result.rows as Array<{ privileged?: boolean }>)[0]?.privileged === true); + void answer.catch(() => this.answers.delete(client)); + this.answers.set(client, answer); + } + return answer; + } +} + +/** + * Refuses `statement` unless every object it changes ({@link PluginStatement.targets}) + * that exists lies in `schema`. Run inside the call's transaction right + * before the statement, so it sees what the statements before it made, + * renamed or dropped. Postgres resolves each name itself with `to_regclass` + * or `to_regtype` on the plugin's `search_path`, where `pg_catalog` is + * searched first: an unqualified `int4`, `text` or `box` is the catalog's + * type and refused here, whatever the role may do. A name that resolves to + * nothing is left to the statement. + */ +export async function refuseForeignTargets( + client: PluginSqlClient, + statement: PluginStatement, + schema: string, +): Promise { + if (statement.targets.length === 0) return; + const result = await client.query({ + text: + 'select t.name, n.nspname from unnest($1::text[], $2::text[]) as t(name, kind) ' + + 'join pg_namespace n on n.oid = case t.kind ' + + "when 'type' then (select typnamespace from pg_type where oid = to_regtype(t.name)) " + + 'else (select relnamespace from pg_class where oid = to_regclass(t.name)) end ' + + 'where n.nspname <> $3', + values: [ + statement.targets.map((t) => t.name.map(quoteName).join('.')), + statement.targets.map((t) => t.kind), + schema, + ], + }); + const [foreign] = result.rows as Array<{ name: string; nspname: string }>; + if (foreign !== undefined) { + throw new PluginSqlRefused( + `${foreign.name} is an object of the schema "${foreign.nspname}"; a plugin changes its own objects only`, + ); + } +} + +/** `part` as a quoted identifier, as `to_regclass` and `to_regtype` read it. */ +function quoteName(part: string): string { + return `"${part.replaceAll('"', '""')}"`; +} + +/** + * The most catalog objects one plugin schema may hold: its relations (tables, + * indexes, sequences, views, materialized views), types (each table brings its + * row type and that type's array), constraints and enum labels. An empty + * table, a view or an enum label takes no disk space the quota sees but adds + * rows to the system catalogs, so their number is held on its own. + */ +export const PLUGIN_SCHEMA_MAX_OBJECTS = 5_000; + +/** What a plugin schema holds, as the quota measures it. */ +export interface SchemaUsage { + /** Bytes on disk of every relation of the schema and of each one's TOAST table. */ + bytes: number; + /** Catalog objects, as {@link PLUGIN_SCHEMA_MAX_OBJECTS} counts them. */ + objects: number; +} + +/** + * The disk space and the catalog objects of `schema`. Every relation is + * counted once, whatever its kind (tables, partitions, indexes, sequences, + * materialized views; a view has no storage), each with all its forks, and + * a table's TOAST table, which lives in `pg_toast`, through its + * `reltoastrelid` with the TOAST table's own index. + */ +export async function schemaUsage(client: PluginSqlClient, schema: string): Promise { + const result = await client.query({ + text: + 'select coalesce((select sum(' + + "pg_relation_size(c.oid, 'main') + pg_relation_size(c.oid, 'fsm') + pg_relation_size(c.oid, 'vm') + " + + "pg_relation_size(c.oid, 'init') + " + + 'case when c.reltoastrelid <> 0 then pg_total_relation_size(c.reltoastrelid) else 0 end) ' + + 'from pg_class c where c.relnamespace = n.oid), 0) as bytes, ' + + '(select count(*) from pg_class c where c.relnamespace = n.oid) + ' + + '(select count(*) from pg_type t where t.typnamespace = n.oid) + ' + + '(select count(*) from pg_constraint k where k.connamespace = n.oid) + ' + + '(select count(*) from pg_enum e join pg_type t on t.oid = e.enumtypid where t.typnamespace = n.oid) as objects ' + + 'from pg_namespace n where n.nspname = $1', + values: [schema], + }); + const [row] = result.rows as Array<{ bytes?: number | string; objects?: number | string }>; + return { bytes: Number(row?.bytes ?? 0), objects: Number(row?.objects ?? 0) }; +} + +/** + * Refuses a plugin's bound value that holds U+0000 (NUL): Postgres text + * cannot store the character, and TREK will not change what a plugin asked + * to store or compare (`'a\0b'` and `'ab'` are different keys on SQLite). + * A string, or a string anywhere in an array, is checked; a Buffer goes to + * `bytea`, which holds any byte. + */ +export function refuseNulValues(values: readonly unknown[]): void { + if (values.some(holdsNul)) { + throw new Error( + 'a plugin argument holds the character U+0000 (NUL), which Postgres text cannot store; ' + + 'TREK refuses it instead of changing the value. Remove it, or bind binary data as a Buffer (bytea)', + ); + } +} + +function holdsNul(value: unknown): boolean { + if (typeof value === 'string') return value.includes('\u0000'); + return Array.isArray(value) && value.some(holdsNul); +} diff --git a/server/src/db/engine/postgres/plugin-data-store.ts b/server/src/db/engine/postgres/plugin-data-store.ts index 3ea7eb25c..aeb131627 100644 --- a/server/src/db/engine/postgres/plugin-data-store.ts +++ b/server/src/db/engine/postgres/plugin-data-store.ts @@ -1,5 +1,13 @@ -import { nulFreeParameter } from '../../nul-free'; import { PLUGIN_DATA_CAPS, type PluginDataStore, type PluginTxOp, type PluginTxResult } from '../plugin-data'; +import { + PLUGIN_SCHEMA_MAX_OBJECTS, + PrivilegedRoleGate, + refuseForeignTargets, + refuseNulValues, + schemaUsage, + type PluginRoleGate, + type SchemaUsage, +} from './plugin-data-checks'; import { isPluginId, pluginSchemaName, quotePluginSchema } from './plugin-schema'; import { checkPlaceholders, checkQualifiers, parsePluginSql, type PluginStatement } from './plugin-sql-guard'; @@ -12,19 +20,27 @@ import Cursor from 'pg-cursor'; * * Every call checks a `pg` client out of the pool for itself * ({@link poolClients}) and runs one transaction on it directly, not through - * Kysely. The transaction first makes sure the schema and its migration table - * exist, then puts only that schema on the `search_path`, with `pg_temp` - * explicitly last so a temporary table is never found before the plugin's - * own, and sets `statement_timeout` to the call's time budget; every setting - * is `local` and ends with the transaction. The plugin's SQL is checked by - * `plugin-sql-guard.ts` before it runs, so it cannot name another schema, and - * each statement goes to Postgres on its own over the extended query - * protocol, which runs one statement and no more: a write as a query with - * `queryMode: 'extended'`, which `pg` honours even without values, and a read - * through a `pg-cursor`, which always uses that protocol. Reads stop at the - * row cap. A call that may write measures the schema's size before and - * after, and rolls back one that leaves it past the quota and larger than it - * found it, so a plugin at its quota can still delete. Before the client goes + * Kysely. Before the transaction it refuses the call outright while TREK's + * role is privileged (`PrivilegedRoleGate` in `plugin-data-checks.ts`, read + * once per connection). The transaction first makes sure the schema and its + * migration table exist, then puts only that schema on the `search_path`, + * with `pg_temp` explicitly last so a temporary table is never found before + * the plugin's own, and sets `statement_timeout` to the call's time budget; + * every setting is `local` and ends with the transaction. The plugin's SQL is + * checked by `plugin-sql-guard.ts` before it runs, so it cannot name another + * schema; right before each statement, the existing objects it changes are + * resolved by Postgres and must lie in the plugin's schema + * (`refuseForeignTargets`); and each statement goes to Postgres on its own + * over the extended query protocol, which runs one statement and no more: a + * write as a query with `queryMode: 'extended'`, which `pg` honours even + * without values, and a read through a `pg-cursor`, which always uses that + * protocol. A bound value or a migration id holding U+0000 is refused before + * anything runs, since Postgres text cannot hold it and changing it would + * change the plugin's data. Reads stop at the row cap. A call that may write + * measures the schema before and after (`schemaUsage`: the bytes of every + * relation with its TOAST table, and the catalog objects), and rolls back one + * that leaves it past the quota or the object cap and larger than it found + * it, so a plugin at either limit can still delete. Before the client goes * back to the pool, whatever session state a statement could have left on it * is dropped ({@link END_SESSION}); a client that cannot be brought back to a * clean state is closed instead of reused. @@ -47,6 +63,8 @@ interface PostgresPluginDataLimits { maxRows: number; timeBudgetMs: number; quotaBytes: number; + /** The most catalog objects the schema may hold (`PLUGIN_SCHEMA_MAX_OBJECTS`). */ + maxObjects: number; /** The clock the time budget reads. */ now: () => number; } @@ -55,6 +73,7 @@ const DEFAULT_LIMITS: PostgresPluginDataLimits = { maxRows: PLUGIN_DATA_CAPS.maxRows, timeBudgetMs: PLUGIN_DATA_CAPS.timeBudgetMs, quotaBytes: PLUGIN_DATA_CAPS.quotaBytes, + maxObjects: PLUGIN_SCHEMA_MAX_OBJECTS, now: () => Date.now(), }; @@ -166,6 +185,7 @@ export class PostgresPluginDataStore implements PluginDataStore { private readonly connect: ConnectPluginClient, private readonly slots: PluginDataSlots, limits: Partial = {}, + private readonly roleGate: PluginRoleGate = new PrivilegedRoleGate(), ) { this.schema = pluginSchemaName(pluginId); this.quoted = quotePluginSchema(this.schema); @@ -242,6 +262,7 @@ export class PostgresPluginDataStore implements PluginDataStore { /** No time budget, as on SQLite: a migration stopped part of the way would fail the same way on every start. */ async migrate(id: string, sql: string): Promise<{ applied: boolean }> { const statements = this.parse(sql); + refuseNulValues([id]); return this.enqueue(() => this.transaction(null, statements, async (client) => { const seen = await client.query({ @@ -264,6 +285,7 @@ export class PostgresPluginDataStore implements PluginDataStore { const statements = this.parse(sql); if (statements.length !== 1) throw new Error('sql must hold exactly one statement here; use exec() for a script'); checkPlaceholders(statements[0], args); + refuseNulValues(args); return statements[0]; } @@ -327,6 +349,7 @@ export class PostgresPluginDataStore implements PluginDataStore { const client = await this.connect(); let reusable = true; try { + await this.roleGate.refuseIfPrivileged(client); await client.query('begin'); try { const result = await this.inTransaction(client, timeoutMs, statements, work); @@ -368,17 +391,29 @@ export class PostgresPluginDataStore implements PluginDataStore { for (const statement of statements) checkQualifiers(statement, this.schema, schemas); } const writes = statements.some((s) => s.mayWrite); - const before = writes ? await this.sizeBytes(client) : 0; + const before = writes ? await schemaUsage(client, this.schema) : null; const result = await work(client); - if (writes) { - const after = await this.sizeBytes(client); - if (after > this.limits.quotaBytes && after > before) { - throw new Error(`the plugin database is past its ${this.limits.quotaBytes / 1024 / 1024} MB quota`); - } - } + if (before !== null) this.checkQuota(before, await schemaUsage(client, this.schema)); return result; } + /** + * Refuses a call that left the schema past its quota or its object cap and + * grew it there, so a plugin at either limit can still delete. + */ + private checkQuota(before: SchemaUsage, after: SchemaUsage): void { + const { quotaBytes, maxObjects } = this.limits; + if (after.bytes > quotaBytes && after.bytes > before.bytes) { + throw new Error(`the plugin database is past its ${quotaBytes / 1024 / 1024} MB quota`); + } + if (after.objects > maxObjects && after.objects > before.objects) { + throw new Error( + `the plugin database holds more than ${maxObjects} catalog objects (tables, indexes, sequences, views, ` + + 'types, constraints, enum labels)', + ); + } + } + private async setTimeout(client: PluginSqlClient, ms: number): Promise { await client.query({ text: "select set_config('statement_timeout', $1, true)", @@ -392,30 +427,19 @@ export class PostgresPluginDataStore implements PluginDataStore { return new Set((result.rows as Array<{ nspname: string }>).map((row) => row.nspname)); } - /** What this plugin's tables, indexes and materialized views take on disk. */ - private async sizeBytes(client: PluginSqlClient): Promise { - const result = await client.query({ - text: - 'select coalesce(sum(pg_total_relation_size(c.oid)), 0) as bytes from pg_class c ' + - "join pg_namespace n on n.oid = c.relnamespace where n.nspname = $1 and c.relkind in ('r', 'm')", - values: [this.schema], - }); - const [row] = result.rows as Array<{ bytes: number | string }>; - return Number(row?.bytes ?? 0); - } - /** * Runs a statement for its effect: the rows it changed, or 0 for a read, * whose rows are read past and dropped. Both go over the extended protocol: * a write as an extended query, a read through a cursor. */ private async run(client: PluginSqlClient, statement: PluginStatement, args: readonly unknown[]): Promise { + await refuseForeignTargets(client, statement, this.schema); if (!statement.mayWrite) { // Read to the end so the statement runs whole; its rows are not wanted. - await readCursor(client.query(new Cursor(statement.text, boundValues(args))), () => undefined); + await readCursor(client.query(new Cursor(statement.text, [...args])), () => undefined); return 0; } - const result = await client.query({ text: statement.text, values: boundValues(args), queryMode: 'extended' }); + const result = await client.query({ text: statement.text, values: [...args], queryMode: 'extended' }); return result.rowCount ?? 0; } @@ -430,8 +454,9 @@ export class PostgresPluginDataStore implements PluginDataStore { budget: RowBudget, checkTime: () => void, ): Promise { + await refuseForeignTargets(client, statement, this.schema); const rows: unknown[] = []; - await readCursor(client.query(new Cursor(statement.text, boundValues(args))), (chunk) => { + await readCursor(client.query(new Cursor(statement.text, [...args])), (chunk) => { if (rows.length > 0) checkTime(); for (const row of chunk) { rows.push(row); @@ -443,11 +468,6 @@ export class PostgresPluginDataStore implements PluginDataStore { } } -/** A plugin's args as they are bound: without NUL, which Postgres text cannot hold (`nul-free.ts`). */ -function boundValues(args: readonly unknown[]): unknown[] { - return args.map(nulFreeParameter); -} - /** * Reads `cursor` to its end a chunk at a time, handing each chunk to `take` * (which may throw to stop the read), and closes it however the read ends. diff --git a/server/src/db/engine/postgres/plugin-sql-guard.ts b/server/src/db/engine/postgres/plugin-sql-guard.ts index 01ea571f4..e12b1797a 100644 --- a/server/src/db/engine/postgres/plugin-sql-guard.ts +++ b/server/src/db/engine/postgres/plugin-sql-guard.ts @@ -7,19 +7,40 @@ import { PLUGIN_SCHEMA_PREFIX } from './plugin-schema'; * TREK's tables at all. On Postgres its schema sits in TREK's database and * its statements run as TREK's role, which may read every table TREK has: a * role per plugin would need CREATEROLE, which TREK's role is not given. So - * three layers keep it in, each enough on its own for what it covers: + * the store (`plugin-data-store.ts`) keeps it in with layers that each hold + * on their own for what they cover, the ones after this guard enforced by + * the database itself: * - * 1. This guard holds the statement to what cannot leave the schema, before - * it runs. - * 2. The store sends every statement on its own over the extended query - * protocol, on a pool client it checked out for the call - * (`plugin-data-store.ts`): Postgres parses exactly one statement there, - * so a second statement the guard missed is an error, never run. - * 3. The transaction puts the plugin's schema on the `search_path` with + * 1. No plugin SQL at all while TREK's role is privileged in the sense of the + * restore check (`PRIVILEGED_ROLE_SQL` in `server-checks.ts`: a superuser + * or a member of one, a role that may reach the server's files or + * programs, ...): every `db:own` call fails with + * `PRIVILEGED_ROLE_PLUGIN_REFUSAL`, and the boot log says why + * (`plugin-data-checks.ts`). Such a role may change the system catalogs, + * which no check on the SQL text can rule out. + * 2. This guard holds each statement to what cannot leave the schema, before + * it runs (below). + * 3. Every existing object a statement changes ({@link PluginTarget}: the + * target of an ALTER, DROP, REFRESH, TRUNCATE or CREATE INDEX ... ON, and + * the tables a CREATE or ALTER TABLE ties to its own) is resolved by + * Postgres in the transaction, right before the statement, with + * `to_regclass` or `to_regtype` on the plugin's `search_path`, and the + * statement is refused unless each one found lies in the plugin's schema. + * `pg_catalog` is searched first, so an unqualified `int4`, `text` or + * `box` is the catalog's type and refused, on a superuser connection too. + * 4. The store sends every statement on its own over the extended query + * protocol, on a pool client it checked out for the call: Postgres parses + * exactly one statement there, so a second statement the guard missed is + * an error, never run. + * 5. The transaction puts the plugin's schema on the `search_path` with * `pg_temp` explicitly last, TREK's own pool connections put `pg_temp` * last too (`session-search-path.ts`), and the store drops every * temporary table on its client before giving it back, so a temporary * table never stands in for a table of TREK's or of the plugin's. + * 6. A call that may write is held to the disk quota, counted over every + * relation of the schema with its TOAST table, and to + * `PLUGIN_SCHEMA_MAX_OBJECTS` catalog objects, so views, types and enum + * labels, which take no disk space of their own, are bounded too. * * The SQL is split into tokens the way Postgres's own lexer splits it (string * and identifier quoting, comments, dollar signs). Where JavaScript and the @@ -27,12 +48,14 @@ import { PLUGIN_SCHEMA_PREFIX } from './plugin-schema'; * instead of guessing: whitespace is exactly what Postgres skips (space, tab, * CR, LF, form feed), a `--` comment ends at CR or LF as it does there, and * any other control character, or whitespace past ASCII (U+00A0, U+FEFF, - * U+2028, ...), outside a string, a quoted name or a comment is refused. The - * SQL is refused when it holds: + * U+2028, ...), outside a string, a quoted name or a comment is refused. A + * NUL is refused anywhere, strings included: the protocol ends a statement's + * text there. The SQL is refused when it holds: * * - a statement other than SELECT, INSERT, UPDATE, DELETE, MERGE, WITH, * VALUES, TABLE, TRUNCATE, REFRESH, or a CREATE, ALTER or DROP of a table, - * index, view, materialized view, sequence or type. No function, schema, + * index, view, materialized view, sequence or type (a composite, enum or + * range type; a base type is built from C functions). No function, schema, * role, setting, transaction control, COPY, LOCK, prepared statement or * notification. Several statements are split at their semicolons and each * is checked; @@ -43,11 +66,21 @@ import { PLUGIN_SCHEMA_PREFIX } from './plugin-schema'; * - a function that runs SQL handed to it as text (`query_to_xml`, `ts_stat`, * `ts_rewrite`), reads or changes settings (`set_config`, * `current_setting`), reaches objects by a name in a string (`nextval`, - * `setval`, the `reg*` casts), large objects (`lo_*`) or `dblink`; - * - the words TEMP and TEMPORARY anywhere outside a string or a quoted name, - * and INTO anywhere but right after INSERT or MERGE, at any depth of - * parentheses: a temporary table outlives the call on a pooled connection, - * and `SELECT ... INTO` creates a table outside what CREATE is held to; + * `setval`, `has_table_privilege` and its siblings, `currtid2`, the BRIN + * and GIN maintenance functions, `to_reg*`), large objects (`lo_*`) or + * `dblink`; + * - the object identifier types that look an object up by name + * (`regclass`, `regtype`, `regproc`, `regprocedure`, `regoper`, + * `regoperator`, `regconfig`, `regdictionary`, `regnamespace`, `regrole`, + * `regcollation`, and their array types `_regclass`, ...), in every + * spelling: `'x'::regtype`, `CAST('x' AS regtype)`, `regtype 'x'`, quoted; + * - a temporary object: CREATE TEMP or TEMPORARY (also after OR REPLACE, + * LOCAL or GLOBAL), and SELECT ... INTO TEMP; a temporary table outlives + * the call on a pooled connection. Elsewhere `temp` is an ordinary name; + * - INTO anywhere but right after the verb INSERT or MERGE of the statement + * itself or of a WITH query's body, at any depth of parentheses: `SELECT + * ... INTO` creates a table outside what CREATE is held to, and in `SELECT + * 1 insert INTO t` the word `insert` is only a column alias; * - `WITH RECURSIVE`, refused on SQLite too; * - in an ALTER, a change of a table's schema, owner or tablespace; * - escape strings (`E'...'`), Unicode escapes (`U&'...'`), dollar-quoted @@ -92,6 +125,27 @@ export interface PluginStatement { readonly qualifies: boolean; /** How many `$n` placeholders it holds. */ readonly params: number; + /** + * The existing objects it changes, which the store resolves in the + * transaction right before the statement runs and refuses unless each one + * found lies in the plugin's own schema ({@link PluginTarget}). + */ + readonly targets: readonly PluginTarget[]; +} + +/** + * An existing object a statement changes: the table, index, view, + * materialized view or sequence (`relation`) or the type an ALTER, DROP, + * REFRESH or TRUNCATE names, the table a CREATE INDEX is ON, and the tables a + * CREATE or ALTER TABLE ties to its own (REFERENCES, INHERITS, PARTITION OF, + * ATTACH, DETACH and INHERIT). `name` holds the name's parts as Postgres + * keeps them (folded unless quoted, cut to 63 bytes), qualifier first. A + * name that resolves to nothing is left to Postgres, which reports it missing + * or, with IF EXISTS, skips it. + */ +export interface PluginTarget { + readonly kind: 'relation' | 'type'; + readonly name: readonly string[]; } /** Postgres keeps the first 63 bytes of a name (NAMEDATALEN - 1) and drops the rest. */ @@ -126,6 +180,26 @@ const VERBS = new Set(['select', 'insert', 'update', 'delete', 'merge', 'values' /** The verbs of a statement that only reads. */ const READ_VERBS = new Set(['select', 'values', 'table']); +/** + * The object identifier types whose input looks an object up by a name in a + * string (`'public.users'::regclass`, `CAST('int4' AS regtype)`, + * `regtype 'text'`), refused in every spelling, their array types + * (`_regclass`) too. + */ +const NAME_LOOKUP_TYPES = [ + 'regclass', + 'regtype', + 'regproc', + 'regprocedure', + 'regoper', + 'regoperator', + 'regconfig', + 'regdictionary', + 'regnamespace', + 'regrole', + 'regcollation', +]; + /** Functions and types refused under any spelling, quoted or not. */ const REFUSED_NAMES = new Set([ 'set_config', @@ -138,14 +212,29 @@ const REFUSED_NAMES = new Set([ 'lastval', 'loread', 'lowrite', - 'regclass', - 'regproc', - 'regprocedure', - 'regoper', - 'regoperator', - 'regnamespace', - 'regrole', - 'regcollation', + ...NAME_LOOKUP_TYPES, + ...NAME_LOOKUP_TYPES.map((type) => `_${type}`), + // Functions that take a relation, a type or another object by a name in a + // string, or reach an index by one to maintain it. + 'has_any_column_privilege', + 'has_column_privilege', + 'has_database_privilege', + 'has_foreign_data_wrapper_privilege', + 'has_function_privilege', + 'has_language_privilege', + 'has_parameter_privilege', + 'has_schema_privilege', + 'has_sequence_privilege', + 'has_server_privilege', + 'has_table_privilege', + 'has_tablespace_privilege', + 'has_type_privilege', + 'row_security_active', + 'currtid2', + 'brin_summarize_new_values', + 'brin_summarize_range', + 'brin_desummarize_range', + 'gin_clean_pending_list', 'information_schema', ]); @@ -166,8 +255,10 @@ const PUNCT = new Set([...'.,()[];+-*/<>=~!@#%^&|?']); * tab is whitespace only from Postgres 16 on, so it is refused instead. */ const WHITESPACE = new Set([' ', '\t', '\n', '\r', '\f']); -/** The keywords INTO may follow. Anywhere else it makes a table (`SELECT ... INTO`). */ +/** The verbs INTO may follow. Anywhere else it makes a table (`SELECT ... INTO`). */ const INTO_AFTER = new Set(['insert', 'merge']); +/** The words that make an object temporary right after CREATE (`CREATE LOCAL TEMP TABLE`). */ +const TEMPORARY = new Set(['temp', 'temporary']); /** * Whether JavaScript and the Postgres lexer read `ch` differently: a control @@ -208,6 +299,12 @@ function truncateName(name: string): string { /** Splits `sql` into tokens, or refuses what this guard does not read the way Postgres would. */ export function tokenize(sql: string): Token[] { + if (sql.includes('\u0000')) { + // The protocol ends a statement's text at a NUL, so what Postgres would read is not what was checked. + throw new PluginSqlRefused( + 'the SQL holds the character U+0000 (NUL), which Postgres cannot read in a statement or store in text', + ); + } const tokens: Token[] = []; let i = 0; const n = sql.length; @@ -341,6 +438,11 @@ function keyword(token: Token | undefined): string | null { return token?.kind === 'word' && !token.quoted ? token.value : null; } +/** Whether `token` is the punctuation `value`. */ +function isPunct(token: Token | undefined, value: string): boolean { + return token?.kind === 'punct' && token.value === value; +} + /** Reads what a statement's caller needs to know about it, and refuses one this guard does not allow. */ function describe(text: string, tokens: Token[]): PluginStatement { const first = tokens[0]; @@ -355,9 +457,12 @@ function describe(text: string, tokens: Token[]): PluginStatement { throw new PluginSqlRefused(`a statement may not start with ${describeToken(first)}`); } if (verb === 'create' || verb === 'alter' || verb === 'drop' || verb === 'refresh') checkObject(verb, tokens); + const targets = targetsOf(verb, tokens); let depth = 0; let main: string | null = verb === 'with' ? null : verb; + // Where the statement's own verb stands: its first token, or after a WITH clause the verb at depth 0. + let mainAt = verb === 'with' ? -1 : 0; let returning = false; let params = 0; let qualifies = false; @@ -370,12 +475,16 @@ function describe(text: string, tokens: Token[]): PluginStatement { const after = tokens[index + 1]; if (after?.kind === 'punct' && after.value === '.') qualifies = true; if (token.quoted) continue; - checkKeyword(token.value, keyword(tokens[index - 1])); + checkKeyword(token.value); + if (token.value === 'into') checkInto(tokens, index, mainAt); if (verb === 'alter' && (token.value === 'schema' || token.value === 'owner')) { throw new PluginSqlRefused(`an ALTER may not change the ${token.value}`); } if (depth !== 0) continue; - if (main === null && VERBS.has(token.value)) main = token.value; + if (main === null && VERBS.has(token.value)) { + main = token.value; + mainAt = index; + } if (token.value === 'returning') returning = true; } @@ -391,6 +500,7 @@ function describe(text: string, tokens: Token[]): PluginStatement { mayWrite: !reads || writesInside, qualifies, params, + targets, }; } @@ -399,6 +509,15 @@ function checkObject(verb: string, tokens: readonly Token[]): void { let at = 1; if (verb === 'create') { if (keyword(tokens[at]) === 'or' && keyword(tokens[at + 1]) === 'replace') at += 2; + const scope = keyword(tokens[at]); + if ( + TEMPORARY.has(scope ?? '') || + ((scope === 'local' || scope === 'global') && TEMPORARY.has(keyword(tokens[at + 1]) ?? '')) + ) { + throw new PluginSqlRefused( + `CREATE ${describeToken(tokens[at])} is not allowed: a temporary object outlives the call on a pooled connection`, + ); + } while (keyword(tokens[at]) === 'unique' || keyword(tokens[at]) === 'unlogged') at++; } const object = keyword(tokens[at]); @@ -406,29 +525,144 @@ function checkObject(verb: string, tokens: readonly Token[]): void { if (!allowed) { throw new PluginSqlRefused(`${verb.toUpperCase()} ${describeToken(tokens[at])} is not allowed`); } + // A base type is made of C functions and needs a superuser; a composite, enum or range type says AS. + if (verb === 'create' && object === 'type' && isPunct(tokens[at + 2], '(')) { + throw new PluginSqlRefused('CREATE TYPE may make a composite, enum or range type (CREATE TYPE x AS ...) only'); + } } /** * Refuses an unquoted keyword that is never safe, at whatever depth of - * parentheses it sits: `previous` is the unquoted keyword before it, if any. - * TEMP and TEMPORARY make an object that outlives the call on a pooled - * connection. INTO anywhere but after INSERT or MERGE is `SELECT ... INTO`, - * which makes a table (a temporary one with TEMP) past what CREATE is held to; - * `(SELECT ... INTO t)` and `WITH x AS (...) SELECT ... INTO t` included. + * parentheses it sits. */ -function checkKeyword(word: string, previous: string | null): void { +function checkKeyword(word: string): void { if (word === 'recursive') throw new PluginSqlRefused('WITH RECURSIVE is not allowed'); if (word === 'tablespace') throw new PluginSqlRefused('a tablespace may not be named'); - if (word === 'temp' || word === 'temporary') { - throw new PluginSqlRefused( - `temporary objects are not allowed (${word.toUpperCase()}); quote a name spelled "${word}"`, - ); +} + +/** + * Refuses the INTO at `index` unless it follows the verb INSERT or MERGE of + * the statement itself (`mainAt`) or of a WITH query's body (`AS (INSERT + * INTO ...`). Anywhere else it is `SELECT ... INTO`, which makes a table (a + * temporary one with TEMP) past what CREATE is held to, at any depth of + * parentheses: `(SELECT ... INTO t)`, `WITH x AS (...) SELECT ... INTO t`, + * and `SELECT 1 insert INTO t`, where `insert` is only a column alias. + */ +function checkInto(tokens: readonly Token[], index: number, mainAt: number): void { + const verbAt = index - 1; + const verb = keyword(tokens[verbAt]); + if (verb !== null && INTO_AFTER.has(verb) && (verbAt === mainAt || opensQueryBody(tokens, verbAt))) return; + const next = keyword(tokens[index + 1]); + if (next !== null && TEMPORARY.has(next)) { + throw new PluginSqlRefused(`SELECT ... INTO ${next.toUpperCase()} is not allowed: no temporary tables`); } - if (word === 'into' && (previous === null || !INTO_AFTER.has(previous))) { - throw new PluginSqlRefused('SELECT ... INTO is not allowed; use CREATE TABLE ... AS'); + throw new PluginSqlRefused('SELECT ... INTO is not allowed; use CREATE TABLE ... AS'); +} + +/** Whether the token at `index` is the first of a WITH query's body: `AS (`, `AS [NOT] MATERIALIZED (`. */ +function opensQueryBody(tokens: readonly Token[], index: number): boolean { + if (!isPunct(tokens[index - 1], '(')) return false; + const before = keyword(tokens[index - 2]); + return before === 'as' || (before === 'materialized' && keyword(tokens[index - 3]) !== null); +} + +/** A name of up to three parts (`t`, `s.t`, `db.s.t`) at `at` and the index past it; null when none starts there. */ +function readName(tokens: readonly Token[], at: number): { name: string[]; next: number } | null { + const name: string[] = []; + let i = at; + for (;;) { + const token = tokens[i]; + if (token?.kind !== 'word') return name.length === 0 ? null : { name, next: i }; + name.push(token.value); + if (!isPunct(tokens[i + 1], '.')) return { name, next: i + 1 }; + if (name.length === 3) throw new PluginSqlRefused('a name may have at most three parts'); + i += 2; } } +/** The index past the unquoted keywords `words` at `at`, or `at` when they are not there. */ +function skip(tokens: readonly Token[], at: number, ...words: string[]): number { + return words.every((word, i) => keyword(tokens[at + i]) === word) ? at + words.length : at; +} + +/** The comma-separated names starting at `at`, each optionally led by ONLY or followed by `*` (TRUNCATE). */ +function readNames(tokens: readonly Token[], at: number, what: string): string[][] { + const names: string[][] = []; + let i = at; + for (;;) { + const read = readName(tokens, skip(tokens, i, 'only')); + if (read === null) throw new PluginSqlRefused(`the object ${what} acts on could not be read`); + names.push(read.name); + i = read.next; + if (isPunct(tokens[i], '*')) i++; + if (!isPunct(tokens[i], ',')) return names; + i++; + } +} + +/** + * The existing objects a statement changes ({@link PluginTarget}). The one an + * ALTER, DROP, REFRESH, TRUNCATE or CREATE INDEX acts on must be readable, or + * the statement is refused; the tables a CREATE or ALTER ties to its own are + * every name after REFERENCES, INHERITS (, PARTITION OF, ATTACH PARTITION, + * DETACH PARTITION and INHERIT, read generously: a word there that is no + * table resolves to nothing and changes nothing. + */ +function targetsOf(verb: string, tokens: readonly Token[]): PluginTarget[] { + const targets: PluginTarget[] = []; + const add = (kind: PluginTarget['kind'], names: string[][]) => { + for (const name of names) targets.push({ kind, name }); + }; + const object = keyword(tokens[1]); + const kind = object === 'type' ? 'type' : 'relation'; + if (verb === 'alter') { + const at = skip(tokens, object === 'materialized' ? 3 : 2, 'if', 'exists'); + add(kind, readNames(tokens, at, 'an ALTER').slice(0, 1)); + } else if (verb === 'drop') { + const at = skip(tokens, skip(tokens, object === 'materialized' ? 3 : 2, 'concurrently'), 'if', 'exists'); + add(kind, readNames(tokens, at, 'a DROP')); + } else if (verb === 'refresh') { + add('relation', readNames(tokens, skip(tokens, 3, 'concurrently'), 'a REFRESH').slice(0, 1)); + } else if (verb === 'truncate') { + add('relation', readNames(tokens, skip(tokens, 1, 'table'), 'a TRUNCATE')); + } else if (verb === 'create' && keyword(tokens[skip(tokens, 1, 'unique')]) === 'index') { + const on = tokens.findIndex((t, i) => keyword(t) === 'on' && depthAt(tokens, i) === 0); + if (on < 0) throw new PluginSqlRefused('a CREATE INDEX must name its table with ON'); + add('relation', readNames(tokens, on + 1, 'a CREATE INDEX').slice(0, 1)); + } + if (verb === 'create' || verb === 'alter') add('relation', tiedTables(tokens)); + return targets; +} + +/** The names after REFERENCES, INHERITS (, PARTITION OF, ATTACH/DETACH PARTITION and INHERIT. */ +function tiedTables(tokens: readonly Token[]): string[][] { + const names: string[][] = []; + for (const [index, token] of tokens.entries()) { + const word = keyword(token); + let at = -1; + if (word === 'references' || word === 'inherit') at = index + 1; + else if (word === 'partition' && keyword(tokens[index + 1]) === 'of') at = index + 2; + else if (word === 'partition' && ['attach', 'detach'].includes(keyword(tokens[index - 1]) ?? '')) at = index + 1; + else if (word === 'inherits' && isPunct(tokens[index + 1], '(')) { + names.push(...readNames(tokens, index + 2, 'INHERITS')); + continue; + } + const read = at < 0 ? null : readName(tokens, at); + if (read !== null) names.push(read.name); + } + return names; +} + +/** How deep in parentheses the token at `index` sits. */ +function depthAt(tokens: readonly Token[], index: number): number { + let depth = 0; + for (const token of tokens.slice(0, index)) { + if (token.kind === 'punct' && token.value === '(') depth++; + if (token.kind === 'punct' && token.value === ')') depth--; + } + return depth; +} + /** Refuses a name that reaches past the plugin's schema whatever qualifies it. */ function checkName(name: string): void { if (REFUSED_NAMES.has(name) || REFUSED_PREFIXES.some((p) => name.startsWith(p)) || XML_EXPORT.test(name)) { diff --git a/server/src/db/nul-free.ts b/server/src/db/nul-free.ts index b2a75cd9b..f7442197f 100644 --- a/server/src/db/nul-free.ts +++ b/server/src/db/nul-free.ts @@ -21,13 +21,14 @@ import type { AbortableOperationOptions, CompiledQuery, DatabaseConnection, Dial * `engine/postgres/postgres-driver.ts`) send every query's parameters * through {@link withNulFreeParameters}, which covers the Kysely * statements of the repositories; - * - a plugin's own SQLite file (`nest/plugins/host/plugin-data.service.ts`) - * and its schema on Postgres (`engine/postgres/plugin-data-store.ts`, - * which sends on a `pg` client of its own) bind its arguments through - * {@link nulFreeParameter}; * - the move from SQLite to Postgres (`engine/postgres/sqlite-move/values.ts`) * copies text the same way. * + * A plugin's own data (`db:own`) is not TREK's to change: its SQLite file + * (`nest/plugins/host/plugin-data.service.ts`) binds a NUL exactly, so keys + * it stored with one stay findable, and its schema on Postgres refuses an + * argument holding one with an error (`engine/postgres/plugin-data-checks.ts`). + * * A filter is stripped as well as a write, so a lookup finds what the write * stored. Text that spells the character as the six characters `\u0000` * (JSON already encoded to a string) is left alone: it holds no NUL. diff --git a/server/src/nest/plugins/host/plugin-data.backend.ts b/server/src/nest/plugins/host/plugin-data.backend.ts index d93b3305b..abad4f1c5 100644 --- a/server/src/nest/plugins/host/plugin-data.backend.ts +++ b/server/src/nest/plugins/host/plugin-data.backend.ts @@ -2,6 +2,7 @@ import type { DbSettings } from '../../../app-config'; import type { EngineId } from '../../../app-config/db-settings'; import { postgresTarget, type DatabaseEngine } from '../../../db/engine'; import type { PluginDataStore } from '../../../db/engine/plugin-data'; +import { PRIVILEGED_ROLE_PLUGIN_REFUSAL, PrivilegedRoleGate } from '../../../db/engine/postgres/plugin-data-checks'; import { dropPluginSchema, PluginDataSlots, @@ -9,12 +10,13 @@ import { poolClients, PostgresPluginDataStore, } from '../../../db/engine/postgres/plugin-data-store'; +import { logWarn } from '../../audit/audit-log.logger'; import { DATABASE_ENGINE, DATABASE_SETTINGS } from '../../database/database-engine.module'; import { pluginDataDir } from '../paths'; import { PluginDataDb, removePluginData } from './plugin-data.service'; import { MikroORM } from '@mikro-orm/core'; import type { AbstractSqlConnection } from '@mikro-orm/sql'; -import { Inject, Injectable, type Provider } from '@nestjs/common'; +import { Inject, Injectable, type OnApplicationBootstrap, type Provider } from '@nestjs/common'; import type { Kysely } from 'kysely'; import fs from 'node:fs'; @@ -58,21 +60,44 @@ export class SqlitePluginDataBackend implements PluginDataBackend { * client. The plugin's data directory is still created, for the files a plugin * keeps beside its database, and it is what marks a plugin uninstalled with * its data kept (the erasure queue reads it). + * + * While TREK's role is privileged (`PRIVILEGED_ROLE_SQL`), every store refuses + * its calls (`plugin-data-checks.ts`), and the boot log says so once. */ @Injectable() -export class PostgresPluginDataBackend implements PluginDataBackend { +export class PostgresPluginDataBackend implements PluginDataBackend, OnApplicationBootstrap { readonly engine = 'postgres' as const; /** Shared by every plugin's store; made on first use, since on SQLite this provider is built but never used. */ private slots: PluginDataSlots | null = null; + /** Shared by every plugin's store, so the role is read once per pool connection. */ + private readonly roleGate = new PrivilegedRoleGate(); constructor( private readonly orm: MikroORM, @Inject(DATABASE_SETTINGS) private readonly settings: DbSettings, ) {} + /** On Postgres, says in the boot log when plugin databases are refused because of TREK's role. */ + async onApplicationBootstrap(): Promise { + if (!postgresTarget(this.settings)) return; + try { + const client = await poolClients(this.orm.em.getConnection())(); + try { + if (await this.roleGate.privileged(client)) logWarn(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + } finally { + client.release(); + } + } catch (err) { + logWarn( + `Could not check whether TREK's role may run plugin SQL: ${err instanceof Error ? err.message : String(err)}`, + ); + } + } + open(pluginId: string): PluginDataStore { fs.mkdirSync(pluginDataDir(pluginId), { recursive: true }); - return new PostgresPluginDataStore(pluginId, poolClients(this.orm.em.getConnection()), this.sharedSlots()); + const connect = poolClients(this.orm.em.getConnection()); + return new PostgresPluginDataStore(pluginId, connect, this.sharedSlots(), {}, this.roleGate); } async erase(pluginId: string): Promise { diff --git a/server/tests/postgres/plugin-data.test.ts b/server/tests/postgres/plugin-data.test.ts index df9b443f5..76a1769a5 100644 --- a/server/tests/postgres/plugin-data.test.ts +++ b/server/tests/postgres/plugin-data.test.ts @@ -1,5 +1,5 @@ /** - * PGPLUG-001..012 — a plugin's own data (`db:own`) on Postgres, against the + * PGPLUG-001..020 — a plugin's own data (`db:own`) on Postgres, against the * real app on the lane's database: each plugin gets a schema of its own and no * SQLite file, its SQL keeps the SQLite store's semantics (migrate once per * id, a script's statements committed one by one, an atomic tx, the row cap, @@ -7,8 +7,16 @@ * plugin's schema however it spells the name, every statement runs alone, * the pool client a call used goes back without anything the call set, and * erasing a plugin drops its schema and nothing else. + * + * The lane connects as a superuser, which is exactly the role plugin SQL is + * refused for (PGPLUG-013). So the stores here run as a role of the test's + * own that may only create schemas in the database, the role TREK is meant + * to run as, and the layers behind the role check are also shown to hold on + * the superuser connection with that check switched off (PGPLUG-014 to 020). */ +import type { DbSettings } from '../../src/app-config'; import { buildApp } from '../../src/bootstrap'; +import { PRIVILEGED_ROLE_PLUGIN_REFUSAL, type PluginRoleGate } from '../../src/db/engine/postgres/plugin-data-checks'; import { type ConnectPluginClient, PluginDataSlots, @@ -16,62 +24,147 @@ import { poolClients, PostgresPluginDataStore, } from '../../src/db/engine/postgres/plugin-data-store'; +import { TrekPostgreSqlDriver } from '../../src/db/engine/postgres/postgres-driver'; +import { putTempSchemaLast } from '../../src/db/engine/postgres/session-search-path'; +import { DATABASE_SETTINGS } from '../../src/nest/database/database-engine.module'; import { withRequestContext } from '../../src/nest/database/request-context'; -import { PLUGIN_DATA, type PluginDataBackend } from '../../src/nest/plugins/host/plugin-data.backend'; +import { + PLUGIN_DATA, + PostgresPluginDataBackend, + type PluginDataBackend, +} from '../../src/nest/plugins/host/plugin-data.backend'; import { closePluginDataDb } from '../../src/nest/plugins/host/plugin-host-state'; import { PluginRpcHostFactory } from '../../src/nest/plugins/host/plugin-rpc-host.factory'; +import { withAdmin } from './databases'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; +import type { DatabaseConnection } from 'kysely'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import Cursor from 'pg-cursor'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +/** The roles this file creates: cluster-wide, so named after the worker and the process. */ +const ROLE = `trek_plugin_lane_${process.env.VITEST_POOL_ID ?? '0'}_${process.pid}`; +const FILE_ROLE = `${ROLE}_files`; +const PASSWORD = 'lane-plugin-role'; + let app: INestApplication; let orm: MikroORM; -let data: PluginDataBackend; +/** The app's own backend, on the lane's superuser connection. */ +let superData: PluginDataBackend; +/** A backend like the app's, on a pool connected as {@link ROLE}. */ +let data: PostgresPluginDataBackend; +let roleOrm: MikroORM; +let fileRoleOrm: MikroORM; let dataDir: string; +/** The lane's URL for this file's database, as the superuser. */ +function superUrl(): string { + return process.env.TREK_DB_URL as string; +} + +/** An ORM on this file's database as `role`, built as the app builds its own (TREK's driver, `pg_temp` last). */ +async function ormAs(role: string): Promise { + const url = new URL(superUrl()); + url.username = role; + url.password = PASSWORD; + return MikroORM.init({ + driver: TrekPostgreSqlDriver, + clientUrl: url.toString(), + entities: [], + discovery: { warnWhenNoEntities: false }, + pool: { min: 0, max: 4 }, + onCreateConnection: (connection: unknown) => putTempSchemaLast(connection as DatabaseConnection), + }); +} + beforeAll(async () => { dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-pg-plugin-data-')); process.env.TREK_PLUGINS_DATA_DIR = dataDir; app = await buildApp(); orm = app.get(MikroORM); - data = app.get(PLUGIN_DATA, { strict: false }); + superData = app.get(PLUGIN_DATA, { strict: false }); + const database = new URL(superUrl()).pathname.slice(1); + await withAdmin(superUrl(), (run) => + inSequence( + [ + ...[ROLE, FILE_ROLE].flatMap((role) => [ + `create role "${role}" login password '${PASSWORD}'`, + `grant create on database "${database}" to "${role}"`, + ]), + // Reading server files is one of the privileges the role check counts. + `grant pg_read_server_files to "${FILE_ROLE}"`, + ], + (sql) => run(sql), + ), + ); + roleOrm = await ormAs(ROLE); + fileRoleOrm = await ormAs(FILE_ROLE); + data = new PostgresPluginDataBackend(roleOrm, app.get(DATABASE_SETTINGS, { strict: false })); }, 60_000); afterAll(async () => { + await roleOrm?.close(true); + await fileRoleOrm?.close(true); await app?.close(); + await withAdmin(superUrl(), (run) => + inSequence( + [ROLE, FILE_ROLE].flatMap((role) => [`drop owned by "${role}" cascade`, `drop role "${role}"`]), + (sql) => run(sql), + ), + ); delete process.env.TREK_PLUGINS_DATA_DIR; fs.rmSync(dataDir, { recursive: true, force: true }); -}); +}, 60_000); + +/** `work` for each item, one after the other. */ +async function inSequence(items: readonly T[], work: (item: T) => Promise): Promise { + await items.reduce>((previous, item) => previous.then(() => work(item)), Promise.resolve()); +} + +/** Runs `sql` as the superuser, outside any plugin store. */ +async function asSuper(sql: string): Promise { + return withRequestContext(orm, () => orm.em.getConnection().execute(sql, [], 'all')); +} /** The plugin schemas the database holds now. */ async function pluginSchemas(): Promise { - const rows = await withRequestContext(orm, () => - orm.em - .getConnection() - .execute>( - "select nspname from pg_namespace where starts_with(nspname, 'trek_plugin_') order by nspname", - [], - 'all', - ), + const rows = await asSuper<{ nspname: string }>( + "select nspname from pg_namespace where starts_with(nspname, 'trek_plugin_') order by nspname", ); return rows.map((row) => row.nspname); } -/** Clients of the app's pool, as the backend hands them to a store. */ +/** Clients of the pool connected as {@link ROLE}, as the backend hands them to a store. */ function appClients(): ConnectPluginClient { - return poolClients(orm.em.getConnection()); + return poolClients(roleOrm.em.getConnection()); } -/** A store on the app's pool with limits small enough to reach in a test. */ -function cappedStore(id: string, limits: { maxRows?: number; timeBudgetMs?: number; quotaBytes?: number }) { +/** A store as {@link ROLE} with limits small enough to reach in a test. */ +function cappedStore( + id: string, + limits: { maxRows?: number; timeBudgetMs?: number; quotaBytes?: number; maxObjects?: number }, +) { return new PostgresPluginDataStore(id, appClients(), new PluginDataSlots(1), limits); } +/** No role check: what the layers behind it hold on their own. */ +const NO_ROLE_CHECK: PluginRoleGate = { refuseIfPrivileged: async () => undefined }; + +/** A store on the lane's superuser connection with the role check switched off. */ +function superuserStore(id: string, limits: { quotaBytes?: number; maxObjects?: number } = {}) { + return new PostgresPluginDataStore( + id, + poolClients(orm.em.getConnection()), + new PluginDataSlots(1), + limits, + NO_ROLE_CHECK, + ); +} + /** `client` for every call of a store, kept checked out by the test: a release only records whether it would close it. */ function keep(client: PluginSqlClient, released: boolean[] = []): ConnectPluginClient { return async () => @@ -106,6 +199,15 @@ function smuggling(swap: Record): ConnectPluginClient { }; } +/** Whether the catalog type `name` still exists under that name. */ +async function catalogType(name: string): Promise { + const rows = await asSuper<{ n: number }>( + `select count(*) as n from pg_type t join pg_namespace n on n.oid = t.typnamespace ` + + `where n.nspname = 'pg_catalog' and t.typname = '${name}'`, + ); + return Number(rows[0].n) === 1; +} + describe('plugin data on Postgres', () => { it('PGPLUG-001: a plugin gets a schema of its own, migrates once per id, reads and writes there, and no SQLite file', async () => { const store = data.open('pg-alpha'); @@ -191,9 +293,7 @@ describe('plugin data on Postgres', () => { ].map((sql) => expect(store.exec(sql), sql).rejects.toThrow(/not allowed/)), ); // Nothing of TREK's was touched along the way. - const [{ n }] = await withRequestContext(orm, () => - orm.em.getConnection().execute>('select count(*) as n from users', [], 'all'), - ); + const [{ n }] = await asSuper<{ n: number }>('select count(*) as n from users'); expect(n).toBeGreaterThanOrEqual(0); }); @@ -256,24 +356,21 @@ describe('plugin data on Postgres', () => { await expect(data.erase('Not_An_Id')).resolves.toBeUndefined(); }); - it('PGPLUG-008: through the RPC host, db.* reaches the plugin schema and db.engine says postgres', async () => { + it('PGPLUG-008: through the RPC host, db.engine says postgres and db.* answers the role refusal on a superuser', async () => { const router = { callPlugin: async () => undefined, emitPluginEvent: async () => {} }; const host = app.get(PluginRpcHostFactory).create('pg-rpc', new Set(['db:own']), router); const call = (method: string, params: Record) => host.dispatch({ k: 'req', id: method, method, params }); expect(await call('db.engine', {})).toMatchObject({ ok: true, result: { engine: 'postgres' } }); - expect(await call('db.migrate', { id: '001', sql: 'CREATE TABLE t (v text)' })).toMatchObject({ ok: true }); - expect(await call('db.exec', { sql: 'INSERT INTO t VALUES ($1)', args: ['x'] })).toMatchObject({ - ok: true, - result: { changes: 1 }, - }); - expect(await call('db.query', { sql: 'SELECT v FROM t' })).toMatchObject({ ok: true, result: [{ v: 'x' }] }); - expect(await call('db.query', { sql: 'SELECT email FROM public.users' })).toMatchObject({ - ok: false, - error: { code: 'HOST_ERROR' }, - }); - expect(await pluginSchemas()).toContain('trek_plugin_pg_rpc'); + const answers = await Promise.all([ + call('db.migrate', { id: '001', sql: 'CREATE TABLE t (v text)' }), + call('db.exec', { sql: 'INSERT INTO t VALUES ($1)', args: ['x'] }), + call('db.query', { sql: 'SELECT 1' }), + ]); + const refused = { ok: false, error: { code: 'HOST_ERROR', message: PRIVILEGED_ROLE_PLUGIN_REFUSAL } }; + expect(answers).toMatchObject([refused, refused, refused]); + expect(await pluginSchemas()).not.toContain('trek_plugin_pg_rpc'); closePluginDataDb('pg-rpc'); }); @@ -289,7 +386,7 @@ describe('plugin data on Postgres', () => { '((select 1 into t))', '(select 1) into t', 'select * from (select 1 into t) s', - 'select 1 from public.users', + 'select 1 from public.users', ]; await Promise.all(reads.map((sql) => expect(store.query(sql), sql).rejects.toThrow(/not allowed for plugin/))); const scripts = [ @@ -304,15 +401,9 @@ describe('plugin data on Postgres', () => { // Nothing hidden ran: no temporary table anywhere in the database, no function. The insert in front of the // CR is a statement of its own, which a script commits before the next one is refused, as on SQLite. - const [{ temps, functions }] = await withRequestContext(orm, () => - orm.em - .getConnection() - .execute>( - "select (select count(*) from pg_class where relpersistence = 't') as temps, " + - "(select count(*) from pg_proc where proname = 'f') as functions", - [], - 'all', - ), + const [{ temps, functions }] = await asSuper<{ temps: number; functions: number }>( + "select (select count(*) from pg_class where relpersistence = 't') as temps, " + + "(select count(*) from pg_proc where proname = 'f') as functions", ); expect({ temps, functions }).toEqual({ temps: 0, functions: 0 }); expect(await store.query('SELECT body FROM notes')).toEqual([{ body: 'x' }]); @@ -356,16 +447,10 @@ describe('plugin data on Postgres', () => { }); it("PGPLUG-011: pg_temp is searched last, so a temporary table never stands in for TREK's tables or a plugin's", async () => { - const [{ path, schema }] = await withRequestContext(orm, () => - orm.em - .getConnection() - .execute>( - "select current_setting('search_path') as path, current_schema() as schema", - [], - 'all', - ), + const [{ path: searchPath, schema }] = await asSuper<{ path: string; schema: string }>( + "select current_setting('search_path') as path, current_schema() as schema", ); - expect(path).toMatch(/, pg_temp$/); + expect(searchPath).toMatch(/, pg_temp$/); expect(schema).toBe('public'); const store = data.open('pg-shadow'); @@ -379,6 +464,8 @@ describe('plugin data on Postgres', () => { const released: boolean[] = []; const onClient = new PostgresPluginDataStore('pg-shadow', keep(client, released), new PluginDataSlots(1)); expect(await onClient.query('SELECT body FROM notes')).toEqual([{ body: 'real' }]); + // Nor can the plugin reach them, or change them, by naming the temporary schema. + await expect(onClient.query('SELECT body FROM pg_temp.notes')).rejects.toThrow(/"pg_temp" may not be named/); // The call dropped them with the rest of the session's state before it gave the client back. const temps = await client.query( "select count(*)::int as n from pg_class where relpersistence = 't' and relnamespace = pg_my_temp_schema()", @@ -416,4 +503,188 @@ describe('plugin data on Postgres', () => { client.release(); } }); + + it('PGPLUG-013: on a privileged role every db:own call is refused with the reason, before anything is created', async () => { + // The lane's own connection is a superuser: the app's backend refuses it. + const store = superData.open('pg-privileged'); + await Promise.all( + [ + store.migrate('001', 'CREATE TABLE t (x int)'), + store.exec('CREATE TABLE t (x int)'), + store.query('SELECT 1'), + store.tx([{ sql: 'SELECT 1' }]), + ].map((call) => expect(call).rejects.toThrow(PRIVILEGED_ROLE_PLUGIN_REFUSAL)), + ); + // A role that may read the server's files is refused as well. + const settings = app.get(DATABASE_SETTINGS, { strict: false }); + const files = new PostgresPluginDataBackend(fileRoleOrm, settings).open('pg-files'); + await expect(files.query('SELECT 1')).rejects.toThrow(/db:own\) are refused on Postgres while TREK_DB_URL/); + const schemas = await pluginSchemas(); + expect(schemas).not.toContain('trek_plugin_pg_privileged'); + expect(schemas).not.toContain('trek_plugin_pg_files'); + }); + + // The re-review's escape: an unqualified ALTER TYPE renamed pg_catalog's types for the whole database. + const CATALOG_TARGETS = [ + 'alter type int4 rename to int4x', + 'alter type text rename to evil', + 'alter type cstring rename to foo', + 'alter type box rename to boxx', + 'ALTER TYPE "int4" RENAME TO x', + 'alter type timestamptz owner to current_user', + 'drop type if exists box cascade', + 'drop type int4, text', + ]; + + it('PGPLUG-014: ALTER and DROP of a catalog type are refused by where Postgres resolves it, on a superuser too', async () => { + const refused = /schema "pg_catalog"; a plugin changes its own objects only|may not change the owner/; + await Promise.all( + [data.open('pg-types'), superuserStore('pg-types-su')].map(async (store) => { + await Promise.all(CATALOG_TARGETS.map((sql) => expect(store.exec(sql), sql).rejects.toThrow(refused))); + // TREK's own tables are not on the plugin's path, so a name of theirs resolves to nothing there. + await expect(store.exec('drop table users')).rejects.toThrow(/does not exist/); + await expect(store.exec('drop table if exists users, trips cascade')).resolves.toEqual({ changes: 0 }); + await expect(store.exec('truncate users')).rejects.toThrow(/does not exist/); + await expect(store.exec('create index evil on users (email)')).rejects.toThrow(/does not exist/); + await expect(store.exec('create table kid (x int) inherits (users)')).rejects.toThrow(/does not exist/); + }), + ); + const names = ['int4', 'text', 'cstring', 'box', 'timestamptz']; + expect(await Promise.all(names.map(catalogType))).toEqual(names.map(() => true)); + expect(await asSuper('select 1::int4 as one, 1::text as two')).toEqual([{ one: 1, two: '1' }]); + expect((await asSuper<{ n: number }>("select count(*) as n from pg_class where relname = 'users'"))[0].n).toBe(1); + }); + + it('PGPLUG-015: a plugin still changes its own objects of every kind', async () => { + const own = async (store: PostgresPluginDataStore | ReturnType) => { + await store.exec( + "CREATE TYPE mood AS ENUM ('ok'); ALTER TYPE mood ADD VALUE 'sad'; ALTER TYPE mood RENAME TO feeling; " + + 'CREATE TABLE t (id bigserial PRIMARY KEY, f feeling, temp real); CREATE TABLE kid (y int) INHERITS (t); ' + + 'CREATE TABLE r (t_id bigint REFERENCES t (id)); CREATE INDEX t_f ON t (f); ALTER INDEX t_f RENAME TO t_g; ' + + 'CREATE MATERIALIZED VIEW mv AS SELECT count(*) AS n FROM t; REFRESH MATERIALIZED VIEW mv; ' + + 'ALTER TABLE t ADD COLUMN z int; CREATE SEQUENCE s; ALTER SEQUENCE s RESTART WITH 5; ' + + 'TRUNCATE r, kid; DROP TABLE r; DROP MATERIALIZED VIEW mv; DROP SEQUENCE s', + ); + await store.exec("INSERT INTO t (f, temp) VALUES ('sad', 21.5)"); + expect(await store.query('SELECT f, temp FROM t ORDER BY temp')).toEqual([{ f: 'sad', temp: 21.5 }]); + await store.exec('DROP TABLE kid; DROP TABLE t; DROP TYPE feeling'); + }; + await Promise.all([own(data.open('pg-own')), own(superuserStore('pg-own-su'))]); + }); + + it('PGPLUG-016: the quota counts every relation, sequences and TOAST included, and the object cap catalog-only objects', async () => { + const sequences = cappedStore('pg-seq', { quotaBytes: 64 * 1024 }); + const script = Array.from({ length: 20 }, (_, i) => `CREATE SEQUENCE s${i}`).join('; '); + await expect(sequences.exec(script)).rejects.toThrow(/past its 0\.0625 MB quota/); + const [{ made }] = await asSuper<{ made: number }>( + 'select count(*) as made from pg_class c join pg_namespace n on n.oid = c.relnamespace ' + + "where n.nspname = 'trek_plugin_pg_seq' and c.relkind = 'S'", + ); + expect(made).toBeGreaterThan(0); + expect(made).toBeLessThan(20); + + const toast = cappedStore('pg-toast', { quotaBytes: 64 * 1024 }); + await toast.migrate('001', 'CREATE TABLE big (body text)'); + await expect( + toast.exec("INSERT INTO big SELECT string_agg(md5(random()::text), '') FROM generate_series(1, 8000)"), + ).rejects.toThrow(/past its 0\.0625 MB quota/); + + const views = Array.from({ length: 30 }, (_, i) => `CREATE VIEW v${i} AS SELECT ${i} AS n`).join('; '); + const capped = async (store: PostgresPluginDataStore) => { + await expect(store.exec(views)).rejects.toThrow(/holds more than 40 catalog objects/); + await expect(store.exec("CREATE TYPE e AS ENUM ('a')")).rejects.toThrow(/holds more than 40 catalog objects/); + // At the cap, dropping still works. + await expect(store.exec('DROP VIEW v0')).resolves.toEqual({ changes: 0 }); + }; + await Promise.all([ + capped(cappedStore('pg-objects', { maxObjects: 40 })), + capped(superuserStore('pg-objects-su', { maxObjects: 40 })), + ]); + }); + + it('PGPLUG-017: the reg* types and the functions that look objects up by name are refused in every spelling', async () => { + const payloads = [ + "select 'public.users'::regtype::oid", + "select cast('public.users' as regtype)", + "select regtype 'public.users'", + `select 'public.users'::"regtype"`, + "select 'english'::regconfig", + "select 'simple'::regdictionary", + "select '{public.users}'::_regclass", + "select has_table_privilege('public.users', 'select')", + "select currtid2('public.users', '(0,1)')", + "select to_regtype('public.users')", + ]; + const stores = [data.open('pg-reg'), superuserStore('pg-reg-su')]; + await Promise.all( + stores.flatMap((store) => + payloads.map((sql) => expect(store.query(sql), sql).rejects.toThrow(/may not be named/)), + ), + ); + }); + + it('PGPLUG-018: INTO makes no table unless INSERT or MERGE is the verb, whatever a column alias is called', async () => { + const aliases = ['select 1 insert into t1', 'select 1 merge into t9', 'select 1 insert into unlogged t10']; + const into = async (store: PostgresPluginDataStore | ReturnType, schema: string) => { + await store.migrate('001', 'CREATE TABLE notes (id bigserial PRIMARY KEY, body text)'); + await Promise.all( + aliases.map((sql) => expect(store.exec(sql), sql).rejects.toThrow(/SELECT \.\.\. INTO is not allowed/)), + ); + const [{ made }] = await asSuper<{ made: number }>( + `select count(*) as made from pg_class c join pg_namespace n on n.oid = c.relnamespace ` + + `where n.nspname = '${schema}' and c.relname in ('t1', 't9', 't10')`, + ); + expect(made).toBe(0); + // INSERT INTO as the verb, after a WITH clause and inside one, still writes. + await store.exec("WITH x AS (SELECT 'a' AS b) INSERT INTO notes (body) SELECT b FROM x"); + expect( + await store.query("WITH n AS (INSERT INTO notes (body) VALUES ('b') RETURNING id) SELECT count(*) AS n FROM n"), + ).toEqual([{ n: 1 }]); + expect(await store.query('SELECT body FROM notes ORDER BY id')).toEqual([{ body: 'a' }, { body: 'b' }]); + }; + await Promise.all([ + into(data.open('pg-into'), 'trek_plugin_pg_into'), + into(superuserStore('pg-into-su'), 'trek_plugin_pg_into_su'), + ]); + }); + + it('PGPLUG-019: TEMP is refused where it makes a temporary object, and a column named temp works again', async () => { + const store = data.open('pg-temp'); + await Promise.all( + [ + 'CREATE TEMP TABLE t (x int)', + 'CREATE LOCAL TEMPORARY TABLE t (x int)', + 'CREATE OR REPLACE TEMP VIEW v AS SELECT 1', + 'CREATE TEMPORARY SEQUENCE s', + 'select 1 into temp t', + '(select 1 as id into temporary users)', + ].map((sql) => expect(store.exec(sql), sql).rejects.toThrow(/not allowed/)), + ); + await store.migrate('001', 'CREATE TABLE readings (day text, temp real)'); + await store.exec('INSERT INTO readings (day, temp) VALUES ($1, $2), ($3, $4)', ['mon', 21.5, 'tue', 18]); + expect(await store.query('SELECT day, temp FROM readings WHERE temp > 1 ORDER BY temp')).toEqual([ + { day: 'tue', temp: 18 }, + { day: 'mon', temp: 21.5 }, + ]); + const [{ temps }] = await asSuper<{ temps: number }>( + "select count(*) as temps from pg_class where relpersistence = 't'", + ); + expect(temps).toBe(0); + }); + + it('PGPLUG-020: a NUL in an argument or in the SQL is refused with the reason, and nothing is stored', async () => { + const store = data.open('pg-nul'); + await store.migrate('001', 'CREATE TABLE kv (k text PRIMARY KEY)'); + const reason = /U\+0000 \(NUL\), which Postgres text cannot store/; + await expect(store.exec('INSERT INTO kv VALUES ($1)', ['a\u0000b'])).rejects.toThrow(reason); + await expect(store.query('SELECT k FROM kv WHERE k = $1', ['ab\u0000'])).rejects.toThrow(reason); + await expect(store.tx([{ sql: 'INSERT INTO kv VALUES ($1)', args: [['x', '\u0000']] }])).rejects.toThrow(reason); + await expect(store.migrate('002\u0000', 'CREATE TABLE more (x int)')).rejects.toThrow(reason); + await expect(store.exec("INSERT INTO kv VALUES ('a\u0000b')")).rejects.toThrow(/U\+0000 \(NUL\)/); + expect(await store.query('SELECT count(*) AS n FROM kv')).toEqual([{ n: 0 }]); + // Binary data with a zero byte goes to bytea. + await store.migrate('003', 'CREATE TABLE raw (b bytea)'); + await store.exec('INSERT INTO raw VALUES ($1)', [Buffer.from([1, 0, 2])]); + expect(await store.query('SELECT length(b) AS n FROM raw')).toEqual([{ n: 3 }]); + }); }); diff --git a/server/tests/postgres/sqlite-move.test.ts b/server/tests/postgres/sqlite-move.test.ts index 40d51ef56..62da5673a 100644 --- a/server/tests/postgres/sqlite-move.test.ts +++ b/server/tests/postgres/sqlite-move.test.ts @@ -269,7 +269,14 @@ describe('moving a SQLite install to Postgres', () => { const highest = Math.max(...(await findRows(sqliteOrm, Users)).map((row) => row.id)); expect(user.id).toBeGreaterThan(highest); - const store = new PostgresPluginDataStore(PLUGIN_ID, poolClients(pgOrm.em.getConnection()), new PluginDataSlots(1)); + // The lane connects as a superuser, for which plugin SQL is refused (PGPLUG-013); the move is what is read here. + const store = new PostgresPluginDataStore( + PLUGIN_ID, + poolClients(pgOrm.em.getConnection()), + new PluginDataSlots(1), + {}, + { refuseIfPrivileged: async () => undefined }, + ); expect(await store.query('select id, body, score from notes order by id')).toEqual([ { id: 1, body: 'first', score: 1.5 }, { id: 2, body: 'second', score: null }, diff --git a/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts b/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts index 4693d9e91..bd7a31a4f 100644 --- a/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts +++ b/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts @@ -10,6 +10,7 @@ * store on a real database is the Postgres lane's * tests/postgres/plugin-data.test.ts. */ +import { PRIVILEGED_ROLE_PLUGIN_REFUSAL } from '../../../../../src/db/engine/postgres/plugin-data-checks'; import { dropPluginSchema, END_SESSION, @@ -18,6 +19,7 @@ import { PostgresPluginDataStore, type PluginSqlClient, } from '../../../../../src/db/engine/postgres/plugin-data-store'; +import { PRIVILEGED_ROLE_SQL } from '../../../../../src/db/engine/postgres/server-checks'; import type { Kysely } from 'kysely'; import Cursor from 'pg-cursor'; @@ -28,6 +30,12 @@ interface FakeOptions { schemas?: string[]; /** The sizes the quota reads answer, in order. */ sizes?: number[]; + /** The catalog object counts the quota reads answer, in order. */ + objects?: number[]; + /** Whether the role check answers that TREK's role is privileged. */ + privileged?: boolean; + /** The objects the target check finds outside the plugin's schema. */ + foreign?: Array<{ name: string; nspname: string }>; /** Rows a cursor yields, by its statement's text; read two at a time. */ rows?: Record; /** What a write reports as changed. */ @@ -62,6 +70,7 @@ function fakePool(options: FakeOptions = {}) { const cursors: Array<{ text: string; values: unknown[] | null; closed: boolean }> = []; const releases: boolean[] = []; let size = 0; + let objects = 0; const run = (sql: string) => { log.push(sql); const err = options.fail?.(sql); @@ -69,7 +78,14 @@ function fakePool(options: FakeOptions = {}) { }; const answer = (sql: string) => { if (sql.startsWith('select nspname')) return { rows: (options.schemas ?? []).map((nspname) => ({ nspname })) }; - if (sql.startsWith('select coalesce(sum(')) return { rows: [{ bytes: options.sizes?.[size++] ?? 0 }], rowCount: 1 }; + if (sql.startsWith('select (exists')) return { rows: [{ privileged: options.privileged === true }], rowCount: 1 }; + if (sql.startsWith('select t.name')) return { rows: options.foreign ?? [], rowCount: 0 }; + if (sql.startsWith('select coalesce((select sum(')) { + return { + rows: [{ bytes: options.sizes?.[size++] ?? 0, objects: options.objects?.[objects++] ?? 0 }], + rowCount: 1, + }; + } if (sql.startsWith('select 1 from')) return { rows: options.migrated ? [{ '?column?': 1 }] : [], rowCount: 0 }; return { rows: [], rowCount: options.changes ?? 0 }; }; @@ -119,6 +135,9 @@ function store(options: FakeOptions = {}, limits = {}) { return { ...pool, store: new PostgresPluginDataStore('notes', pool.connect, new PluginDataSlots(4), limits) }; } +/** The role check every first call on a client runs before its transaction. */ +const ROLE_CHECK = `select (${PRIVILEGED_ROLE_SQL}) as privileged`; + /** A pg error with its SQLSTATE. */ function pgError(code: string, message: string): Error { return Object.assign(new Error(message), { code }); @@ -132,6 +151,7 @@ describe('Postgres plugin data store', () => { expect(s.engine).toBe('postgres'); expect(await s.query('SELECT body FROM notes')).toEqual([{ body: 'a' }, { body: 'b' }, { body: 'c' }]); expect(log).toEqual([ + ROLE_CHECK, 'begin', [ 'create schema if not exists "trek_plugin_notes"', @@ -199,7 +219,7 @@ describe('Postgres plugin data store', () => { const reads = store({}, { quotaBytes: 1 }); await reads.store.query('SELECT 1'); - expect(reads.log.some((sql) => sql.startsWith('select coalesce(sum('))).toBe(false); + expect(reads.log.some((sql) => sql.startsWith('select coalesce((select sum('))).toBe(false); }); it('PGSTORE-006: a script commits statement by statement and stops at the first that fails', async () => { @@ -304,7 +324,7 @@ describe('Postgres plugin data store', () => { expect(configs.filter((q) => q.queryMode === 'extended')).toHaveLength(plugin.length); }); - it('PGSTORE-013: every read goes through a cursor on the same client, closed however the read ends, args without NUL', async () => { + it('PGSTORE-013: every read goes through a cursor on the same client, closed however the read ends', async () => { // EXTQ-002 before the store checked out its own clients: what is not a plugin write reaches pg as it was. const rows = Array.from({ length: 7 }, (_, i) => ({ i })); const { @@ -312,9 +332,9 @@ describe('Postgres plugin data store', () => { cursors, configs, } = store({ rows: { 'SELECT i FROM t WHERE x = $1': rows, 'SELECT 1 FROM t': [{}] } }, { maxRows: 5 }); - await expect(s.query('SELECT i FROM t WHERE x = $1', ['a\u0000b'])).rejects.toThrow('more than 5 rows'); + await expect(s.query('SELECT i FROM t WHERE x = $1', ['ab'])).rejects.toThrow('more than 5 rows'); await s.exec('SELECT 1 FROM t', ['x']); - await s.exec('INSERT INTO t VALUES ($1)', ['c\u0000d']); + await s.exec('INSERT INTO t VALUES ($1)', ['cd']); expect(cursors).toEqual([ { text: 'SELECT i FROM t WHERE x = $1', values: ['ab'], closed: true }, { text: 'SELECT 1 FROM t', values: ['x'], closed: true }, @@ -322,6 +342,77 @@ describe('Postgres plugin data store', () => { expect(configs.find((q) => q.text.startsWith('INSERT'))?.values).toEqual(['cd']); }); + it('PGSTORE-016: an argument or a migration id holding NUL is refused before a client is taken, never altered', async () => { + const { store: s, log, releases } = store(); + const nul = /U\+0000 \(NUL\), which Postgres text cannot store/; + await expect(s.query('SELECT 1 FROM t WHERE k = $1', ['a\u0000b'])).rejects.toThrow(nul); + await expect(s.exec('INSERT INTO t VALUES ($1)', [['x', 'c\u0000d']])).rejects.toThrow(nul); + await expect(s.tx([{ sql: 'SELECT 1' }, { sql: 'DELETE FROM t WHERE k = $1', args: ['\u0000'] }])).rejects.toThrow( + nul, + ); + await expect(s.migrate('001\u0000', 'CREATE TABLE t (k text)')).rejects.toThrow(nul); + expect({ log, releases }).toEqual({ log: [], releases: [] }); + // A Buffer goes to bytea, which holds any byte. + await expect(s.exec('INSERT INTO t VALUES ($1)', [Buffer.from([0, 1])])).resolves.toEqual({ changes: 0 }); + }); + + it('PGSTORE-017: a privileged role is refused before the transaction opens, and the answer is kept per connection', async () => { + const privileged = store({ privileged: true }); + await expect(privileged.store.query('SELECT 1')).rejects.toThrow(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + await expect(privileged.store.exec('CREATE TABLE t (x int)')).rejects.toThrow(/db:own\) are refused on Postgres/); + // One read for the connection, no transaction, and the client goes back clean. + expect(privileged.log).toEqual([ROLE_CHECK, END_SESSION, END_SESSION]); + expect(privileged.releases).toEqual([false, false]); + + const plain = store(); + await plain.store.query('SELECT 1'); + await plain.store.query('SELECT 2'); + expect(plain.log.filter((sql) => sql === ROLE_CHECK)).toHaveLength(1); + + // A read that failed is asked again on the next call. + let fails = 1; + const flaky = store({ fail: (sql) => (sql === ROLE_CHECK && fails-- > 0 ? new Error('reset') : undefined) }); + await expect(flaky.store.query('SELECT 1')).rejects.toThrow('reset'); + await expect(flaky.store.query('SELECT 1')).resolves.toEqual([]); + expect(flaky.log.filter((sql) => sql === ROLE_CHECK)).toHaveLength(2); + }); + + it('PGSTORE-018: the objects a statement changes are resolved right before it runs, and one outside the schema refuses it', async () => { + const own = store(); + await own.store.migrate('001', 'CREATE TABLE a (x int); ALTER TYPE mood RENAME TO feeling; DROP TABLE "B", c'); + const checks = own.configs.filter((q) => q.text.startsWith('select t.name')); + expect(checks.map((q) => q.values)).toEqual([ + [['"mood"'], ['type'], 'trek_plugin_notes'], + [['"B"', '"c"'], ['relation', 'relation'], 'trek_plugin_notes'], + ]); + // Each check comes right before its statement, after the ones before it ran. + const order = own.log + .filter((sql) => /^(select t\.name|CREATE|ALTER|DROP)/.test(sql)) + .map((sql) => sql.slice(0, 13)); + expect(order).toEqual(['CREATE TABLE ', 'select t.name', 'ALTER TYPE mo', 'select t.name', 'DROP TABLE "B']); + + const foreign = store({ foreign: [{ name: '"int4"', nspname: 'pg_catalog' }] }); + await expect(foreign.store.exec('ALTER TYPE int4 RENAME TO int4x')).rejects.toThrow( + /"int4" is an object of the schema "pg_catalog"; a plugin changes its own objects only/, + ); + expect(foreign.log).not.toContain('ALTER TYPE int4 RENAME TO int4x'); + expect(foreign.log.slice(-2)).toEqual(['rollback', END_SESSION]); + // A statement that changes no existing object asks nothing. + const plain = store(); + await plain.store.exec('INSERT INTO a VALUES (1)'); + expect(plain.log.some((sql) => sql.startsWith('select t.name'))).toBe(false); + }); + + it('PGSTORE-019: a write that takes the schema past its object cap and adds objects rolls back; a drop still runs', async () => { + const grows = store({ sizes: [0, 0], objects: [10, 11] }, { maxObjects: 10 }); + await expect(grows.store.exec('CREATE TABLE t (x int)')).rejects.toThrow( + 'the plugin database holds more than 10 catalog objects', + ); + expect(grows.log.slice(-2)).toEqual(['rollback', END_SESSION]); + const drops = store({ sizes: [0, 0], objects: [12, 11] }, { maxObjects: 10 }); + await expect(drops.store.exec('DROP TABLE t')).resolves.toEqual({ changes: 0 }); + }); + it('PGSTORE-014: each transaction checks out a client of its own and gives it back clean, or closes it', async () => { const ok = store({ changes: 1 }); await ok.store.exec('INSERT INTO a VALUES (1); INSERT INTO b VALUES (2)'); diff --git a/server/tests/unit/db/engine/postgres/plugin-sql-guard.test.ts b/server/tests/unit/db/engine/postgres/plugin-sql-guard.test.ts index 14826b68e..ad63a8593 100644 --- a/server/tests/unit/db/engine/postgres/plugin-sql-guard.test.ts +++ b/server/tests/unit/db/engine/postgres/plugin-sql-guard.test.ts @@ -11,6 +11,7 @@ import { parsePluginSql, PluginSqlRefused, tokenize, + type PluginTarget, } from '../../../../../src/db/engine/postgres/plugin-sql-guard'; import { describe, expect, it } from 'vitest'; @@ -63,6 +64,20 @@ describe('plugin SQL guard', () => { '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(); }); @@ -126,7 +141,6 @@ describe('plugin SQL guard', () => { ['SELECT `x`', /unexpected character/], ['SELECT 1; SELECT * FROM public.users', /another schema/], ['SELECT 1 FROM notes INTO TEMP copy', /SELECT \.\.\. INTO/], - ['SELECT * FROM notes WHERE temp > 1', /TEMP/], ['SELECT 0x1F', /hexadecimal/], ['SELECT 0o17, 0b1', /hexadecimal/], ['SELECT 1_000', /digit separators/], @@ -165,8 +179,33 @@ describe('plugin SQL guard', () => { ['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/], - ['select temp from notes', /TEMP/], - ['select 1 from notes as temporary', /TEMPORARY/], + // 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/], + // 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); @@ -234,6 +273,54 @@ describe('plugin SQL guard', () => { 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/); diff --git a/server/tests/unit/plugins/plugin-data-backend.test.ts b/server/tests/unit/plugins/plugin-data-backend.test.ts index f44d66827..26d278bb8 100644 --- a/server/tests/unit/plugins/plugin-data-backend.test.ts +++ b/server/tests/unit/plugins/plugin-data-backend.test.ts @@ -7,7 +7,9 @@ */ import type { DbSettings } from '../../../src/app-config'; import type { DatabaseEngine } from '../../../src/db/engine'; +import { PRIVILEGED_ROLE_PLUGIN_REFUSAL } from '../../../src/db/engine/postgres/plugin-data-checks'; import { PostgresPluginDataStore } from '../../../src/db/engine/postgres/plugin-data-store'; +import { PRIVILEGED_ROLE_DESCRIPTION } from '../../../src/db/engine/postgres/server-checks'; import { PLUGIN_DATA, PLUGIN_DATA_PROVIDERS, @@ -21,7 +23,13 @@ import type { FactoryProvider } from '@nestjs/common'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; + +const { logWarn } = vi.hoisted(() => ({ logWarn: vi.fn() })); +vi.mock('../../../src/nest/audit/audit-log.logger', async (importOriginal) => ({ + ...(await importOriginal()), + logWarn, +})); let tmp: string; beforeAll(() => { @@ -33,15 +41,30 @@ afterAll(() => { fs.rmSync(tmp, { recursive: true, force: true }); }); -/** An ORM whose Kysely client records the SQL it is handed. */ -function fakeOrm(executed: string[]): MikroORM { +/** + * An ORM whose Kysely client records the SQL it is handed, and whose pool + * hands out a client answering the role check with `privileged` (or failing + * with it, when it is an Error). + */ +function fakeOrm(executed: string[], privileged: boolean | Error = false, released: boolean[] = []): MikroORM { const client = { executeQuery: async (query: { sql: string }) => { executed.push(query.sql); return { rows: [] }; }, }; - return { em: { getConnection: () => ({ getClient: () => client }) } } as unknown as MikroORM; + const pgClient = { + query: async (sql: string) => { + executed.push(sql); + if (privileged instanceof Error) throw privileged; + return { rows: [{ privileged }], rowCount: 1 }; + }, + release: (destroy?: boolean) => released.push(destroy === true), + }; + const pool = { connect: async () => pgClient }; + return { + em: { getConnection: () => ({ getClient: () => client, getNativeClient: async () => pool }) }, + } as unknown as MikroORM; } const POSTGRES = { engine: 'postgres', url: 'postgres://trek@db/trek', poolMax: 4 } as unknown as DbSettings; @@ -101,4 +124,30 @@ describe('plugin data port', () => { await expect(store.query('SELECT 1')).rejects.toThrow('The database connection is not open'); await expect(store.query('SELECT * FROM public.users')).rejects.toThrow('The database connection is not open'); }); + + it("PLUGDATA-006: on Postgres the boot log says when TREK's role keeps plugin databases refused, and why", async () => { + logWarn.mockClear(); + const released: boolean[] = []; + await new PostgresPluginDataBackend(fakeOrm([], true, released), POSTGRES).onApplicationBootstrap(); + expect(logWarn).toHaveBeenCalledWith(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + expect(PRIVILEGED_ROLE_PLUGIN_REFUSAL).toContain(PRIVILEGED_ROLE_DESCRIPTION); + expect(released).toEqual([false]); + + logWarn.mockClear(); + await new PostgresPluginDataBackend(fakeOrm([], false), POSTGRES).onApplicationBootstrap(); + await new PostgresPluginDataBackend(fakeOrm([], true), SQLITE).onApplicationBootstrap(); + expect(logWarn).not.toHaveBeenCalled(); + + await new PostgresPluginDataBackend(fakeOrm([], new Error('down')), POSTGRES).onApplicationBootstrap(); + expect(logWarn).toHaveBeenCalledWith("Could not check whether TREK's role may run plugin SQL: down"); + }); + + it('PLUGDATA-007: the stores of one backend share its role check, so a connection is asked once', async () => { + const executed: string[] = []; + const backend = new PostgresPluginDataBackend(fakeOrm(executed, true), POSTGRES); + await backend.onApplicationBootstrap(); + await expect(backend.open('pg-four').query('SELECT 1')).rejects.toThrow(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + await expect(backend.open('pg-five').query('SELECT 1')).rejects.toThrow(PRIVILEGED_ROLE_PLUGIN_REFUSAL); + expect(executed.filter((sql) => sql.includes('as privileged'))).toHaveLength(1); + }); });