mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
fix(db): run plugin SQL one statement at a time and search pg_temp last
The plugin SQL guard was the only thing between a plugin and TREK's tables on Postgres, and two gaps in it were enough to leave the plugin schema. Two layers now hold even when the guard is fooled. A plugin write with no bound args went over the simple query protocol, which runs every statement in its text. Plugin statements now go through extendedQuery, which pg sends over the extended protocol whatever the args, so a second statement is a syntax error instead of a statement that runs. A marker parameter carries the request to the pool's clients, and a client without the hook binds one value too many, so it fails closed. pg_temp is searched before the search_path unless the path names it, and a temporary table lives as long as its pooled connection, so one left behind would stand in for users, sessions and every other table for the next caller. Every connection of TREK's pool now puts pg_temp last on the search_path the role configured, and a plugin transaction sets its schema with pg_temp last.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* The extended-protocol marker of plugin SQL
|
||||
* (src/db/engine/postgres/extended-query.ts), against a stand-in `pg` client
|
||||
* that records what it is handed. That Postgres then runs one statement and
|
||||
* no more is PGPLUG-010 in the Postgres lane.
|
||||
*/
|
||||
import {
|
||||
extendedQuery,
|
||||
sendMarkedQueriesExtended,
|
||||
unmarkedValues,
|
||||
} from '../../../../../src/db/engine/postgres/extended-query';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
/** A `pg` client that records the arguments of every `query` call. */
|
||||
function recordingClient() {
|
||||
const calls: unknown[][] = [];
|
||||
const client = {
|
||||
query: (...args: unknown[]): unknown => {
|
||||
calls.push(args);
|
||||
return Promise.resolve({ rows: [] });
|
||||
},
|
||||
};
|
||||
return { client, calls };
|
||||
}
|
||||
|
||||
describe('extended query', () => {
|
||||
it('EXTQ-001: a marked query reaches pg as one extended-protocol query with its own values, even with none', async () => {
|
||||
const { client, calls } = recordingClient();
|
||||
sendMarkedQueriesExtended(client);
|
||||
const none = extendedQuery('insert into t values (1)', []);
|
||||
const some = extendedQuery('insert into t values ($1, $2)', ['a', 2]);
|
||||
await client.query(none.sql, none.parameters);
|
||||
await client.query(some.sql, some.parameters);
|
||||
expect(calls).toEqual([
|
||||
[{ text: 'insert into t values (1)', values: [], queryMode: 'extended' }],
|
||||
[{ text: 'insert into t values ($1, $2)', values: ['a', 2], queryMode: 'extended' }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('EXTQ-002: any other query, a cursor included, reaches pg unchanged', async () => {
|
||||
const { client, calls } = recordingClient();
|
||||
sendMarkedQueriesExtended(client);
|
||||
const cursor = { submit: () => undefined };
|
||||
await client.query('select 1; select 2', []);
|
||||
await client.query('select $1', [1]);
|
||||
await client.query(cursor);
|
||||
expect(calls).toEqual([['select 1; select 2', []], ['select $1', [1]], [cursor]]);
|
||||
});
|
||||
|
||||
it('EXTQ-003: without the hook the marker stays a parameter, so pg still binds and Postgres refuses the count', () => {
|
||||
const query = extendedQuery('select 1', []);
|
||||
expect(query.parameters).toHaveLength(1);
|
||||
expect(unmarkedValues(query.parameters)).toEqual([]);
|
||||
expect(unmarkedValues([])).toBeNull();
|
||||
expect(unmarkedValues(['a'])).toBeNull();
|
||||
expect(unmarkedValues(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@
|
||||
* tx and migrate shapes, and one call at a time per plugin. The same store on
|
||||
* a real database is the Postgres lane's tests/postgres/plugin-data.test.ts.
|
||||
*/
|
||||
import { unmarkedValues } from '../../../../../src/db/engine/postgres/extended-query';
|
||||
import {
|
||||
dropPluginSchema,
|
||||
PluginDataSlots,
|
||||
@@ -38,6 +39,8 @@ interface FakeOptions {
|
||||
/** A Kysely client that records each statement, and `begin`/`commit`/`rollback` around each transaction. */
|
||||
function fakeDb(options: FakeOptions = {}) {
|
||||
const log: string[] = [];
|
||||
/** Every statement handed to executeQuery, with its parameters as Kysely passes them to pg. */
|
||||
const executed: Array<{ sql: string; parameters: readonly unknown[] }> = [];
|
||||
let size = 0;
|
||||
const run = (sql: string) => {
|
||||
log.push(sql);
|
||||
@@ -46,6 +49,7 @@ function fakeDb(options: FakeOptions = {}) {
|
||||
};
|
||||
const trx = {
|
||||
executeQuery: async (query: { sql: string; parameters: readonly unknown[] }) => {
|
||||
executed.push(query);
|
||||
run(query.sql);
|
||||
if (query.sql.startsWith('select nspname'))
|
||||
return { rows: (options.schemas ?? []).map((nspname) => ({ nspname })) };
|
||||
@@ -81,7 +85,7 @@ function fakeDb(options: FakeOptions = {}) {
|
||||
}),
|
||||
executeQuery: trx.executeQuery,
|
||||
};
|
||||
return { db: db as unknown as Kysely<object>, log };
|
||||
return { db: db as unknown as Kysely<object>, log, executed };
|
||||
}
|
||||
|
||||
function store(options: FakeOptions = {}, limits = {}) {
|
||||
@@ -106,7 +110,7 @@ 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"',
|
||||
'set local search_path to "trek_plugin_notes", pg_temp',
|
||||
'set local standard_conforming_strings to on',
|
||||
'set local statement_timeout to 2000',
|
||||
].join(';\n'),
|
||||
@@ -252,6 +256,23 @@ describe('Postgres plugin data store', () => {
|
||||
expect(log.filter((sql) => sql === 'begin' || sql === 'commit')).toEqual(['begin', 'commit', 'begin', 'commit']);
|
||||
});
|
||||
|
||||
it('PGSTORE-012: every plugin statement it executes goes over the extended protocol, alone and with its own args', async () => {
|
||||
const { store: s, executed } = store({ changes: 1 });
|
||||
await s.exec('INSERT INTO a VALUES (1); UPDATE a SET x = 2 -- note\rWHERE x = 1');
|
||||
await s.exec('INSERT INTO a VALUES ($1)', ['x']);
|
||||
await s.tx([{ sql: 'DELETE FROM a WHERE x = $1', args: [3] }]);
|
||||
await s.migrate('002', 'CREATE TABLE b (y int); CREATE INDEX b_y ON b (y)');
|
||||
const plugin = executed.filter((q) => /^(INSERT|UPDATE|DELETE|CREATE)/.test(q.sql));
|
||||
expect(plugin.map((q) => [q.sql, unmarkedValues(q.parameters)])).toEqual([
|
||||
['INSERT INTO a VALUES (1)', []],
|
||||
['UPDATE a SET x = 2 -- note\rWHERE x = 1', []],
|
||||
['INSERT INTO a VALUES ($1)', ['x']],
|
||||
['DELETE FROM a WHERE x = $1', [3]],
|
||||
['CREATE TABLE b (y int)', []],
|
||||
['CREATE INDEX b_y ON b (y)', []],
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGSTORE-011: dropping a plugin schema names it quoted, and an id no schema comes from drops nothing', async () => {
|
||||
const { db, log } = fakeDb();
|
||||
await dropPluginSchema(db, 'trip-notes');
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* `pg_temp` last on the search_path of TREK's pool connections
|
||||
* (src/db/engine/postgres/session-search-path.ts), without a database. That a
|
||||
* temporary table then never stands in for TREK's own is PGPLUG-011 in the
|
||||
* Postgres lane.
|
||||
*/
|
||||
import { putTempSchemaLast, searchPathWithTempLast } from '../../../../../src/db/engine/postgres/session-search-path';
|
||||
|
||||
import type { CompiledQuery, DatabaseConnection, QueryResult } from 'kysely';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('session search_path', () => {
|
||||
it.each([
|
||||
['"$user", public', '"$user", public, pg_temp'],
|
||||
['trek', 'trek, pg_temp'],
|
||||
['pg_temp, public', 'public, pg_temp'],
|
||||
['PG_TEMP,public', 'public, pg_temp'],
|
||||
['"pg_temp", "$user", public', '"$user", public, pg_temp'],
|
||||
['"a, b", "c""d", public', '"a, b", "c""d", public, pg_temp'],
|
||||
['', 'pg_temp'],
|
||||
])('SPATH-001: %j keeps its schemas in order and gets pg_temp last: %j', (path, expected) => {
|
||||
expect(searchPathWithTempLast(path)).toBe(expected);
|
||||
});
|
||||
|
||||
it('SPATH-002: the connection hook reads the path and sets it for the session, the value bound', async () => {
|
||||
const sent: Array<{ sql: string; parameters: readonly unknown[] }> = [];
|
||||
const connection = {
|
||||
executeQuery: async (query: CompiledQuery): Promise<QueryResult<unknown>> => {
|
||||
sent.push({ sql: query.sql, parameters: query.parameters });
|
||||
return { rows: query.sql === 'show search_path' ? [{ search_path: 'trek, public' }] : [] };
|
||||
},
|
||||
} as unknown as DatabaseConnection;
|
||||
await putTempSchemaLast(connection);
|
||||
expect(sent).toEqual([
|
||||
{ sql: 'show search_path', parameters: [] },
|
||||
{ sql: "select set_config('search_path', $1, false)", parameters: ['trek, public, pg_temp'] },
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user