Skip to content

Commit 505476d

Browse files
cliffhallclaude
andcommitted
fix(filesystem): address review round 4 for GHSA-p9cv-8g75-4xcg
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 3d22dc1 commit 505476d

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎src/filesystem/__tests__/path-validation.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { describe, it, expect, beforeEach, afterEach } from "vitest";
22
import * as path from "path";
33
import * as fs from "fs/promises";
44
import * as os from "os";
5-
import { isPathWithinAllowedDirectories } from "../path-validation.js";
5+
import {
6+
isPathWithinAllowedDirectories,
7+
isWindowsUncRoot,
8+
} from "../path-validation.js";
69

710
/**
811
* Check if the current environment supports symlink creation
@@ -1381,3 +1384,25 @@ describe("Path Validation", () => {
13811384
});
13821385
});
13831386
});
1387+
1388+
describe("isWindowsUncRoot", () => {
1389+
it.each([
1390+
"\\\\server\\share",
1391+
"\\\\server\\share\\",
1392+
"\\\\?\\UNC\\server\\share",
1393+
"\\\\.\\UNC\\server\\share\\",
1394+
])("is true for the share root %s", (root) => {
1395+
expect(isWindowsUncRoot(root)).toBe(true);
1396+
});
1397+
1398+
it.each([
1399+
"\\\\server\\share\\f.txt",
1400+
"\\\\server",
1401+
"\\\\?\\UNC\\server",
1402+
"\\\\?\\C:\\",
1403+
"C:\\",
1404+
"/srv/share",
1405+
])("is false for %s", (notRoot) => {
1406+
expect(isWindowsUncRoot(notRoot)).toBe(false);
1407+
});
1408+
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,4 +1382,16 @@ describe("the anchored reads", () => {
13821382
expect(statInDirectory(root).isDirectory()).toBe(true);
13831383
expect(process.cwd()).toBe(cwd);
13841384
});
1385+
1386+
it("stats a UNC share root directly on Windows, as it does any root", () => {
1387+
Object.defineProperty(process, "platform", { value: "win32" });
1388+
const stats = fs.lstatSync(dir);
1389+
const lstat = vi.spyOn(fs, "lstatSync").mockReturnValue(stats);
1390+
const chdir = vi.spyOn(process, "chdir");
1391+
for (const root of ["\\\\server\\share", "\\\\?\\UNC\\server\\share\\"]) {
1392+
expect(statInDirectory(root)).toBe(stats);
1393+
expect(lstat).toHaveBeenLastCalledWith(root);
1394+
}
1395+
expect(chdir).not.toHaveBeenCalled();
1396+
});
13851397
});

‎src/filesystem/path-validation.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ export function namesAlternateDataStream(normalizedPath: string): boolean {
4343
const WINDOWS_UNC_ROOT =
4444
/^(?:\\\\[?.]\\UNC\\|\\\\(?![?.]\\))[^\\/]+[\\/][^\\/]+/i;
4545

46+
/**
47+
* True when `windowsPath` is a UNC root itself (`\\server\share`,
48+
* `\\?\UNC\server\share`, with or without a trailing separator): a root,
49+
* like `C:\`, with no parent directory above it. Node's `path.win32` takes
50+
* the share for a name inside the server, or, for the `\\?\UNC\` form,
51+
* the server for a directory.
52+
*/
53+
export function isWindowsUncRoot(windowsPath: string): boolean {
54+
const match = WINDOWS_UNC_ROOT.exec(windowsPath);
55+
return match !== null && /^[\\/]?$/.test(windowsPath.slice(match[0].length));
56+
}
57+
4658
const WINDOWS_DEVICE_NAME =
4759
/^(?:CON|PRN|AUX|NUL|COM[1-9¹²³]|LPT[1-9¹²³]) *(?:\..*)?$/is;
4860

‎src/filesystem/pinned-directory.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import { randomBytes } from "crypto";
3131
import fs from "fs";
3232
import path from "path";
33+
import { isWindowsUncRoot } from "./path-validation.js";
3334

3435
// Descriptor I/O, looked up on fs at call time so tests can stand in for it.
3536
function ftruncate(fd: number): Promise<void> {
@@ -912,7 +913,8 @@ export async function readFileInDirectory(filePath: string): Promise<Buffer> {
912913
*/
913914
export function statInDirectory(filePath: string): fs.Stats {
914915
const name = path.basename(filePath);
915-
if (name === "") {
916+
// A root (`/`, `C:\`, a UNC share) has no directory above it to anchor at.
917+
if (name === "" || (onWindows() && isWindowsUncRoot(filePath))) {
916918
return fs.lstatSync(filePath);
917919
}
918920
return inDirectory(path.dirname(filePath), () => {

0 commit comments

Comments
 (0)