|
| 1 | +// search_files over a tree with a subdirectory it cannot read (#5189). The |
| 2 | +// walk used to swallow that readdir failure with the validation errors it |
| 3 | +// skips on purpose, so the result came back short with nothing to say so. Now |
| 4 | +// the walk skips the directory, keeps going, and the tool names it after the |
| 5 | +// matches. These tests make readdir fail for one directory by spying on the |
| 6 | +// fs/promises object the server shares with this file, which works on every |
| 7 | +// platform; one more test uses a real chmod 000 directory where POSIX |
| 8 | +// permissions apply. |
| 9 | + |
| 10 | +import fs from "fs/promises"; |
| 11 | +import type { PathLike } 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 | +import { searchFilesWithValidation } from "../lib.js"; |
| 24 | + |
| 25 | +const realReaddir = fs.readdir.bind(fs); |
| 26 | + |
| 27 | +let dir: string; |
| 28 | +let conn: Connected; |
| 29 | +let client: Client; |
| 30 | + |
| 31 | +beforeEach(async () => { |
| 32 | + quietStderr(); |
| 33 | + dir = await makeTempDir("mcp-fs-unreadable-"); |
| 34 | + await fs.mkdir(path.join(dir, "open")); |
| 35 | + await fs.mkdir(path.join(dir, "locked")); |
| 36 | + await fs.mkdir(path.join(dir, "sealed")); |
| 37 | + await fs.writeFile(path.join(dir, "open", "a.txt"), ""); |
| 38 | + await fs.writeFile(path.join(dir, "locked", "hidden.txt"), ""); |
| 39 | + await fs.writeFile(path.join(dir, "sealed", "also-hidden.txt"), ""); |
| 40 | + conn = await connect([dir]); |
| 41 | + client = conn.client; |
| 42 | +}); |
| 43 | + |
| 44 | +afterEach(async () => { |
| 45 | + vi.restoreAllMocks(); |
| 46 | + await conn.close(); |
| 47 | + await fs.chmod(path.join(dir, "locked"), 0o755); |
| 48 | + await fs.rm(dir, { recursive: true, force: true }); |
| 49 | +}); |
| 50 | + |
| 51 | +/** Make readdir reject with `error` for each directory in `failing`. */ |
| 52 | +function failReaddir(failing: string[], error: Error): void { |
| 53 | + vi.spyOn(fs, "readdir").mockImplementation((( |
| 54 | + target: PathLike, |
| 55 | + ...rest: unknown[] |
| 56 | + ) => |
| 57 | + failing.includes(target.toString()) |
| 58 | + ? Promise.reject(error) |
| 59 | + : ( |
| 60 | + realReaddir as ( |
| 61 | + target: PathLike, |
| 62 | + ...rest: unknown[] |
| 63 | + ) => Promise<unknown> |
| 64 | + )(target, ...rest)) as typeof fs.readdir); |
| 65 | +} |
| 66 | + |
| 67 | +function eacces(): Error { |
| 68 | + return Object.assign(new Error("EACCES: permission denied"), { |
| 69 | + code: "EACCES", |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +describe("search_files with an unreadable subdirectory (#5189)", () => { |
| 74 | + it("returns the matches it found and names the directory it could not read", async () => { |
| 75 | + const locked = path.join(dir, "locked"); |
| 76 | + failReaddir([locked], eacces()); |
| 77 | + const result = await call(client, "search_files", { |
| 78 | + path: dir, |
| 79 | + pattern: "**/*.txt", |
| 80 | + }); |
| 81 | + expect(result.isError).toBeFalsy(); |
| 82 | + const text = textOf(result); |
| 83 | + expect(text.split("\n\n")).toEqual([ |
| 84 | + expect.stringContaining(path.join(dir, "open", "a.txt")), |
| 85 | + "The search is incomplete: 1 directory could not be read, and any matches inside are missing:\n" + |
| 86 | + `${locked} (EACCES)`, |
| 87 | + ]); |
| 88 | + expect(text).toContain(path.join(dir, "sealed", "also-hidden.txt")); |
| 89 | + expect(text).not.toContain(path.join(locked, "hidden.txt")); |
| 90 | + expect(result.structuredContent).toEqual({ content: text }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('says "No matches found" and lists every unreadable directory when nothing else matched', async () => { |
| 94 | + const locked = path.join(dir, "locked"); |
| 95 | + const sealed = path.join(dir, "sealed"); |
| 96 | + failReaddir([locked, sealed], eacces()); |
| 97 | + const result = await call(client, "search_files", { |
| 98 | + path: dir, |
| 99 | + pattern: "**/*hidden*", |
| 100 | + }); |
| 101 | + expect(result.isError).toBeFalsy(); |
| 102 | + const [matches, note] = textOf(result).split("\n\n"); |
| 103 | + expect(matches).toBe("No matches found"); |
| 104 | + const [heading, ...listed] = note.split("\n"); |
| 105 | + expect(heading).toBe( |
| 106 | + "The search is incomplete: 2 directories could not be read, and any matches inside are missing:", |
| 107 | + ); |
| 108 | + expect(listed.sort()).toEqual([`${locked} (EACCES)`, `${sealed} (EACCES)`]); |
| 109 | + }); |
| 110 | + |
| 111 | + it("names the directory without a code when the error carries none", async () => { |
| 112 | + const locked = path.join(dir, "locked"); |
| 113 | + failReaddir([locked], new Error("unreadable")); |
| 114 | + const result = await call(client, "search_files", { |
| 115 | + path: dir, |
| 116 | + pattern: "**/a.txt", |
| 117 | + }); |
| 118 | + expect(textOf(result)).toBe( |
| 119 | + `${path.join(dir, "open", "a.txt")}\n\n` + |
| 120 | + "The search is incomplete: 1 directory could not be read, and any matches inside are missing:\n" + |
| 121 | + locked, |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it("still fails the request when the starting directory cannot be read", async () => { |
| 126 | + failReaddir([dir], eacces()); |
| 127 | + const result = await call(client, "search_files", { |
| 128 | + path: dir, |
| 129 | + pattern: "**/*.txt", |
| 130 | + }); |
| 131 | + expect(result.isError).toBe(true); |
| 132 | + expect(textOf(result)).toContain("EACCES"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("reports nothing extra when every directory reads", async () => { |
| 136 | + const result = await call(client, "search_files", { |
| 137 | + path: dir, |
| 138 | + pattern: "**/a.txt", |
| 139 | + }); |
| 140 | + expect(textOf(result)).toBe(path.join(dir, "open", "a.txt")); |
| 141 | + }); |
| 142 | + |
| 143 | + it.skipIf(process.platform === "win32" || process.getuid?.() === 0)( |
| 144 | + "reports a real chmod 000 directory", |
| 145 | + async () => { |
| 146 | + const locked = path.join(dir, "locked"); |
| 147 | + await fs.chmod(locked, 0o000); |
| 148 | + const result = await call(client, "search_files", { |
| 149 | + path: dir, |
| 150 | + pattern: "**/*.txt", |
| 151 | + }); |
| 152 | + expect(result.isError).toBeFalsy(); |
| 153 | + expect(textOf(result)).toContain(`\n${locked} (EACCES)`); |
| 154 | + expect(textOf(result)).toContain(path.join(dir, "open", "a.txt")); |
| 155 | + }, |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +describe("searchFilesWithValidation without an onUnreadable callback", () => { |
| 160 | + it("skips the unreadable directory and returns the rest", async () => { |
| 161 | + const locked = path.join(dir, "locked"); |
| 162 | + failReaddir([locked], eacces()); |
| 163 | + const realDir = await fs.realpath(dir); |
| 164 | + const results = await searchFilesWithValidation(realDir, "**/a.txt", [ |
| 165 | + realDir, |
| 166 | + ]); |
| 167 | + expect(results).toEqual([path.join(realDir, "open", "a.txt")]); |
| 168 | + }); |
| 169 | +}); |
0 commit comments