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:
@@ -60,6 +60,12 @@ describe('functions a plugin may call on Postgres', () => {
|
||||
"select has_largeobject_privilege(1, 'select')",
|
||||
'select acldefault(1, 10)',
|
||||
'select setseed(0.5)',
|
||||
// A text search configuration is a regconfig, looked up by a name in a string.
|
||||
"select to_tsvector('trek_plugin_other.cfg', 'a')",
|
||||
"select to_tsvector('english', body) @@ plainto_tsquery('english', $1) from notes",
|
||||
"select to_tsquery('a'), phraseto_tsquery('a'), websearch_to_tsquery('a')",
|
||||
"select ts_headline('a', 'a'::tsquery)",
|
||||
"select jsonb_to_tsvector('{}'::jsonb, '[\"all\"]')",
|
||||
// Every spelling of the call.
|
||||
'SELECT OBJ_DESCRIPTION(16386)',
|
||||
'select "obj_description"(16386)',
|
||||
@@ -92,6 +98,15 @@ describe('functions a plugin may call on Postgres', () => {
|
||||
expect(() => parsePluginSql(sql)).toThrow(/a plugin may not (call|read)/);
|
||||
});
|
||||
|
||||
it.each([
|
||||
// xid is the transaction id type age() reads the server's counter with; after a dot it is a cast.
|
||||
"select age('3'::xid)",
|
||||
'select n.xid from notes n',
|
||||
'select $1.xid',
|
||||
])('refuses %s as a transaction id', (sql) => {
|
||||
expect(() => parsePluginSql(sql)).toThrow(/stands for a transaction id type/);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"select lower(body), substring(body from 2 for 3), regexp_replace(body, 'a', 'b') from notes",
|
||||
"select now(), date_trunc('day', now()), extract(epoch from now()), to_char(now(), 'YYYY')",
|
||||
@@ -100,7 +115,9 @@ describe('functions a plugin may call on Postgres', () => {
|
||||
'select gen_random_uuid(), md5(body), sha256(body::bytea) from notes',
|
||||
'select x from generate_series(1, 5) as g(x)',
|
||||
'select unnest(array[1, 2]), cardinality(array[1])',
|
||||
"select to_tsvector('english', body) @@ plainto_tsquery('english', $1) from notes",
|
||||
// Text search on vectors a plugin builds itself, with no configuration to look up.
|
||||
"select ts_rank(array_to_tsvector(tags), 'a & b'::tsquery), setweight(array_to_tsvector(tags), 'A') from notes",
|
||||
'select body from notes where array_to_tsvector(tags) @@ $1::tsquery',
|
||||
'select coalesce(a, b), nullif(a, 0), greatest(a, b), least(a, b) from notes',
|
||||
"select cast(n as numeric(10, 2)), body::varchar(20), 'now'::timestamp(3) from notes",
|
||||
'select current_date, current_timestamp, localtimestamp',
|
||||
@@ -115,7 +132,7 @@ describe('functions a plugin may call on Postgres', () => {
|
||||
'CREATE VIEW version (a) AS SELECT 1',
|
||||
'select n.version, n.user_id, "user", "current_user" from notes n',
|
||||
// The refused names a table may well have as a column, after a dot.
|
||||
'select n.name, n.money, n.oid, n.xid, n.xml, n.version from notes n',
|
||||
'select n.name, n.money, n.oid, n.xml, n.version from notes n',
|
||||
'select (n).name, $1.name from notes n',
|
||||
"insert into notes (id, name) values (1, 'a') on conflict (id) do update set name = excluded.name",
|
||||
'select n.body from trek_plugin_notes.notes n join trek_plugin_notes.version v on v.id = n.id',
|
||||
|
||||
Reference in New Issue
Block a user