fix(db): carry the plugin data schemas in Postgres backups and restores

pg_dump now names the trek_plugin_ schemas TREK's role owns beside its own
schema. A restore reads the dump's plugin schemas from its table of
contents, drops the plugin schemas the role owns, creates the dump's and
loads them with TREK's schema, all in the one psql transaction. Schemas of
another role and every other schema of the database are never read or
dropped.

The dump's migration history is read from TREK's mikro_orm_migrations only,
so a plugin table of that name cannot pass for it.
This commit is contained in:
Maurice
2026-11-17 13:00:55 +01:00
parent 3460511fa4
commit b82cd03a28
4 changed files with 155 additions and 23 deletions
@@ -1,3 +1,5 @@
import { PLUGIN_SCHEMA_PREFIX, quotePluginSchema } from './plugin-schema';
/**
* Drops every table, view, sequence, routine, collation and standalone type in
* the current schema, except an extension's, before a dump is loaded: a dump
@@ -39,3 +41,25 @@ BEGIN
END
$clear$;
`;
/**
* Drops every plugin data schema TREK's role owns, then creates the ones
* `schemas` names (a dump's) empty, for the restore script to fill: the script
* holds what is in them, never the schemas themselves. Runs right after
* CLEAR_SCHEMA_SQL, in the same transaction. A schema another role owns is
* never dropped, whatever its name.
*/
export function pluginSchemasSql(schemas: readonly string[]): string {
const drop = `DO $plugins$
DECLARE r record;
BEGIN
FOR r IN SELECT nspname FROM pg_namespace
WHERE starts_with(nspname, '${PLUGIN_SCHEMA_PREFIX}') AND pg_get_userbyid(nspowner) = current_user
LOOP
EXECUTE format('DROP SCHEMA %I CASCADE', r.nspname);
END LOOP;
END
$plugins$;
`;
return drop + schemas.map((name) => `CREATE SCHEMA ${quotePluginSchema(name)};\n`).join('');
}
+59 -17
View File
@@ -1,6 +1,7 @@
import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog';
import { CLEAR_SCHEMA_SQL } from './clear-schema';
import { CLEAR_SCHEMA_SQL, pluginSchemasSql } from './clear-schema';
import { pgConnectionEnv, PgOutputLimitError, refuseClientOlderThanServer, run, runToFile } from './pg-process';
import { isPluginSchemaName, PLUGIN_SCHEMA_PREFIX } from './plugin-schema';
import { refuseToolsWithoutRestrict } from './restore-safety';
import { PRIVILEGED_ROLE_SQL, serverMajor } from './server-checks';
@@ -37,16 +38,24 @@ interface PgSchema {
/** What a dump or a restore needs to know about the server before it runs. */
interface PgServer {
schema: PgSchema;
/** The plugin data schemas TREK's role owns (`plugin-schema.ts`), which a dump holds beside TREK's own. */
pluginSchemas: string[];
/** The server's major version, which the client tools must reach. */
major: number;
/** Whether TREK's role could run programs or write files on the database host (`PRIVILEGED_ROLE_SQL`). */
privileged: boolean;
}
/** One line, tab-separated: the current schema, its quoted name, the server version, whether the role is privileged. */
/**
* One line, tab-separated: the current schema, its quoted name, the server
* version, whether the role is privileged, and the plugin schemas the role
* owns, comma-separated.
*/
const SERVER_SQL =
"select concat_ws(E'\\t', coalesce(current_schema(), ''), coalesce(quote_ident(current_schema()), ''), " +
`current_setting('server_version_num'), (${PRIVILEGED_ROLE_SQL})::text)`;
`current_setting('server_version_num'), (${PRIVILEGED_ROLE_SQL})::text, ` +
"(select coalesce(string_agg(nspname, ',' order by nspname), '') from pg_namespace " +
`where starts_with(nspname, '${PLUGIN_SCHEMA_PREFIX}') and pg_get_userbyid(nspowner) = current_user))`;
/**
* The current schema of the database at `url`, the one TREK creates its
@@ -62,16 +71,22 @@ async function pgServer(url: string, binDir: string | undefined): Promise<PgServ
['--no-psqlrc', '--no-align', '--tuples-only', `--command=${SERVER_SQL}`],
pgConnectionEnv(url),
);
const [name, quoted, version, privileged] = out.replace(/\n$/, '').split('\t');
const [name, quoted, version, privileged, plugins = ''] = out.replace(/\n$/, '').split('\t');
if (!name || !quoted) {
throw new Error('The database has no current schema: set the search_path of the role in TREK_DB_URL.');
}
return { schema: { name, quoted }, major: serverMajor(version), privileged: privileged === 'true' };
return {
schema: { name, quoted },
pluginSchemas: plugins.split(',').filter(isPluginSchemaName),
major: serverMajor(version),
privileged: privileged === 'true',
};
}
/**
* Writes a custom-format dump of the current schema of the database at `url`
* to `target`, readable by its owner only. It is written beside the target and
* Writes a custom-format dump of the current schema of the database at `url`,
* and of the plugin data schemas TREK's role owns, to `target`, readable by
* its owner only. No other schema of the database is read. It is written beside the target and
* renamed into place, so a dump that fails or is killed never leaves a file
* that passes for a copy. A pg_dump older than the server is refused first.
*/
@@ -79,7 +94,7 @@ export async function pgDump(url: string, target: string, binDir: string | undef
const partial = `${target}.partial`;
try {
createPrivateFile(partial);
const { schema, major } = await pgServer(url, binDir);
const { schema, pluginSchemas, major } = await pgServer(url, binDir);
await refuseClientOlderThanServer(['pg_dump'], binDir, major);
await run(
'pg_dump',
@@ -90,6 +105,8 @@ export async function pgDump(url: string, target: string, binDir: string | undef
'--no-privileges',
`--file=${partial}`,
`--schema="${schema.name.replaceAll('"', '""')}"`,
// A plugin schema's name holds lowercase letters, digits and underscores, none of them pattern syntax.
...pluginSchemas.map((plugin) => `--schema=${plugin}`),
],
pgConnectionEnv(url),
);
@@ -131,11 +148,12 @@ async function scriptHoldsTrekSchema(script: string, schema: PgSchema): Promise<
async function writeRestoreScript(
source: string,
script: string,
schema: PgSchema,
schemas: readonly string[],
binDir: string | undefined,
maxBytes: number,
): Promise<void> {
const args = ['--no-owner', '--no-privileges', `--schema=${schema.name}`, '--file=-', '--', source];
const only = schemas.map((name) => `--schema=${name}`);
const args = ['--no-owner', '--no-privileges', ...only, '--file=-', '--', source];
try {
await runToFile('pg_restore', binDir, args, {}, script, maxBytes);
} catch (err) {
@@ -150,10 +168,12 @@ async function writeRestoreScript(
/**
* Replaces the current schema of the database at `url` with the dump at
* `source`: pg_restore turns the dump's objects in that schema into a script
* beside it (anything else the dump holds is left out), then psql clears the
* schema and runs the script in one transaction, so a restore that fails
* leaves the database as it was. Refused before anything is cleared:
* `source`, and the plugin data schemas with the dump's: pg_restore turns the
* dump's objects in those schemas into a script beside it (anything else the
* dump holds is left out), then psql clears the schema, drops the plugin
* schemas TREK's role owns, creates the dump's and runs the script, all in
* one transaction, so a restore that fails leaves the database as it was. No
* other schema of the database is touched. Refused before anything is cleared:
*
* - client tools without the CVE-2025-8714 fix, or older than the server;
* - a role that could run programs or write files on the database host,
@@ -179,9 +199,10 @@ export async function pgRestore(
try {
const clear = path.join(workDir, 'clear-schema.sql');
const script = path.join(workDir, 'restore.sql');
fs.writeFileSync(clear, CLEAR_SCHEMA_SQL, { mode: 0o600 });
const pluginSchemas = await dumpPluginSchemas(source, path.join(workDir, 'toc.txt'), binDir);
fs.writeFileSync(clear, CLEAR_SCHEMA_SQL + pluginSchemasSql(pluginSchemas), { mode: 0o600 });
createPrivateFile(script);
await writeRestoreScript(source, script, schema, binDir, maxScriptBytes);
await writeRestoreScript(source, script, [schema.name, ...pluginSchemas], binDir, maxScriptBytes);
if (!(await scriptHoldsTrekSchema(script, schema))) {
throw new Error(`The backup holds no TREK tables in the schema "${schema.name}"; nothing was replaced.`);
}
@@ -204,6 +225,23 @@ export async function pgRestore(
}
}
/**
* The plugin data schemas the dump at `source` holds, read from its table of
* contents (`pg_restore --list`, written to `toc`), which has a line
* `<id>; <oid> <oid> SCHEMA - <name> <owner>` per schema. Only a name TREK
* gives a plugin schema counts.
*/
async function dumpPluginSchemas(source: string, toc: string, binDir: string | undefined): Promise<string[]> {
createPrivateFile(toc);
await run('pg_restore', binDir, ['--list', `--file=${toc}`, '--', source], {});
const names = new Set<string>();
for (const line of fs.readFileSync(toc, 'utf8').split('\n')) {
const name = /^\d+; \d+ \d+ SCHEMA - (\S+) /.exec(line)?.[1];
if (name !== undefined && isPluginSchemaName(name)) names.add(name);
}
return [...names].sort((a, b) => a.localeCompare(b));
}
/** The tables in the current schema of the database at `url`, read before TREK opens it. */
export async function pgTableNames(url: string, binDir: string | undefined): Promise<string[]> {
const out = await run(
@@ -222,7 +260,11 @@ export async function pgTableNames(url: string, binDir: string | undefined): Pro
*/
function migrationNamesFromCopy(script: string): string[] {
const lines = script.split('\n');
const copy = lines.findIndex((line) => /^COPY \S*mikro_orm_migrations \(/.test(line));
// A plugin may name a table of its own schema mikro_orm_migrations too; only TREK's counts.
const copy = lines.findIndex((line) => {
const match = /^COPY (?:(\S+)\.)?mikro_orm_migrations \(/.exec(line);
return match !== null && !isPluginSchemaName(match[1] ?? '');
});
if (copy < 0) return [];
const columns =
/\(([^)]*)\)/
+16 -4
View File
@@ -44,16 +44,19 @@ interface FakeServer {
versionNum?: string;
/** Whether TREK's role could reach the database host. */
privileged?: boolean;
/** The plugin data schemas the role owns; none by default. */
pluginSchemas?: readonly string[];
}
/**
* psql's answer about the server, as pgServer reads it: the schema's name, its
* quote_ident spelling, the server's version number and whether the role is
* privileged, tab-separated.
* quote_ident spelling, the server's version number, whether the role is
* privileged and the plugin schemas it owns, tab-separated.
*/
export function answersSchema(name = 'public', quoted = name, server: FakeServer = {}): string {
const { versionNum = '170000', privileged = false } = server;
return printsWhen('quote_ident', `${name}\t${quoted}\t${versionNum}\t${String(privileged)}\n`);
const { versionNum = '170000', privileged = false, pluginSchemas = [] } = server;
const plugins = pluginSchemas.join(',');
return printsWhen('quote_ident', `${name}\t${quoted}\t${versionNum}\t${String(privileged)}\t${plugins}\n`);
}
/** psql's table listing, one name per line, as pgTableNames reads it. */
@@ -71,6 +74,15 @@ export function writesRestrictedScript(table = 'public.mikro_orm_migrations'): s
return writesFile(`\\restrict k1\nCREATE TABLE ${table} (\n);\n`);
}
/**
* Answers a `--list` call with a table of contents holding a SCHEMA line for
* each of `schemas`, written to its `--file=`, as `pg_restore --list` does.
*/
export function listsSchemas(schemas: readonly string[]): string {
const toc = schemas.map((name, i) => `${i + 4}; 2615 ${16000 + i} SCHEMA - ${name} trek\n`).join('');
return `case "$*" in *--list*) printf '%s' ${shellWord(toc)} > "$(fake_file_target "$@")"; exit 0;; esac`;
}
/** Answers a `--data-only` call with the COPY block of `mikro_orm_migrations`, one row per name. */
export function listsMigrations(names: readonly string[]): string {
const rows = names.map((name, i) => `${i + 1}\t${name}\tx\n`).join('');
@@ -17,6 +17,7 @@ import {
flagValues,
installFakeTool,
listsMigrations,
listsSchemas,
printsWhen,
readToolCall,
touches,
@@ -264,7 +265,9 @@ describe('pg-tools', () => {
it('PGTOOLS-013: a restore refuses a role that could reach the database host, before it writes a script', async () => {
const ran = path.join(bin, 'ran');
installFakeTool(bin, 'pg_restore', { snippets: [touches(ran)] });
const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema('public', 'public', { privileged: true })] });
const psqlLog = installFakeTool(bin, 'psql', {
snippets: [answersSchema('public', 'public', { privileged: true })],
});
await expect(pgRestore(URL_WITH_SECRET, path.join(out, 'in.pgdump'), bin, LIMIT)).rejects.toThrow(
/connects as a superuser, or as a role that may run programs or write files on the database server/,
);
@@ -326,7 +329,8 @@ describe('pg-tools', () => {
it('PGTOOLS-017: a failed tool leaves the rows it quoted out of the error, its cause included', async () => {
installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
const stderr = 'pg_dump: error: COPY failed\nDETAIL: Key (email)=(a@b.c)\nCONTEXT: COPY users, line 2: "secret-row"';
const stderr =
'pg_dump: error: COPY failed\nDETAIL: Key (email)=(a@b.c)\nCONTEXT: COPY users, line 2: "secret-row"';
installFakeTool(bin, 'pg_dump', { snippets: [failsWith(stderr)] });
const failure = await pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin).catch((err: unknown) => err);
const printed = inspect(failure, { depth: null });
@@ -335,4 +339,54 @@ describe('pg-tools', () => {
expect(printed).not.toContain('secret-row');
expect(printed).not.toContain('a@b.c');
});
it("PGTOOLS-018: a dump holds the plugin schemas TREK's role owns beside its own schema, and nothing else", async () => {
const plugins = ['trek_plugin_notes', 'trek_plugin_trip_weather'];
const psqlLog = installFakeTool(bin, 'psql', {
snippets: [answersSchema('public', 'public', { pluginSchemas: [...plugins, 'auth'] })],
});
const dumpLog = installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
await pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin);
const args = readToolCall(dumpLog).args;
expect(flagValues(args, '--schema=')).toEqual(['"public"', ...plugins]);
const sql = readToolCall(psqlLog).args.join(' ');
expect(sql).toContain("starts_with(nspname, 'trek_plugin_')");
expect(sql).toContain('pg_get_userbyid(nspowner) = current_user');
});
it("PGTOOLS-019: a restore loads the dump's plugin schemas, after dropping the role's own and creating the dump's", async () => {
const clearCopy = path.join(bin, 'clear.sql');
const restoreLog = installFakeTool(bin, 'pg_restore', {
snippets: [listsSchemas(['trek_plugin_notes', 'auth', 'public']), writesRestrictedScript()],
});
installFakeTool(bin, 'psql', {
snippets: [answersSchema(), copiesFileEndingWith('clear-schema.sql', clearCopy)],
});
const source = path.join(out, 'in.pgdump');
await pgRestore(URL_WITH_SECRET, source, bin, LIMIT);
expect(flagValues(readToolCall(restoreLog).args, '--schema=')).toEqual(['public', 'trek_plugin_notes']);
const clear = readFileSync(clearCopy, 'utf8');
expect(clear).toMatch(/DROP %s IF EXISTS %s CASCADE/);
expect(clear).toContain("EXECUTE format('DROP SCHEMA %I CASCADE', r.nspname);");
expect(clear).toContain('pg_get_userbyid(nspowner) = current_user');
expect(clear.endsWith('CREATE SCHEMA "trek_plugin_notes";\n')).toBe(true);
expect(clear).not.toContain('CREATE SCHEMA "auth"');
expect(readdirSync(out)).toEqual([]);
});
it("PGTOOLS-020: a plugin's own table named mikro_orm_migrations never passes for TREK's history", async () => {
const forged =
'COPY trek_plugin_notes.mikro_orm_migrations (id, name) FROM stdin;\n1\tMigration29990101000000_forged\n\.\n';
const real =
'COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n1\tMigration20200101000000_baseline_schema\tx\n\.\n';
installFakeTool(bin, 'pg_restore', { snippets: [printsWhen('--data-only', forged + real)] });
await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([
'Migration20200101000000_baseline_schema',
]);
installFakeTool(bin, 'pg_restore', { snippets: [printsWhen('--data-only', forged)] });
await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([]);
});
});