diff --git a/src/js/internal/fs/cp-symlink-dotdot.ts b/src/js/internal/fs/cp-symlink-dotdot.ts new file mode 100644 index 000000000000..a1230cd80d69 --- /dev/null +++ b/src/js/internal/fs/cp-symlink-dotdot.ts @@ -0,0 +1,118 @@ +// For a cp root with `..`: if app/cur links to releases/v2, "app/cur/../x" is releases/x, and path.join() gives app/x. +import type { KeptRoot } from "./cp-sync"; + +const { lstatSync, realpathSync, statSync } = require("node:fs"); +const { lstat, realpath, stat } = require("node:fs/promises"); +const { basename, dirname, join, normalize } = require("node:path"); + +const realpathNativeSync = realpathSync.native; + +const StringPrototypeIndexOf = String.prototype.indexOf; +const StringPrototypeSlice = String.prototype.slice; + +// The names of `path` that a later `..` cancels: where each one ends in `path`, in the order of the `..`. +function cancelledNames(path: string) { + const names: number[] = []; + let depth = 0; + const cancelled: number[] = []; + for (let start = 0; start < path.length;) { + let end = StringPrototypeIndexOf.$call(path, "/", start); + if (end === -1) end = path.length; + const length = end - start; + if (length === 2 && path[start] === "." && path[start + 1] === ".") { + if (depth > 0) $arrayPush(cancelled, names[--depth]); + } else if (length > 1 || (length === 1 && path[start] !== ".")) { + if (depth < names.length) names[depth] = end; + else $arrayPush(names, end); + depth++; + } + start = end + 1; + } + return cancelled; +} + +// True when the system and path.join() give `path` two directories. That takes a `..` that cancels a symbolic link. +function keepsPathSync(path: string) { + const cancelled = cancelledNames(path); + for (let i = 0; i < cancelled.length; i++) { + let isLink: boolean; + try { + // No name that a `..` cancelled before this one is a link, so the text up to here is right. + isLink = lstatSync(normalize(StringPrototypeSlice.$call(path, 0, cancelled[i]))).isSymbolicLink(); + } catch { + // A name that the copy makes. Any other failure is for the calls of the copy to report. + continue; + } + if (!isLink) continue; + try { + // The names at the end that do not exist yet are the same for the system and for the text. + while (statSync(path, { throwIfNoEntry: false }) === undefined) { + const parent = dirname(path); + // After `new/..` the system resolves the rest only once the copy made `new`. + if (parent === path || basename(path) === "..") return true; + path = parent; + } + return realpathNativeSync(path) !== realpathNativeSync(normalize(path)); + } catch { + // Nothing shows that the text is right. + return true; + } + } + return false; +} + +async function keepsPath(path: string) { + const cancelled = cancelledNames(path); + for (let i = 0; i < cancelled.length; i++) { + let isLink: boolean; + try { + isLink = (await lstat(normalize(StringPrototypeSlice.$call(path, 0, cancelled[i])))).isSymbolicLink(); + } catch { + continue; + } + if (!isLink) continue; + try { + while (!(await exists(path))) { + const parent = dirname(path); + if (parent === path || basename(path) === "..") return true; + path = parent; + } + return (await realpath(path)) !== (await realpath(normalize(path))); + } catch { + return true; + } + } + return false; +} + +async function exists(path: string) { + try { + await stat(path); + return true; + } catch (err: any) { + if (err.code === "ENOENT") return false; + throw err; + } +} + +// The real directory of `link` below a kept root. The walk follows no link below a root, so the rest is text. +function linkDirSync(link: string, kept: KeptRoot) { + try { + kept.real ??= realpathNativeSync(kept.path); + } catch { + // What node's JS walk takes for every link. + return dirname(link); + } + return dirname(join(kept.real, StringPrototypeSlice.$call(link, kept.path.length))); +} + +async function linkDir(link: string, kept: KeptRoot) { + try { + kept.real ??= await realpath(kept.path); + } catch { + return dirname(link); + } + return dirname(join(kept.real, StringPrototypeSlice.$call(link, kept.path.length))); +} + +export default { keepsPathSync, keepsPath, linkDirSync, linkDir }; diff --git a/src/js/internal/fs/cp-sync.ts b/src/js/internal/fs/cp-sync.ts index 981592c5cc26..db06624016c0 100644 --- a/src/js/internal/fs/cp-sync.ts +++ b/src/js/internal/fs/cp-sync.ts @@ -11,17 +11,19 @@ const { mkdirSync, readdirSync, readlinkSync, + realpathSync, statSync, symlinkSync, unlinkSync, utimesSync, } = require("node:fs"); -const { dirname, isAbsolute, join, parse, resolve, sep } = require("node:path"); +const { basename, dirname, isAbsolute, join, parse, resolve, sep } = require("node:path"); const { EEXIST, EISDIR, EINVAL, ENOTDIR } = $processBindingConstants.os.errno; const ArrayPrototypeEvery = Array.prototype.every; const ArrayPrototypeFilter = Array.prototype.filter; +const StringPrototypeIndexOf = String.prototype.indexOf; const StringPrototypeSplit: (this: string, separator: string, limit?: number) => string[] = String.prototype.split; // COPYFILE_EXCL | COPYFILE_FICLONE | COPYFILE_FICLONE_FORCE @@ -86,6 +88,16 @@ function fsCpEinvalError(context: SystemErrorContext) { return decorateSystemError($ERR_FS_CP_EINVAL(context.message), "Invalid src or dest", context); } +function fsCpSubdirOfSelfError(src: string, dest: string) { + return fsCpEinvalError({ + message: `cannot copy ${src} to a subdirectory of self ${dest}`, + path: dest, + syscall: "cp", + errno: EINVAL, + code: "EINVAL", + }); +} + function fsCpFifoPipeError(context: SystemErrorContext) { return decorateSystemError($ERR_FS_CP_FIFO_PIPE(context.message), "Cannot copy a FIFO pipe", context); } @@ -179,6 +191,70 @@ type Stats = $Node.Stats; type BigIntStats = $Node.BigIntStats; type AnyStats = Stats | BigIntStats; +// A dest directory on the current recursion path. `stat` is null for one this copy made, until it is needed. +export interface CpFrame { + stat: BigIntStats | null; + path: string; + up: CpFrame | null; +} + +// A root that keeps its path as given (see internal/fs/cp-symlink-dotdot). `real` is its real path, read on first use. +export interface KeptRoot { + path: string; + real: string | undefined; +} + +// One per recursive directory copy. +export interface CpWalk { + parent: BigIntStats | undefined; // the directory a new root was made in + root: CpFrame | null; // that root's frame, while its stat is unread + rootInSrc: boolean; // src is that directory, so the walk starts inside it + entered: boolean; // a filter let the walk into its own output, and the walk is still in it + src: KeptRoot | undefined; + dest: KeptRoot | undefined; +} + +function newCpWalk(src?: KeptRoot, dest?: KeptRoot): CpWalk { + return { parent: undefined, root: null, rootInSrc: false, entered: false, src, dest }; +} + +function isOwnOutput(frame: CpFrame | null, srcStat: BigIntStats) { + for (let f = frame; f !== null; f = f.up) { + const stat = f.stat; + if (stat !== null && areIdentical(srcStat, stat)) return true; + } + return false; +} + +function followedLink(dirent: Pick<$Node.Dirent, "isDirectory">, opts: CpOptions) { + return opts.dereference && !dirent.isDirectory(); +} + +function unreadRootIn(walk: CpWalk, srcStat: BigIntStats) { + const root = walk.root; + return root !== null && root.stat === null && areIdentical(srcStat, walk.parent!) ? root : null; +} + +// Only a filter can end a walk of the copy's own output, so only then is an entry let in, and not from inside it. +function enterOnce(walk: CpWalk, opts: CpOptions) { + if (!opts.filter || walk.entered) return false; + walk.entered = true; + return true; +} + +// Loaded for the first root with `..` in it. +let symlinkDotDot: $ModuleValue | undefined; + +// path.join() and the system can differ only for a root with `..`. Not on Windows: it cancels `name\..` as text too. +function mayKeepPath(path: string) { + return process.platform !== "win32" && StringPrototypeIndexOf.$call(path, "..") !== -1; +} + +// The path of an entry of `dir`, below a root that keeps its path: path.join() would cancel the `..` of the root. +function keptChildPath(dir: string, name: string) { + return dir[dir.length - 1] === "/" ? dir + name : dir + "/" + name; +} + interface CpOptions { dereference: boolean; errorOnExist: boolean; @@ -195,6 +271,9 @@ interface CheckedPaths { srcStat: BigIntStats; destStat: BigIntStats | null | undefined; skipped: false; + // For the two roots. Not there for the entries below them. + keepSrc?: boolean; + keepDest?: boolean; } interface SkippedPaths { @@ -205,8 +284,22 @@ interface SkippedPaths { } function checkPathsSync(src: string, dest: string, opts: CpOptions & { filter: undefined }): CheckedPaths; -function checkPathsSync(src: string, dest: string, opts: CpOptions): CheckedPaths | SkippedPaths; -function checkPathsSync(src: string, dest: string, opts: CpOptions): CheckedPaths | SkippedPaths { +function checkPathsSync( + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + frame?: CpFrame, + dirent?: Pick<$Node.Dirent, "isDirectory">, +): CheckedPaths | SkippedPaths; +function checkPathsSync( + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + frame?: CpFrame, + dirent?: Pick<$Node.Dirent, "isDirectory">, +): CheckedPaths | SkippedPaths { if (opts.filter) { const shouldCopy = opts.filter(src, dest); if ($isPromise(shouldCopy)) { @@ -246,14 +339,30 @@ function checkPathsSync(src: string, dest: string, opts: CpOptions): CheckedPath } } - if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { - throw fsCpEinvalError({ - message: `cannot copy ${src} to a subdirectory of self ${dest}`, - path: dest, - syscall: "cp", - errno: EINVAL, - code: "EINVAL", - }); + if (srcStat.isDirectory()) { + let own = isSrcSubdir(src, dest); + if (!own && walk !== undefined && frame !== undefined) { + if (followedLink(dirent!, opts)) { + for (let f: CpFrame | null = frame; f !== null; f = f.up) f.stat ??= lstatSync(f.path, { bigint: true }); + } else { + const root = unreadRootIn(walk, srcStat); + if (root !== null) root.stat = lstatSync(root.path, { bigint: true }); + } + own = isOwnOutput(frame, srcStat) && !enterOnce(walk, opts); + } + if (own) throw fsCpSubdirOfSelfError(src, dest); + if (walk === undefined) { + return { + __proto__: null, + srcStat, + destStat, + skipped: false, + keepSrc: mayKeepPath(src) && (symlinkDotDot ??= require("internal/fs/cp-symlink-dotdot")).keepsPathSync(src), + keepDest: mayKeepPath(dest) && (symlinkDotDot ??= require("internal/fs/cp-symlink-dotdot")).keepsPathSync(dest), + }; + } + } else if (walk === undefined) { + return { __proto__: null, srcStat, destStat, skipped: false, keepSrc: false, keepDest: false }; } return { __proto__: null, srcStat, destStat, skipped: false }; } @@ -273,7 +382,7 @@ function getStatsSync(src: string, dest: string, opts: CpOptions) { return { srcStat, destStat }; } -function checkParentPathsSync(src: string, srcStat: BigIntStats, dest: string) { +function checkParentPathsSync(src: string, srcStat: BigIntStats, dest: string, keepDest?: boolean) { const srcParent = resolve(dirname(src)); const destParent = resolve(dirname(dest)); if (destParent === srcParent || destParent === parse(destParent).root) return; @@ -281,19 +390,41 @@ function checkParentPathsSync(src: string, srcStat: BigIntStats, dest: string) { try { destStat = statSync(destParent, { bigint: true }); } catch (err: any) { - if (err.code === "ENOENT") return; + // A destination that keeps its path is not below its text. A file in that text is not in its way. + if (err.code === "ENOENT" || (keepDest && err.code === "ENOTDIR")) return; throw err; } - if (areIdentical(srcStat, destStat)) { - throw fsCpEinvalError({ - message: `cannot copy ${src} to a subdirectory of self ${dest}`, - path: dest, - syscall: "cp", - errno: EINVAL, - code: "EINVAL", - }); + if (areIdentical(srcStat, destStat)) throw fsCpSubdirOfSelfError(src, dest); + return checkParentPathsSync(src, srcStat, destParent, keepDest); +} + +function resolveExistingRealpathSync(p: string) { + let missing = ""; + for (;;) { + try { + return join(realpathSync(p), missing); + } catch (err: any) { + const parent = dirname(p); + if (err?.code !== "ENOENT" || parent === p) return undefined; + missing = join(basename(p), missing); + p = parent; + } + } +} + +// The native walker has no own-output check, so only a dest that provably resolves outside src may reach it. +function nativeDirCopyIsSafeSync(src: string, dest: string) { + let realSrc: string; + try { + // realpathSync() cancels `..` as text. No root that keeps its path comes here. + realSrc = realpathSync(src); + } catch { + return false; } - return checkParentPathsSync(src, srcStat, destParent); + const realDestParent = resolveExistingRealpathSync(dirname(dest)); + if (realDestParent === undefined) return false; + if (isSrcSubdir(realSrc, join(realDestParent, basename(dest)))) throw fsCpSubdirOfSelfError(src, dest); + return true; } // The native recursive copy (a single clonefile() on macOS) copies symlinks @@ -329,8 +460,8 @@ function treeContainsOnlyFilesAndDirsSync(root: string) { // (which performs the copy but does not implement node's cp error codes). function tryNativeFastPathSync(src: string, dest: string, opts: CpOptions & { filter: undefined }) { const checked = checkPathsSync(src, dest, opts); - const { srcStat, destStat } = checked; - checkParentPathsSync(src, srcStat, dest); + const { srcStat, destStat, keepSrc, keepDest } = checked; + checkParentPathsSync(src, srcStat, dest, keepDest); if (srcStat.isDirectory() && !opts.recursive) { throw fsEisdirError({ message: `${src} is a directory (not copied)`, @@ -346,7 +477,13 @@ function tryNativeFastPathSync(src: string, dest: string, opts: CpOptions & { fi // node's walker: dest must not exist (no merge semantics) and the tree // must contain only regular files and directories. return { - ok: process.platform === "darwin" && !destStat && treeContainsOnlyFilesAndDirsSync(src), + ok: + process.platform === "darwin" && + !destStat && + !keepSrc && + !keepDest && + treeContainsOnlyFilesAndDirsSync(src) && + nativeDirCopyIsSafeSync(src, dest), checked, }; } @@ -360,24 +497,64 @@ function tryNativeFastPathSync(src: string, dest: string, opts: CpOptions & { fi function cpSyncFn(src: string, dest: string, opts: CpOptions, checked?: CheckedPaths) { // `checked` carries the stats from a preceding tryNativeFastPathSync so the // fallback doesn't re-run the same checkPaths/checkParentPaths syscalls. - const { srcStat, destStat, skipped } = checked ?? checkPathsSync(src, dest, opts); - if (skipped) return; - if (checked === undefined) checkParentPathsSync(src, srcStat, dest); - return checkParentDir(destStat, src, dest, opts); -} - -function checkParentDir(destStat: BigIntStats | null | undefined, src: string, dest: string, opts: CpOptions) { + const roots = checked ?? checkPathsSync(src, dest, opts); + if (roots.skipped) return; + const { srcStat, destStat, keepSrc, keepDest } = roots; + if (checked === undefined) checkParentPathsSync(src, srcStat, dest, keepDest); + const walk = + opts.recursive && srcStat.isDirectory() + ? newCpWalk( + keepSrc ? { path: src, real: undefined } : undefined, + keepDest ? { path: dest, real: undefined } : undefined, + ) + : undefined; + return checkParentDir(destStat, src, dest, opts, walk, srcStat); +} + +function checkParentDir( + destStat: BigIntStats | null | undefined, + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + srcStat?: BigIntStats, +) { const destParent = dirname(dest); - if (!existsSync(destParent)) mkdirSync(destParent, { recursive: true }); - return getStats(destStat, src, dest, opts); -} - -function getStats(destStat: BigIntStats | null | undefined, src: string, dest: string, opts: CpOptions) { + if (walk === undefined) { + if (!existsSync(destParent)) mkdirSync(destParent, { recursive: true }); + return getStats(destStat, src, dest, opts); + } + // dirname("sub/..") is "sub", not the directory that holds it. The frame of a root that exists has its stat already. + const last = basename(dest); + if (destStat && (last === ".." || last === ".")) return getStats(destStat, src, dest, opts, walk, null); + let parent = statSync(destParent, { bigint: true, throwIfNoEntry: false }) as BigIntStats | undefined; + if (parent === undefined) { + // `new/../link` resolves, to what can be src, only once `new` exists. + mkdirSync(destParent, { recursive: true }); + parent = statSync(destParent, { bigint: true }); + } + if (areIdentical(srcStat!, parent)) { + // With no filter the walk is certain to reach the root, unless the copy stops first on the existing dest. + if (!opts.filter && !(destStat && opts.errorOnExist && !opts.force)) throw fsCpSubdirOfSelfError(src, dest); + walk.rootInSrc = true; + } + if (!destStat) walk.parent = parent; + return getStats(destStat, src, dest, opts, walk, null); +} + +function getStats( + destStat: BigIntStats | null | undefined, + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + frame?: CpFrame | null, +) { const statSyncFn = opts.dereference ? statSync : lstatSync; const srcStat = statSyncFn(src); if (srcStat.isDirectory() && opts.recursive) { - return onDir(srcStat, destStat, src, dest, opts); + return onDir(srcStat, destStat, src, dest, opts, walk ?? newCpWalk(), frame ?? null); } else if (srcStat.isDirectory()) { throw fsEisdirError({ message: `${src} is a directory (not copied)`, @@ -389,7 +566,7 @@ function getStats(destStat: BigIntStats | null | undefined, src: string, dest: s } else if (srcStat.isFile() || srcStat.isCharacterDevice() || srcStat.isBlockDevice()) { return onFile(srcStat, destStat, src, dest, opts); } else if (srcStat.isSymbolicLink()) { - return onLink(destStat, src, dest, opts); + return onLink(destStat, src, dest, opts, walk); } else if (srcStat.isSocket()) { throw fsCpSocketError({ message: `cannot copy a socket file: ${dest}`, @@ -470,8 +647,16 @@ function setDestTimestamps(src: string, dest: string) { return utimesSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime); } -function onDir(srcStat: Stats, destStat: BigIntStats | null | undefined, src: string, dest: string, opts: CpOptions) { - if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts); +function onDir( + srcStat: Stats, + destStat: BigIntStats | null | undefined, + src: string, + dest: string, + opts: CpOptions, + walk: CpWalk, + frame: CpFrame | null, +) { + if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts, walk, frame); if (opts.errorOnExist && !opts.force) { throw fsCpEExistError({ message: `${dest} already exists`, @@ -481,29 +666,56 @@ function onDir(srcStat: Stats, destStat: BigIntStats | null | undefined, src: st code: "EEXIST", }); } - return copyDir(src, dest, opts); + return copyDir(src, dest, opts, walk, { stat: destStat, path: dest, up: frame }); } -function mkDirAndCopy(srcMode: number, src: string, dest: string, opts: CpOptions) { +function mkDirAndCopy( + srcMode: number, + src: string, + dest: string, + opts: CpOptions, + walk: CpWalk, + frame: CpFrame | null, +) { mkdirSync(dest); - copyDir(src, dest, opts); + if (frame === null) { + frame = { stat: walk.rootInSrc ? lstatSync(dest, { bigint: true }) : null, path: dest, up: null }; + if (walk.parent !== undefined) walk.root = frame; + } else if (walk.entered) { + frame = { stat: lstatSync(dest, { bigint: true }), path: dest, up: frame }; + } else if (opts.dereference) { + frame = { stat: null, path: dest, up: frame }; + } + copyDir(src, dest, opts, walk, frame); return setDestMode(dest, srcMode); } -function copyDir(src: string, dest: string, opts: CpOptions) { +function copyDir(src: string, dest: string, opts: CpOptions, walk: CpWalk, frame: CpFrame) { + const keepSrc = walk.src !== undefined; + const keepDest = walk.dest !== undefined; + const mayEnter = opts.filter !== undefined && !walk.entered; for (const dirent of readdirSync(src, { withFileTypes: true })) { const { name } = dirent; - const srcItem = join(src, name); - const destItem = join(dest, name); - const { destStat, skipped } = checkPathsSync(srcItem, destItem, opts); - if (!skipped) getStats(destStat, srcItem, destItem, opts); + const srcItem = keepSrc ? keptChildPath(src, name) : join(src, name); + const destItem = keepDest ? keptChildPath(dest, name) : join(dest, name); + const { destStat, skipped } = checkPathsSync(srcItem, destItem, opts, walk, frame, dirent); + if (!skipped) getStats(destStat, srcItem, destItem, opts, walk, frame); + // The walk is out of its output again once the entry that led into it is copied. + if (mayEnter) walk.entered = false; } } -function onLink(destStat: BigIntStats | null | undefined, src: string, dest: string, opts: CpOptions) { +function onLink( + destStat: BigIntStats | null | undefined, + src: string, + dest: string, + opts: CpOptions, + walk: CpWalk | undefined, +) { let resolvedSrc = readlinkSync(src); if (!opts.verbatimSymlinks && !isAbsolute(resolvedSrc)) { - resolvedSrc = resolve(dirname(src), resolvedSrc); + const kept = walk?.src; + resolvedSrc = resolve(kept === undefined ? dirname(src) : symlinkDotDot!.linkDirSync(src, kept), resolvedSrc); } if (!destStat) { return symlinkSync(resolvedSrc, dest); @@ -521,7 +733,8 @@ function onLink(destStat: BigIntStats | null | undefined, src: string, dest: str throw err; } if (!isAbsolute(resolvedDest)) { - resolvedDest = resolve(dirname(dest), resolvedDest); + const kept = walk?.dest; + resolvedDest = resolve(kept === undefined ? dirname(dest) : symlinkDotDot!.linkDirSync(dest, kept), resolvedDest); } let srcIsDir = false; try { @@ -572,4 +785,12 @@ export default { fsEisdirError, areIdentical, isSrcSubdir, + fsCpSubdirOfSelfError, + newCpWalk, + isOwnOutput, + followedLink, + unreadRootIn, + enterOnce, + mayKeepPath, + keptChildPath, }; diff --git a/src/js/internal/fs/cp.ts b/src/js/internal/fs/cp.ts index be2c5177a62f..d1da1bc80387 100644 --- a/src/js/internal/fs/cp.ts +++ b/src/js/internal/fs/cp.ts @@ -1,4 +1,6 @@ // Taken and modified from node.js: https://github.com/nodejs/node/blob/main/lib/internal/fs/cp/cp.js +import type { CpFrame, CpWalk } from "./cp-sync"; + const { errno: { EEXIST, EINVAL, EISDIR, ENOTDIR }, fsCpDirToNonDirError, @@ -12,6 +14,14 @@ const { fsEisdirError, areIdentical, isSrcSubdir, + fsCpSubdirOfSelfError, + newCpWalk, + isOwnOutput, + followedLink, + unreadRootIn, + enterOnce, + mayKeepPath, + keptChildPath, } = require("internal/fs/cp-sync"); const { @@ -22,16 +32,20 @@ const { opendir, readdir, readlink, + realpath, stat, symlink, unlink, utimes, } = require("node:fs/promises"); -const { dirname, isAbsolute, join, parse, resolve } = require("node:path"); +const { basename, dirname, isAbsolute, join, normalize, parse, resolve } = require("node:path"); const PromisePrototypeThen = $Promise.prototype.$then; const PromiseReject = Promise.$reject; +// See internal/fs/cp-sync. +let symlinkDotDot: $ModuleValue | undefined; + type Stats = $Node.Stats; type BigIntStats = $Node.BigIntStats; type DestinationStats = BigIntStats | null | undefined; @@ -52,6 +66,8 @@ interface CheckedPaths { srcStat: BigIntStats; destStat: DestinationStats; skipped: false; + keepSrc?: boolean; + keepDest?: boolean; } interface SkippedPaths { @@ -62,8 +78,22 @@ interface SkippedPaths { } async function checkPaths(src: string, dest: string, opts: CpOptions & { filter: undefined }): Promise; -async function checkPaths(src: string, dest: string, opts: CpOptions): Promise; -async function checkPaths(src: string, dest: string, opts: CpOptions): Promise { +async function checkPaths( + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + frame?: CpFrame, + dirent?: Pick<$Node.Dirent, "isDirectory">, +): Promise; +async function checkPaths( + src: string, + dest: string, + opts: CpOptions, + walk?: CpWalk, + frame?: CpFrame, + dirent?: Pick<$Node.Dirent, "isDirectory">, +): Promise { if (opts.filter && !(await opts.filter(src, dest))) { return { __proto__: null, skipped: true }; } @@ -98,14 +128,33 @@ async function checkPaths(src: string, dest: string, opts: CpOptions): Promise; "internal/fs/binding": $ModuleValue; "internal/fs/cp": $ModuleValue; + "internal/fs/cp-symlink-dotdot": $ModuleValue; "internal/fs/cp-sync": $ModuleValue; "internal/fs/file_handles": $ModuleValue; "internal/fs/glob": $ModuleValue; diff --git a/test/js/node/fs/cp.test.ts b/test/js/node/fs/cp.test.ts index 036ff3e26658..0cebb3459eb3 100644 --- a/test/js/node/fs/cp.test.ts +++ b/test/js/node/fs/cp.test.ts @@ -1,14 +1,35 @@ import { describe, expect, jest, test } from "bun:test"; import fs from "fs"; -import { bunEnv, bunExe, type DirectoryTree, isLinux, isPosix, isWindows, tempDir } from "harness"; +import { + bunEnv, + bunExe, + type DirectoryTree, + isLinux, + isMacOS, + isPosix, + isWindows, + tempDir, + unprivilegedSpawnOptions, +} from "harness"; import { mkfifo } from "mkfifo"; -import { isAbsolute, join } from "path"; +import { basename, isAbsolute, join, sep } from "path"; const impls = [ ["cpSync", fs.cpSync], ["cp", fs.promises.cp], ] as const; +function listTree(root: string, rel = ""): string[] { + return fs + .readdirSync(join(root, rel), { withFileTypes: true }) + .sort((a, b) => (a.name < b.name ? -1 : 1)) + .flatMap(entry => + entry.isDirectory() + ? [`${rel}${entry.name}/`, ...listTree(root, `${rel}${entry.name}/`)] + : [`${rel}${entry.name}`], + ); +} + for (const [name, copy] of impls) { async function copyShouldThrow(...args: Parameters) { try { @@ -405,6 +426,252 @@ for (const [name, copy] of impls) { assertContent(basename + "/result/a.dir/c.txt", "c"); }); + // The walker must never copy a directory it is writing into. The layouts + // below reach the copy's own output through a second name that the + // path-string guard cannot see. Without the identity check each one nests + // copies until ENAMETOOLONG. + describe("copy into its own output", () => { + // Long enough that an unguarded copy runs out of path after a few levels. + const deep = Buffer.alloc(isWindows ? 100 : 200, "d").toString(); + + test("dest through a symlink to a source subdirectory", async () => { + using dir = tempDir("cp-own-alias", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + + const err = await copyShouldThrow(src, join(base, "alias", deep), { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + // macOS takes the native route here, which is refused before it starts. + expect(listTree(join(src, "sub"))).toEqual(isMacOS ? [] : [`${deep}/`, `${deep}/sub/`]); + }); + + test("dest through a relative symlink, walked in JS on every platform", async () => { + using dir = tempDir("cp-own-alias-rel", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join("src", "sub"), join(base, "alias"), "junction"); + + const err = await copyShouldThrow(src, join(base, "alias", deep), { recursive: true, force: false }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(src, "sub"))).toEqual([`${deep}/`, `${deep}/sub/`]); + }); + + test("dest through a symlink, with missing parent directories", async () => { + using dir = tempDir("cp-own-alias-deep", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + + const err = await copyShouldThrow(src, join(base, "alias", "a", "b", deep), { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(src, "sub"))).toEqual( + isMacOS + ? [] + : ["a/", "a/b/", `a/b/${deep}/`, `a/b/${deep}/sub/`, `a/b/${deep}/sub/a/`, `a/b/${deep}/sub/a/b/`], + ); + }); + + // On Windows ".." is resolved as text, so this spelling names a sibling of src there. + test.skipIf(isWindows)("dest whose parent resolves to src through a symlink and '..'", async () => { + using dir = tempDir("cp-own-dotdot", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias")); + + const err = await copyShouldThrow(src, `${base}/alias/../${deep}`, { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(src)).toEqual(["sub/"]); + }); + + // The system resolves the parent only once the copy made `new`. Its text names a directory in src now. + test.skipIf(isWindows)("dest through a symlink, after a name that does not exist yet", async () => { + using dir = tempDir("cp-own-alias-new", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias")); + + const err = await copyShouldThrow(src, `${base}/new/../alias/${deep}`, { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + // macOS takes the native route here, which is refused before it starts. + expect(listTree(base)).toEqual( + isMacOS + ? ["alias", "src/", "src/sub/"] + : ["alias", "new/", "src/", "src/sub/", `src/sub/${deep}/`, `src/sub/${deep}/sub/`], + ); + }); + + // The parent resolves only once the copy made `new`. + test.skipIf(isWindows)("dest whose parent resolves to src after a name that does not exist yet", async () => { + using dir = tempDir("cp-own-dotdot-new", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias")); + + const err = await copyShouldThrow(src, `${base}/new/../alias/../${deep}`, { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(base)).toEqual(["alias", "new/", "src/", "src/sub/"]); + }); + + test.skipIf(isWindows)("an existing dest whose parent resolves to src through a symlink and '..'", async () => { + using dir = tempDir("cp-own-dotdot-merge", { "src": { "sub": {}, [deep]: {} } }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias")); + + const err = await copyShouldThrow(src, `${base}/alias/../${deep}`, { recursive: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(base)).toEqual(["alias", "src/", `src/${deep}/`, "src/sub/"]); + }); + + test("dereference: a symlink inside an existing dest points into src", async () => { + using dir = tempDir("cp-own-deref-dest", { "src/sub": { [deep]: {} }, "dest": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub", deep), join(base, "dest", "sub"), "junction"); + + const err = await copyShouldThrow(src, join(base, "dest"), { recursive: true, dereference: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(src)).toEqual(["sub/", `sub/${deep}/`]); + }); + + test("dereference: a symlink inside src points at the directory being created", async () => { + using dir = tempDir("cp-own-deref-src", { "src": { [deep]: {} } }); + const base = String(dir); + const src = join(base, "src"); + const link = Buffer.alloc(isWindows ? 50 : 200, "l").toString(); + fs.symlinkSync(join(base, "dest", deep), join(src, deep, link), "junction"); + + const err = await copyShouldThrow(src, join(base, "dest"), { recursive: true, dereference: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(base, "dest"))).toEqual([`${deep}/`]); + }); + + // A directory this copy made and already finished is ordinary input again. + test("dereference: a symlink to an already copied sibling is still copied", async () => { + using dir = tempDir("cp-own-deref-sibling", { "src": { "p": {}, "q": {} } }); + const base = String(dir); + const src = join(base, "src"); + const order: string[] = []; + if (name === "cpSync") order.push(...fs.readdirSync(src)); + else for await (const dirent of await fs.promises.opendir(src)) order.push(dirent.name); + const [first, second] = order; + fs.writeFileSync(join(src, first, "f.txt"), "payload"); + fs.symlinkSync(join(base, "dest", first), join(src, second, "link"), "junction"); + + await copy(src, join(base, "dest"), { recursive: true, dereference: true }); + expect(listTree(join(base, "dest")).sort()).toEqual( + [`${first}/`, `${first}/f.txt`, `${second}/`, `${second}/link/`, `${second}/link/f.txt`].sort(), + ); + }); + + test("dereference: src is a symlink to the directory that holds dest", async () => { + using dir = tempDir("cp-own-deref-self", { "P/file.txt": "payload" }); + const base = String(dir); + const src = join(base, "P", "self"); + fs.symlinkSync(join(base, "P"), src, "junction"); + + const err = await copyShouldThrow(src, join(base, "P", deep), { recursive: true, dereference: true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(base, "P"))).toEqual(["file.txt", "self"]); + }); + + test("dereference: a filter that skips the output keeps a copy made inside src legal", async () => { + using dir = tempDir("cp-own-deref-self-filter", { "P/file.txt": "payload" }); + const base = String(dir); + const src = join(base, "P", "self"); + fs.symlinkSync(join(base, "P"), src, "junction"); + + await copy(src, join(base, "P", "out"), { + recursive: true, + dereference: true, + filter: (s: string) => s === src || !["out", "self"].includes(basename(s)), + }); + expect(listTree(join(base, "P"))).toEqual(["file.txt", "out/", "out/file.txt", "self"]); + }); + + // The walk is out of its output again when the filter ended the copy of the first link. + test("dereference: a filter that prunes below two links to dest keeps the copy legal", async () => { + using dir = tempDir("cp-own-deref-two", { "src": { "a": {}, "b": {} }, "out": {} }); + const base = String(dir); + fs.symlinkSync(join(base, "out"), join(base, "src", "a", "l1"), "junction"); + fs.symlinkSync(join(base, "out"), join(base, "src", "b", "l2"), "junction"); + + await copy(join(base, "src"), join(base, "out"), { + recursive: true, + dereference: true, + filter: (s: string) => !/[\\/]l[12][\\/]/.test(s.slice(base.length)), + }); + expect(listTree(join(base, "out"))).toEqual(["a/", "a/l1/", "b/", "b/l2/"]); + }); + + // The filter is asked first, so a copy that skips its own output still succeeds, as in Node.js. + test("a filter that skips the output directory keeps the copy legal", async () => { + using dir = tempDir("cp-own-filter", { "src/file.txt": "payload", "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + + await copy(src, join(base, "alias", "copy"), { recursive: true, filter: (s: string) => !s.endsWith("copy") }); + expect(listTree(join(src, "sub"))).toEqual(["copy/", "copy/file.txt", "copy/sub/"]); + }); + + test("a filter that prunes everything below the output ends the copy, as in Node.js", async () => { + using dir = tempDir("cp-own-filter-prune", { "src/file.txt": "payload", "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + + const filter = (s: string) => !s.includes(`${sep}out${sep}`); + await copy(src, join(base, "alias", "out"), { recursive: true, filter }); + expect(listTree(join(src, "sub"))).toEqual(["out/", "out/file.txt", "out/sub/", "out/sub/out/"]); + }); + + test("a filter that keeps everything cannot make the copy re-enter its output", async () => { + using dir = tempDir("cp-own-filter-all", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + + const err = await copyShouldThrow(src, join(base, "alias", deep), { recursive: true, filter: () => true }); + expect(err.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(src, "sub"))).toEqual([ + `${deep}/`, + `${deep}/sub/`, + `${deep}/sub/${deep}/`, + `${deep}/sub/${deep}/sub/`, + ]); + }); + + test("dest through a symlink that leads outside src is copied", async () => { + using dir = tempDir("cp-alias-outside", { "src/file.txt": "payload", "other": {} }); + const base = String(dir); + fs.symlinkSync(join(base, "other"), join(base, "alias"), "junction"); + + await copy(join(base, "src"), join(base, "alias", "copy"), { recursive: true }); + expect(listTree(join(base, "other"))).toEqual(["copy/", "copy/file.txt"]); + }); + + // dirname() of this dest is src, and the directory that holds dest is not. + test("a source spelled 'dir/.' is copied into 'dir/..'", async () => { + using dir = tempDir("cp-into-parent", { "P/src/file.txt": "payload" }); + const base = String(dir); + + await copy(`${base}/P/src/.`, `${base}/P/src/..`, { recursive: true }); + expect(listTree(join(base, "P"))).toEqual(["file.txt", "src/", "src/file.txt"]); + }); + + test("dest through a symlink to the parent of src is copied as a sibling", async () => { + using dir = tempDir("cp-alias-sibling", { "src/file.txt": "payload" }); + const base = fs.realpathSync(String(dir)); + fs.symlinkSync(base, join(base, "plink"), "junction"); + + await copy(join(base, "src"), join(base, "plink", "pcopy"), { recursive: true }); + expect(listTree(join(base, "pcopy"))).toEqual(["file.txt"]); + expect(listTree(join(base, "src"))).toEqual(["file.txt"]); + }); + }); + test.if(process.platform === "win32")("should not throw EBUSY when copying the same file on windows", async () => { await using basename = tempDir("cp", { "hey": "hi", @@ -430,6 +697,721 @@ test("cp with missing callback throws", () => { }).toThrow(/"cb"/); }); +test("fs.cp reports a copy into its own output to the callback", async () => { + using dir = tempDir("cp-own-callback", { "src/sub": {} }); + const base = String(dir); + const src = join(base, "src"); + fs.symlinkSync(join(src, "sub"), join(base, "alias"), "junction"); + const deep = Buffer.alloc(isWindows ? 100 : 200, "d").toString(); + + const { promise, resolve } = Promise.withResolvers(); + fs.cp(src, join(base, "alias", deep), { recursive: true, force: false }, resolve); + expect((await promise)?.code).toBe("ERR_FS_CP_EINVAL"); + expect(listTree(join(src, "sub"))).toEqual([`${deep}/`, `${deep}/sub/`]); +}); + +// A bind mount gives a directory a second name that no path resolution sees through. +const canBindMount = + isLinux && + (() => { + try { + const probe = "mount -t tmpfs tmpfs /tmp && mkdir /tmp/a /tmp/b && mount -o bind /tmp/a /tmp/b"; + return ( + Bun.spawnSync({ cmd: ["unshare", "-Urm", "sh", "-c", probe], stdout: "ignore", stderr: "ignore" }).exitCode === + 0 + ); + } catch { + return false; + } + })(); + +describe.skipIf(!canBindMount)("copy into its own output through a bind mount", () => { + test.concurrent.each([ + ["cpSync", `fs.cpSync("a/sub", "b/t", { recursive: true })`], + ["promises.cp", `await fs.promises.cp("a/sub", "b/t", { recursive: true })`], + ])("%s", async (_, call) => { + using dir = tempDir("cp-own-bind", { "a": {}, "b": {} }); + // The inode limit ends an unguarded copy quickly. + const script = [ + "set -e", + "mount -t tmpfs -o nr_inodes=64,size=1m tmpfs a", + "mkdir -p a/sub/inner", + "mount -o bind a/sub/inner b", + 'exec "$BUN_EXE" -e "$CP_FIXTURE"', + ].join("\n"); + const fixture = ` + const fs = require("fs"); + const list = (rel) => fs.readdirSync(rel, { withFileTypes: true }).sort((x, y) => (x.name < y.name ? -1 : 1)) + .flatMap(entry => (entry.isDirectory() ? [rel + "/" + entry.name, ...list(rel + "/" + entry.name)] : [])); + (async () => { + let code = "none"; + try { + ${call}; + } catch (e) { + code = e.code; + } + console.log(JSON.stringify({ code, dirs: list("a") })); + })(); + `; + await using proc = Bun.spawn({ + cmd: ["unshare", "-Urm", "sh", "-c", script], + env: { ...bunEnv, BUN_EXE: bunExe(), CP_FIXTURE: fixture }, + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual({ + code: "ERR_FS_CP_EINVAL", + dirs: ["a/sub", "a/sub/inner", "a/sub/inner/t", "a/sub/inner/t/inner"], + }); + expect(exitCode).toBe(0); + }); +}); + +// `app/cur` is a symlink to `releases/v2`. For the system `app/cur/..` is +// `releases`. As text (path.join, path.resolve) it is `app`. On Windows the +// system cancels `cur/..` as text too, so the two never differ there. +describe.skipIf(isWindows)("a root that holds /..", () => { + const impls = [ + [ + "cpSync", + async (src: fs.PathLike, dest: string, opts?: fs.CopySyncOptions) => fs.cpSync(src as string, dest, opts), + ], + [ + "promises.cp", + (src: fs.PathLike, dest: string, opts?: fs.CopyOptions) => fs.promises.cp(src as string, dest, opts), + ], + ] as const; + const apis = impls.map(([api]) => api); + + // `below`: the directory in the temporary directory that gets the tree, with a "/" at the end. + function fixture(below = "") { + const dir = tempDir("cp-dotdot", { + [`${below}app/config.json`]: "text twin", + [`${below}releases/config.json`]: "live", + [`${below}releases/v2/inner.txt`]: "inner", + [`${below}from/a.txt`]: "a", + [`${below}from/sub/b.txt`]: "b", + }); + fs.symlinkSync("../releases/v2", join(dir, below, "app", "cur")); + return dir; + } + + // Every entry below `root`: the content of a file, the target of a link, or "dir". + function tree(root: string, prefix = "", out: Record = {}) { + for (const entry of fs.readdirSync(join(root, prefix), { withFileTypes: true })) { + const path = prefix + entry.name; + if (entry.isDirectory()) { + out[path] = "dir"; + tree(root, path + "/", out); + } else { + out[path] = entry.isSymbolicLink() + ? "-> " + fs.readlinkSync(join(root, path)) + : fs.readFileSync(join(root, path), "utf8"); + } + } + return out; + } + + // `entries` below the directory `name`, in the form that tree() gives. + const below = (name: string, entries: Record) => + Object.fromEntries([[name, "dir"], ...Object.entries(entries).map(([path, value]) => [`${name}/${path}`, value])]); + + const releases = { "config.json": "live", "v2": "dir", "v2/inner.txt": "inner" }; + const from = { "a.txt": "a", "sub": "dir", "sub/b.txt": "b" }; + + const ignoresCase = (() => { + using dir = tempDir("cp-case", { "name": "" }); + return fs.existsSync(join(dir, "NAME")); + })(); + + const subdirOfSelf = (src: string, dest: string) => ({ + code: "ERR_FS_CP_EINVAL", + path: dest, + message: `Invalid src or dest: cp returned EINVAL (cannot copy ${src} to a subdirectory of self ${dest}) ${dest}`, + }); + + // For a copy of `src` to `dest` (both below `dir`) that starts, and that the walk stops. `parent` is the directory + // that the copy makes for its output, `/copy`. The walk throws when it comes to that output as an entry of + // the source. Before that it made one level: the name of `parent`, in the output. What else it copied depends on + // the order of readdir. + async function refusedAtOutput( + copyError: (src: string, dest: string) => Promise, + dir: string, + src: string, + dest: string, + parent: string, + ) { + const name = basename(parent); + const before = tree(dir); + expect(await copyError(`${dir}/${src}`, `${dir}/${dest}`)).toEqual( + subdirOfSelf(`${dir}/${src}/${name}/copy`, `${dir}/${dest}/${name}/copy`), + ); + const after = tree(dir); + for (const path of Object.keys(after)) { + if (path.startsWith(`${parent}/copy/`) && !`${path}/`.startsWith(`${parent}/copy/${name}/`)) delete after[path]; + } + expect(after).toEqual({ ...before, [parent]: "dir", [`${parent}/copy`]: "dir", [`${parent}/copy/${name}`]: "dir" }); + } + + // Makes each copy of `cases` with each API, in a child with the working directory `cwd`. A "%" in a destination is + // the name of the API. `lock` is a directory that the child gives a mode for the time of the copies: a child that + // starts below a directory it cannot search does not know its working directory on macOS. `wrap` is a command + // that runs the child, with shell to run first. + async function copyInChild( + cwd: string, + cases: [src: string, dest: string, outcome: string][], + { lock, wrap, ...options }: { lock?: [path: string, mode: number]; wrap?: [cmd: string[], shell: string] } & object, + ) { + const fixture = ` + const fs = require("node:fs"); + const apis = { + "cpSync": async (src, dest) => fs.cpSync(src, dest, { recursive: true }), + "promises.cp": (src, dest) => fs.promises.cp(src, dest, { recursive: true }), + }; + (async () => { + const lock = ${JSON.stringify(lock ?? null)}; + const results = {}; + if (lock) fs.chmodSync(lock[0], lock[1]); + try { + for (const [src, dest] of ${JSON.stringify(cases)}) { + for (const api in apis) { + results[api + ": " + src + " -> " + dest] = await apis[api](src, dest.replace("%", api)).then( + () => "copied", + err => err.code, + ); + } + } + } finally { + if (lock) fs.chmodSync(lock[0], 0o755); + } + console.log(JSON.stringify(results)); + })(); + `; + await using proc = Bun.spawn({ + cmd: wrap + ? [...wrap[0], "sh", "-c", `set -e\n${wrap[1]}\nexec "$BUN_EXE" -e "$CP_FIXTURE"`] + : [bunExe(), "-e", fixture], + env: { ...bunEnv, BUN_EXE: bunExe(), CP_FIXTURE: fixture }, + cwd, + stdout: "pipe", + stderr: "pipe", + ...options, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect({ stderr, exitCode }).toEqual({ stderr: "", exitCode: 0 }); + expect(JSON.parse(stdout)).toEqual( + Object.fromEntries( + cases.flatMap(([src, dest, outcome]) => apis.map(api => [`${api}: ${src} -> ${dest}`, outcome])), + ), + ); + } + + test("a relative root that starts above the working directory", async () => { + using dir = fixture(); + await copyInChild(`${dir}/from/sub`, [["../../app/cur/..", "../../out-%", "copied"]], {}); + expect(Object.fromEntries(apis.map(api => [api, tree(`${dir}/out-${api}`)]))).toEqual( + Object.fromEntries(apis.map(api => [api, releases])), + ); + }); + + // On Linux the real path of a directory comes from /proc/self/fd. The child keeps the process id of the shell. + test.skipIf(!canBindMount)("copies when the system gives no real path", async () => { + using dir = fixture(); + fs.symlinkSync("config.json", `${dir}/releases/link`); + await copyInChild(`${dir}`, [["app/cur/..", "out-%", "copied"]], { + wrap: [["unshare", "-Urm"], "mount -t tmpfs tmpfs /proc/$$/fd"], + }); + // The relative link then resolves against the text of its directory, as in node's JS walk. + expect(Object.fromEntries(apis.map(api => [api, tree(`${dir}/out-${api}`)]))).toEqual( + Object.fromEntries(apis.map(api => [api, { ...releases, "link": `-> ${dir}/app/config.json` }])), + ); + }); + + test("fs.cp copies the entries of the source that the system resolves", async () => { + using dir = fixture(); + const { promise, resolve } = Promise.withResolvers(); + fs.cp(`${dir}/app/cur/..`, `${dir}/out`, { recursive: true }, resolve); + expect(await promise).toBeNull(); + expect(tree(`${dir}/out`)).toEqual(releases); + }); + + // In a child, because root ignores permission bits (the harness then runs the child as a user that they apply + // to) and because the working directory is one for the process. + describe("with a directory that the copy cannot list or cannot search", () => { + // Serial: `unprivilegedSpawnOptions` may change and restore the modes of directories above the test's own. + test.serial("does not need read permission on a directory above the destination", async () => { + using dir = fixture(); + using unprivileged = unprivilegedSpawnOptions(`${dir}`); + try { + // The path goes through `app`, which the copy can search but not list. + await copyInChild(`${dir}`, [[`${dir}/from`, `${dir}/app/cur/../copy-%`, "copied"]], { + lock: [`${dir}/app`, 0o311], + ...unprivileged, + }); + } finally { + fs.chmodSync(`${dir}/app`, 0o755); + } + expect(tree(`${dir}/releases`)).toEqual( + Object.assign({ ...releases }, ...apis.map(api => below(`copy-${api}`, from))), + ); + expect(tree(`${dir}/app`)).toEqual({ "config.json": "text twin", "cur": "-> ../releases/v2" }); + }); + + // No root has `..` here. As on main, the first lookup through the destination is for an entry of the source. + test.serial("does not need search permission on a destination that gets no entry", async () => { + using dir = tempDir("cp-dest-locked", { "empty": {}, "one/f": "f", "dest": {} }); + using unprivileged = unprivilegedSpawnOptions(`${dir}`); + try { + await copyInChild( + `${dir}`, + [ + ["empty", "dest", "copied"], + ["one", "dest", "EACCES"], + ], + { lock: ["dest", 0o000], ...unprivileged }, + ); + } finally { + fs.chmodSync(`${dir}/dest`, 0o755); + } + expect(tree(`${dir}/dest`)).toEqual({}); + }); + + test.serial("does not need search permission on a directory above the working directory", async () => { + using dir = fixture("locked/work/t/"); + using unprivileged = unprivilegedSpawnOptions(`${dir}`); + const work = `${dir}/locked/work`; + try { + // For the child, `..` is `locked`. With no search permission on it, no absolute path reaches the tree. + await copyInChild( + work, + [ + // No name is a link, so main copies this one too. Each root has a directory part, because + // path.resolve(".") asks the system for the working directory, which macOS refuses here. + ["t/from", "t/from/../plain-%", "copied"], + // As on main: checkParentPaths() stats the absolute parent of a destination that is not beside the + // source. + ["t/app/cur/..", "t/out-%", "EACCES"], + ], + { lock: ["..", 0o000], ...unprivileged }, + ); + } finally { + fs.chmodSync(`${dir}/locked`, 0o755); + } + expect(tree(`${work}/t`)).toEqual( + Object.assign( + below("app", { "config.json": "text twin", "cur": "-> ../releases/v2" }), + below("from", from), + below("releases", releases), + ...apis.map(api => below(`plain-${api}`, from)), + ), + ); + }); + }); + + describe.concurrent.each(impls)("fs.%s", (_, copy) => { + async function copyError(src: string, dest: string, opts: fs.CopySyncOptions = {}) { + try { + await copy(src, dest, { recursive: true, ...opts }); + } catch (e: any) { + return { code: e.code, path: e.path, message: e.message }; + } + return "copied"; + } + + test.each([ + ["app/cur/..", releases], + ["app/cur/../", releases], + ["app/cur/../v2", { "inner.txt": "inner" }], + // The target of `abs` is absolute. + ["app/abs/..", releases], + ])("copies the entries of the source that the system resolves: %s", async (src, expected) => { + using dir = fixture(); + fs.symlinkSync(`${dir}/releases/v2`, `${dir}/app/abs`); + await copy(`${dir}/${src}`, `${dir}/out`, { recursive: true }); + expect(tree(`${dir}/out`)).toEqual(expected); + }); + + // Unlike node with a `filter`: its JS walk joins, gives the filter `app/config.json` and throws ENOENT. + // https://github.com/nodejs/node/blob/v26.12.1/lib/internal/fs/cp/cp-sync.js#L173 + test.each(["", "/"])("gives the filter paths that name the entries: app/cur/..%s", async slash => { + using dir = fixture(); + const src = `${dir}/app/cur/..`; + const dest = `${dir}/out`; + const seen: string[][] = []; + await copy(src + slash, dest, { + recursive: true, + filter: (from, to) => { + seen.push([from, to, fs.statSync(from).isDirectory() ? "dir" : fs.readFileSync(from, "utf8")]); + return true; + }, + }); + // The filter gets the root first, as given. The order below it is the order of readdir. + expect(seen.shift()).toEqual([src + slash, dest, "dir"]); + expect(seen.sort()).toEqual([ + [`${src}/config.json`, `${dest}/config.json`, "live"], + [`${src}/v2`, `${dest}/v2`, "dir"], + [`${src}/v2/inner.txt`, `${dest}/v2/inner.txt`, "inner"], + ]); + expect(tree(dest)).toEqual(releases); + }); + + test.each([ + ["copy", "releases/copy", {}, true], + ["copy", "releases/copy", {}, false], + ["n1/n2/copy", "releases/n1/n2/copy", {}, false], + ["copy", "releases/copy", { preserveTimestamps: true }, true], + ["copy", "releases/copy", { dereference: true }, true], + ])( + "writes into the destination that the system resolves: app/cur/../%s -> %s %j", + async (dest, real, opts, twin) => { + using dir = fixture(); + // The text twin of the destination. Nothing may land in it. + if (twin) fs.mkdirSync(`${dir}/app/copy`); + await copy(`${dir}/from`, `${dir}/app/cur/../${dest}`, { recursive: true, ...opts }); + expect(tree(`${dir}/${real}`)).toEqual(from); + if (twin) expect(tree(`${dir}/app/copy`)).toEqual({}); + }, + ); + + test("merges into a destination that exists", async () => { + using dir = fixture(); + fs.writeFileSync(`${dir}/releases/a.txt`, "old"); + await copy(`${dir}/from`, `${dir}/app/cur/..`, { recursive: true }); + expect(tree(`${dir}/releases`)).toEqual({ ...releases, ...from }); + expect(tree(`${dir}/app`)).toEqual({ "config.json": "text twin", "cur": "-> ../releases/v2" }); + }); + + test("gives the filter the two roots as given when both hold the link", async () => { + using dir = fixture(); + const src = `${dir}/app/cur/../v2`; + const dest = `${dir}/app/cur/../out`; + const seen: string[][] = []; + await copy(src, dest, { recursive: true, filter: (from, to) => (seen.push([from, to]), true) }); + expect(seen).toEqual([ + [src, dest], + [`${src}/inner.txt`, `${dest}/inner.txt`], + ]); + expect(tree(`${dir}/releases/out`)).toEqual({ "inner.txt": "inner" }); + }); + + test("keeps the strings of path.join when the text is the same directory", async () => { + using dir = fixture(); + // `cur` is a directory, then a link to a directory beside it, then a link out of `app`. Only that changes the + // strings, and only for a root that the text does not name: a second `..` leads back to `app`. + const seen: string[][] = []; + const filter = (from: string) => (seen.push([from]), true); + const src = `${dir}/app/cur/..`; + + fs.unlinkSync(`${dir}/app/cur`); + fs.mkdirSync(`${dir}/app/cur`); + await copy(src, `${dir}/out1`, { recursive: true, filter }); + fs.rmdirSync(`${dir}/app/cur`); + fs.mkdirSync(`${dir}/app/real`); + fs.symlinkSync("real", `${dir}/app/cur`); + await copy(src, `${dir}/out2`, { recursive: true, filter }); + fs.unlinkSync(`${dir}/app/cur`); + fs.symlinkSync("../releases/v2", `${dir}/app/cur`); + await copy(src, `${dir}/out3`, { recursive: true, filter }); + await copy(`${src}/../app`, `${dir}/out4`, { recursive: true, filter }); + + expect(seen.flat().sort()).toEqual( + [ + src, + `${dir}/app/config.json`, + `${dir}/app/cur`, + src, + `${dir}/app/config.json`, + `${dir}/app/cur`, + `${dir}/app/real`, + src, + `${src}/config.json`, + `${src}/v2`, + `${src}/v2/inner.txt`, + `${src}/../app`, + `${dir}/app/config.json`, + `${dir}/app/cur`, + `${dir}/app/real`, + ].sort(), + ); + }); + + // No name is a link. `mkdir -p` makes `new`, so `new/..` is what the text says. + test("keeps the strings of path.join for a destination through a name that does not exist", async () => { + using dir = fixture(); + const seen: string[] = []; + const dest = `${dir}/new/../out`; + await copy(`${dir}/from/sub`, dest, { recursive: true, filter: (_, to) => (seen.push(to), true) }); + expect(seen).toEqual([dest, `${dir}/out/b.txt`]); + expect(tree(`${dir}/out`)).toEqual({ "b.txt": "b" }); + }); + + test.each([ + // main gives ENOENT for the first two, and leaves a directory in the source. + ["the destination is inside the source", "releases", "app/cur/../copy"], + ["the target of the link is absolute", "releases", "app/abs/../copy"], + // The same as main and node: the text of the roots says the same, or no name is a link. + ["both roots hold the link", "app/cur/..", "app/cur/../v2/copy"], + ["no name is a link", "releases/v2/..", "releases/v2/../new/copy"], + ["a name that does not exist, no link", "from", "new/../from/x"], + ["the destination is the parent of the source", "releases/v2", "releases/v2/.."], + ["the destination is the parent of the source, through the link", "releases/v2", "app/cur/.."], + ["the text of the destination is inside the source", "app", "app/cur/../copy"], + // Node's cpSync copies this one. Its fs.cp and fs.promises.cp compare the text, as main does: + // https://github.com/nodejs/node/blob/v26.12.1/lib/internal/fs/cp/cp.js#L113 + ["only the text of the source holds the destination", "app/cur/..", "app/out"], + // checkParentPaths() finds the source above the destination, and names the directory below it, as in node: + // https://github.com/nodejs/node/blob/v26.12.1/lib/internal/fs/cp/cp.js#L175-L184 + ["the source holds the link", "app/cur/..", "releases/v2/copy", "releases/v2"], + ])("refuses to copy a directory into itself: %s", async (_, src, dest, named = dest) => { + using dir = fixture(); + fs.symlinkSync(`${dir}/releases/v2`, `${dir}/app/abs`); + const before = tree(`${dir}`); + expect(await copyError(`${dir}/${src}`, `${dir}/${dest}`)).toEqual( + subdirOfSelf(`${dir}/${src}`, `${dir}/${named}`), + ); + expect(tree(`${dir}`)).toEqual(before); + }); + + // No check on the roots climbs from a parent that does not exist, so these copies start. The walk throws when + // it comes to its output, `new/copy`. Before that it made `new` in the output. What else it copied depends on the + // order of readdir. + test.each([ + ["the destination holds the link", "releases", "app/cur/../new/copy"], + ["the source holds the link", "app/cur/..", "releases/new/copy"], + ])("stops a copy into itself below a parent that does not exist: %s", async (_, src, dest) => { + using dir = fixture(); + await refusedAtOutput(copyError, `${dir}`, src, dest, "releases/new"); + }); + + // The filter is asked before the walk looks at an entry, as for a root without the link. + test("copies into the source when the filter skips the output", async () => { + using dir = fixture(); + await copy(`${dir}/app/cur/..`, `${dir}/releases/new/copy`, { + recursive: true, + filter: (from: string) => basename(from) !== "new", + }); + expect(tree(`${dir}/releases/new/copy`)).toEqual(releases); + }); + + // `alias/d` is in the source, and no parent of its text is. + test.each([ + ["ERR_FS_CP_EEXIST", { errorOnExist: true, force: false }], + ["ERR_FS_EISDIR", { recursive: false }], + ])("gives %s for a destination in the source, as for a source without the link", async (code, opts) => { + using dir = fixture(); + fs.mkdirSync(`${dir}/releases/v2/d`); + fs.symlinkSync("releases/v2", `${dir}/alias`); + const before = tree(`${dir}`); + const results = [ + await copyError(`${dir}/app/cur/..`, `${dir}/alias/d`, opts), + await copyError(`${dir}/releases`, `${dir}/alias/d`, opts), + ]; + expect(results.map(result => (result === "copied" ? result : result.code))).toEqual([code, code]); + expect(tree(`${dir}`)).toEqual(before); + }); + + test("refuses a destination in the source that resolves only when a name before the link exists", async () => { + using dir = fixture(); + const before = tree(`${dir}`); + const dest = `${dir}/new/../app/cur/../copy`; + expect(await copyError(`${dir}/releases`, dest)).toEqual(subdirOfSelf(`${dir}/releases`, dest)); + // `mkdir -p` of the parent made `new`. Before that the system does not resolve the parent. + expect(tree(`${dir}`)).toEqual({ ...before, new: "dir" }); + }); + + test("copies to a destination beside the source that climbs out of a name that does not exist", async () => { + using dir = fixture(); + // `new` is below the source. The destination is `releases/out`, beside it. + await copy(`${dir}/releases/v2`, `${dir}/app/cur/new/../../out`, { recursive: true }); + // `mkdir -p` of the parent made `new` in the source, before the walk listed the source. + expect(tree(`${dir}/releases/out`)).toEqual({ "inner.txt": "inner", "new": "dir" }); + }); + + test("with dereference, refuses when the text of the destination is inside the source", async () => { + using dir = fixture(); + const before = tree(`${dir}`); + const result = await copyError(`${dir}/app`, `${dir}/app/cur/../v2/copy`, { dereference: true }); + expect(result).toEqual(subdirOfSelf(`${dir}/app`, `${dir}/app/cur/../v2/copy`)); + expect(tree(`${dir}`)).toEqual(before); + }); + + // Node's cpSync gives these targets (its C++ walk). Its JS walk resolves against the text of the directory. + test("resolves a relative link target against the directory that holds the link", async () => { + using dir = fixture(); + fs.symlinkSync("config.json", `${dir}/releases/link`); + fs.symlinkSync("../config.json", `${dir}/releases/v2/up`); + + await copy(`${dir}/app/cur/..`, `${dir}/out`, { recursive: true }); + expect(tree(`${dir}/out`)).toEqual({ + ...releases, + "link": `-> ${dir}/releases/config.json`, + "v2/up": `-> ${dir}/releases/config.json`, + }); + }); + + test("a link that is the root resolves against the text of its directory, as in node", async () => { + using dir = fixture(); + fs.symlinkSync("config.json", `${dir}/releases/link`); + await copy(`${dir}/app/cur/../link`, `${dir}/out-link`); + expect(fs.readlinkSync(`${dir}/out-link`)).toBe(`${dir}/app/config.json`); + }); + + // `current` in the destination is a relative link. Its directory is `releases`, which the destination names + // through the link. + test.each([ + // `from/current` and `releases/current` lead to one directory. + ["refuses a link over a link to the same directory", "v2", "releases/v2", "ERR_FS_CP_EINVAL"], + // As text `releases/current` is `app/x`, where `from/current` leads. They are two directories. + ["replaces a link whose target is not its text twin", "x", "app/x", "copied"], + ])("%s", async (_, target, srcTarget, expected) => { + using dir = fixture(); + fs.mkdirSync(`${dir}/app/x`); + fs.mkdirSync(`${dir}/releases/x`); + fs.symlinkSync(`${dir}/${srcTarget}`, `${dir}/from/current`); + fs.symlinkSync(target, `${dir}/releases/current`); + const result = await copyError(`${dir}/from`, `${dir}/app/cur/..`); + expect(result === "copied" ? result : result.code).toBe(expected); + expect(fs.readlinkSync(`${dir}/releases/current`)).toBe(expected === "copied" ? `${dir}/${srcTarget}` : target); + }); + + test("a file in the way of the text form does not stop the copy", async () => { + using dir = fixture(); + // `app/v2` is the text form of `app/cur/../v2`. Here it is a file. + fs.writeFileSync(`${dir}/app/v2`, "in the way"); + fs.symlinkSync("../config.json", `${dir}/releases/v2/up`); + + await copy(`${dir}/app/cur/..`, `${dir}/out`, { recursive: true }); + expect(tree(`${dir}/out`)).toEqual({ ...releases, "v2/up": `-> ${dir}/releases/config.json` }); + + await copy(`${dir}/from`, `${dir}/app/cur/../v2/copy`, { recursive: true }); + expect(tree(`${dir}/releases/v2/copy`)).toEqual(from); + + await copy(`${dir}/from`, `${dir}/app/cur/../v2/two/levels`, { recursive: true }); + expect(tree(`${dir}/releases/v2/two/levels`)).toEqual(from); + }); + + // The same as main and node: the text check of node does not ask whether `app/sub` exists. + test.each([false, true])( + "refuses when the text of the destination is inside the source, with its text twin: %p", + async twin => { + using dir = fixture(); + fs.mkdirSync(`${dir}/releases/sub`); + if (twin) fs.mkdirSync(`${dir}/app/sub`); + const before = tree(`${dir}`); + expect(await copyError(`${dir}/app`, `${dir}/app/cur/../sub/copy`)).toEqual( + subdirOfSelf(`${dir}/app`, `${dir}/app/cur/../sub/copy`), + ); + expect(tree(`${dir}`)).toEqual(before); + }, + ); + + test("resolves a link target below a link that has the name of its target directory", async () => { + using dir = fixture(); + // `app/same/../same` and its text form `app/same` are one directory. Its parent is `releases`, not `app`. + fs.mkdirSync(`${dir}/releases/same`); + fs.symlinkSync("../releases/same", `${dir}/app/same`); + fs.symlinkSync("../config.json", `${dir}/releases/same/up`); + + await copy(`${dir}/app/same/..`, `${dir}/out`, { recursive: true }); + expect(fs.readlinkSync(`${dir}/out/same/up`)).toBe(`${dir}/releases/config.json`); + }); + + test("a Buffer root with /.. throws as a Buffer root without it does", async () => { + using dir = fixture(); + const code = async (src: string) => { + try { + await copy(Buffer.from(src), `${dir}/out`, { recursive: true }); + } catch (e: any) { + return e.code; + } + return "copied"; + }; + expect([await code(`${dir}/app/cur/..`), await code(`${dir}/releases`)]).toEqual([ + "ERR_INVALID_ARG_TYPE", + "ERR_INVALID_ARG_TYPE", + ]); + }); + }); + + // One directory has two real paths: through a bind mount, and on a volume that ignores case (the default on + // macOS). `app/self/..` is `app` and `app/other/..` is `releases`. The links name them `APP` and `RELEASES`. + describe("a link that names a directory by a second real path", () => { + // Source, destination, and for a copy that the walk stops: the directory that the copy makes for its output. + const cases: [src: string, dest: string, parent: string][] = [ + // The text of this source holds the destination, so no copy starts, as on main. + ["app/self/..", "app/new/out-%", ""], + ["app/other/..", "releases/new2-%/copy", "releases/new2-%"], + ["releases", "app/other/../new3-%/copy", "releases/new3-%"], + ]; + function fixtureWithLinks() { + const dir = fixture(); + fs.mkdirSync(`${dir}/app/v1`); + fs.symlinkSync("../APP/v1", `${dir}/app/self`); + fs.symlinkSync("../RELEASES/v2", `${dir}/app/other`); + return dir; + } + + test.skipIf(!canBindMount)("through bind mounts: refuses to copy a directory into itself", async () => { + using dir = fixtureWithLinks(); + fs.mkdirSync(`${dir}/out`); + const before = tree(`${dir}`); + await copyInChild( + `${dir}`, + [ + ...cases.map(([src, dest]): [string, string, string] => [src, dest, "ERR_FS_CP_EINVAL"]), + // `out/in-%` is not below the source for any check on the roots: the source holds a second name of `out`. + ["app/cur/..", "out/in-%", "ERR_FS_CP_EINVAL"], + ], + { + wrap: [ + ["unshare", "-Urm"], + "mkdir APP RELEASES releases/m\nmount -o bind app APP\nmount -o bind releases RELEASES\nmount -o bind out releases/m", + ], + }, + ); + // The mounts went away with the child. Each walk that started made one level of its output in that output, + // and nothing in it. + const after = tree(`${dir}`); + const stopped = apis.flatMap(api => [`releases/new2-${api}`, `releases/new3-${api}`]); + expect(stopped.map(parent => tree(`${dir}/${parent}/copy/${basename(parent)}`))).toEqual(stopped.map(() => ({}))); + for (const path of Object.keys(after)) if (/^(out|releases\/new[23]-[^/]+)\//.test(path)) delete after[path]; + expect(after).toEqual({ + ...before, + "APP": "dir", + "RELEASES": "dir", + "releases/m": "dir", + ...Object.fromEntries(stopped.map(parent => [parent, "dir"])), + }); + }); + + describe.skipIf(!ignoresCase)("on a volume that ignores case", () => { + describe.concurrent.each(impls)("fs.%s", (api, copy) => { + const copyError = (src: string, dest: string) => + copy(src, dest, { recursive: true }).then( + () => "copied" as const, + (e: any) => ({ code: e.code, path: e.path, message: e.message }), + ); + test.each(cases)("refuses to copy a directory into itself: %s -> %s", async (src, dest, parent) => { + using dir = fixtureWithLinks(); + dest = dest.replace("%", api); + if (parent) { + await refusedAtOutput(copyError, `${dir}`, src, dest, parent.replace("%", api)); + } else { + const before = tree(`${dir}`); + expect(await copyError(`${dir}/${src}`, `${dir}/${dest}`)).toEqual( + subdirOfSelf(`${dir}/${src}`, `${dir}/${dest}`), + ); + expect(tree(`${dir}`)).toEqual(before); + } + }); + }); + }); + }); +}); + // On Windows, _copySingleFileSync's reparse-point branch opens a handle to the // source symlink to resolve its target via GetFinalPathNameByHandleW. Previously // that handle was never closed, leaking one OS handle per symlink copied. Over a