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.
This commit is contained in:
Maurice
2026-11-17 11:55:09 +01:00
parent ce1c3cf62d
commit 6f4dfc486f
10 changed files with 537 additions and 62 deletions
+22 -5
View File
@@ -38,9 +38,22 @@ export function printsWhen(pattern: string, text: string): string {
return `case "$*" in *${pattern}*) printf '%s' ${shellWord(text)}; exit 0;; esac`;
}
/** psql's current-schema answer: the schema's name, and its quote_ident spelling, as pgCurrentSchema reads them. */
export function answersSchema(name = 'public', quoted = name): string {
return printsWhen('quote_ident', `${name}\t${quoted}\n`);
/** 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. */
@@ -80,9 +93,13 @@ export function copiesFileEndingWith(suffix: string, destination: string): strin
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. */
/**
* 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 \'%s\' "${a#--file=}";; esac; done; }';
'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.