1- // Verifies backup archives by validating their manifest, payload entries, and hardlink targets.
1+ // Verifies backup archives, including payload paths and hardlink/symbolic-link targets.
22import fs from "node:fs/promises" ;
33import os from "node:os" ;
44import path from "node:path" ;
@@ -7,6 +7,12 @@ import { toStringifiedError } from "@openclaw/normalization-core/error-coercion"
77import { readStringValue } from "@openclaw/normalization-core/string-coerce" ;
88import * as tar from "tar" ;
99import { loadSqliteVecExtension } from "../../packages/memory-host-sdk/src/engine-storage.js" ;
10+ import {
11+ assertArchiveSymbolicLinkTarget ,
12+ isArchivePathWithin ,
13+ normalizeArchivePath ,
14+ normalizeArchiveRoot ,
15+ } from "../infra/backup-archive-path-policy.js" ;
1016import { isTransientSqliteBackupPath } from "../infra/backup-volatile-filter.js" ;
1117import { formatDiskSpaceBytes , tryReadDiskSpace } from "../infra/disk-space.js" ;
1218import { openNodeSqliteDatabase } from "../infra/node-sqlite.js" ;
@@ -15,7 +21,6 @@ import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
1521import { isRecord , resolveUserPath } from "../utils.js" ;
1622import { BACKUP_MAX_DECOMPRESSION_RATIO , buildBackupArchivePath } from "./backup-shared.js" ;
1723
18- const WINDOWS_ABSOLUTE_ARCHIVE_PATH_RE = / ^ [ A - Z a - z ] : [ \\ / ] / ;
1924const MAX_MANIFEST_BYTES = 1024 * 1024 ;
2025const MAX_SQLITE_SNAPSHOT_EXTRACT_BYTES = 64 * 1024 * 1024 * 1024 ;
2126const SQLITE_SNAPSHOT_FREE_SPACE_RESERVE_BYTES = 256 * 1024 * 1024 ;
@@ -88,45 +93,6 @@ type SqliteSnapshotEntry = NormalizedArchiveEntry & {
8893
8994type ExpectedSqliteRole = "agent" | "global" ;
9095
91- function stripTrailingSlashes ( value : string ) : string {
92- return value . replace ( / \/ + $ / u, "" ) ;
93- }
94-
95- function normalizeArchivePath ( entryPath : string , label : string ) : string {
96- const trimmed = stripTrailingSlashes ( entryPath . trim ( ) ) ;
97- if ( ! trimmed ) {
98- throw new Error ( `${ label } is empty.` ) ;
99- }
100- if ( trimmed . startsWith ( "/" ) || WINDOWS_ABSOLUTE_ARCHIVE_PATH_RE . test ( trimmed ) ) {
101- throw new Error ( `${ label } must be relative: ${ entryPath } ` ) ;
102- }
103- if ( trimmed . includes ( "\\" ) ) {
104- throw new Error ( `${ label } must use forward slashes: ${ entryPath } ` ) ;
105- }
106- if ( trimmed . split ( "/" ) . some ( ( segment ) => segment === "." || segment === ".." ) ) {
107- throw new Error ( `${ label } contains path traversal segments: ${ entryPath } ` ) ;
108- }
109-
110- const normalized = stripTrailingSlashes ( path . posix . normalize ( trimmed ) ) ;
111- if ( ! normalized || normalized === "." || normalized === ".." || normalized . startsWith ( "../" ) ) {
112- throw new Error ( `${ label } resolves outside the archive root: ${ entryPath } ` ) ;
113- }
114- return normalized ;
115- }
116-
117- function normalizeArchiveRoot ( rootName : string ) : string {
118- const normalized = normalizeArchivePath ( rootName , "Backup manifest archiveRoot" ) ;
119- if ( normalized . includes ( "/" ) ) {
120- throw new Error ( `Backup manifest archiveRoot must be a single path segment: ${ rootName } ` ) ;
121- }
122- return normalized ;
123- }
124-
125- function isArchivePathWithin ( child : string , parent : string ) : boolean {
126- const relative = path . posix . relative ( parent , child ) ;
127- return relative === "" || ( ! relative . startsWith ( "../" ) && relative !== ".." ) ;
128- }
129-
13096function parseManifest ( raw : string ) : BackupManifest {
13197 let parsed : unknown ;
13298 try {
@@ -312,34 +278,6 @@ function verifyHardlinkTargetsAgainstArchiveRoot(
312278 }
313279}
314280
315- function verifySymbolicLinkTargetsAgainstArchiveRoot (
316- symbolicLinks : Array < { entryPath : string ; linkpath : string } > ,
317- archiveRoot : string ,
318- ) : void {
319- const normalizedRoot = normalizeArchiveRoot ( archiveRoot ) ;
320- for ( const link of symbolicLinks ) {
321- if ( link . linkpath . startsWith ( "/" ) || WINDOWS_ABSOLUTE_ARCHIVE_PATH_RE . test ( link . linkpath ) ) {
322- throw new Error (
323- `Archive symbolic link target must be relative: ${ link . entryPath } -> ${ link . linkpath } ` ,
324- ) ;
325- }
326- if ( link . linkpath . includes ( "\\" ) ) {
327- throw new Error (
328- `Archive symbolic link target must use forward slashes: ${ link . entryPath } -> ${ link . linkpath } ` ,
329- ) ;
330- }
331- const entryPath = normalizeArchivePath ( link . entryPath , "Archive symbolic link path" ) ;
332- const targetPath = path . posix . normalize (
333- path . posix . join ( path . posix . dirname ( entryPath ) , link . linkpath ) ,
334- ) ;
335- if ( ! isArchivePathWithin ( targetPath , normalizedRoot ) ) {
336- throw new Error (
337- `Archive symbolic link target is outside the declared archive root: ${ link . entryPath } -> ${ link . linkpath } ` ,
338- ) ;
339- }
340- }
341- }
342-
343281function formatResult ( result : BackupVerifyResult ) : string {
344282 return [
345283 `Backup archive OK: ${ result . archivePath } ` ,
@@ -764,12 +702,7 @@ export async function verifyBackupArchive(archive: string): Promise<BackupVerify
764702 } ) ) ;
765703 const symbolicLinks = rawEntries
766704 . filter ( ( entry ) => entry . type === "SymbolicLink" )
767- . map ( ( entry ) => {
768- if ( ! entry . linkpath ) {
769- throw new Error ( `Archive symbolic link is missing its target: ${ entry . path } ` ) ;
770- }
771- return { entryPath : entry . path , linkpath : entry . linkpath } ;
772- } ) ;
705+ . map ( ( entry ) => ( { entryPath : entry . path , linkpath : entry . linkpath } ) ) ;
773706 const normalizedEntrySet = new Set ( entries . map ( ( entry ) => entry . normalized ) ) ;
774707
775708 const manifestMatches = entries . filter ( ( entry ) => isRootManifestEntry ( entry . normalized ) ) ;
@@ -799,7 +732,9 @@ export async function verifyBackupArchive(archive: string): Promise<BackupVerify
799732 manifest . archiveRoot ,
800733 normalizedEntrySet ,
801734 ) ;
802- verifySymbolicLinkTargetsAgainstArchiveRoot ( symbolicLinks , manifest . archiveRoot ) ;
735+ for ( const link of symbolicLinks ) {
736+ assertArchiveSymbolicLinkTarget ( { ...link , archiveRoot : manifest . archiveRoot } ) ;
737+ }
803738 await verifySqliteSnapshots ( { archivePath, entries, manifest } ) ;
804739
805740 const result : BackupVerifyResult = {
@@ -834,4 +769,3 @@ export async function backupVerifyCommand(
834769export const testApi = {
835770 assertSqliteExtractionBudget,
836771} ;
837- /* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */
0 commit comments