fix(db): refuse restores for every role that can reach the database host

The privileged-role check looked only at the role's own rolsuper and at
pg_execute_server_program and pg_write_server_files. A role that is no
superuser itself but a member of one passed it and could SET ROLE to the
superuser from a crafted archive; on PostgreSQL 14 and 15 a role with
CREATEROLE could grant itself pg_execute_server_program; and
pg_read_server_files let COPY read the server's files into TREK's tables.

The check now counts membership in any superuser role, the three
predefined server roles, and CREATEROLE before 16. The boot warning and
the restore refusal describe the same list from one constant. A Postgres
lane suite creates each kind of role and evaluates the check as it, and
shows on the server it runs against whether CREATEROLE can grant itself
pg_execute_server_program.
This commit is contained in:
Maurice
2026-11-17 20:45:53 +01:00
parent e6a0a39a51
commit 2983971c8b
4 changed files with 141 additions and 13 deletions
+4 -4
View File
@@ -3,7 +3,7 @@ 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';
import { PRIVILEGED_ROLE_DESCRIPTION, PRIVILEGED_ROLE_SQL, serverMajor } from './server-checks';
import fs from 'node:fs';
import os from 'node:os';
@@ -19,7 +19,7 @@ const RESTORE_SESSION_OPTIONS = '-c lock_timeout=60s -c client_min_messages=warn
/** Why a restore refuses a role that could reach the database host (`PRIVILEGED_ROLE_SQL`). */
const PRIVILEGED_RESTORE_REFUSAL =
'TREK_DB_URL connects as a superuser, or as a role that may run programs or write files on the database server. ' +
`TREK_DB_URL connects as ${PRIVILEGED_ROLE_DESCRIPTION}. ` +
'A backup archive is a SQL script that runs as this role, so a crafted one could take over the database host; ' +
'nothing was replaced. Connect TREK as a role that owns its database and is not a superuser ' +
'(the wiki page "Database: Postgres" shows how).';
@@ -42,7 +42,7 @@ interface PgServer {
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`). */
/** Whether TREK's role could reach the database host from SQL (`PRIVILEGED_ROLE_SQL`). */
privileged: boolean;
}
@@ -176,7 +176,7 @@ async function writeRestoreScript(
* 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,
* - a role that could reach the database host from SQL (`PRIVILEGED_ROLE_SQL`),
* since the script comes from an archive and runs as that role;
* - a script past `maxScriptBytes` (BACKUP_MAX_DECOMPRESSED_MB): the dump is
* compressed once more inside the archive, so its script is held to the