Skip to content

Commit ee969e7

Browse files
cliffhallclaude
andcommitted
fix(filesystem): fold the macOS Data-volume spelling in read-only checks
realpath keeps a /System/Volumes/Data/... spelling it is given, so with an allowed directory covering it a write or an ancestor move through that spelling slipped past a read-only marking named by the firmlinked one (or the reverse). Both operands of both containment checks now drop the prefix on darwin, as pinned-directory.ts does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 8a3fe59 commit ee969e7

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎src/filesystem/__tests__/read-only-dirs.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,54 @@ describe("validatePath's access mode", () => {
338338
});
339339
});
340340

341+
describe("the macOS Data-volume spelling", () => {
342+
const vault = "/Users/me/ref";
343+
const aliased = "/System/Volumes/Data/Users/me/ref";
344+
345+
it.each([
346+
["the aliased path, firmlinked marking", `${aliased}/notes.txt`, vault],
347+
["the firmlinked path, aliased marking", `${vault}/notes.txt`, aliased],
348+
])("refuses a write through %s on darwin", (_label, target, marking) => {
349+
expect(() => assertWritable(target, [marking], "write", "darwin")).toThrow(
350+
"Access denied - read-only directory",
351+
);
352+
});
353+
354+
it.each([
355+
["the aliased parent", "/System/Volumes/Data/Users/me", vault],
356+
["the firmlinked parent", "/Users/me", aliased],
357+
])(
358+
"refuses moving %s of a read-only directory on darwin",
359+
(_label, source, marking) => {
360+
expect(() => assertWritable(source, [marking], "move", "darwin")).toThrow(
361+
`contains ${marking}`,
362+
);
363+
},
364+
);
365+
366+
it("keeps the two spellings apart off darwin", () => {
367+
expect(() =>
368+
assertWritable(`${aliased}/notes.txt`, [vault], "move", "linux"),
369+
).not.toThrow();
370+
});
371+
372+
it.runIf(process.platform === "darwin")(
373+
"refuses a write through the Data-volume spelling over the protocol",
374+
async () => {
375+
const client = await connect(
376+
"legacy",
377+
[writable, `/System/Volumes/Data${protectedDir}`],
378+
[protectedDir],
379+
);
380+
await expectRefused(client, "write_file", {
381+
path: `/System/Volumes/Data${protectedDir}/keep.txt`,
382+
content: "changed",
383+
});
384+
expect(await keepContent()).toBe("original\n");
385+
},
386+
);
387+
});
388+
341389
describe("resolveReadOnlyDirectories", () => {
342390
it("keeps both spellings of a symlinked directory", async () => {
343391
const link = path.join(writable, "alias");

‎src/filesystem/read-only-directories.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,34 @@ export async function resolveReadOnlyDirectories(
6565
return [...new Set(resolved.flat())];
6666
}
6767

68+
// The macOS Data volume firmlinks /Users, /private and the rest into the
69+
// root, and realpath keeps a /System/Volumes/Data/... spelling it is given, so
70+
// one directory has two resolved spellings there. Both sides of every
71+
// containment check below drop that prefix on darwin, so neither spelling
72+
// reaches a read-only directory the other one names. pinned-directory.ts
73+
// folds the same prefix for the same reason.
74+
const DATA_VOLUME = "/System/Volumes/Data";
75+
76+
/** `p` normalized, without the macOS Data-volume prefix on darwin. */
77+
function comparable(p: string, platform: string): string {
78+
const normalized = normalizePathFormat(p);
79+
return platform === "darwin" && normalized.startsWith(`${DATA_VOLUME}/`)
80+
? normalized.slice(DATA_VOLUME.length)
81+
: normalized;
82+
}
83+
6884
/**
6985
* The read-only directory that `resolvedPath` (a path validatePath resolved)
7086
* falls in, or undefined when it is in none.
7187
*/
7288
export function findReadOnlyDirectory(
7389
resolvedPath: string,
7490
readOnlyDirectories: readonly string[],
91+
platform: string = process.platform,
7592
): string | undefined {
76-
const normalized = normalizePathFormat(resolvedPath);
93+
const target = comparable(resolvedPath, platform);
7794
return readOnlyDirectories.find((dir) =>
78-
isPathWithinAllowedDirectories(normalized, [dir]),
95+
isPathWithinAllowedDirectories(target, [comparable(dir, platform)]),
7996
);
8097
}
8198

@@ -89,17 +106,22 @@ export function assertWritable(
89106
resolvedPath: string,
90107
readOnlyDirectories: readonly string[],
91108
access: "write" | "move" = "write",
109+
platform: string = process.platform,
92110
): void {
93-
const directory = findReadOnlyDirectory(resolvedPath, readOnlyDirectories);
111+
const directory = findReadOnlyDirectory(
112+
resolvedPath,
113+
readOnlyDirectories,
114+
platform,
115+
);
94116
if (directory !== undefined) {
95117
throw new Error(
96118
`Access denied - read-only directory: ${resolvedPath} is in ${directory}, which the server was started with --read-only-dir`,
97119
);
98120
}
99121
if (access === "move") {
100-
const normalized = normalizePathFormat(resolvedPath);
122+
const target = comparable(resolvedPath, platform);
101123
const contained = readOnlyDirectories.find((dir) =>
102-
isPathWithinAllowedDirectories(dir, [normalized]),
124+
isPathWithinAllowedDirectories(comparable(dir, platform), [target]),
103125
);
104126
if (contained !== undefined) {
105127
throw new Error(

0 commit comments

Comments
 (0)