@@ -310,7 +310,11 @@ const databaseBackupDir = (baseDir: string, updateId: string) => {
310310
311311function resolvePathForContainment ( baseDir : string , candidate : string ) {
312312 const useWin32 =
313- IS_WINDOWS || / ^ [ A - Z a - z ] : [ \\ / ] / . test ( baseDir ) || / ^ [ A - Z a - z ] : [ \\ / ] / . test ( candidate ) ;
313+ IS_WINDOWS ||
314+ baseDir . startsWith ( "\\\\" ) ||
315+ candidate . startsWith ( "\\\\" ) ||
316+ / ^ [ A - Z a - z ] : [ \\ / ] / . test ( baseDir ) ||
317+ / ^ [ A - Z a - z ] : [ \\ / ] / . test ( candidate ) ;
314318 const pathMod = useWin32 ? NodePath . win32 : NodePath . posix ;
315319 const normalizeCase = ( value : string ) => ( useWin32 ? value . toLowerCase ( ) : value ) ;
316320 if ( ! pathMod . isAbsolute ( baseDir ) || ! pathMod . isAbsolute ( candidate ) ) return undefined ;
@@ -668,9 +672,9 @@ async function backupDatabaseOnce(baseDir: string, pending: PendingServiceUpdate
668672 throw createStateDirOwnedDuringBackupError ( ) ;
669673 }
670674 const stagingDir = `${ backupDir } .staging` ;
671- await NodeFSP . rm ( stagingDir , { recursive : true , force : true } ) ;
672- await NodeFSP . mkdir ( stagingDir , { recursive : true , mode : 0o700 } ) ;
673675 try {
676+ await NodeFSP . rm ( stagingDir , { recursive : true , force : true } ) ;
677+ await NodeFSP . mkdir ( stagingDir , { recursive : true , mode : 0o700 } ) ;
674678 for ( const suffix of DB_FILE_SUFFIXES ) {
675679 const source = `${ pending . dbPath } ${ suffix } ` ;
676680 if ( suffix !== "" && ! ( await pathExists ( source ) ) ) continue ;
@@ -709,25 +713,31 @@ async function markDatabaseRestorePending(backupDir: string): Promise<void> {
709713 }
710714}
711715
712- // Resolve symlink targets even if a crashed cp -c left the final target missing .
713- async function restoreTarget ( path : string ) : Promise < string > {
716+ // Resolve symlink targets and missing leaves under symlinked ancestors .
717+ async function restoreTarget ( path : string , depth = 0 ) : Promise < string > {
714718 let current = path ;
715- for ( let hops = 0 ; hops < 40 ; hops ++ ) {
719+ for ( let hops = depth ; hops < 40 ; hops ++ ) {
716720 try {
717721 return await NodeFSP . realpath ( current ) ;
718722 } catch ( cause ) {
719- if ( ( cause as NodeJS . ErrnoException ) . code !== "ENOENT" ) throw cause ;
723+ const code = ( cause as NodeJS . ErrnoException ) . code ;
724+ if ( code !== "ENOENT" && code !== "ENOTDIR" ) throw cause ;
720725 }
721726 const link = await NodeFSP . readlink ( current ) . catch ( ( cause : unknown ) => {
722727 const code = ( cause as NodeJS . ErrnoException ) . code ;
723- if ( code === "ENOENT" || code === "EINVAL" ) return undefined ;
728+ if ( code === "ENOENT" || code === "EINVAL" || code === "ENOTDIR" ) return undefined ;
724729 throw cause ;
725730 } ) ;
726- if ( link === undefined ) return current ;
727- // Do not normalize .. before following directory symlinks; match filesystem resolution.
728- current = NodePath . isAbsolute ( link )
729- ? link
730- : `${ await NodeFSP . realpath ( NodePath . dirname ( current ) ) } ${ NodePath . sep } ${ link } ` ;
731+ if ( link !== undefined ) {
732+ // Do not normalize .. before following directory symlinks; match filesystem resolution.
733+ const resolvedParent = await restoreTarget ( NodePath . dirname ( current ) , hops + 1 ) ;
734+ current = NodePath . isAbsolute ( link ) ? link : `${ resolvedParent } ${ NodePath . sep } ${ link } ` ;
735+ continue ;
736+ }
737+ const parent = NodePath . dirname ( current ) ;
738+ if ( parent === current ) return current ;
739+ const resolvedParent = await restoreTarget ( parent , hops + 1 ) ;
740+ return NodePath . join ( resolvedParent , NodePath . basename ( current ) ) ;
731741 }
732742 throw new Error ( `Too many symbolic links resolving ${ path } .` ) ;
733743}
@@ -738,13 +748,15 @@ async function isDatabaseRestoreTargetInsideBaseDir(
738748) : Promise < boolean > {
739749 if ( ! isDatabasePathInsideBaseDir ( baseDir , dbPath ) ) return false ;
740750 try {
741- const realBaseDir = await NodeFSP . realpath ( baseDir ) . catch ( ( ) => baseDir ) ;
751+ const realBaseDir = await restoreTarget ( baseDir ) ;
742752 const mainTarget = await restoreTarget ( dbPath ) ;
743753 if ( resolvePathForContainment ( realBaseDir , mainTarget ) === undefined ) return false ;
744754 for ( const suffix of DB_FILE_SUFFIXES ) {
745- const livePath = suffix === "" ? dbPath : `${ mainTarget } ${ suffix } ` ;
746- const target = await restoreTarget ( livePath ) ;
747- if ( resolvePathForContainment ( realBaseDir , target ) === undefined ) return false ;
755+ if ( suffix === "" ) continue ;
756+ for ( const candidate of [ `${ dbPath } ${ suffix } ` , `${ mainTarget } ${ suffix } ` ] ) {
757+ const target = await restoreTarget ( candidate ) ;
758+ if ( resolvePathForContainment ( realBaseDir , target ) === undefined ) return false ;
759+ }
748760 }
749761 return true ;
750762 } catch {
0 commit comments