mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
87612c0c79
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.
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
/**
|
|
* 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();
|
|
});
|
|
});
|