Skip to content

Commit c83a1dd

Browse files
cliffhallclaude
andcommitted
fix(filesystem): address review findings for GHSA-p9cv-8g75-4xcg
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 036850a commit c83a1dd

2 files changed

Lines changed: 293 additions & 96 deletions

File tree

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

Lines changed: 194 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -432,104 +432,240 @@ describe("renameIntoDirectory", () => {
432432
},
433433
);
434434

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) => {
435+
const movingNames = (directory: string) =>
436+
fs.readdirSync(directory).filter((n) => n.endsWith(".moving"));
437+
438+
// Stand in for renameSync, calling `around(call, from, to, real)` on each
439+
// call; the first call is step 1 (inside the source's directory), the
440+
// second step 2 (across), the third step 3 (to the destination's name).
441+
const onRename = (
442+
around: (
443+
call: number,
444+
from: fs.PathLike,
445+
to: fs.PathLike,
446+
real: typeof fs.renameSync,
447+
) => void,
448+
) => {
439449
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"));
450+
let call = 0;
451+
vi.spyOn(fs, "renameSync").mockImplementation((from, to) =>
452+
around(++call, from, to, realRename),
453+
);
454+
};
455+
456+
// The source's directory swapped for a link to an outside one between
457+
// step 1 and step 2: step 2's path now leads outside, where nothing has
458+
// the fresh name, so nothing comes in from there.
459+
const swapSourceBeforeStep2 = (before?: (held: string) => void) =>
460+
onRename((call, from, to, real) => {
461+
if (call === 2) {
462+
real(path.join(dir, "sub"), path.join(dir, "sub-real"));
445463
fs.symlinkSync(outside, path.join(dir, "sub"));
446-
realRename(from, to);
447-
after?.();
448-
return;
464+
before?.(path.basename(String(from)));
449465
}
450-
realRename(from, to);
466+
real(from, to);
451467
});
452-
};
453468

454469
it.skipIf(!symlinks)(
455-
"puts back an outside file a swapped source directory redirected the rename to",
470+
"never brings in an outside entry when the source directory is swapped",
456471
() => {
457472
fs.mkdirSync(path.join(dir, "sub"));
458473
fs.writeFileSync(path.join(dir, "sub", "x.conf"), "inside");
459474
fs.writeFileSync(path.join(outside, "x.conf"), "secret");
460-
swapSourceOnRename();
475+
swapSourceBeforeStep2();
461476
expect(() =>
462477
renameIntoDirectory(
463478
path.join(dir, "sub", "x.conf"),
464479
path.join(dir, "moved.conf"),
465480
),
466481
).toThrow("Access denied");
467-
expect(fs.readFileSync(path.join(outside, "x.conf"), "utf-8")).toBe(
468-
"secret",
469-
);
482+
// Nothing came in, and nothing left the directory that was moved away:
483+
// the source stays there under its fresh name, since the swapped path
484+
// is never entered to put it back.
485+
expect(fs.readdirSync(outside)).toEqual(["x.conf"]);
470486
expect(fs.readdirSync(dir).sort()).toEqual(["sub", "sub-real"]);
487+
const [held] = movingNames(path.join(dir, "sub-real"));
488+
expect(fs.readFileSync(path.join(dir, "sub-real", held), "utf-8")).toBe(
489+
"inside",
490+
);
471491
expect(process.cwd()).toBe(cwd);
472492
},
473493
);
474494

495+
it("puts back a directory source, which cannot be linked, by renaming it", () => {
496+
fs.mkdirSync(path.join(dir, "d"));
497+
fs.mkdirSync(path.join(dir, "sub"));
498+
onRename((call, from, to, real) => {
499+
if (call === 2) {
500+
throw errno("EXDEV");
501+
}
502+
real(from, to);
503+
});
504+
expect(() =>
505+
renameIntoDirectory(path.join(dir, "d"), path.join(dir, "sub", "e")),
506+
).toThrow(expect.objectContaining({ code: "EXDEV" }));
507+
expect(fs.readdirSync(dir).sort()).toEqual(["d", "sub"]);
508+
});
509+
475510
it.skipIf(!symlinks)(
476-
"puts back an outside directory, which cannot be linked, by renaming it",
511+
"sends back an entry planted outside under the fresh name",
477512
() => {
478513
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();
514+
fs.writeFileSync(path.join(dir, "sub", "x.conf"), "inside");
515+
let planted = "";
516+
swapSourceBeforeStep2((held) => {
517+
planted = held;
518+
fs.writeFileSync(path.join(outside, held), "secret");
519+
});
483520
expect(() =>
484-
renameIntoDirectory(path.join(dir, "sub", "d"), path.join(dir, "e")),
521+
renameIntoDirectory(
522+
path.join(dir, "sub", "x.conf"),
523+
path.join(dir, "moved.conf"),
524+
),
485525
).toThrow("Access denied");
486-
expect(fs.readFileSync(path.join(outside, "d", "f"), "utf-8")).toBe(
526+
expect(fs.readFileSync(path.join(outside, planted), "utf-8")).toBe(
487527
"secret",
488528
);
489-
expect(fs.readdirSync(dir).sort()).toEqual(["sub", "sub-real"]);
529+
expect(movingNames(dir)).toEqual([]);
530+
expect(movingNames(path.join(dir, "sub-real"))).toHaveLength(1);
490531
},
491532
);
492533

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-
);
534+
it("puts the source back, keeping the error's code, when step 2 fails", () => {
535+
fs.mkdirSync(path.join(dir, "sub"));
536+
fs.writeFileSync(path.join(dir, "a.txt"), "a");
537+
onRename((call, from, to, real) => {
538+
if (call === 2) {
539+
throw errno("EXDEV");
540+
}
541+
real(from, to);
542+
});
543+
expect(() =>
544+
renameIntoDirectory(
545+
path.join(dir, "a.txt"),
546+
path.join(dir, "sub", "b.txt"),
547+
),
548+
).toThrow(expect.objectContaining({ code: "EXDEV" }));
549+
expect(fs.readFileSync(path.join(dir, "a.txt"), "utf-8")).toBe("a");
550+
expect(movingNames(dir)).toEqual([]);
551+
});
508552

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(() => {
553+
it("puts the source back when the destination's name cannot be taken", () => {
554+
fs.mkdirSync(path.join(dir, "sub"));
555+
fs.writeFileSync(path.join(dir, "a.txt"), "a");
556+
onRename((call, from, to, real) => {
557+
if (call === 3) {
558+
throw errno("EISDIR");
559+
}
560+
real(from, to);
561+
});
562+
expect(() =>
563+
renameIntoDirectory(
564+
path.join(dir, "a.txt"),
565+
path.join(dir, "sub", "b.txt"),
566+
),
567+
).toThrow(expect.objectContaining({ code: "EISDIR" }));
568+
expect(fs.readFileSync(path.join(dir, "a.txt"), "utf-8")).toBe("a");
569+
expect(movingNames(dir)).toEqual([]);
570+
expect(fs.readdirSync(path.join(dir, "sub"))).toEqual([]);
571+
});
572+
573+
it("says where the source was left when its name was taken meanwhile", () => {
574+
fs.mkdirSync(path.join(dir, "sub"));
575+
fs.writeFileSync(path.join(dir, "a.txt"), "a");
576+
onRename((call, from, to, real) => {
577+
if (call === 3) {
578+
fs.writeFileSync(path.join(dir, "a.txt"), "someone else's");
579+
throw errno("EISDIR");
580+
}
581+
real(from, to);
582+
});
583+
let error: unknown;
584+
try {
585+
renameIntoDirectory(
586+
path.join(dir, "a.txt"),
587+
path.join(dir, "sub", "b.txt"),
588+
);
589+
} catch (caught) {
590+
error = caught;
591+
}
592+
const [left] = movingNames(path.join(dir, "sub"));
593+
expect(error).toMatchObject({
594+
code: "EISDIR",
595+
message: expect.stringContaining(
596+
`was left as ${left} in ${path.join(dir, "sub")}`,
597+
),
598+
});
599+
expect(fs.readFileSync(path.join(dir, "sub", left), "utf-8")).toBe("a");
600+
});
601+
602+
it("says where a directory source was left when its name was taken", () => {
603+
fs.mkdirSync(path.join(dir, "d"));
604+
fs.mkdirSync(path.join(dir, "sub"));
605+
onRename((call, from, to, real) => {
606+
if (call === 2) {
607+
fs.writeFileSync(path.join(dir, "d"), "someone else's");
608+
throw errno("EIO");
609+
}
610+
real(from, to);
611+
});
612+
expect(() =>
613+
renameIntoDirectory(path.join(dir, "d"), path.join(dir, "sub", "e")),
614+
).toThrow(/was left as \.mcp-[0-9a-f]{16}\.moving in /);
615+
expect(movingNames(dir)).toHaveLength(1);
616+
});
617+
618+
it("leaves the source staged when its directory cannot be checked", () => {
619+
fs.mkdirSync(path.join(dir, "d"));
620+
fs.mkdirSync(path.join(dir, "sub"));
621+
const realLstat = fs.lstatSync;
622+
onRename((call, from, to, real) => {
623+
if (call === 2) {
517624
vi.spyOn(fs, "lstatSync").mockImplementation(((
518625
p: fs.PathLike,
519626
options?: fs.StatSyncOptions,
520627
) => {
521-
if (p === path.join(dir, "sub", "d")) {
628+
if (p === "d") {
522629
throw errno("EACCES");
523630
}
524631
return realLstat(p, options);
525632
}) 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-
);
633+
throw errno("EIO");
634+
}
635+
real(from, to);
636+
});
637+
expect(() =>
638+
renameIntoDirectory(path.join(dir, "d"), path.join(dir, "sub", "e")),
639+
).toThrow("was left as");
640+
expect(movingNames(dir)).toHaveLength(1);
641+
});
642+
643+
it("reports the source left when its directory can no longer be entered", () => {
644+
fs.mkdirSync(path.join(dir, "sub"));
645+
fs.mkdirSync(path.join(dir, "src"));
646+
fs.writeFileSync(path.join(dir, "src", "a.txt"), "a");
647+
onRename((call, from, to, real) => {
648+
if (call === 2) {
649+
fs.renameSync(path.join(dir, "src"), path.join(dir, "src-gone"));
650+
throw errno("EIO");
651+
}
652+
real(from, to);
653+
});
654+
expect(() =>
655+
renameIntoDirectory(
656+
path.join(dir, "src", "a.txt"),
657+
path.join(dir, "sub", "b.txt"),
658+
),
659+
).toThrow("could not be put back");
660+
});
661+
662+
it("moves to a name as long as the filesystem allows", () => {
663+
fs.mkdirSync(path.join(dir, "sub"));
664+
fs.writeFileSync(path.join(dir, "a.txt"), "a");
665+
const long = `${"n".repeat(251)}.txt`;
666+
renameIntoDirectory(path.join(dir, "a.txt"), path.join(dir, "sub", long));
667+
expect(fs.readFileSync(path.join(dir, "sub", long), "utf-8")).toBe("a");
668+
});
533669

534670
it("names the source when it is missing", () => {
535671
expect(() =>

0 commit comments

Comments
 (0)