Files
MikrotikManager/backend/src/modules/mcp/database-backup-artifact.ts
T

139 lines
14 KiB
TypeScript

import { createHash, randomUUID, timingSafeEqual } from "node:crypto"
import { z } from "zod"
import type { PoolClient } from "pg"
import { pool } from "../../db/index.js"
import { env } from "../../config.js"
import { hasPermission, type AuthUser } from "../../lib/permissions.js"
import { exportSystemDatabaseBackupWithEvent } from "../../services/system-database-backup.js"
import { assertVerifiedConfigurationAccess, executeGuardedApplicationWrite } from "../../services/config-mutation-policy.js"
import { DatabaseDumpCommandError, MAX_DATABASE_DUMP_BYTES } from "../../services/bounded-database-dump.js"
import { databaseDumpServiceFile } from "../../services/database-dump-connection.js"
import { MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES } from "./log-artifacts.js"
import { assertPortalClaimsCurrent } from "./portal-revocation.js"
import { connectionWrite, effectiveMcpConnection, type McpPrincipal } from "./store.js"
import { accessGeneration, decryptBinary, encryptBinary, fingerprint, McpError, privateSecretFingerprint } from "./security.js"
export const MAX_DATABASE_BACKUP_CIPHER_BYTES = Math.ceil((MAX_DATABASE_DUMP_BYTES + 28) / 3) * 4
const digest = z.string().regex(/^[a-f0-9]{64}$/)
export const databaseBackupAuthoritySchema = z.object({
principal: z.object({ connectionId: z.string().uuid(), credentialId: z.string().uuid(), credentialExpiresAt: z.iso.datetime() }).strict(),
ownerId: z.string().min(1).max(200), policyProof: digest, generation: digest, endpointProof: digest,
}).strict()
export type DatabaseBackupAuthority = z.infer<typeof databaseBackupAuthoritySchema>
const scopeSchema = z.object({ version: z.literal(1), kind: z.literal("database-backup"), authority: databaseBackupAuthoritySchema,
attemptRef: z.string().uuid(), sourceProof: digest, bytes: z.number().int().positive().max(MAX_DATABASE_DUMP_BYTES), sha256: digest, encryptedSha256: digest, proof: digest }).strict()
type Header = { id: string; connection_id: string; filename: string; media_type: string; expires_at: Date }
type Meta = Header & { access_scope: z.infer<typeof scopeSchema>; encrypted_bytes: number }
const hash = (value: string | Buffer) => createHash("sha256").update(value).digest("hex")
const sign = (value: unknown) => privateSecretFingerprint(fingerprint(value), "database-backup-artifact-v1")
const endpoint = () => privateSecretFingerprint(env.DATABASE_URL, "database-backup-endpoint-v1")
const signed = (header: Header, scope: Omit<z.infer<typeof scopeSchema>, "proof">) => ({ id: header.id, connection_id: header.connection_id, filename: header.filename, media_type: header.media_type, expires_at: header.expires_at.toISOString(), scope })
const fail = (message: string, code = "ARTIFACT_INVALID") => new McpError(code, message, 409)
/** Only bounded topology and protection metadata is read here; never pg_dump or archive bytes. */
export async function readDatabaseBackupSource(raw: unknown, client: Pick<PoolClient, "query"> = pool) {
const authority = databaseBackupAuthoritySchema.parse(raw), connection = await effectiveMcpConnection(authority.principal, client)
if (connection.owner_id !== authority.ownerId || !hasPermission(connection.owner_permissions, "mm:settings:admin") || !hasPermission(connection.policy.permissions, "mm:settings:admin") || !connection.policy.scope.allowGlobal || !connection.policy.scope.operationIds.includes("database.backup")) throw new McpError("PERMISSION_DENIED", "Нет допуска к полной базе приложения", 403)
if (fingerprint(connection.policy) !== authority.policyProof || await accessGeneration() !== authority.generation || await endpoint() !== authority.endpointProof) throw fail("Допуск к базе изменился", "RESET_REQUIRED")
// Security epochs rotate for endpoint/credential/enabled changes (including
// A→B→A), while ordinary scheduler telemetry updates keep the proof stable.
const servers = (await client.query("SELECT id,routing_endpoint_epoch AS revision FROM servers ORDER BY id LIMIT 1001")).rows
const protections = (await client.query("SELECT CASE WHEN octet_length(object_ref::text)<=32768 THEN object_ref ELSE NULL END AS object_ref,xmin::text AS revision FROM mcp_protections ORDER BY object_ref::text LIMIT 1001")).rows
if (servers.length > 1000 || servers.some(row => !z.string().uuid().safeParse(row.revision).success) || protections.length > 1000 || protections.some(row => !row.object_ref)) throw fail("Область полной базы превышает предел", "STATE_UNAVAILABLE")
const serverIds = servers.map(row => Number(row.id))
if (serverIds.some(id => !Number.isSafeInteger(id) || id < 1 || !connection.policy.scope.serverIds.includes(id))) throw fail("Полная база содержит сервер вне подключения", "SERVER_DENIED")
if (protections.some(row => row.object_ref.protection === "deny")) throw fail("Полная база содержит запрещённый объект", "PROTECTED_OBJECT")
// Validate representability during planning/current checks after access and
// full-scope protection checks, before approval or an execution intent exists.
// This is pure validation: no service file, PostgreSQL child or public URI.
try { databaseDumpServiceFile(env.DATABASE_URL) }
catch (error) {
if (error instanceof DatabaseDumpCommandError) throw new McpError(error.code, error.message, 409)
throw error
}
return { serverIds, objects: protections.map(row => row.object_ref), proof: await sign({ authority, servers, protections }) }
}
export async function databaseBackupAuthority(principal: McpPrincipal, ownerId: string, policy: unknown) {
return databaseBackupAuthoritySchema.parse({ principal, ownerId, policyProof: fingerprint(policy), generation: await accessGeneration(), endpointProof: await endpoint() })
}
export async function createDatabaseBackupArtifact(authority: DatabaseBackupAuthority, expectedProof: string, attemptRef: string) {
z.string().uuid().parse(attemptRef)
const source = await readDatabaseBackupSource(authority)
const authorize = async () => { await assertVerifiedConfigurationAccess(); if ((await readDatabaseBackupSource(authority)).proof !== expectedProof) throw fail("Область базы изменилась после подтверждения", "STATE_CONFLICT") }
return executeGuardedApplicationWrite({ kind: "database-backup", id: "global" }, source.serverIds, async () => {
let stage = "authorize"
try {
await authorize()
stage = "export"
const result = await exportSystemDatabaseBackupWithEvent({ authorize })
stage = "export-authorize"
await authorize()
if (!result.buffer.subarray(0, 5).equals(Buffer.from("PGDMP"))) throw fail("Экспорт не является архивом PostgreSQL")
stage = "encrypt"
const ciphertext = (await encryptBinary(result.buffer)).toString("base64")
if (Buffer.byteLength(ciphertext) > MAX_DATABASE_BACKUP_CIPHER_BYTES) throw fail("Зашифрованный архив превышает предел", "EXPORT_LIMIT")
const header: Header = { id: randomUUID(), connection_id: authority.principal.connectionId, filename: result.filename, media_type: "application/octet-stream", expires_at: new Date(Date.now() + 900000) }
const access = { version: 1 as const, kind: "database-backup" as const, authority, attemptRef, sourceProof: expectedProof, bytes: result.buffer.length, sha256: hash(result.buffer), encryptedSha256: hash(ciphertext) }
const scope = scopeSchema.parse({ ...access, proof: await sign(signed(header, access)) })
stage = "publish"
await connectionWrite(async client => {
await client.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [header.connection_id])
if ((await readDatabaseBackupSource(authority, client)).proof !== expectedProof) throw fail("Область базы изменилась", "STATE_CONFLICT")
await client.query("SELECT pg_advisory_xact_lock(hashtextextended('mcp-log-artifact-storage',0))")
await client.query("DELETE FROM mcp_artifacts WHERE expires_at<=now()")
const usage = (await client.query("SELECT count(*) FILTER(WHERE connection_id=$1 AND access_scope->>'kind'='database-backup')::text AS files,COALESCE(sum(octet_length(ciphertext)),0)::text AS total_bytes FROM mcp_artifacts", [header.connection_id])).rows[0]
if (BigInt(usage.files) >= 2n || BigInt(usage.total_bytes) + BigInt(Buffer.byteLength(ciphertext)) > BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES)) throw new McpError("EXPORT_QUOTA_EXCEEDED", "Квота временных архивов исчерпана", 429)
await client.query("INSERT INTO mcp_artifacts(id,connection_id,filename,media_type,ciphertext,access_scope,expires_at) VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)", [header.id, header.connection_id, header.filename, header.media_type, ciphertext, JSON.stringify(scope), header.expires_at])
})
stage = "publication-authorize"
await authorize()
return { artifactId: header.id, downloadPath: `/api/mcp/artifacts/${header.id}`, expiresInSeconds: 900, bytes: access.bytes, sha256: access.sha256, humanDownloadOnly: true, databaseContentReturned: false, configurationApplied: false,
warnings: ["listenerError", "schedulerError", "eventError"].filter(key => !!result[key as keyof typeof result]) }
} catch (error) {
if (process.env.NODE_ENV === "test") {
const code = error instanceof DatabaseDumpCommandError || error instanceof McpError ? error.code : error && typeof error === "object" && "code" in error && typeof error.code === "string" && /^[0-9A-Z]{5}$/.test(error.code) ? error.code : null
console.error("Database backup failure diagnostic:", JSON.stringify({ stage, code, ...(error instanceof DatabaseDumpCommandError ? { diagnosticKinds: error.diagnosticKinds, exitCode: error.exitCode } : {}) }))
}
if (error instanceof DatabaseDumpCommandError) throw new McpError(error.code, error.message, 503)
throw error
}
}, async () => (await readDatabaseBackupSource(authority)).objects)
}
async function loadMeta(id: string): Promise<Meta> {
const row = (await pool.query<Meta>("/* MCP database artifact metadata */ SELECT id,connection_id,filename,media_type,expires_at,CASE WHEN octet_length(access_scope::text)<=32768 THEN access_scope ELSE NULL END AS access_scope,octet_length(ciphertext)::int AS encrypted_bytes FROM mcp_artifacts WHERE id=$1 AND expires_at>now() AND octet_length(filename)<=1024 AND octet_length(media_type)<=200", [id])).rows[0]
if (!row) throw new McpError("NOT_FOUND", "Архив отсутствует или срок истёк", 404)
const scope = scopeSchema.safeParse(row.access_scope)
if (!scope.success || !(row.expires_at instanceof Date) || !Number.isFinite(row.expires_at.getTime()) || !Number.isSafeInteger(row.encrypted_bytes) || row.encrypted_bytes < 1 || row.encrypted_bytes > MAX_DATABASE_BACKUP_CIPHER_BYTES) throw fail("Метаданные архива не подтверждены")
const { proof, ...access } = scope.data, expected = await sign(signed(row, access))
if (!timingSafeEqual(Buffer.from(proof, "hex"), Buffer.from(expected, "hex")) || row.connection_id !== access.authority.principal.connectionId) throw fail("Подпись архива не совпала")
return { ...row, access_scope: scope.data }
}
export async function verifyDatabaseBackupArtifact(id: string, authority: DatabaseBackupAuthority, sourceProof: string, attemptRef: string, receipt?: { bytes?: unknown; sha256?: unknown }) {
const meta = await loadMeta(id), source = await readDatabaseBackupSource(authority)
if (fingerprint(meta.access_scope.authority) !== fingerprint(authority) || meta.access_scope.attemptRef !== attemptRef || meta.access_scope.sourceProof !== sourceProof || source.proof !== sourceProof || (receipt && (receipt.bytes !== meta.access_scope.bytes || receipt.sha256 !== meta.access_scope.sha256))) return false
const payload = (await pool.query<{ ciphertext: string }>("SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=$3", [id, meta.access_scope.proof, MAX_DATABASE_BACKUP_CIPHER_BYTES])).rows[0]
return !!payload && hash(payload.ciphertext) === meta.access_scope.encryptedSha256 && (await readDatabaseBackupSource(authority)).proof === sourceProof
}
export async function humanDatabaseBackupMetadata(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent) {
const meta = await loadMeta(id), scope = meta.access_scope
await checkClaims(actor)
if (actor.id !== scope.authority.ownerId || !hasPermission(actor.permissions, "mm:settings:admin")) throw new McpError("PERMISSION_DENIED", "Архив доступен только владельцу с правом администрирования", 403)
if ((await readDatabaseBackupSource(scope.authority)).proof !== scope.sourceProof) throw fail("Область архива изменилась", "STATE_CONFLICT")
return scope
}
export async function readHumanDatabaseBackupArtifact(id: string, actor: AuthUser, checkClaims = assertPortalClaimsCurrent, signal?: AbortSignal) {
const bounded = signal ? AbortSignal.any([signal, AbortSignal.timeout(45000)]) : AbortSignal.timeout(45000)
const meta = await loadMeta(id), scope = meta.access_scope
const authorize = async () => { bounded.throwIfAborted(); await humanDatabaseBackupMetadata(id, actor, checkClaims); bounded.throwIfAborted(); if (meta.expires_at.getTime() <= Date.now()) throw fail("Срок архива истёк") }
await authorize()
const payload = (await pool.query<{ ciphertext: string }>("/* MCP database artifact payload */ SELECT ciphertext FROM mcp_artifacts WHERE id=$1 AND access_scope->>'proof'=$2 AND expires_at>now() AND octet_length(ciphertext)<=$3", [id, scope.proof, MAX_DATABASE_BACKUP_CIPHER_BYTES])).rows[0]
await authorize()
if (!payload || hash(payload.ciphertext) !== scope.encryptedSha256) throw fail("Зашифрованный архив изменился")
let content: Buffer
try { content = await decryptBinary(Buffer.from(payload.ciphertext, "base64")) } catch { throw fail("Архив не расшифрован") }
await authorize()
if (content.length !== scope.bytes || hash(content) !== scope.sha256 || !content.subarray(0, 5).equals(Buffer.from("PGDMP"))) throw fail("Размер или SHA-256 архива не совпал")
await authorize()
return { filename: meta.filename, mediaType: meta.media_type, content }
}