Skip to content

Commit 2a10498

Browse files
Yigtwxxclaude
andcommitted
fix(root): reject drive-relative input before absolute reads resolve it
readAbsolute() and reader() enter through readPathInRoot(), which resolved the raw input against the root before any validation ran. On Windows that consumed a drive-relative prefix, so C:secret.txt reached readFileInRoot() as secret.txt and aliased the plain key the rest of the branch already rejected. Validate the raw input first. The check is the drive-relative one only, so drive-absolute paths inside the root keep working. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ah1kwRo32A6sGU1tbMFEFk
1 parent 7024025 commit 2a10498

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎src/root-context.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export function assertValidRootRelativePath(relativePath: string): void {
2727
}
2828
}
2929

30+
export function assertValidRootPathInput(filePath: string): void {
31+
assertNoNulPathInput(filePath, "file path contains a NUL byte");
32+
if (isDriveRelativePath(filePath)) {
33+
throw new FsSafeError("invalid-path", "file path must not start with a drive letter");
34+
}
35+
}
36+
3037
let cachedHomePath: { raw: string; real: string } | undefined;
3138

3239
export async function expandRelativePathWithHome(relativePath: string): Promise<string> {

‎src/root-impl.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { readOpenedFileSafely, type ReadResult } from "./read-opened-file.js";
3636
import { pathStatFromStats } from "./path-stat.js";
3737
import { resolveRootPath } from "./root-path.js";
3838
import {
39+
assertValidRootPathInput,
3940
assertValidRootRelativePath,
4041
ensureTrailingSep,
4142
expandRelativePathWithHome,
@@ -683,6 +684,7 @@ async function readPathInRoot(
683684
symlinks?: SymlinkPolicy;
684685
},
685686
): Promise<ReadResult> {
687+
assertValidRootPathInput(params.filePath);
686688
const rootDir = root.rootDir;
687689
const candidatePath = path.isAbsolute(params.filePath)
688690
? path.resolve(params.filePath)

‎test/windows-path.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ describe("drive-relative relative paths", () => {
8585
["root.remove", () => root.remove(key)],
8686
["root.write", () => root.write(key, "aliased")],
8787
["root.move", () => root.move(key, "moved.txt")],
88+
["root.readAbsolute", () => root.readAbsolute(key)],
89+
["root.reader", () => root.reader()(key)],
8890
["store.readText", () => store.readText(key)],
8991
["store.open", () => store.open(key)],
9092
["store.exists", () => store.exists(key)],
@@ -107,6 +109,12 @@ describe("drive-relative relative paths", () => {
107109
await root.write("note.txt", "kept");
108110

109111
await expect(root.readText(path.join(rootDir, "note.txt"))).resolves.toBe("kept");
112+
await expect(
113+
root.readAbsolute(path.join(rootDir, "note.txt")),
114+
).resolves.toMatchObject({ buffer: Buffer.from("kept") });
115+
await expect(root.reader()(path.join(rootDir, "note.txt"))).resolves.toEqual(
116+
Buffer.from("kept"),
117+
);
110118
} finally {
111119
await rm(rootDir, { force: true, recursive: true });
112120
}

0 commit comments

Comments
 (0)