mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
fix(plugins): hold plugin data on Postgres to its own schema and to one contract on both engines
The plugin SQL guard now refuses what still reached outside a plugin's schema or depended on the server: - reads of the system columns (xmin, xmax, cmin, cmax, ctid, tableoid), which show transaction ids and storage layout and do not exist on SQLite; a value of the plugin's own may still be named xmin with AS - the xid and xid8 types wherever the word stands for the type, since age() of an xid is the cluster's transaction counter - the text search functions that coerce a text argument to regconfig, a catalog lookup by name, which leave the allowlist - collations and access methods outside the set every server has, which could tell whether a locale or an extension exists Every plugin transaction pins, with SET LOCAL, the settings that decide how values print and parse (TimeZone UTC, DateStyle, the C locales, float, bytea and xml output, the text search configuration), so a plugin's output neither depends on nor reveals the server's configuration. Both stores now keep one contract. Its texts and caps live once, in plugin-data-contract.ts, and both stores throw only those: NUL in SQL, an argument or a migration id is refused on SQLite as on Postgres, transaction control is refused in every call on both engines (tx() is how a batch becomes atomic), a closed store refuses every call, and the Postgres store answers the cases better-sqlite3 names with its words. The SQLite store moves beside the Postgres one as SqlitePluginDataStore in db/engine/sqlite, takes its file from the backend so src/db never imports src/nest, and no longer needs the driver exemption. A contract test runs every violation on both stores and fails when they answer differently. The Postgres lane holds the allowlist, the pinned settings and the SQL plugins commonly write against a real database.
This commit is contained in:
@@ -10,6 +10,11 @@
|
||||
* store on a real database is the Postgres lane's
|
||||
* tests/postgres/plugin-data.test.ts.
|
||||
*/
|
||||
import {
|
||||
PLUGIN_DATA_ERRORS,
|
||||
PLUGIN_SQL_NUL_REFUSAL,
|
||||
PLUGIN_VALUE_NUL_REFUSAL,
|
||||
} from '../../../../../src/db/engine/plugin-data-contract';
|
||||
import { PRIVILEGED_ROLE_PLUGIN_REFUSAL } from '../../../../../src/db/engine/postgres/plugin-data-checks';
|
||||
import {
|
||||
dropPluginSchema,
|
||||
@@ -19,6 +24,7 @@ import {
|
||||
PostgresPluginDataStore,
|
||||
type PluginSqlClient,
|
||||
} from '../../../../../src/db/engine/postgres/plugin-data-store';
|
||||
import { PLUGIN_SESSION_SETTINGS } from '../../../../../src/db/engine/postgres/plugin-session';
|
||||
import { PRIVILEGED_ROLE_SQL } from '../../../../../src/db/engine/postgres/server-checks';
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
@@ -144,7 +150,7 @@ function pgError(code: string, message: string): Error {
|
||||
}
|
||||
|
||||
describe('Postgres plugin data store', () => {
|
||||
it('PGSTORE-001: a query runs in a transaction that makes the schema, scopes the search_path and sets the budget', async () => {
|
||||
it('PGSTORE-001: a query runs in a transaction that makes the schema, scopes the search_path, pins the settings and sets the budget', async () => {
|
||||
const { store: s, log } = store({
|
||||
rows: { 'SELECT body FROM notes': [{ body: 'a' }, { body: 'b' }, { body: 'c' }] },
|
||||
});
|
||||
@@ -157,7 +163,24 @@ describe('Postgres plugin data store', () => {
|
||||
'create schema if not exists "trek_plugin_notes"',
|
||||
'create table if not exists "trek_plugin_notes"._plugin_migrations (id text primary key, applied_at bigint)',
|
||||
'set local search_path to "trek_plugin_notes", pg_temp',
|
||||
'set local standard_conforming_strings to on',
|
||||
"set local TimeZone to 'UTC'",
|
||||
"set local DateStyle to 'ISO, MDY'",
|
||||
"set local IntervalStyle to 'postgres'",
|
||||
"set local timezone_abbreviations to 'Default'",
|
||||
"set local lc_monetary to 'C'",
|
||||
"set local lc_numeric to 'C'",
|
||||
"set local lc_time to 'C'",
|
||||
"set local extra_float_digits to '1'",
|
||||
"set local bytea_output to 'hex'",
|
||||
"set local xmlbinary to 'base64'",
|
||||
"set local xmloption to 'content'",
|
||||
"set local default_text_search_config to 'pg_catalog.simple'",
|
||||
"set local standard_conforming_strings to 'on'",
|
||||
"set local backslash_quote to 'safe_encoding'",
|
||||
"set local array_nulls to 'on'",
|
||||
"set local transform_null_equals to 'off'",
|
||||
"set local quote_all_identifiers to 'off'",
|
||||
"set local gin_fuzzy_search_limit to '0'",
|
||||
'set local statement_timeout to 2000',
|
||||
].join(';\n'),
|
||||
'SELECT body FROM notes',
|
||||
@@ -166,6 +189,13 @@ describe('Postgres plugin data store', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGSTORE-020: every pinned setting is a plain name and value, set with SET LOCAL', () => {
|
||||
for (const [name, value] of Object.entries(PLUGIN_SESSION_SETTINGS)) {
|
||||
expect(name).toMatch(/^[A-Za-z_]+$/);
|
||||
expect(value).not.toMatch(/['\\]/);
|
||||
}
|
||||
});
|
||||
|
||||
it("PGSTORE-002: a qualified name is checked against the database's schemas before the statement runs", async () => {
|
||||
const { store: s, log } = store({ schemas: ['public', 'auth', 'trek_plugin_notes'] });
|
||||
await expect(s.query('SELECT x FROM auth.secret')).rejects.toThrow(/"auth" is another schema/);
|
||||
@@ -252,7 +282,7 @@ describe('Postgres plugin data store', () => {
|
||||
|
||||
await expect(s.tx([])).resolves.toEqual({ results: [] });
|
||||
await expect(s.tx(Array.from({ length: 101 }, () => ({ sql: 'SELECT 1' })))).rejects.toThrow(/at most 100/);
|
||||
await expect(s.tx([{ sql: '/* x */ COMMIT' }])).rejects.toThrow(/transaction-control/);
|
||||
await expect(s.tx([{ sql: '/* x */ COMMIT' }])).rejects.toThrow(PLUGIN_DATA_ERRORS.txControl);
|
||||
await expect(s.tx('nope' as never)).rejects.toThrow(/requires an array/);
|
||||
});
|
||||
|
||||
@@ -281,7 +311,7 @@ describe('Postgres plugin data store', () => {
|
||||
await expect(s.query(42 as never)).rejects.toThrow('sql must be a string');
|
||||
await expect(s.query('x'.repeat(100_001))).rejects.toThrow('sql too long');
|
||||
await expect(s.query('-- only a comment')).rejects.toThrow('contains no statements');
|
||||
await expect(s.query('SELECT 1; SELECT 2')).rejects.toThrow(/exactly one statement/);
|
||||
await expect(s.query('SELECT 1; SELECT 2')).rejects.toThrow(PLUGIN_DATA_ERRORS.moreThanOneStatement);
|
||||
await expect(s.exec('DO $$ BEGIN END $$')).rejects.toThrow(/dollar-quoted/);
|
||||
expect(log).toEqual([]);
|
||||
expect(releases).toEqual([]);
|
||||
@@ -342,9 +372,12 @@ 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 () => {
|
||||
it('PGSTORE-016: SQL, 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/;
|
||||
const nul = PLUGIN_VALUE_NUL_REFUSAL;
|
||||
// The same refusal as on SQLite, before the guard reads the SQL.
|
||||
await expect(s.exec("INSERT INTO t VALUES ('a\u0000')")).rejects.toThrow(PLUGIN_SQL_NUL_REFUSAL);
|
||||
await expect(s.query('SELECT 1 FROM t WHERE k = $1', [{ nested: ['\u0000'] }])).rejects.toThrow(nul);
|
||||
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(
|
||||
@@ -416,7 +449,7 @@ describe('Postgres plugin data store', () => {
|
||||
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)');
|
||||
await expect(ok.store.query('SELECT 1; SELECT 2')).rejects.toThrow(/exactly one statement/);
|
||||
await expect(ok.store.query('SELECT 1; SELECT 2')).rejects.toThrow(PLUGIN_DATA_ERRORS.moreThanOneStatement);
|
||||
// One client per statement of the script, each after its session state was dropped; the refused read took none.
|
||||
expect(ok.releases).toEqual([false, false]);
|
||||
expect(ok.log.filter((sql) => sql === END_SESSION)).toHaveLength(2);
|
||||
|
||||
Reference in New Issue
Block a user