mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +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.
96 lines
3.9 KiB
TypeScript
96 lines
3.9 KiB
TypeScript
import type { DbEngineSettings } from '../../../app-config/db-settings';
|
|
import { NO_SAFETY_NET } from '../../legacy-baseline';
|
|
import { finishSchemaBootstrap, type AnyOrm } from '../../orm';
|
|
import type { DatabaseEngine } from '../engine';
|
|
import { redactUrl } from '../redact-url';
|
|
import { TrekPostgreSqlDriver } from './postgres-driver';
|
|
import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap';
|
|
import { putTempSchemaLast } from './session-search-path';
|
|
import { sslOptionFor } from './ssl';
|
|
import type { EntityManager } from '@mikro-orm/core';
|
|
|
|
import type { DatabaseConnection } from 'kysely';
|
|
|
|
/** What `TREK_DB_URL` and `TREK_DB_POOL_MAX` resolve to, when they name a Postgres database. */
|
|
type PostgresSettings = Extract<DbEngineSettings, { engine: 'postgres' }>;
|
|
|
|
/** The advisory lock key every write transaction takes: "TREK" in ASCII. */
|
|
const TREK_WRITE_LOCK = 0x5452454b;
|
|
|
|
/**
|
|
* The Postgres engine: MikroORM's own `pg` driver on `TREK_DB_URL`, with a
|
|
* pool of at most `TREK_DB_POOL_MAX` connections that keeps none open while
|
|
* idle. The pool belongs to the ORM, which the Nest container closes; the
|
|
* schema comes from the baseline plus the migrations after it
|
|
* (`schema-bootstrap.ts`).
|
|
*/
|
|
export class PostgresEngine implements DatabaseEngine {
|
|
readonly id = 'postgres';
|
|
|
|
constructor(
|
|
private readonly settings: PostgresSettings,
|
|
private readonly bootstrapDeps: (url: string) => PostgresBootstrapDeps = defaultPostgresBootstrapDeps,
|
|
) {}
|
|
|
|
private onReopened: (() => Promise<void>) | null = null;
|
|
|
|
/**
|
|
* Nothing of its own to open: the ORM connects the pool. Keeps what a reopen
|
|
* runs. A reopen replaces the database's contents, which the pool's own
|
|
* connections keep reaching, so the ORM is not pointed at anything new and
|
|
* the pool is never closed (ending a pool waits for every connection it has
|
|
* lent out, and one lent to a job during the restore would never come back).
|
|
*/
|
|
open(_orm: AnyOrm, onReopened: () => Promise<void>): void {
|
|
this.onReopened = onReopened;
|
|
}
|
|
|
|
close(): void {
|
|
// Nothing of its own to close: the pool goes with the ORM.
|
|
}
|
|
|
|
/**
|
|
* After `pg_restore` replaced the database's contents: the restored
|
|
* database is migrated forward on the same pool (`onReopened`, which
|
|
* `DatabaseLifecycle` hands to `open`).
|
|
*/
|
|
async reopen(): Promise<void> {
|
|
if (!this.onReopened) throw new Error('The Postgres engine was reopened before it was opened.');
|
|
await this.onReopened();
|
|
}
|
|
|
|
/**
|
|
* `pg_advisory_xact_lock` on one key for the whole database: held until the
|
|
* transaction commits or rolls back, taken again by a savepoint in the same
|
|
* session without waiting, and shared by every process on the database.
|
|
*/
|
|
async serializeWrites(em: EntityManager): Promise<void> {
|
|
await em
|
|
.getConnection()
|
|
.execute('select pg_advisory_xact_lock(?)', [TREK_WRITE_LOCK], 'run', em.getTransactionContext());
|
|
}
|
|
|
|
/** `snapshot: false` (a restore, whose archive is the copy) migrates without a pg_dump first. */
|
|
async schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void> {
|
|
const deps = this.bootstrapDeps(this.settings.url);
|
|
await bootstrapPostgresSchema(orm, options.snapshot ? deps : { ...deps, safetyNet: NO_SAFETY_NET });
|
|
await finishSchemaBootstrap(orm);
|
|
}
|
|
|
|
describe(): string {
|
|
return `Postgres ${redactUrl(this.settings.url)}`;
|
|
}
|
|
|
|
/** Every new pool connection first puts `pg_temp` last on its `search_path` (`session-search-path.ts`). */
|
|
ormOptions() {
|
|
const ssl = sslOptionFor(this.settings.url);
|
|
return {
|
|
driver: TrekPostgreSqlDriver,
|
|
clientUrl: this.settings.url,
|
|
pool: { min: 0, max: this.settings.poolMax },
|
|
driverOptions: ssl === undefined ? {} : { ssl },
|
|
onCreateConnection: (connection: unknown) => putTempSchemaLast(connection as DatabaseConnection),
|
|
};
|
|
}
|
|
}
|