Files
reverse-engineering-anything/tests/boundary/filesystem/callerPath.test.ts
T
RRFZ 23adfc610f fix(process): require a drive or UNC root for selected Windows tools (#11786)
* fix(process): require a drive or UNC root for selected Windows tools

REA_ADB_PATH, REA_APKTOOL_COMMAND, REA_TSHARK_COMMAND and
REA_CAPINFOS_COMMAND must be absolute. On Windows the check used
win32.isAbsolute, which also accepts \tools\adb.exe: a path rooted on
the current drive. Such a setting names a different file depending on
REA's working drive, and the owned Windows launcher refuses it.

Add isQualifiedCallerPath to the caller-path owner, which on Windows
requires a drive root or a UNC or device root, and use it for all four
settings. POSIX keeps posix.isAbsolute.

* fix(filesystem): require a complete UNC root for a qualified Windows path

Two leading separators were treated as a UNC root, so ///tools/adb.exe,
\\\tools\adb.exe and a bare \\server passed, though win32 parses them
with a single-separator root on the current drive. Use win32's own root:
qualified only when the path is absolute and its root is longer than one
separator.

* test(process): use qualified paths for simulated Windows tools
2026-11-18 10:09:30 -05:00

127 lines
4.5 KiB
TypeScript

import { expect, it, vi } from "vitest";
import {
absoluteCallerPath,
isQualifiedCallerPath,
} from "../../../src/filesystem/callerPath.js";
it.each([
["win32", "C:\\tools\\adb.exe", true],
["win32", "c:/tools/adb.exe", true],
["win32", "\\\\server\\share\\adb.exe", true],
["win32", "//server/share/adb.exe", true],
["win32", "\\\\?\\C:\\tools\\adb.exe", true],
["win32", "//server//share/adb.exe", true],
["win32", "\\tools\\adb.exe", false],
// Repeated separators without a share still resolve on the current drive.
["win32", "///tools/adb.exe", false],
["win32", "\\\\\\tools\\adb.exe", false],
["win32", "\\\\server", false],
["win32", "/tools/adb.exe", false],
["win32", "C:tools\\adb.exe", false],
["win32", "tools\\adb.exe", false],
["linux", "/opt/tools/adb", true],
["linux", "tools/adb", false],
["linux", "C:\\tools\\adb", false],
] as const)("on %s treats %j as qualified: %s", (platform, path, qualified) => {
expect(isQualifiedCallerPath(path, platform)).toBe(qualified);
});
it("resolves absolute POSIX paths independently of an unavailable cwd", () => {
const cwd = vi.spyOn(process, "cwd").mockImplementation(() => {
throw new Error("working directory was removed");
});
try {
expect(
absoluteCallerPath("/cache//rea/kuna/.", { platform: "linux" }),
).toBe("/cache/rea/kuna/");
expect(
absoluteCallerPath("sample", { cwd: "/explicit", platform: "linux" }),
).toBe("/explicit/sample");
} finally {
cwd.mockRestore();
}
});
it("keeps POSIX parent components for the filesystem to resolve", () => {
const posix = { cwd: "/work", platform: "linux" } as const;
expect(absoluteCallerPath("link/../sample", posix)).toBe(
"/work/link/../sample",
);
expect(absoluteCallerPath("/abs/link/../sample", posix)).toBe(
"/abs/link/../sample",
);
expect(absoluteCallerPath("../sample", posix)).toBe("/work/../sample");
});
it("removes only POSIX spellings that cannot change the named file", () => {
const posix = { cwd: "/work/", platform: "darwin" } as const;
expect(absoluteCallerPath("./module.wasm", posix)).toBe("/work/module.wasm");
expect(absoluteCallerPath("a//./b", posix)).toBe("/work/a/b");
expect(absoluteCallerPath("/x/./y", posix)).toBe("/x/y");
expect(absoluteCallerPath("sample", { ...posix, cwd: "/" })).toBe("/sample");
expect(absoluteCallerPath("/", posix)).toBe("/");
// A trailing separator or `.` still requires a directory.
expect(absoluteCallerPath("sample/", posix)).toBe("/work/sample/");
expect(absoluteCallerPath("sample/.", posix)).toBe("/work/sample/");
expect(absoluteCallerPath("a//b//", posix)).toBe("/work/a/b/");
});
it("makes a relative POSIX cwd absolute before appending the path", () => {
const cwd = vi.spyOn(process, "cwd").mockReturnValue("/work");
try {
expect(
absoluteCallerPath("sample", { cwd: "fixtures/../x", platform: "linux" }),
).toBe("/work/fixtures/../x/sample");
} finally {
cwd.mockRestore();
}
});
it("uses lexical resolution on Windows, where it is exact", () => {
const win32 = { cwd: "C:\\work", platform: "win32" } as const;
expect(absoluteCallerPath("link\\..\\sample", win32)).toBe(
"C:\\work\\sample",
);
expect(absoluteCallerPath("D:\\x\\..\\y", win32)).toBe("D:\\y");
});
it("normalizes separator spellings of absolute Windows paths (#4707)", () => {
const win32 = { cwd: "C:\\work", platform: "win32" } as const;
for (const spelling of [
"C:\\Users\\u\\h.exe",
"C:/Users/u/h.exe",
"C:\\\\Users\\\\u\\\\h.exe",
"C://Users//u//h.exe",
])
expect(absoluteCallerPath(spelling, win32)).toBe("C:\\Users\\u\\h.exe");
});
it("keeps unselected Windows share paths lexical", () => {
expect(
absoluteCallerPath("\\\\unselected-server\\share\\Source.JS", {
platform: "win32",
}),
).toBe("\\\\unselected-server\\share\\Source.JS");
expect(
absoluteCallerPath("C:\\CaseSensitive\\Different.js", {
platform: "win32",
}),
).toBe("C:\\CaseSensitive\\Different.js");
});
it("refuses POSIX resolution from a Windows working directory instead of recursing", () => {
const cwd = vi.spyOn(process, "cwd").mockReturnValue("C:\\work");
try {
// A POSIX PATH search on a Windows host reached this with a relative entry.
expect(() => absoluteCallerPath("bin/uv", { platform: "linux" })).toThrow(
TypeError,
);
expect(() =>
absoluteCallerPath("bin/uv", { cwd: "relative", platform: "linux" }),
).toThrow(TypeError);
} finally {
cwd.mockRestore();
}
});