mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
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:
@@ -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
|
||||
|
||||
@@ -11,16 +11,34 @@ import { REFUSAL } from '../../legacy-baseline';
|
||||
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.
|
||||
* True when the current role could reach the database host, or make itself
|
||||
* able to, from SQL. A restored archive is a SQL script run as that role, so
|
||||
* such a role turns a crafted archive into a way onto the host. It counts:
|
||||
*
|
||||
* - membership in any superuser role, its own included: `SET ROLE` to one is a
|
||||
* superuser session, whatever the role's own `rolsuper` says;
|
||||
* - membership in `pg_execute_server_program` (`COPY ... PROGRAM`),
|
||||
* `pg_write_server_files` or `pg_read_server_files` (`COPY` to or from a
|
||||
* server file), which a superuser holds implicitly;
|
||||
* - CREATEROLE before PostgreSQL 16, where it may grant itself any role that
|
||||
* is not a superuser, those predefined roles included. From 16 on it needs
|
||||
* ADMIN OPTION on a role to grant it, which the checks above already see.
|
||||
*
|
||||
* `MEMBER` is membership whatever its INHERIT and SET options, since a member
|
||||
* that can neither may still be one that can grant itself the rest.
|
||||
*/
|
||||
export const PRIVILEGED_ROLE_SQL =
|
||||
'coalesce((select r.rolsuper from pg_roles r where r.rolname = current_user), false)' +
|
||||
"exists (select 1 from pg_roles r where r.rolsuper and pg_has_role(current_user, r.oid, 'MEMBER'))" +
|
||||
" or pg_has_role(current_user, 'pg_execute_server_program', 'MEMBER')" +
|
||||
" or pg_has_role(current_user, 'pg_write_server_files', 'MEMBER')";
|
||||
" or pg_has_role(current_user, 'pg_write_server_files', 'MEMBER')" +
|
||||
" or pg_has_role(current_user, 'pg_read_server_files', 'MEMBER')" +
|
||||
" or (current_setting('server_version_num')::int < 160000" +
|
||||
' and coalesce((select r.rolcreaterole from pg_roles r where r.rolname = current_user), false))';
|
||||
|
||||
/** What {@link PRIVILEGED_ROLE_SQL} refuses, as the boot warning and the restore refusal say it. */
|
||||
export const PRIVILEGED_ROLE_DESCRIPTION =
|
||||
'a superuser or a member of a superuser role, a role that may run programs or read or write files on the ' +
|
||||
'database server, or (before PostgreSQL 16) a role with CREATEROLE, which may grant itself those';
|
||||
|
||||
/** 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 {
|
||||
@@ -47,6 +65,6 @@ export function refuseUnsupportedServer(major: number): void {
|
||||
|
||||
/** 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. ' +
|
||||
`TREK_DB_URL connects as ${PRIVILEGED_ROLE_DESCRIPTION}. ` +
|
||||
'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").';
|
||||
|
||||
Reference in New Issue
Block a user