Skip to content

Commit 259f65c

Browse files
fix(core): a project path typed in another letter case stays inside the project (#4514)
* fix(core): a project path typed in another letter case stays inside the project * fix(core): project paths still resolve on volumes that refuse the native realpath
1 parent 2393749 commit 259f65c

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { mkdtempSync, rmSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { afterEach, expect, it, vi } from "vitest";
5+
6+
vi.mock("node:fs", async (importOriginal) => {
7+
const actual = await importOriginal<typeof import("node:fs")>();
8+
const realpathSync = Object.assign((path: string) => actual.realpathSync(path), {
9+
native: () => {
10+
throw Object.assign(new Error("EISDIR: illegal operation on a directory"), {
11+
code: "EISDIR",
12+
});
13+
},
14+
});
15+
return { ...actual, realpathSync, default: { ...actual, realpathSync } };
16+
});
17+
18+
const { isSafePath } = await import("./safePath.js");
19+
const dirs: string[] = [];
20+
afterEach(() => {
21+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
22+
});
23+
24+
it("still resolves paths on a volume whose native realpath refuses with EISDIR", () => {
25+
const base = mkdtempSync(join(tmpdir(), "safepath-eisdir-"));
26+
dirs.push(base);
27+
expect(isSafePath(base, join(base, "file.txt"))).toBe(true);
28+
expect(isSafePath(base, join(tmpdir(), "outside.txt"))).toBe(false);
29+
});

‎packages/core/src/safePath.test.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { describe, it, expect, afterEach } from "vitest";
2-
import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync } from "node:fs";
2+
import { existsSync, mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
4-
import { join } from "node:path";
4+
import { basename, dirname, join } from "node:path";
55
import { isSafePath, resolveWithinProject } from "./safePath.js";
66

7+
const recased = (path: string) => join(dirname(path), basename(path).toUpperCase());
8+
9+
function caseInsensitive(): boolean {
10+
const dir = mkdtempSync(join(tmpdir(), "safepath-probe-"));
11+
try {
12+
return existsSync(recased(dir));
13+
} finally {
14+
rmSync(dir, { recursive: true, force: true });
15+
}
16+
}
17+
718
describe("isSafePath", () => {
819
const tmpDirs: string[] = [];
920

@@ -101,6 +112,14 @@ describe("isSafePath", () => {
101112
expect(isSafePath(baseLink, join(baseLink, "file.txt"))).toBe(true);
102113
});
103114

115+
it.skipIf(!caseInsensitive())("admits an in-base path typed in another letter case", () => {
116+
const base = tmpDir("safepath-case-");
117+
writeFileSync(join(base, "file.txt"), "x");
118+
expect(isSafePath(base, join(recased(base), "file.txt"))).toBe(true);
119+
expect(isSafePath(recased(base), join(base, "new.txt"))).toBe(true);
120+
expect(isSafePath(recased(base), join(tmpdir(), "outside.txt"))).toBe(false);
121+
});
122+
104123
it("fails closed when the base directory does not exist", () => {
105124
const base = join(tmpdir(), "safepath-does-not-exist-zzz", "nope");
106125
expect(isSafePath(base, join(base, "file.txt"))).toBe(false);

‎packages/core/src/safePath.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { resolve, sep, join, dirname, basename } from "node:path";
22
import { lstatSync, realpathSync } from "node:fs";
33

4+
function realpath(path: string): string {
5+
try {
6+
return realpathSync.native(path);
7+
} catch (error) {
8+
// Some Windows volumes (RAM disks) refuse the native call with EISDIR.
9+
if ((error as NodeJS.ErrnoException).code === "EISDIR") return realpathSync(path);
10+
throw error;
11+
}
12+
}
13+
414
// realpath also fails for dangling/cyclic symlinks. Existing entries must not
515
// become missing segments: writes could follow them outside the project.
616
function isMissingPath(path: string): boolean {
@@ -25,7 +35,7 @@ function isMissingPath(path: string): boolean {
2535
* symlink that lives *inside* `base` but points outside it (e.g.
2636
* `base/link -> /etc`). A downstream `readFileSync`/`writeFileSync`/`statSync`
2737
* then follows that link to a file outside `base`. To close this we canonicalize
28-
* both sides with `realpathSync` before comparing.
38+
* both sides with `realpathSync.native` (the on-disk letter case too) before comparing.
2939
*
3040
* The target may not exist yet (e.g. creating a new file), so we canonicalize the
3141
* deepest *existing* ancestor and re-attach the trailing not-yet-existing
@@ -42,7 +52,7 @@ function isMissingPath(path: string): boolean {
4252
export function isSafePath(base: string, resolved: string): boolean {
4353
let baseReal: string;
4454
try {
45-
baseReal = realpathSync(resolve(base));
55+
baseReal = realpath(resolve(base));
4656
} catch {
4757
// Base must exist and be resolvable; fail closed if not.
4858
return false;
@@ -55,7 +65,7 @@ export function isSafePath(base: string, resolved: string): boolean {
5565
for (;;) {
5666
let ancestorReal: string;
5767
try {
58-
ancestorReal = realpathSync(probe);
68+
ancestorReal = realpath(probe);
5969
} catch {
6070
if (!isMissingPath(probe)) return false;
6171
const parent = dirname(probe);

0 commit comments

Comments
 (0)