Files
TREK/server/src/db/engine/postgres/session-search-path.ts
T
Maurice 87612c0c79 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.
2026-11-17 23:04:07 +01:00

44 lines
2.0 KiB
TypeScript

import { CompiledQuery, type DatabaseConnection } from 'kysely';
/**
* Puts `pg_temp` last on the `search_path` of every connection of TREK's pool.
*
* Postgres searches the session's temporary schema before everything on the
* `search_path` unless the path names `pg_temp` itself, and a temporary table
* lives as long as its connection. On a pool, whatever one caller left there
* would stand in for TREK's tables (`users`, `sessions`, ...) for every later
* caller of that connection. Named last, the temporary schema is searched
* after every schema TREK uses, so its tables are never found first. The rest
* of the path stays as the role or the database configured it: TREK may run
* in a schema other than `public`, and finds it through that path.
*
* Plugin transactions set their own path the same way
* (`plugin-data-store.ts`); `plugin-sql-guard.ts` explains the layers.
*/
/** `path` (as `SHOW search_path` prints it) with every `pg_temp` entry taken out and one put last. */
export function searchPathWithTempLast(path: string): string {
const entries: string[] = [];
let current = '';
let quoted = false;
for (const ch of path) {
if (ch === '"') quoted = !quoted;
if (ch === ',' && !quoted) {
entries.push(current.trim());
current = '';
} else {
current += ch;
}
}
entries.push(current.trim());
const kept = entries.filter((entry) => entry !== '' && entry.toLowerCase() !== 'pg_temp' && entry !== '"pg_temp"');
return [...kept, 'pg_temp'].join(', ');
}
/** The connection hook (`onCreateConnection`) that sets {@link searchPathWithTempLast} for the session. */
export async function putTempSchemaLast(connection: DatabaseConnection): Promise<void> {
const shown = await connection.executeQuery<{ search_path: string }>(CompiledQuery.raw('show search_path'));
const path = searchPathWithTempLast(shown.rows[0]?.search_path ?? '');
await connection.executeQuery(CompiledQuery.raw("select set_config('search_path', $1, false)", [path]));
}