|
| 1 | +// Protocol-level tests that every directory listing is anchored to the |
| 2 | +// directory validation resolved, as every read and write is |
| 3 | +// (pinned-directory.ts). The swap an attacker would race is staged |
| 4 | +// deterministically, as read-anchored.test.ts stages it: right after |
| 5 | +// validation realpath's a directory the tool is about to list, that directory |
| 6 | +// is swapped for a symlink to a directory outside the allowed one. Each |
| 7 | +// listing must then refuse rather than return the outside directory's entry |
| 8 | +// names. Before listings were anchored, each of these returned them. |
| 9 | + |
| 10 | +import fs from "fs/promises"; |
| 11 | +import fsSync from "fs"; |
| 12 | +import path from "path"; |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 14 | +import type { Client } from "@modelcontextprotocol/client"; |
| 15 | +import { |
| 16 | + call, |
| 17 | + connect, |
| 18 | + makeTempDir, |
| 19 | + quietStderr, |
| 20 | + textOf, |
| 21 | + type Connected, |
| 22 | +} from "./helpers.js"; |
| 23 | + |
| 24 | +// The name is what a listing would leak, so it is the marker. |
| 25 | +const OUTSIDE_NAME = "OUTSIDE-NAME-CANARY.txt"; |
| 26 | +const INSIDE_NAME = "inside-marker.txt"; |
| 27 | + |
| 28 | +let dir: string; |
| 29 | +let outside: string; |
| 30 | +let flip: string; |
| 31 | +let flipReal: string; |
| 32 | +let conn: Connected; |
| 33 | +let client: Client; |
| 34 | + |
| 35 | +beforeEach(async () => { |
| 36 | + quietStderr(); |
| 37 | + dir = await makeTempDir("mcp-fs-list-anchor-"); |
| 38 | + outside = await makeTempDir("mcp-fs-list-anchor-outside-"); |
| 39 | + flip = path.join(dir, "flip"); |
| 40 | + flipReal = path.join(dir, "flip-real"); |
| 41 | + await fs.mkdir(flip); |
| 42 | + await fs.writeFile(path.join(flip, INSIDE_NAME), "inside"); |
| 43 | + await fs.writeFile(path.join(outside, OUTSIDE_NAME), "outside"); |
| 44 | + conn = await connect([dir]); |
| 45 | + client = conn.client; |
| 46 | +}); |
| 47 | + |
| 48 | +afterEach(async () => { |
| 49 | + vi.restoreAllMocks(); |
| 50 | + await conn.close(); |
| 51 | + await fs.rm(dir, { recursive: true, force: true }); |
| 52 | + await fs.rm(outside, { recursive: true, force: true }); |
| 53 | +}); |
| 54 | + |
| 55 | +/** Swap `flip` for a symlink to `outside`, once, right after it is realpath'd. */ |
| 56 | +function swapAfterRealpath(): void { |
| 57 | + const realRealpath = fs.realpath.bind(fs); |
| 58 | + let swapped = false; |
| 59 | + vi.spyOn(fs, "realpath").mockImplementation(async (p, opts) => { |
| 60 | + const resolved = await realRealpath(p, opts); |
| 61 | + if (!swapped && p === flip) { |
| 62 | + swapped = true; |
| 63 | + fsSync.renameSync(flip, flipReal); |
| 64 | + fsSync.symlinkSync(outside, flip); |
| 65 | + } |
| 66 | + return resolved; |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +const DIRECTORY_CHANGED = |
| 71 | + /Access denied - a directory on the path changed during the operation/; |
| 72 | + |
| 73 | +describe.skipIf(process.platform === "win32")( |
| 74 | + "a listed directory swapped for a symlink after validation", |
| 75 | + () => { |
| 76 | + it.each([ |
| 77 | + ["list_directory", {}], |
| 78 | + ["list_directory_with_sizes", {}], |
| 79 | + ["directory_tree", {}], |
| 80 | + ["search_files", { pattern: "**" }], |
| 81 | + ])("%s does not list through it", async (tool, extra) => { |
| 82 | + swapAfterRealpath(); |
| 83 | + const result = await call(client, tool, { path: flip, ...extra }); |
| 84 | + expect(result.isError).toBe(true); |
| 85 | + expect(textOf(result)).toMatch(DIRECTORY_CHANGED); |
| 86 | + expect(JSON.stringify(result)).not.toContain(OUTSIDE_NAME); |
| 87 | + }); |
| 88 | + |
| 89 | + it("directory_tree does not list a subdirectory through it", async () => { |
| 90 | + swapAfterRealpath(); |
| 91 | + const result = await call(client, "directory_tree", { path: dir }); |
| 92 | + expect(result.isError).toBe(true); |
| 93 | + expect(textOf(result)).toMatch(DIRECTORY_CHANGED); |
| 94 | + expect(JSON.stringify(result)).not.toContain(OUTSIDE_NAME); |
| 95 | + }); |
| 96 | + |
| 97 | + it("search_files does not search a subdirectory through it, and says so", async () => { |
| 98 | + swapAfterRealpath(); |
| 99 | + const result = await call(client, "search_files", { |
| 100 | + path: dir, |
| 101 | + pattern: "**", |
| 102 | + }); |
| 103 | + expect(result.isError).toBeFalsy(); |
| 104 | + expect(JSON.stringify(result)).not.toContain(OUTSIDE_NAME); |
| 105 | + // The swapped directory is reported as unreadable, not as empty. |
| 106 | + expect(textOf(result)).toContain(`${flip}`); |
| 107 | + expect(textOf(result)).toMatch(/could not be read/i); |
| 108 | + }); |
| 109 | + }, |
| 110 | +); |
| 111 | + |
| 112 | +describe("listings that are not swapped", () => { |
| 113 | + it("list every tool's way, from the directory validation resolved", async () => { |
| 114 | + expect(textOf(await call(client, "list_directory", { path: flip }))).toBe( |
| 115 | + `[FILE] ${INSIDE_NAME}`, |
| 116 | + ); |
| 117 | + expect( |
| 118 | + textOf(await call(client, "list_directory_with_sizes", { path: flip })), |
| 119 | + ).toContain(`[FILE] ${INSIDE_NAME}`); |
| 120 | + expect( |
| 121 | + JSON.parse(textOf(await call(client, "directory_tree", { path: dir }))), |
| 122 | + ).toEqual([ |
| 123 | + { |
| 124 | + name: "flip", |
| 125 | + type: "directory", |
| 126 | + children: [{ name: INSIDE_NAME, type: "file" }], |
| 127 | + }, |
| 128 | + ]); |
| 129 | + expect( |
| 130 | + textOf( |
| 131 | + await call(client, "search_files", { path: dir, pattern: "**/*.txt" }), |
| 132 | + ), |
| 133 | + ).toBe(path.join(flip, INSIDE_NAME)); |
| 134 | + }); |
| 135 | + |
| 136 | + it("refuses a static symlink to a directory outside, as before", async () => { |
| 137 | + const link = path.join(dir, "link"); |
| 138 | + await fs.symlink(outside, link); |
| 139 | + for (const tool of ["list_directory", "directory_tree"]) { |
| 140 | + const result = await call(client, tool, { path: link }); |
| 141 | + expect(result.isError).toBe(true); |
| 142 | + expect(JSON.stringify(result)).not.toContain(OUTSIDE_NAME); |
| 143 | + } |
| 144 | + }); |
| 145 | +}); |
0 commit comments