mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
fix(db): hold plugin SQL on Postgres in layers the database enforces
The token guard kept leaking: on a superuser connection an unqualified `alter type int4 rename to int4x` renamed pg_catalog's type for the whole server, sequences and catalog-only objects grew the database past the quota, regtype/regconfig/regdictionary looked TREK's objects up by name, and a column alias named insert or merge in front of INTO made a table. Instead of a longer word list, the store now asks the database: - No plugin SQL while TREK's role is privileged in the sense of the restore check (PRIVILEGED_ROLE_SQL, imported from server-checks.ts): every db:own call fails with PRIVILEGED_ROLE_PLUGIN_REFUSAL, read once per pool connection, and the boot log says why. - The objects an ALTER, DROP, REFRESH, TRUNCATE or CREATE INDEX ... ON changes, and the tables a CREATE or ALTER TABLE ties to its own, are resolved with to_regclass/to_regtype in the transaction right before the statement, and it is refused unless each one found lies in the plugin's schema. An unqualified int4, text or box is pg_catalog's and refused even with the role check off. - The quota counts every relation of the schema with its TOAST table, and a schema holds at most PLUGIN_SCHEMA_MAX_OBJECTS catalog objects (relations, types, constraints, enum labels). - The guard refuses every reg* type and its array in every spelling, the has_*_privilege functions, currtid2 and the BRIN/GIN maintenance functions; INTO only after the INSERT or MERGE that is the statement's verb or a WITH body's; TEMP only where it makes a temporary object (CREATE TEMP/TEMPORARY, SELECT ... INTO TEMP), so a column named temp works again; a base type; and a NUL anywhere in the SQL text. - A bound value or a migration id holding NUL is refused with the reason instead of being stripped, so the plugin's keys never change meaning. Every bypass from the re-review is a test on the real database, on the role the plugin data is meant to run as and on a superuser connection with the role check off.
This commit is contained in:
@@ -2,6 +2,7 @@ import type { DbSettings } from '../../../app-config';
|
||||
import type { EngineId } from '../../../app-config/db-settings';
|
||||
import { postgresTarget, type DatabaseEngine } from '../../../db/engine';
|
||||
import type { PluginDataStore } from '../../../db/engine/plugin-data';
|
||||
import { PRIVILEGED_ROLE_PLUGIN_REFUSAL, PrivilegedRoleGate } from '../../../db/engine/postgres/plugin-data-checks';
|
||||
import {
|
||||
dropPluginSchema,
|
||||
PluginDataSlots,
|
||||
@@ -9,12 +10,13 @@ import {
|
||||
poolClients,
|
||||
PostgresPluginDataStore,
|
||||
} from '../../../db/engine/postgres/plugin-data-store';
|
||||
import { logWarn } from '../../audit/audit-log.logger';
|
||||
import { DATABASE_ENGINE, DATABASE_SETTINGS } from '../../database/database-engine.module';
|
||||
import { pluginDataDir } from '../paths';
|
||||
import { PluginDataDb, removePluginData } from './plugin-data.service';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import type { AbstractSqlConnection } from '@mikro-orm/sql';
|
||||
import { Inject, Injectable, type Provider } from '@nestjs/common';
|
||||
import { Inject, Injectable, type OnApplicationBootstrap, type Provider } from '@nestjs/common';
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
import fs from 'node:fs';
|
||||
@@ -58,21 +60,44 @@ export class SqlitePluginDataBackend implements PluginDataBackend {
|
||||
* client. The plugin's data directory is still created, for the files a plugin
|
||||
* keeps beside its database, and it is what marks a plugin uninstalled with
|
||||
* its data kept (the erasure queue reads it).
|
||||
*
|
||||
* While TREK's role is privileged (`PRIVILEGED_ROLE_SQL`), every store refuses
|
||||
* its calls (`plugin-data-checks.ts`), and the boot log says so once.
|
||||
*/
|
||||
@Injectable()
|
||||
export class PostgresPluginDataBackend implements PluginDataBackend {
|
||||
export class PostgresPluginDataBackend implements PluginDataBackend, OnApplicationBootstrap {
|
||||
readonly engine = 'postgres' as const;
|
||||
/** Shared by every plugin's store; made on first use, since on SQLite this provider is built but never used. */
|
||||
private slots: PluginDataSlots | null = null;
|
||||
/** Shared by every plugin's store, so the role is read once per pool connection. */
|
||||
private readonly roleGate = new PrivilegedRoleGate();
|
||||
|
||||
constructor(
|
||||
private readonly orm: MikroORM,
|
||||
@Inject(DATABASE_SETTINGS) private readonly settings: DbSettings,
|
||||
) {}
|
||||
|
||||
/** On Postgres, says in the boot log when plugin databases are refused because of TREK's role. */
|
||||
async onApplicationBootstrap(): Promise<void> {
|
||||
if (!postgresTarget(this.settings)) return;
|
||||
try {
|
||||
const client = await poolClients(this.orm.em.getConnection())();
|
||||
try {
|
||||
if (await this.roleGate.privileged(client)) logWarn(PRIVILEGED_ROLE_PLUGIN_REFUSAL);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
} catch (err) {
|
||||
logWarn(
|
||||
`Could not check whether TREK's role may run plugin SQL: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
open(pluginId: string): PluginDataStore {
|
||||
fs.mkdirSync(pluginDataDir(pluginId), { recursive: true });
|
||||
return new PostgresPluginDataStore(pluginId, poolClients(this.orm.em.getConnection()), this.sharedSlots());
|
||||
const connect = poolClients(this.orm.em.getConnection());
|
||||
return new PostgresPluginDataStore(pluginId, connect, this.sharedSlots(), {}, this.roleGate);
|
||||
}
|
||||
|
||||
async erase(pluginId: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user