|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { movePathWithCopyFallback } from "../src/move-path.js"; |
| 6 | + |
| 7 | +const tempDirs: string[] = []; |
| 8 | + |
| 9 | +async function tempRoot(prefix: string): Promise<string> { |
| 10 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); |
| 11 | + tempDirs.push(dir); |
| 12 | + return dir; |
| 13 | +} |
| 14 | + |
| 15 | +async function installStagingModeSwap(params: { |
| 16 | + targetDir: string; |
| 17 | + victimPath: string; |
| 18 | + symlinkType: "dir" | "file"; |
| 19 | +}): Promise<() => number> { |
| 20 | + const probePath = path.join(params.targetDir, "handle-probe"); |
| 21 | + const probe = await fs.open(probePath, "w"); |
| 22 | + const handlePrototype = Object.getPrototypeOf(probe) as { |
| 23 | + chmod(mode: number): Promise<void>; |
| 24 | + }; |
| 25 | + const realHandleChmod = handlePrototype.chmod; |
| 26 | + await probe.close(); |
| 27 | + await fs.unlink(probePath); |
| 28 | + |
| 29 | + const realPathChmod = fs.chmod; |
| 30 | + let swaps = 0; |
| 31 | + |
| 32 | + const findStagingPath = async (): Promise<string> => { |
| 33 | + const name = (await fs.readdir(params.targetDir)).find( |
| 34 | + (entry) => entry.startsWith(".fs-safe-move-") && entry.endsWith(".tmp"), |
| 35 | + ); |
| 36 | + if (!name) { |
| 37 | + throw new Error("move staging entry not found"); |
| 38 | + } |
| 39 | + return path.join(params.targetDir, name); |
| 40 | + }; |
| 41 | + |
| 42 | + const swapAround = async (stagingPath: string, applyMode: () => Promise<void>) => { |
| 43 | + swaps += 1; |
| 44 | + const parkedPath = `${stagingPath}.parked`; |
| 45 | + await fs.rename(stagingPath, parkedPath); |
| 46 | + await fs.symlink(params.victimPath, stagingPath, params.symlinkType); |
| 47 | + try { |
| 48 | + await applyMode(); |
| 49 | + } finally { |
| 50 | + await fs.unlink(stagingPath); |
| 51 | + await fs.rename(parkedPath, stagingPath); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + vi.spyOn(fs, "chmod").mockImplementation(async (candidate, mode) => { |
| 56 | + const candidatePath = String(candidate); |
| 57 | + if (!path.basename(candidatePath).startsWith(".fs-safe-move-")) { |
| 58 | + await realPathChmod(candidate, mode); |
| 59 | + return; |
| 60 | + } |
| 61 | + await swapAround(candidatePath, async () => await realPathChmod(candidate, mode)); |
| 62 | + }); |
| 63 | + vi.spyOn(handlePrototype, "chmod").mockImplementation(async function (mode) { |
| 64 | + const stagingPath = await findStagingPath(); |
| 65 | + await swapAround(stagingPath, async () => await realHandleChmod.call(this, mode)); |
| 66 | + }); |
| 67 | + |
| 68 | + return () => swaps; |
| 69 | +} |
| 70 | + |
| 71 | +afterEach(async () => { |
| 72 | + vi.restoreAllMocks(); |
| 73 | + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { force: true, recursive: true }))); |
| 74 | +}); |
| 75 | + |
| 76 | +describe.runIf(process.platform !== "win32")("move staging descriptor modes", () => { |
| 77 | + it("does not redirect a staged file mode through a swapped symlink", async () => { |
| 78 | + const sourceDir = await tempRoot("fs-safe-move-file-source-"); |
| 79 | + const targetDir = await tempRoot("fs-safe-move-file-target-"); |
| 80 | + const victimDir = await tempRoot("fs-safe-move-file-victim-"); |
| 81 | + const sourcePath = path.join(sourceDir, "source.txt"); |
| 82 | + const targetPath = path.join(targetDir, "target.txt"); |
| 83 | + const victimPath = path.join(victimDir, "victim.txt"); |
| 84 | + await fs.writeFile(sourcePath, "source"); |
| 85 | + await fs.chmod(sourcePath, 0o666); |
| 86 | + await fs.writeFile(victimPath, "victim"); |
| 87 | + await fs.chmod(victimPath, 0o644); |
| 88 | + const swaps = await installStagingModeSwap({ |
| 89 | + targetDir, |
| 90 | + victimPath, |
| 91 | + symlinkType: "file", |
| 92 | + }); |
| 93 | + |
| 94 | + const previousUmask = process.umask(0o077); |
| 95 | + try { |
| 96 | + await movePathWithCopyFallback({ |
| 97 | + from: sourcePath, |
| 98 | + sourceHardlinks: "reject", |
| 99 | + to: targetPath, |
| 100 | + }); |
| 101 | + } finally { |
| 102 | + process.umask(previousUmask); |
| 103 | + } |
| 104 | + |
| 105 | + expect({ |
| 106 | + publishedMode: (await fs.stat(targetPath)).mode & 0o777, |
| 107 | + swaps: swaps(), |
| 108 | + victimMode: (await fs.stat(victimPath)).mode & 0o777, |
| 109 | + }).toEqual({ publishedMode: 0o666, swaps: 1, victimMode: 0o644 }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("does not redirect a staged directory mode through a swapped symlink", async () => { |
| 113 | + const sourceParent = await tempRoot("fs-safe-move-dir-source-"); |
| 114 | + const targetDir = await tempRoot("fs-safe-move-dir-target-"); |
| 115 | + const victimParent = await tempRoot("fs-safe-move-dir-victim-"); |
| 116 | + const sourcePath = path.join(sourceParent, "source"); |
| 117 | + const targetPath = path.join(targetDir, "target"); |
| 118 | + const victimPath = path.join(victimParent, "victim"); |
| 119 | + await fs.mkdir(sourcePath, { mode: 0o777 }); |
| 120 | + await fs.chmod(sourcePath, 0o777); |
| 121 | + await fs.mkdir(victimPath, { mode: 0o755 }); |
| 122 | + await fs.chmod(victimPath, 0o755); |
| 123 | + const swaps = await installStagingModeSwap({ |
| 124 | + targetDir, |
| 125 | + victimPath, |
| 126 | + symlinkType: "dir", |
| 127 | + }); |
| 128 | + |
| 129 | + const previousUmask = process.umask(0o077); |
| 130 | + try { |
| 131 | + await movePathWithCopyFallback({ |
| 132 | + from: sourcePath, |
| 133 | + sourceHardlinks: "reject", |
| 134 | + to: targetPath, |
| 135 | + }); |
| 136 | + } finally { |
| 137 | + process.umask(previousUmask); |
| 138 | + } |
| 139 | + |
| 140 | + expect({ |
| 141 | + publishedMode: (await fs.stat(targetPath)).mode & 0o777, |
| 142 | + swaps: swaps(), |
| 143 | + victimMode: (await fs.stat(victimPath)).mode & 0o777, |
| 144 | + }).toEqual({ publishedMode: 0o777, swaps: 1, victimMode: 0o755 }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments