|
| 1 | +// The Windows listing check (#5600): readDirectoryInPlace cannot anchor a |
| 2 | +// listing on Windows (the working directory is a path string there), so it |
| 3 | +// checks the listed directory before and after the read: the path must |
| 4 | +// resolve to itself, with no junction or symlink on it, and must name the |
| 5 | +// same directory both times (pinned-directory.ts). |
| 6 | +// |
| 7 | +// These tests run that branch on every host. On Windows (the CI Windows leg) |
| 8 | +// it runs for real, against real junctions. Elsewhere process.platform is |
| 9 | +// set to "win32", which is all the branch reads; the filesystem stays the |
| 10 | +// host's, so the swaps are real there too, and the coverage gate (which runs |
| 11 | +// on Linux) sees the branch. |
| 12 | +// |
| 13 | +// A swap staged from inside the read can be refused by the host: Windows will |
| 14 | +// not rename a directory a process is in, and the server is in the listed |
| 15 | +// directory. So those tests assert what must hold either way: the entries |
| 16 | +// returned are never the outside directory's, and a swap that did happen is |
| 17 | +// refused. The protocol-level tests of a swap staged before the listing are |
| 18 | +// in list-anchored.test.ts, and run on Windows too. |
| 19 | + |
| 20 | +import fsSync from "fs"; |
| 21 | +import path from "path"; |
| 22 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 23 | +import { readDirectoryInPlace } from "../pinned-directory.js"; |
| 24 | +import { makeTempDir } from "./helpers.js"; |
| 25 | + |
| 26 | +const OUTSIDE_NAME = "OUTSIDE-NAME-CANARY.txt"; |
| 27 | +const INSIDE_NAME = "inside-marker.txt"; |
| 28 | +const DIRECTORY_CHANGED = |
| 29 | + /Access denied - a directory on the path changed during the operation/; |
| 30 | + |
| 31 | +const realPlatform = process.platform; |
| 32 | + |
| 33 | +let dir: string; |
| 34 | +let outside: string; |
| 35 | +let middle: string; |
| 36 | +let listed: string; |
| 37 | + |
| 38 | +beforeEach(async () => { |
| 39 | + dir = await makeTempDir("mcp-fs-list-win-"); |
| 40 | + outside = await makeTempDir("mcp-fs-list-win-outside-"); |
| 41 | + middle = path.join(dir, "middle"); |
| 42 | + listed = path.join(middle, "listed"); |
| 43 | + fsSync.mkdirSync(listed, { recursive: true }); |
| 44 | + fsSync.writeFileSync(path.join(listed, INSIDE_NAME), "inside"); |
| 45 | + // The outside tree mirrors the inside one, so a swapped parent still leads |
| 46 | + // to a real directory named "listed". |
| 47 | + fsSync.mkdirSync(path.join(outside, "listed")); |
| 48 | + fsSync.writeFileSync(path.join(outside, OUTSIDE_NAME), "outside"); |
| 49 | + fsSync.writeFileSync(path.join(outside, "listed", OUTSIDE_NAME), "outside"); |
| 50 | + Object.defineProperty(process, "platform", { value: "win32" }); |
| 51 | +}); |
| 52 | + |
| 53 | +afterEach(() => { |
| 54 | + vi.restoreAllMocks(); |
| 55 | + Object.defineProperty(process, "platform", { value: realPlatform }); |
| 56 | + fsSync.rmSync(dir, { recursive: true, force: true }); |
| 57 | + fsSync.rmSync(outside, { recursive: true, force: true }); |
| 58 | +}); |
| 59 | + |
| 60 | +/** A junction on Windows, which needs no privilege; a symlink elsewhere. */ |
| 61 | +function link(target: string, at: string): void { |
| 62 | + fsSync.symlinkSync(target, at, "junction"); |
| 63 | +} |
| 64 | + |
| 65 | +/** Run `swap` on the first readdirSync, before the real read; say if it ran. */ |
| 66 | +function swapDuringRead(swap: () => void): { swapped: () => boolean } { |
| 67 | + const realReaddirSync = fsSync.readdirSync.bind(fsSync) as ( |
| 68 | + target: fsSync.PathLike, |
| 69 | + options: { withFileTypes: true }, |
| 70 | + ) => fsSync.Dirent[]; |
| 71 | + let attempted = false; |
| 72 | + let swapped = false; |
| 73 | + vi.spyOn(fsSync, "readdirSync").mockImplementation((( |
| 74 | + target: fsSync.PathLike, |
| 75 | + options: { withFileTypes: true }, |
| 76 | + ) => { |
| 77 | + if (!attempted) { |
| 78 | + attempted = true; |
| 79 | + try { |
| 80 | + swap(); |
| 81 | + swapped = true; |
| 82 | + } catch { |
| 83 | + // The host refused the swap (see the header); nothing changed. |
| 84 | + } |
| 85 | + } |
| 86 | + return realReaddirSync(target, options); |
| 87 | + }) as typeof fsSync.readdirSync); |
| 88 | + return { swapped: () => swapped }; |
| 89 | +} |
| 90 | + |
| 91 | +/** List `listed`; the names it returned, or the refusal it threw. */ |
| 92 | +function listNames(): { names?: string[]; refusal?: string } { |
| 93 | + try { |
| 94 | + return { names: readDirectoryInPlace(listed).map((entry) => entry.name) }; |
| 95 | + } catch (error) { |
| 96 | + return { refusal: (error as Error).message }; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +describe("the Windows listing check", () => { |
| 101 | + it("lists a directory nothing was swapped on, naming it as the parent", () => { |
| 102 | + const entries = readDirectoryInPlace(listed); |
| 103 | + expect(entries.map((entry) => entry.name)).toEqual([INSIDE_NAME]); |
| 104 | + expect(entries[0].parentPath).toBe(listed); |
| 105 | + expect(entries[0].isFile()).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it("refuses a directory that is now a link to one outside", () => { |
| 109 | + fsSync.renameSync(listed, path.join(middle, "listed-real")); |
| 110 | + link(outside, listed); |
| 111 | + const { names, refusal } = listNames(); |
| 112 | + expect(names).toBeUndefined(); |
| 113 | + expect(refusal).toMatch(DIRECTORY_CHANGED); |
| 114 | + }); |
| 115 | + |
| 116 | + it("refuses a directory whose parent is now a link to one outside", () => { |
| 117 | + // The last component is a real directory, the outside tree's "listed", so |
| 118 | + // only the path's real location shows the swap. |
| 119 | + fsSync.renameSync(middle, path.join(dir, "middle-real")); |
| 120 | + link(outside, middle); |
| 121 | + const { names, refusal } = listNames(); |
| 122 | + expect(names).toBeUndefined(); |
| 123 | + expect(refusal).toMatch(DIRECTORY_CHANGED); |
| 124 | + }); |
| 125 | + |
| 126 | + it("never returns the outside entries when the directory is swapped for a link during the read", () => { |
| 127 | + const { swapped } = swapDuringRead(() => { |
| 128 | + fsSync.renameSync(listed, path.join(middle, "listed-real")); |
| 129 | + link(outside, listed); |
| 130 | + }); |
| 131 | + const { names, refusal } = listNames(); |
| 132 | + expect(names ?? []).not.toContain(OUTSIDE_NAME); |
| 133 | + if (swapped()) { |
| 134 | + expect(refusal).toMatch(DIRECTORY_CHANGED); |
| 135 | + } else { |
| 136 | + expect(names).toEqual([INSIDE_NAME]); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it("never returns the outside entries when a parent is swapped for a link during the read", () => { |
| 141 | + const { swapped } = swapDuringRead(() => { |
| 142 | + fsSync.renameSync(middle, path.join(dir, "middle-real")); |
| 143 | + link(outside, middle); |
| 144 | + }); |
| 145 | + const { names, refusal } = listNames(); |
| 146 | + expect(names ?? []).not.toContain(OUTSIDE_NAME); |
| 147 | + if (swapped()) { |
| 148 | + expect(refusal).toMatch(DIRECTORY_CHANGED); |
| 149 | + } else { |
| 150 | + expect(names).toEqual([INSIDE_NAME]); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it("refuses when another directory takes the name during the read", () => { |
| 155 | + // No link anywhere, so only the identity shows it is not the directory |
| 156 | + // the first check saw. |
| 157 | + const { swapped } = swapDuringRead(() => { |
| 158 | + fsSync.renameSync(listed, path.join(middle, "listed-real")); |
| 159 | + fsSync.mkdirSync(listed); |
| 160 | + }); |
| 161 | + const { names, refusal } = listNames(); |
| 162 | + if (swapped()) { |
| 163 | + expect(refusal).toMatch(DIRECTORY_CHANGED); |
| 164 | + } else { |
| 165 | + expect(names).toEqual([INSIDE_NAME]); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + it.skipIf(realPlatform === "win32")( |
| 170 | + "is not applied off Windows, where the listing is anchored", |
| 171 | + () => { |
| 172 | + Object.defineProperty(process, "platform", { value: realPlatform }); |
| 173 | + const lstat = vi.spyOn(fsSync, "lstatSync"); |
| 174 | + expect(readDirectoryInPlace(listed).map((entry) => entry.name)).toEqual([ |
| 175 | + INSIDE_NAME, |
| 176 | + ]); |
| 177 | + expect(lstat).not.toHaveBeenCalled(); |
| 178 | + }, |
| 179 | + ); |
| 180 | +}); |
0 commit comments