import { constants } from "node:fs" import { chmod, lstat, mkdir, mkdtemp, open, realpath, rm, rmdir, writeFile } from "node:fs/promises" import path from "node:path" import { mcpStateDirectory } from "../modules/mcp/security.js" import { boundedDatabaseCommand, DatabaseDumpCommandError } from "./bounded-database-dump.js" import { DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG } from "./database-control-archive-boundary.js" import { LEGACY_PROBE_RELATIONS } from "./server-probe-legacy-source-inventory.js" import type { DatabaseArchivePeer } from "../db/database-runtime-source.js" const SERVICE = "mmapp_database_export" const RESTORE_SERVICE = "mmapp_database_restore" const unsupported = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Нужен PostgreSQL URI с одним host и значениями, представимыми в service file") /** Deliberately bounded URI subset. Unsupported syntax fails, never loses options. */ function databaseConnectionFields(connection: string): Map { if (connection.trim() !== connection || /[\u0000-\u0020\u007f]/.test(connection)) throw unsupported() // URL parsing can otherwise normalize invalid escape sequences or strip raw // controls. Reject those inputs before constructing any service parameters. try { decodeURIComponent(connection) } catch { throw unsupported() } const raw = connection.match(/^[a-z]+:\/\/([^/?#]*)(\/[^?#]*)?(?:\?[^#]*)?(?:#.*)?$/i) if (!raw || (raw[1].match(/@/g)?.length ?? 0) > 1) throw unsupported() // WHATWG URL removes dot segments before exposing pathname; libpq treats // the URI path as a database name. Refuse normalization instead of changing DB. for (const segment of (raw[2] ?? "").split("/")) { const decoded = decodeURIComponent(segment) if (decoded === "." || decoded === "..") throw unsupported() } let uri: URL try { uri = new URL(connection) } catch { throw unsupported() } if (!["postgres:", "postgresql:"].includes(uri.protocol) || connection.includes("#") || !uri.hostname || /[,/%]/.test(uri.hostname)) throw unsupported() const fields = new Map() const put = (key: string, value: string, required = false) => { if (!/^[a-z_][a-z0-9_]*$/.test(key) || key === "service" || key === "servicefile" || key === "ssl" || /[\u0000-\u001f\u007f]/.test(value) || value.trim() !== value || (required && !value)) throw unsupported() if (value) fields.set(key, value) } const decode = (value: string) => { try { return decodeURIComponent(value) } catch { throw unsupported() } } put("host", uri.hostname.replace(/^\[|\]$/g, ""), true) put("port", uri.port) put("user", decode(uri.username)) put("password", decode(uri.password)) put("dbname", decode(uri.pathname.replace(/^\//, ""))) const seen = new Set() // Query parameters override URI authority/path, as in libpq URI parsing. // Keep every key for libpq validation; do not reinterpret SSL or options. // libpq uses URI percent decoding, not HTML form decoding: literal '+' stays // '+'. URLSearchParams would silently turn it into a space (even in passwords). for (const pair of uri.search.slice(1).split("&").filter(Boolean)) { const separator = pair.indexOf("=") if (separator < 1) throw unsupported() const key = decode(pair.slice(0, separator)), value = decode(pair.slice(separator + 1)) if (seen.has(key)) throw unsupported() seen.add(key); put(key, value, true) } if (fields.get("host")?.includes(",") || fields.get("host")?.includes("/") || fields.get("hostaddr")?.includes(",") || fields.get("port")?.includes(",")) throw unsupported() return fields } function databaseServiceFile(connection: string, service: typeof SERVICE | typeof RESTORE_SERVICE): string { const fields = databaseConnectionFields(connection) if (service === RESTORE_SERVICE && (!fields.get("user") || !fields.get("dbname"))) throw unsupported() return `[${service}]\n` + [...fields].map(([key, value]) => `${key}=${value}\n`).join("") } export const databaseDumpServiceFile = (connection: string) => databaseServiceFile(connection, SERVICE) export function restoreDatabaseToolEnvironment(): NodeJS.ProcessEnv { return Object.fromEntries(["PATH", "SystemRoot", "WINDIR", "TEMP", "TMP", "LANG", "LC_ALL"] .map(key => [key, process.env[key]]).filter(([, value]) => value !== undefined)) } async function withPrivateDatabaseService(connection: string, service: typeof SERVICE | typeof RESTORE_SERVICE, run: (file: string) => Promise): Promise { const contents = databaseServiceFile(connection, service) return withPrivateDatabaseServiceFiles(service, () => contents, run) } async function withPrivateDatabaseServiceFiles(service: typeof SERVICE | typeof RESTORE_SERVICE, contents: (leafFile: string) => string, run: (file: string) => Promise, leaf?: string, root = mcpStateDirectory()): Promise { await mkdir(root, { recursive: true, mode: 0o700 }) const directory = await mkdtemp(path.join(root, service === SERVICE ? "database-export-" : "database-restore-")) const file = path.join(directory, "pg_service.conf") const leafFile = path.join(directory, "postgresql-leaf.pem") try { await chmod(directory, 0o700) if (leaf) await writeFile(leafFile, leaf, { flag: "wx", mode: 0o600 }) await writeFile(file, contents(leafFile), { flag: "wx", mode: 0o600 }) return await run(file) } finally { try { await Promise.all([rm(file, { force: true }), rm(leafFile, { force: true })]); await rmdir(directory) } catch { throw new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_CLEANUP", "Не удалось удалить временные параметры подключения к базе") } } } /** No connection URI or password enters argv. The file outlives child draining only. */ export async function runPrivateDatabaseDump(connection: string, options: Omit[2]>, "env">, command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { return withPrivateDatabaseService(connection, SERVICE, file => command("pg_dump", ["-Fc", DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG], { ...options, env: { ...process.env, PGSERVICEFILE: file, PGSERVICE: SERVICE } })) } type DumpOptions = Omit[2]>, "env"> export async function runPinnedDatabaseSnapshotDump(peer: DatabaseArchivePeer, passwordFile: string, snapshot: string, options: DumpOptions, command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { return runPinnedSnapshotDump(peer, await readDatabaseArchivePassword(passwordFile), snapshot, ["--schema=public"], options, command) } /** Publisher's transport already owns the password and the protected output directory. */ export async function runPinnedLegacyProbeSnapshotDump(peer: DatabaseArchivePeer, password: string, snapshot: string, directory: string, options: DumpOptions, command: typeof boundedDatabaseCommand = boundedDatabaseCommand): Promise { return runPinnedSnapshotDump(peer, password, snapshot, ["--data-only", ...LEGACY_PROBE_RELATIONS.map(name => `--table=public.${name}`)], options, command, directory) } async function runPinnedSnapshotDump(peer: DatabaseArchivePeer, password: string, snapshot: string, selection: string[], options: DumpOptions, command: typeof boundedDatabaseCommand, directory?: string): Promise { const refused = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Не подтверждены параметры защищённого экспорта PostgreSQL") if (!/^[0-9A-F]{8,16}-[0-9A-F]{8,16}-[1-9][0-9]*$/.test(snapshot)) throw refused() const value = (field: string) => { if (!field || field.trim() !== field || /[\x00-\x1f\x7f]/.test(field)) throw refused() return field } return withPrivateDatabaseServiceFiles(SERVICE, leafFile => `[${SERVICE}] host=${value(peer.host)} port=${peer.port} user=${value(peer.capture.name)} password=${value(password)} dbname=${value(peer.database)} sslmode=verify-full sslrootcert=${value(leafFile)} sslcertmode=disable gssencmode=disable connect_timeout=5 options=-c default_transaction_read_only=on `, file => command("pg_dump", ["-Fc", "--no-password", ...selection, "--strict-names", `--snapshot=${snapshot}`], { ...options, env: { ...restoreDatabaseToolEnvironment(), PGSERVICEFILE: file, PGSERVICE: SERVICE }, }), peer.leaf, directory) } export async function readDatabaseArchivePassword(file: string): Promise { const refused = () => new DatabaseDumpCommandError("DATABASE_DUMP_CONNECTION_UNSUPPORTED", "Не подтверждён файл пароля роли архивирования PostgreSQL") const password = await readPrivateConnectionFile(file).catch(() => { throw refused() }) if (!password || password.length > 4096 || password.trim() !== password || /[\x00-\x1f\x7f]/.test(password)) throw refused() return password } export function validatePrivateRestoreConnection(connection: string): void { databaseServiceFile(connection, RESTORE_SERVICE) } /** The optional owner secret stays on disk until an admitted restore needs it. */ export async function readRestoreOwnerConnection(file: string): Promise<{ connection: string; owner: string }> { const refused = () => new Error("Не удалось подтвердить файл подключения владельца базы") try { const connection = await readPrivateConnectionFile(file) validatePrivateRestoreConnection(connection) const owner = databaseConnectionFields(connection).get("user")! if (!/^[a-z_][a-z0-9_]{0,62}$/.test(owner)) throw refused() return { connection, owner } } catch { throw refused() } } async function readPrivateConnectionFile(file: string): Promise { const refused = () => new Error("Не удалось подтвердить файл подключения PostgreSQL") if (!path.isAbsolute(file) || await realpath(file) !== path.resolve(file)) throw refused() const entry = await lstat(file, { bigint: true }) if (!entry.isFile()) throw refused() const handle = await open(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)) try { const before = await handle.stat({ bigint: true }) if (!before.isFile() || before.dev !== entry.dev || before.ino !== entry.ino || before.size < 1n || before.size > 8192n || (typeof process.getuid === "function" && (before.uid !== BigInt(process.getuid()) || (before.mode & 0o077n) !== 0n))) throw refused() const buffer = Buffer.alloc(Number(before.size) + 1) try { let bytes = 0 while (bytes < buffer.length) { const next = await handle.read(buffer, bytes, buffer.length - bytes, bytes) if (!next.bytesRead) break bytes += next.bytesRead } const after = await handle.stat({ bigint: true }) if (BigInt(bytes) !== before.size || after.size !== before.size || after.mtimeNs !== before.mtimeNs || after.ctimeNs !== before.ctimeNs) throw refused() return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, bytes)).replace(/\r?\n$/, "") } finally { buffer.fill(0) } } finally { await handle.close() } } export function withPrivateRestoreService(connection: string, run: (service: { dsn: string; env: NodeJS.ProcessEnv }) => Promise): Promise { return withPrivateDatabaseService(connection, RESTORE_SERVICE, file => run({ dsn: `service=${RESTORE_SERVICE}`, env: { ...restoreDatabaseToolEnvironment(), PGSERVICEFILE: file, PGSERVICE: RESTORE_SERVICE } })) }