|
| 1 | +// --confine-roots (#3133): a 2025-era client's Roots may narrow the operator's |
| 2 | +// command-line directories but never replace or widen them. The overlap rule |
| 3 | +// is pinned on confineRootsToDirectories directly; the protocol tests drive a |
| 4 | +// real Client that answers roots/list over the in-memory transport and assert |
| 5 | +// what the server then allows, logs and refuses, including the withdrawal and |
| 6 | +// symlink cases the unconfined flow already guards. The 2026-07-28 era never |
| 7 | +// asks for Roots, so there the flag changes nothing, which main() pins. |
| 8 | + |
| 9 | +import fs from "fs/promises"; |
| 10 | +import path from "path"; |
| 11 | +import { PassThrough } from "node:stream"; |
| 12 | +import { pathToFileURL } from "url"; |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 14 | +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; |
| 15 | +import type { Root } from "@modelcontextprotocol/server"; |
| 16 | +import { |
| 17 | + confineRootsToDirectories, |
| 18 | + describeAllowedDirectories, |
| 19 | +} from "../allowed-directories.js"; |
| 20 | +import { main, parseArgs } from "../index.js"; |
| 21 | +import { createServer } from "../server.js"; |
| 22 | +import { |
| 23 | + allowedDirectoriesOf, |
| 24 | + call, |
| 25 | + makeTempDir, |
| 26 | + quietStderr, |
| 27 | + textOf, |
| 28 | +} from "./helpers.js"; |
| 29 | + |
| 30 | +let base: string; |
| 31 | +let outside: string; |
| 32 | +let stderr: ReturnType<typeof quietStderr>; |
| 33 | +let closers: Array<() => Promise<void>>; |
| 34 | + |
| 35 | +beforeEach(async () => { |
| 36 | + stderr = quietStderr(); |
| 37 | + base = await makeTempDir("mcp-fs-confine-base-"); |
| 38 | + outside = await makeTempDir("mcp-fs-confine-outside-"); |
| 39 | + await fs.mkdir(path.join(base, "inner")); |
| 40 | + await fs.writeFile(path.join(base, "inner", "in.txt"), "inside"); |
| 41 | + await fs.writeFile(path.join(base, "top.txt"), "top"); |
| 42 | + await fs.writeFile(path.join(outside, "out.txt"), "outside"); |
| 43 | + closers = []; |
| 44 | +}); |
| 45 | + |
| 46 | +afterEach(async () => { |
| 47 | + for (const close of closers.reverse()) await close(); |
| 48 | + vi.restoreAllMocks(); |
| 49 | + await fs.rm(base, { recursive: true, force: true }); |
| 50 | + await fs.rm(outside, { recursive: true, force: true }); |
| 51 | +}); |
| 52 | + |
| 53 | +function rootOf(dir: string): Root { |
| 54 | + return { uri: pathToFileURL(dir).href, name: path.basename(dir) }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * A 2025-era client that supports Roots and answers roots/list with |
| 59 | + * `roots()`, connected to a server for `directories` with confinement on. |
| 60 | + */ |
| 61 | +async function connect( |
| 62 | + directories: string[], |
| 63 | + roots: () => Root[], |
| 64 | + confineRoots = true, |
| 65 | +): Promise<Client> { |
| 66 | + const server = createServer(directories, { confineRoots }); |
| 67 | + const client = new Client( |
| 68 | + { name: "fs-confine-test", version: "0.0.0" }, |
| 69 | + { capabilities: { roots: { listChanged: true } } }, |
| 70 | + ); |
| 71 | + client.setRequestHandler("roots/list", async () => ({ roots: roots() })); |
| 72 | + const [clientTransport, serverTransport] = |
| 73 | + InMemoryTransport.createLinkedPair(); |
| 74 | + await Promise.all([ |
| 75 | + server.connect(serverTransport), |
| 76 | + client.connect(clientTransport), |
| 77 | + ]); |
| 78 | + closers.push(async () => { |
| 79 | + await client.close(); |
| 80 | + await server.close(); |
| 81 | + }); |
| 82 | + return client; |
| 83 | +} |
| 84 | + |
| 85 | +describe("confineRootsToDirectories", () => { |
| 86 | + it("keeps a root inside a command-line directory", () => { |
| 87 | + expect(confineRootsToDirectories(["/b/x"], ["/b"])).toEqual({ |
| 88 | + confined: ["/b/x"], |
| 89 | + ignored: [], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("narrows a root that contains command-line directories to them", () => { |
| 94 | + expect(confineRootsToDirectories(["/"], ["/b", "/c/d"])).toEqual({ |
| 95 | + confined: ["/b", "/c/d"], |
| 96 | + ignored: [], |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("ignores a root that overlaps no command-line directory", () => { |
| 101 | + expect(confineRootsToDirectories(["/elsewhere", "/b"], ["/b"])).toEqual({ |
| 102 | + confined: ["/b"], |
| 103 | + ignored: ["/elsewhere"], |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not treat a sibling with a common prefix as inside", () => { |
| 108 | + expect(confineRootsToDirectories(["/base-other"], ["/base"])).toEqual({ |
| 109 | + confined: [], |
| 110 | + ignored: ["/base-other"], |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("lists each directory once", () => { |
| 115 | + expect(confineRootsToDirectories(["/b", "/", "/b"], ["/b"])).toEqual({ |
| 116 | + confined: ["/b"], |
| 117 | + ignored: [], |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe("with --confine-roots on 2025-11-25", () => { |
| 123 | + it("narrows the allowed directories to a root inside them", async () => { |
| 124 | + const inner = path.join(base, "inner"); |
| 125 | + const client = await connect([base], () => [rootOf(inner)]); |
| 126 | + await vi.waitFor(async () => |
| 127 | + expect(await allowedDirectoriesOf(client)).toEqual([inner]), |
| 128 | + ); |
| 129 | + expect(stderr).toHaveBeenCalledWith( |
| 130 | + describeAllowedDirectories([inner], { |
| 131 | + from: "roots", |
| 132 | + replacedCommandLine: true, |
| 133 | + confined: true, |
| 134 | + }), |
| 135 | + ); |
| 136 | + const top = await call(client, "read_text_file", { |
| 137 | + path: path.join(base, "top.txt"), |
| 138 | + }); |
| 139 | + expect(textOf(top)).toMatch(/^Access denied/); |
| 140 | + }); |
| 141 | + |
| 142 | + it("ignores a root outside them and keeps the command-line directories", async () => { |
| 143 | + const client = await connect([base], () => [rootOf(outside)]); |
| 144 | + await vi.waitFor(() => |
| 145 | + expect(stderr).toHaveBeenCalledWith( |
| 146 | + `Ignored client roots outside the command-line directories (--confine-roots): ${outside}`, |
| 147 | + ), |
| 148 | + ); |
| 149 | + expect(await allowedDirectoriesOf(client)).toEqual([base]); |
| 150 | + const out = await call(client, "read_text_file", { |
| 151 | + path: path.join(outside, "out.txt"), |
| 152 | + }); |
| 153 | + expect(textOf(out)).toMatch(/^Access denied/); |
| 154 | + const top = await call(client, "read_text_file", { |
| 155 | + path: path.join(base, "top.txt"), |
| 156 | + }); |
| 157 | + expect(textOf(top)).toBe("top"); |
| 158 | + }); |
| 159 | + |
| 160 | + it("narrows a root that contains a command-line directory to that directory", async () => { |
| 161 | + const client = await connect([base], () => [ |
| 162 | + rootOf(path.dirname(base)), |
| 163 | + rootOf(outside), |
| 164 | + ]); |
| 165 | + await vi.waitFor(async () => |
| 166 | + expect(await allowedDirectoriesOf(client)).toEqual([base]), |
| 167 | + ); |
| 168 | + const out = await call(client, "read_text_file", { |
| 169 | + path: path.join(outside, "out.txt"), |
| 170 | + }); |
| 171 | + expect(textOf(out)).toMatch(/^Access denied/); |
| 172 | + }); |
| 173 | + |
| 174 | + it("refuses a symlink inside the directories that points outside them", async () => { |
| 175 | + const link = path.join(base, "link"); |
| 176 | + await fs.symlink(outside, link); |
| 177 | + const client = await connect([base], () => [rootOf(link)]); |
| 178 | + await vi.waitFor(() => |
| 179 | + expect(stderr).toHaveBeenCalledWith( |
| 180 | + `Ignored client roots outside the command-line directories (--confine-roots): ${outside}`, |
| 181 | + ), |
| 182 | + ); |
| 183 | + const out = await call(client, "read_text_file", { |
| 184 | + path: path.join(link, "out.txt"), |
| 185 | + }); |
| 186 | + expect(textOf(out)).toMatch(/^Access denied/); |
| 187 | + }); |
| 188 | + |
| 189 | + it("revokes access once roots were in force and every new root is outside", async () => { |
| 190 | + const inner = path.join(base, "inner"); |
| 191 | + let roots = [rootOf(inner)]; |
| 192 | + const client = await connect([base], () => roots); |
| 193 | + await vi.waitFor(async () => |
| 194 | + expect(await allowedDirectoriesOf(client)).toEqual([inner]), |
| 195 | + ); |
| 196 | + roots = [rootOf(outside)]; |
| 197 | + await client.sendRootsListChanged(); |
| 198 | + await vi.waitFor(async () => |
| 199 | + expect(await allowedDirectoriesOf(client)).toEqual([]), |
| 200 | + ); |
| 201 | + const out = await call(client, "read_text_file", { |
| 202 | + path: path.join(outside, "out.txt"), |
| 203 | + }); |
| 204 | + expect(textOf(out)).toMatch(/^Access denied/); |
| 205 | + }); |
| 206 | + |
| 207 | + it("lets the roots set the directories when none were given on the command line", async () => { |
| 208 | + const client = await connect([], () => [rootOf(outside)]); |
| 209 | + await vi.waitFor(async () => |
| 210 | + expect(await allowedDirectoriesOf(client)).toEqual([outside]), |
| 211 | + ); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
| 215 | +describe("without --confine-roots", () => { |
| 216 | + it("still lets a root outside the command-line directories replace them", async () => { |
| 217 | + const client = await connect([base], () => [rootOf(outside)], false); |
| 218 | + await vi.waitFor(async () => |
| 219 | + expect(await allowedDirectoriesOf(client)).toEqual([outside]), |
| 220 | + ); |
| 221 | + }); |
| 222 | +}); |
| 223 | + |
| 224 | +describe("the command line", () => { |
| 225 | + it("parses --confine-roots anywhere before --", () => { |
| 226 | + expect(parseArgs(["/a", "--confine-roots"])).toEqual({ |
| 227 | + directories: ["/a"], |
| 228 | + readOnly: false, |
| 229 | + confineRoots: true, |
| 230 | + }); |
| 231 | + expect(parseArgs(["--", "--confine-roots"])).toEqual({ |
| 232 | + directories: ["--confine-roots"], |
| 233 | + readOnly: false, |
| 234 | + confineRoots: false, |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + it("warns that --confine-roots does nothing without directories", async () => { |
| 239 | + const handle = await main( |
| 240 | + ["--confine-roots"], |
| 241 | + new PassThrough(), |
| 242 | + new PassThrough(), |
| 243 | + ); |
| 244 | + closers.push(() => handle.close()); |
| 245 | + expect(stderr).toHaveBeenCalledWith( |
| 246 | + "Warning: --confine-roots has no effect without command-line directories; a client's Roots will set the allowed directories", |
| 247 | + ); |
| 248 | + }); |
| 249 | + |
| 250 | + it("serves the command-line directories on 2026-07-28, as without the flag", async () => { |
| 251 | + const stdin = new PassThrough(); |
| 252 | + const stdout = new PassThrough(); |
| 253 | + stdout.setEncoding("utf8"); |
| 254 | + const handle = await main(["--confine-roots", base], stdin, stdout); |
| 255 | + closers.push(() => handle.close()); |
| 256 | + const line = new Promise<string>((resolve) => { |
| 257 | + let buffered = ""; |
| 258 | + stdout.on("data", (chunk: string) => { |
| 259 | + buffered += chunk; |
| 260 | + const newline = buffered.indexOf("\n"); |
| 261 | + if (newline !== -1) resolve(buffered.slice(0, newline)); |
| 262 | + }); |
| 263 | + }); |
| 264 | + stdin.write( |
| 265 | + `${JSON.stringify({ |
| 266 | + jsonrpc: "2.0", |
| 267 | + id: 1, |
| 268 | + method: "tools/call", |
| 269 | + params: { |
| 270 | + _meta: { |
| 271 | + "io.modelcontextprotocol/protocolVersion": "2026-07-28", |
| 272 | + "io.modelcontextprotocol/clientCapabilities": { roots: {} }, |
| 273 | + }, |
| 274 | + name: "list_allowed_directories", |
| 275 | + arguments: {}, |
| 276 | + }, |
| 277 | + })}\n`, |
| 278 | + ); |
| 279 | + const { result } = JSON.parse(await line) as { |
| 280 | + result: { content: Array<{ text: string }> }; |
| 281 | + }; |
| 282 | + expect(result.content[0].text).toBe(`Allowed directories:\n${base}`); |
| 283 | + }); |
| 284 | +}); |
0 commit comments