Skip to content

Commit 036850a

Browse files
cliffhallclaude
andcommitted
fix(filesystem): address GHSA-7m4x-rv66-mxhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent f3f636d commit 036850a

3 files changed

Lines changed: 168 additions & 9 deletions

File tree

‎.changeset/filesystem-anchor-reads.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@modelcontextprotocol/server-filesystem": patch
33
---
44

5-
Anchor read tools to the validated file, as write tools are. A FIFO, socket or device is refused rather than read.
5+
Anchor read tools to the validated file, as write tools are, and pin the source of a same-device `move_file` to the entry that was validated. A FIFO, socket or device is refused rather than read.

‎src/filesystem/__tests__/pinned-directory.test.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,111 @@ describe("renameIntoDirectory", () => {
431431
expect(fs.existsSync(path.join(dir, "a.txt"))).toBe(true);
432432
},
433433
);
434+
435+
// The source's directory swapped for a link to an outside one between the
436+
// source's identity check and the rename: the rename resolves the swapped
437+
// path and stages the outside entry, which must go back, never through.
438+
const swapSourceOnRename = (after?: () => void) => {
439+
const realRename = fs.renameSync;
440+
let swapped = false;
441+
vi.spyOn(fs, "renameSync").mockImplementation((from, to) => {
442+
if (!swapped) {
443+
swapped = true;
444+
realRename(path.join(dir, "sub"), path.join(dir, "sub-real"));
445+
fs.symlinkSync(outside, path.join(dir, "sub"));
446+
realRename(from, to);
447+
after?.();
448+
return;
449+
}
450+
realRename(from, to);
451+
});
452+
};
453+
454+
it.skipIf(!symlinks)(
455+
"puts back an outside file a swapped source directory redirected the rename to",
456+
() => {
457+
fs.mkdirSync(path.join(dir, "sub"));
458+
fs.writeFileSync(path.join(dir, "sub", "x.conf"), "inside");
459+
fs.writeFileSync(path.join(outside, "x.conf"), "secret");
460+
swapSourceOnRename();
461+
expect(() =>
462+
renameIntoDirectory(
463+
path.join(dir, "sub", "x.conf"),
464+
path.join(dir, "moved.conf"),
465+
),
466+
).toThrow("Access denied");
467+
expect(fs.readFileSync(path.join(outside, "x.conf"), "utf-8")).toBe(
468+
"secret",
469+
);
470+
expect(fs.readdirSync(dir).sort()).toEqual(["sub", "sub-real"]);
471+
expect(process.cwd()).toBe(cwd);
472+
},
473+
);
474+
475+
it.skipIf(!symlinks)(
476+
"puts back an outside directory, which cannot be linked, by renaming it",
477+
() => {
478+
fs.mkdirSync(path.join(dir, "sub"));
479+
fs.mkdirSync(path.join(dir, "sub", "d"));
480+
fs.mkdirSync(path.join(outside, "d"));
481+
fs.writeFileSync(path.join(outside, "d", "f"), "secret");
482+
swapSourceOnRename();
483+
expect(() =>
484+
renameIntoDirectory(path.join(dir, "sub", "d"), path.join(dir, "e")),
485+
).toThrow("Access denied");
486+
expect(fs.readFileSync(path.join(outside, "d", "f"), "utf-8")).toBe(
487+
"secret",
488+
);
489+
expect(fs.readdirSync(dir).sort()).toEqual(["sub", "sub-real"]);
490+
},
491+
);
492+
493+
it.skipIf(!symlinks)(
494+
"leaves an outside directory staged when the source name is taken again",
495+
() => {
496+
fs.mkdirSync(path.join(dir, "sub"));
497+
fs.mkdirSync(path.join(dir, "sub", "d"));
498+
fs.mkdirSync(path.join(outside, "d"));
499+
swapSourceOnRename(() => fs.mkdirSync(path.join(outside, "d")));
500+
expect(() =>
501+
renameIntoDirectory(path.join(dir, "sub", "d"), path.join(dir, "e")),
502+
).toThrow("Access denied");
503+
const names = fs.readdirSync(dir);
504+
expect(names).not.toContain("e");
505+
expect(names.filter((n) => n.endsWith(".moving"))).toHaveLength(1);
506+
},
507+
);
508+
509+
it.skipIf(!symlinks)(
510+
"leaves the staged entry when the source name cannot be checked",
511+
() => {
512+
fs.mkdirSync(path.join(dir, "sub"));
513+
fs.mkdirSync(path.join(dir, "sub", "d"));
514+
fs.mkdirSync(path.join(outside, "d"));
515+
const realLstat = fs.lstatSync;
516+
swapSourceOnRename(() => {
517+
vi.spyOn(fs, "lstatSync").mockImplementation(((
518+
p: fs.PathLike,
519+
options?: fs.StatSyncOptions,
520+
) => {
521+
if (p === path.join(dir, "sub", "d")) {
522+
throw errno("EACCES");
523+
}
524+
return realLstat(p, options);
525+
}) as never);
526+
});
527+
expect(() =>
528+
renameIntoDirectory(path.join(dir, "sub", "d"), path.join(dir, "e")),
529+
).toThrow("Access denied");
530+
expect(fs.readdirSync(dir)).not.toContain("e");
531+
},
532+
);
533+
534+
it("names the source when it is missing", () => {
535+
expect(() =>
536+
renameIntoDirectory(path.join(dir, "gone.txt"), path.join(dir, "b.txt")),
537+
).toThrow(path.join(dir, "gone.txt"));
538+
});
434539
});
435540

436541
// Each test here runs on one device: the helper is what moveFile falls back

‎src/filesystem/pinned-directory.ts‎

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
// cannot move the operation. A read opens the file that way and then reads
1616
// only through the descriptor, which stays bound to the file it opened, so
1717
// the read is anchored however long it takes. Every check happens before a
18-
// byte is written or read, so nothing ever needs rolling back, except in the
19-
// cross-device move, which copies and so removes its copy if a later step
20-
// fails.
18+
// byte is written or read, so nothing ever needs rolling back, except in two
19+
// moves: the cross-device move copies, and so removes its copy if a later
20+
// step fails; the same-device move can anchor only its destination (rename
21+
// takes two paths), so it pins its source by identity and puts back an entry
22+
// that is not the one it checked.
2123
//
2224
// On Windows the working directory is a string and relative paths resolve
2325
// against it, so reading it back proves nothing; there the leaf is lstat'd
2426
// relative to the anchor and must be the file the descriptor opened, which
2527
// narrows the race without closing it.
2628

29+
import { randomBytes } from "crypto";
2730
import fs from "fs";
2831
import path from "path";
2932

@@ -349,17 +352,68 @@ export async function appendFileInDirectory(
349352
}
350353
}
351354

355+
/** A name in the destination directory to stage a rename under. */
356+
function stagingName(destinationPath: string): string {
357+
return `.${path.basename(destinationPath)}.${randomBytes(8).toString("hex")}.moving`;
358+
}
359+
360+
/**
361+
* Put back an entry a rename staged at `staged` (relative to the anchored
362+
* destination directory) that turned out not to be the source: at
363+
* `sourcePath`, where it came from, without overwriting anything there. A
364+
* hard link refuses an existing name; a directory, which cannot be linked,
365+
* is renamed back only while nothing is at `sourcePath`. If neither is
366+
* possible it stays at its staged name, which is never the destination.
367+
*/
368+
function returnStaged(staged: string, sourcePath: string): void {
369+
try {
370+
fs.linkSync(staged, sourcePath);
371+
fs.unlinkSync(staged);
372+
return;
373+
} catch {
374+
// Not linkable (a directory), or something is at sourcePath now.
375+
}
376+
try {
377+
fs.lstatSync(sourcePath);
378+
} catch (error) {
379+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
380+
fs.renameSync(staged, sourcePath);
381+
}
382+
}
383+
}
384+
352385
/**
353-
* Rename `sourcePath` (absolute, outside this fix's scope) to the final
354-
* component of `destinationPath`, relative to its anchored directory.
386+
* Rename `sourcePath` to the final component of `destinationPath`, both
387+
* anchored. rename() takes two paths and Node has no renameat(), so only one
388+
* side can be resolved relative to an anchor: the destination is, and the
389+
* source is pinned by identity instead. Its entry is read relative to its own
390+
* anchored directory first, the rename then stages whatever the kernel
391+
* resolved `sourcePath` to under a fresh name in the destination directory,
392+
* and only an entry that is the one read moves on to the destination name.
393+
* Anything else (a directory on the source path was swapped in between) is
394+
* put back and the move refused, so it is never at the destination name.
355395
*/
356396
export function renameIntoDirectory(
357397
sourcePath: string,
358398
destinationPath: string,
359399
): void {
360-
inDirectory(path.dirname(destinationPath), () =>
361-
fs.renameSync(sourcePath, path.basename(destinationPath)),
362-
);
400+
const name = path.basename(sourcePath);
401+
const expected = inDirectory(path.dirname(sourcePath), () => {
402+
try {
403+
return fs.lstatSync(name, { bigint: true });
404+
} catch (error) {
405+
throw namingPath(error, name, sourcePath);
406+
}
407+
});
408+
inDirectory(path.dirname(destinationPath), () => {
409+
const staged = stagingName(destinationPath);
410+
fs.renameSync(sourcePath, staged);
411+
if (!isSameEntry(fs.lstatSync(staged, { bigint: true }), expected)) {
412+
returnStaged(staged, sourcePath);
413+
throw directoryChangedError(path.dirname(sourcePath));
414+
}
415+
fs.renameSync(staged, path.basename(destinationPath));
416+
});
363417
}
364418

365419
// The cross-device move below (#5188). rename() cannot cross a device

0 commit comments

Comments
 (0)