Files
reverse-engineering-anything/src/filesystem/callerPath.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

76 lines
3.2 KiB
TypeScript

import { posix, win32 } from "node:path";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
/**
* Whether a caller-selected path names one location without the working
* directory. Win32 calls `\tools\x.exe` absolute, but it is rooted on the
* current drive, and `C:tools` follows that drive's directory. Only a drive
* root (`C:\`) or a complete UNC or device root (`\\server\share\`,
* `\\?\C:\`) is independent; win32 parses `///tools`, `\\\tools` and a bare
* `\\server` with a single-separator root on the current drive.
*/
export const isQualifiedCallerPath = (
path: string,
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): boolean =>
platform === "win32"
? win32.isAbsolute(path) && win32.parse(path).root.length > 1
: posix.isAbsolute(path);
/** Append a relative child without collapsing POSIX symlink/.. traversal.
* Preserve relative parents too: configuration admission owns absolute-path validation.
* The platform defaults to the running host because most callers then open the
* joined path on this host; a caller describing another host's layout passes it.
*/
export const appendCallerPath = (
parent: string,
child: string,
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): string =>
platform === "win32"
? win32.join(parent, child)
: parent === ""
? child
: `${parent.endsWith("/") ? parent : `${parent}/`}${child}`;
/**
* Make a caller-selected local path absolute without changing which file it
* names. POSIX resolves `..` after following the preceding symlink, so lexical
* normalization of `link/../file` can select a different file. On POSIX, only
* interior `.` segments and repeated separators are removed, which never
* change the named file; every `..` is kept for the filesystem to resolve, and
* a trailing `/` or `/.` keeps its directory requirement as one trailing `/`.
* A relative `cwd` is made absolute the same way. Win32 resolves `..`
* lexically, so normalizing there is exact. Absolute POSIX paths never need
* the ambient cwd, which may already have been removed during cleanup.
* Without `platform` the running host's rules apply, as they do to its cwd.
*/
export const absoluteCallerPath = (
path: string,
{
cwd,
platform = HOST_PLATFORM.platform,
}: { readonly cwd?: string; readonly platform?: NodeJS.Platform } = {},
): string => {
if (platform === "win32") return win32.resolve(cwd ?? process.cwd(), path);
if (!posix.isAbsolute(path)) {
const selectedCwd = cwd ?? process.cwd();
// This process's own directory must be POSIX-absolute to anchor POSIX
// semantics; resolving a Windows directory against itself never ends.
if (cwd === undefined && !posix.isAbsolute(selectedCwd))
throw new TypeError(
`Cannot resolve ${path} with POSIX semantics from the non-POSIX working directory ${selectedCwd}`,
);
const base = posix.isAbsolute(selectedCwd)
? selectedCwd
: absoluteCallerPath(selectedCwd, { platform });
return absoluteCallerPath(`${base}/${path}`, { platform });
}
const segments = path
.split("/")
.filter((segment) => segment !== "" && segment !== ".");
const directory = /\/\.?$/u.test(path) && segments.length > 0;
return `/${segments.join("/")}${directory ? "/" : ""}`;
};