|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 6 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 7 | +import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js"; |
| 8 | +import { createServer } from "../src/server"; |
| 9 | + |
| 10 | +const FIXTURE_PREFIX = path.join(os.tmpdir(), "gpd-mcp-containment-"); |
| 11 | +const EXTERNAL_CONTENT = "# External fixture\nOUTSIDE_MARKER\n"; |
| 12 | +const providerRequest = vi.fn<typeof fetch>(async () => new Response( |
| 13 | + 'data: {"choices":[{"delta":{"content":"Generated documentation"}}]}\n\ndata: [DONE]\n\n', |
| 14 | + { status: 200 }, |
| 15 | +)); |
| 16 | +let fixture: string; |
| 17 | +let root: string; |
| 18 | +let outside: string; |
| 19 | +let client: Client; |
| 20 | +let server: ReturnType<typeof createServer>; |
| 21 | + |
| 22 | +function toolText(result: unknown): string { |
| 23 | + return CallToolResultSchema.parse(result).content.flatMap((block) => block.type === "text" ? [block.text] : []).join("\n"); |
| 24 | +} |
| 25 | + |
| 26 | +beforeEach(async () => { |
| 27 | + fixture = mkdtempSync(FIXTURE_PREFIX); |
| 28 | + root = path.join(fixture, "root"); |
| 29 | + outside = path.join(fixture, "outside"); |
| 30 | + mkdirSync(path.join(root, "docs"), { recursive: true }); |
| 31 | + mkdirSync(outside); |
| 32 | + writeFileSync(path.join(outside, "secret.md"), EXTERNAL_CONTENT, "utf8"); |
| 33 | + symlinkSync(outside, path.join(root, "docs", "linked"), process.platform === "win32" ? "junction" : "dir"); |
| 34 | + providerRequest.mockClear(); |
| 35 | + vi.stubEnv("OPENAI_API_KEY", "fixture-key"); |
| 36 | + vi.stubGlobal("fetch", providerRequest); |
| 37 | + server = createServer(root); |
| 38 | + client = new Client({ name: "filesystem-containment-test", version: "1.0.0" }); |
| 39 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 40 | + await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(async () => { |
| 44 | + await client?.close(); |
| 45 | + await server?.close(); |
| 46 | + vi.unstubAllGlobals(); |
| 47 | + vi.unstubAllEnvs(); |
| 48 | + if (!path.resolve(fixture).startsWith(FIXTURE_PREFIX)) throw new Error("Unexpected fixture path"); |
| 49 | + rmSync(fixture, { recursive: true, force: true }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("MCP filesystem containment", () => { |
| 53 | + it("rejects documentation updates through an external directory link before contacting the provider", async () => { |
| 54 | + const result = await client.callTool({ name: "update_documentation", arguments: { path: "docs/linked/secret.md" } }); |
| 55 | + expect(result.isError).toBe(true); |
| 56 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: Path escapes the project root/); |
| 57 | + expect(providerRequest).not.toHaveBeenCalled(); |
| 58 | + expect(readFileSync(path.join(outside, "secret.md"), "utf8")).toBe(EXTERNAL_CONTENT); |
| 59 | + }); |
| 60 | + |
| 61 | + it("rejects writes through an external directory link without replacing the external file", async () => { |
| 62 | + const result = await client.callTool({ |
| 63 | + name: "write_file", |
| 64 | + arguments: { path: "docs/linked/secret.md", content: "# Replacement\n" }, |
| 65 | + }); |
| 66 | + expect(result.isError).toBe(true); |
| 67 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: /); |
| 68 | + expect(readFileSync(path.join(outside, "secret.md"), "utf8")).toBe(EXTERNAL_CONTENT); |
| 69 | + }); |
| 70 | + |
| 71 | + it("rejects new documentation beneath an external directory link before contacting the provider", async () => { |
| 72 | + const result = await client.callTool({ |
| 73 | + name: "update_documentation", |
| 74 | + arguments: { path: "docs/linked/new/guide.md" }, |
| 75 | + }); |
| 76 | + expect(result.isError).toBe(true); |
| 77 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: Path escapes the project root/); |
| 78 | + expect(providerRequest).not.toHaveBeenCalled(); |
| 79 | + expect(existsSync(path.join(outside, "new"))).toBe(false); |
| 80 | + expect(readFileSync(path.join(outside, "secret.md"), "utf8")).toBe(EXTERNAL_CONTENT); |
| 81 | + }); |
| 82 | + |
| 83 | + it("rejects reading an external file through a symbolic link", async () => { |
| 84 | + symlinkSync(path.join(outside, "secret.md"), path.join(root, "linked.md"), "file"); |
| 85 | + const result = await client.callTool({ name: "read_file", arguments: { path: "linked.md" } }); |
| 86 | + expect(result.isError).toBe(true); |
| 87 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: Path escapes the project root/); |
| 88 | + expect(toolText(result)).not.toContain(EXTERNAL_CONTENT); |
| 89 | + }); |
| 90 | + |
| 91 | + it("rejects listing an external directory through a link", async () => { |
| 92 | + const result = await client.callTool({ |
| 93 | + name: "list_files", |
| 94 | + arguments: { path: "docs/linked", recursive: true }, |
| 95 | + }); |
| 96 | + expect(result.isError).toBe(true); |
| 97 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: Path escapes the project root/); |
| 98 | + expect(toolText(result)).not.toContain("secret.md"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("searches internal files without returning content from external file links", async () => { |
| 102 | + symlinkSync(path.join(outside, "secret.md"), path.join(root, "linked.md"), "file"); |
| 103 | + writeFileSync(path.join(root, "docs", "internal.md"), "# Internal\nOUTSIDE_MARKER inside root\n", "utf8"); |
| 104 | + const result = await client.callTool({ |
| 105 | + name: "search_project", |
| 106 | + arguments: { query: "OUTSIDE_MARKER", extension: ".md" }, |
| 107 | + }); |
| 108 | + expect(result.isError).not.toBe(true); |
| 109 | + expect(JSON.parse(toolText(result))).toEqual([ |
| 110 | + { file: "docs/internal.md", line: 2, text: "OUTSIDE_MARKER inside root" }, |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it("rejects creating files beneath an external directory link without creating external directories", async () => { |
| 115 | + const result = await client.callTool({ |
| 116 | + name: "write_file", |
| 117 | + arguments: { path: "docs/linked/new/guide.md", content: "# New document\n" }, |
| 118 | + }); |
| 119 | + expect(result.isError).toBe(true); |
| 120 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: Path escapes the project root/); |
| 121 | + expect(existsSync(path.join(outside, "new"))).toBe(false); |
| 122 | + expect(readFileSync(path.join(outside, "secret.md"), "utf8")).toBe(EXTERNAL_CONTENT); |
| 123 | + }); |
| 124 | + |
| 125 | + it("rejects writing through a file link without replacing the external file", async () => { |
| 126 | + symlinkSync(path.join(outside, "secret.md"), path.join(root, "linked.md"), "file"); |
| 127 | + const result = await client.callTool({ |
| 128 | + name: "write_file", |
| 129 | + arguments: { path: "linked.md", content: "# Replacement\n" }, |
| 130 | + }); |
| 131 | + expect(result.isError).toBe(true); |
| 132 | + expect(toolText(result)).toMatch(/^VALIDATION_ERROR: /); |
| 133 | + expect(readFileSync(path.join(outside, "secret.md"), "utf8")).toBe(EXTERNAL_CONTENT); |
| 134 | + }); |
| 135 | + |
| 136 | +}); |
0 commit comments