import { createHash } from "node:crypto" import { constants, type BigIntStats } from "node:fs" import { lstat, open, realpath } from "node:fs/promises" import path from "node:path" import { z } from "zod" import type { RestoreJournalHead } from "../db/database-restore-quarantine.js" import { epoch } from "../db/application-writer-profile-format.js" import { parseMaintenanceJSON, runtimeDatabaseTargetSha256 } from "./database-maintenance-resolution.js" const MAX_CHECKPOINT_BYTES = 4096 const digest = z.string().length(64).regex(/^[a-f0-9]{64}$/) const head = z.object({ bytes: z.number().int().min(1).max(8_388_608), sha256: digest }).strict().nullable() const writerHead = z.object({ epoch: z.string().refine(value => { try { return epoch(value) === value } catch { return false } }), sha256: digest }).strict() const fields = { format: z.literal("database-restore-checkpoint-v1"), runtimeTargetSha256: digest, activationSha256: digest.nullable(), head, writerHead: writerHead.optional() } const permit = { ...fields, operationId: z.string().length(36).regex(/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/), archiveSha256: digest, dataSha256: digest, } const checkpointSchema = z.discriminatedUnion("status", [ z.object({ ...fields, status: z.literal("sealed") }).strict(), z.object({ ...permit, status: z.literal("pending") }).strict(), z.object({ ...permit, status: z.literal("consumed"), claimId: digest }).strict(), ]) export type RestoreCheckpoint = z.infer export type RestoreWriterHead = z.infer export function restoreWriterHeadIsCurrent(expected: RestoreWriterHead | undefined, observed: RestoreWriterHead | undefined): boolean { if (!expected || !observed) return expected === observed return BigInt(observed.epoch) > BigInt(expected.epoch) || (observed.epoch === expected.epoch && observed.sha256 === expected.sha256) } export class DatabaseRestoreCheckpointRefused extends Error { readonly code = "DATABASE_RESTORE_CHECKPOINT_REFUSED" readonly statusCode = 503 constructor() { super("Контрольная точка восстановления PostgreSQL не подтверждена") } } const refused = () => new DatabaseRestoreCheckpointRefused() export function parseDatabaseRestoreCheckpoint(bytes: Buffer): RestoreCheckpoint { if (bytes.length > MAX_CHECKPOINT_BYTES) throw refused() try { return checkpointSchema.parse(parseMaintenanceJSON(bytes)) } catch { throw refused() } } export function restoreJournalHeadsEqual(left: RestoreJournalHead | null | undefined, right: RestoreJournalHead | null | undefined): boolean { return left === right || (!!left && !!right && left.bytes === right.bytes && left.sha256 === right.sha256) } const comparablePath = (value: string) => process.platform === "win32" ? value.toLowerCase() : value function containsFile(directory: string, file: string): boolean { const relative = path.relative(comparablePath(directory), comparablePath(file)) return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)) } export function sameRestoreCheckpointFile(left: BigIntStats, right: BigIntStats): boolean { return right.isFile() && right.nlink === 1n && left.dev === right.dev && left.ino === right.ino && left.size === right.size && left.mode === right.mode && left.mtimeNs === right.mtimeNs && left.ctimeNs === right.ctimeNs } export async function readRestoreCheckpointFile(file: string) { if (!path.isAbsolute(file) || path.resolve(file) !== file || comparablePath(await realpath(file)) !== comparablePath(file)) throw refused() const identity = await lstat(file, { bigint: true }) if (!identity.isFile() || identity.nlink !== 1n || identity.size < 1n || identity.size > BigInt(MAX_CHECKPOINT_BYTES)) throw refused() const handle = await open(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)) try { if (!sameRestoreCheckpointFile(identity, await handle.stat({ bigint: true }))) throw refused() const bytes = Buffer.alloc(Number(identity.size) + 1) let size = 0 while (size < bytes.length) { const { bytesRead } = await handle.read(bytes, size, bytes.length - size, size) if (!bytesRead) break size += bytesRead } if (BigInt(size) !== identity.size || !sameRestoreCheckpointFile(identity, await handle.stat({ bigint: true })) || !sameRestoreCheckpointFile(identity, await lstat(file, { bigint: true })) || comparablePath(await realpath(file)) !== comparablePath(file)) throw refused() const raw = bytes.subarray(0, size) return { checkpoint: parseDatabaseRestoreCheckpoint(raw), identity, sha256: createHash("sha256").update(raw).digest("hex") } } finally { await handle.close() } } async function assertNoCheckpointTransition(file: string) { for (const suffix of [".lock", ".pending"]) { try { await lstat(file + suffix) } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") continue; throw error } throw refused() } } export async function readDatabaseRestoreCheckpointSnapshot(stateDirectory: string, databaseUrl: string) { const file = process.env.DATABASE_RESTORE_CHECKPOINT_FILE if (file === undefined) return undefined try { if (!path.isAbsolute(file) || path.resolve(file) !== file || containsFile(path.resolve(stateDirectory), file)) throw refused() const canonicalState = await realpath(stateDirectory).catch((error: NodeJS.ErrnoException) => { if (error.code !== "ENOENT") throw error }) if (canonicalState && containsFile(canonicalState, file)) throw refused() await assertNoCheckpointTransition(file) const { checkpoint, identity, sha256 } = await readRestoreCheckpointFile(file) if (checkpoint.runtimeTargetSha256 !== runtimeDatabaseTargetSha256(databaseUrl) || checkpoint.activationSha256 !== (process.env.DATABASE_RUNTIME_ACTIVATION_SHA256 ?? null)) throw refused() await assertNoCheckpointTransition(file) if (!sameRestoreCheckpointFile(identity, await lstat(file, { bigint: true }))) throw refused() return { file, checkpoint, sha256 } } catch { throw refused() } } export async function readDatabaseRestoreCheckpoint(stateDirectory: string, databaseUrl: string): Promise { return (await readDatabaseRestoreCheckpointSnapshot(stateDirectory, databaseUrl))?.checkpoint }