Files
TREK/server/tests/helpers/fake-pg-tools.ts
T
Maurice 116ba52fbb fix(db): check the Postgres server and tools before a dump or a restore
- The client tools are looked for newest first, from 18 down: a client
  handles every server up to its own major and refuses a newer one. A dump
  or a restore now refuses a client older than the server up front, naming
  both versions, and the boot refuses a server outside PostgreSQL 14 to 18.
- sslrootcert=system is used only by a client from libpq 16 on; an older
  client is refused with a message that names sslrootcert and the bin dir.
- The restore's CVE-2025-8714 table covers 18.
- A restore refuses a role that is a superuser or may run programs or write
  files on the database host, since the archive's SQL runs as that role;
  the boot warns about such a role.
- pg_restore's script is streamed to its work file and stopped once it
  passes BACKUP_MAX_DECOMPRESSED_MB, so a small dump can no longer expand
  without limit on the data volume.
- A failed tool's error keeps only the summarized stderr; its cause names
  the exit code and signal instead of carrying the process error, whose
  message and stderr quoted row data into the log.
- The migration history is looked for in the current schema, where the
  tables and the backups are, so a role whose search_path is not public
  boots a second time.
2026-11-17 23:03:51 +01:00

142 lines
6.0 KiB
TypeScript

/**
* Stand-in PostgreSQL client tools (pg_dump, pg_restore and psql) for the unit
* tests of the Postgres backup and restore. Each is an executable shell script
* in a bin directory, which the test hands TREK as its client-tool directory.
*
* A stand-in writes its argv and its PG* environment to `<bin>/<name>.log`
* before anything else it does, so readToolCall() says what the tool was given.
* The log holds the last call. What a call does is a list of shell snippets,
* run in order: the builders below name what a test sets up (answersSchema,
* writesFile, failsWith, ...), so a test reads as the scenario it describes.
* Output files are found by their `--file=` argument, not by position.
*/
import { chmodSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
export type PgToolName = 'pg_dump' | 'pg_restore' | 'psql';
export interface FakeToolOptions {
/** What `--version` answers after "PostgreSQL". Defaults to 17.11. */
version?: string;
/** Shell fragments run after the call is logged, in order. */
snippets?: readonly string[];
}
/** What a stand-in was called with: its arguments, and its PG* environment. */
export interface ToolCall {
args: string[];
env: Record<string, string>;
}
/** A value as one single-quoted shell word. */
function shellWord(value: string): string {
return `'${value.replaceAll("'", "'\\''")}'`;
}
/** Answers a call whose arguments contain `pattern` with `text`, and ends the call. */
export function printsWhen(pattern: string, text: string): string {
return `case "$*" in *${pattern}*) printf '%s' ${shellWord(text)}; exit 0;; esac`;
}
/** What the server says about itself beside its current schema. */
export interface FakeServer {
/** `server_version_num`; 170000 by default, which the default tool version (17.11) reaches. */
versionNum?: string;
/** Whether TREK's role could reach the database host. */
privileged?: boolean;
}
/**
* 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.
*/
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`);
}
/** psql's table listing, one name per line, as pgTableNames reads it. */
export function answersTables(names: readonly string[]): string {
return printsWhen('table_name', names.map((name) => `${name}\n`).join(''));
}
/** Writes `content` to the output file the call names with `--file=`. */
export function writesFile(content: string): string {
return `printf '%s' ${shellWord(content)} > "$(fake_file_target "$@")"`;
}
/** Writes the start of a restore script that restricts psql (CVE-2025-8714) and creates TREK's migration table. */
export function writesRestrictedScript(table = 'public.mikro_orm_migrations'): string {
return writesFile(`\\restrict k1\nCREATE TABLE ${table} (\n);\n`);
}
/** 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('');
const copy = `COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n${rows}\\.\n`;
return `case "$*" in *--data-only*) printf '%s' ${shellWord(copy)}; exit 0;; esac`;
}
/** Fails the way a tool that cannot connect does: the message on stderr, then a non-zero exit. */
export function failsWith(message: string, exitCode = 1): string {
return `printf '%s\\n' ${shellWord(message)} >&2; exit ${exitCode}`;
}
/** Creates `file`: a marker that the tool ran. */
export function touches(file: string): string {
return `touch ${shellWord(file)}`;
}
/** Copies the `--file=` output whose path ends with `suffix` to `destination`, so a test can read a work file the tool has since removed. */
export function copiesFileEndingWith(suffix: string, destination: string): string {
return `for a in "$@"; do case "$a" in --file=*${suffix}) cp "\${a#--file=}" ${shellWord(destination)};; esac; done`;
}
/**
* The function every stand-in uses to find its `--file=` argument, wherever it
* sits. `--file=-` is stdout, as the real tools read it.
*/
const FILE_TARGET_FUNCTION =
'fake_file_target() { for a in "$@"; do case "$a" in --file=-) printf /dev/stdout;; ' +
'--file=*) printf \'%s\' "${a#--file=}";; esac; done; }';
/**
* Installs the stand-in `name` in `bin` and returns the path of its call log.
* The script is executable and runs by its absolute path, as TREK runs it.
*/
export function installFakeTool(bin: string, name: PgToolName, options: FakeToolOptions = {}): string {
const { version = '17.11', snippets = [] } = options;
const log = path.join(bin, `${name}.log`);
const script = [
'#!/bin/sh',
FILE_TARGET_FUNCTION,
`[ "$1" = --version ] && { echo '${name} (PostgreSQL) ${version} (Debian)'; exit 0; }`,
`printf '%s\\n' "$@" > ${shellWord(log)}`,
`env | grep '^PG' | sort >> ${shellWord(log)}`,
...snippets,
'',
].join('\n');
writeFileSync(path.join(bin, name), script);
chmodSync(path.join(bin, name), 0o755);
return log;
}
/** What the stand-in logged last: its arguments, and the PG* variables it was run with. */
export function readToolCall(log: string): ToolCall {
const lines = readFileSync(log, 'utf8')
.split('\n')
.filter((line) => line !== '');
const env: Record<string, string> = {};
for (const line of lines.filter((line) => line.startsWith('PG'))) {
const eq = line.indexOf('=');
env[line.slice(0, eq)] = line.slice(eq + 1);
}
return { args: lines.filter((line) => !line.startsWith('PG')), env };
}
/** Every argument that starts with `flag`, with the flag removed (`--file=` gives the file paths). */
export function flagValues(args: readonly string[], flag: string): string[] {
return args.filter((arg) => arg.startsWith(flag)).map((arg) => arg.slice(flag.length));
}