Skip to content

Commit 956e7f9

Browse files
cliffhallclaude
andcommitted
fix(filesystem): report a missing path found by validatePath as resource not found
A dangling symlink, a missing parent or a path beneath a regular file fails inside validatePath with ENOENT or ENOTDIR (directly or as the cause). Map those to ResourceNotFoundError, with the URI in the error data, rather than a plain invalid-params error. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 3f173d6 commit 956e7f9

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎src/filesystem/__tests__/file-resources.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ describe("resources/read", () => {
149149
expect(error.data).toEqual({ uri });
150150
});
151151

152+
it("reports a dangling symlink as resource not found", async () => {
153+
const link = path.join(allowed, "dangling.txt");
154+
await fs.symlink(path.join(allowed, "gone", "nowhere.txt"), link);
155+
const client = await connectTo([allowed]);
156+
const uri = uriOf(link);
157+
const error = await readError(client, uri);
158+
expect(error).toBeInstanceOf(ResourceNotFoundError);
159+
expect(error.data).toEqual({ uri });
160+
});
161+
162+
it("reports a path beneath a regular file as resource not found", async () => {
163+
await fs.writeFile(path.join(allowed, "parent.txt"), "x");
164+
const client = await connectTo([allowed]);
165+
const uri = uriOf(path.join(allowed, "parent.txt", "child.txt"));
166+
const error = await readError(client, uri);
167+
expect(error).toBeInstanceOf(ResourceNotFoundError);
168+
expect(error.data).toEqual({ uri });
169+
});
170+
152171
it("reports a directory as not a resource", async () => {
153172
await fs.mkdir(path.join(allowed, "sub"));
154173
const client = await connectTo([allowed]);

‎src/filesystem/file-resources.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ export function fileResourceContents(
112112
};
113113
}
114114

115+
/** The errno codes that mean a path does not exist. */
116+
const MISSING_PATH_CODES = new Set(["ENOENT", "ENOTDIR"]);
117+
118+
/**
119+
* Whether `error` says the path does not exist, directly or through its
120+
* cause: validatePath wraps a missing parent's ENOENT in an Error of its own.
121+
*/
122+
function isMissingPathError(error: unknown): boolean {
123+
for (let e = error; e instanceof Error; e = e.cause) {
124+
const code = (e as NodeJS.ErrnoException).code;
125+
if (code !== undefined && MISSING_PATH_CODES.has(code)) return true;
126+
}
127+
return false;
128+
}
129+
115130
export interface FileResourceContext {
116131
/** The directories reads are confined to, read at the moment of each read. */
117132
allowedDirectories: () => readonly string[];
@@ -173,8 +188,14 @@ export function registerFileResources(
173188
context.allowedDirectories(),
174189
);
175190
} catch (error) {
176-
// Outside the allowed directories, a symlink out of them, or a parent
177-
// that does not exist: the message says which, as the tools do.
191+
// A path that does not exist (a dangling symlink, a missing parent, a
192+
// path beneath a regular file) is a missing resource, so a client can
193+
// recognise it by the URI in the error data.
194+
if (isMissingPathError(error)) {
195+
throw new ResourceNotFoundError(uri.href, (error as Error).message);
196+
}
197+
// Outside the allowed directories, or a symlink out of them: the
198+
// message says which, as the tools do.
178199
throw new ProtocolError(
179200
ProtocolErrorCode.InvalidParams,
180201
(error as Error).message,
@@ -185,9 +206,10 @@ export function registerFileResources(
185206
try {
186207
bytes = await fs.readFile(validPath);
187208
} catch (error) {
188-
const code = (error as NodeJS.ErrnoException).code;
189-
if (code === "ENOENT") throw new ResourceNotFoundError(uri.href);
190-
if (code === "EISDIR") {
209+
if (isMissingPathError(error)) {
210+
throw new ResourceNotFoundError(uri.href);
211+
}
212+
if ((error as NodeJS.ErrnoException).code === "EISDIR") {
191213
throw new ResourceNotFoundError(
192214
uri.href,
193215
`Not a file: ${uri.href} is a directory; directories are not resources`,

0 commit comments

Comments
 (0)