Files
TREK/server/src/db/engine/postgres/server-checks.ts
T
Maurice 6f4dfc486f 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 12:23:51 +01:00

53 lines
2.5 KiB
TypeScript

import { REFUSAL } from '../../legacy-baseline';
/**
* What TREK checks about the Postgres server and its own role: the server
* versions it supports, and whether the role could reach the database host.
* Read at boot (`schema-bootstrap.ts`) and before a dump or a restore
* (`pg-tools.ts`).
*/
/** The PostgreSQL majors TREK supports and CI runs: the oldest and the newest. */
export const SUPPORTED_SERVER_MAJORS = { oldest: 14, newest: 18 } as const;
/**
* True when the current role is a superuser or may run programs or write
* files on the database server (the predefined roles
* `pg_execute_server_program` and `pg_write_server_files`, which a superuser
* holds implicitly). Such a role turns a restored archive, which is a SQL
* script, into a way onto the database host.
*/
export const PRIVILEGED_ROLE_SQL =
'coalesce((select r.rolsuper from pg_roles r where r.rolname = current_user), false)' +
" or pg_has_role(current_user, 'pg_execute_server_program', 'MEMBER')" +
" or pg_has_role(current_user, 'pg_write_server_files', 'MEMBER')";
/** The major version in `server_version_num` (`170011` is 17, `180001` is 18). Throws on anything else. */
export function serverMajor(versionNum: string | number | undefined): number {
const value = Number(versionNum);
if (!Number.isInteger(value) || value < 10000) {
throw new Error(`The Postgres server reported the version number "${String(versionNum)}", which TREK cannot read.`);
}
return Math.floor(value / 10000);
}
/**
* Refuses a server outside {@link SUPPORTED_SERVER_MAJORS} before TREK writes
* anything to it: an older one lacks what the schema uses, and a newer one is
* untested and may be newer than the client tools that back it up.
*/
export function refuseUnsupportedServer(major: number): void {
const { oldest, newest } = SUPPORTED_SERVER_MAJORS;
if (major >= oldest && major <= newest) return;
throw new Error(
`${REFUSAL} the Postgres server is version ${major}; TREK supports PostgreSQL ${oldest} to ${newest}. ` +
'Point TREK_DB_URL at a server of a supported version.',
);
}
/** The boot warning for a privileged role (see {@link PRIVILEGED_ROLE_SQL}); a restore refuses such a role outright. */
export const PRIVILEGED_ROLE_WARNING =
'TREK_DB_URL connects as a superuser, or as a role that may run programs or write files on the database server. ' +
'Restores are refused for such a role. Connect TREK as a role that owns its database and is not a superuser ' +
'(see the wiki page "Database: Postgres").';