mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
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
This commit is contained in:
11 files changed
+78
-22
No files matched your search
@@ -45,6 +45,10 @@ describe("resolveAdbBinary", () => {
|
||||
await expect(
|
||||
resolveAdbBinary({ [ADB_BINARY_ENV]: "tools/adb" }, "linux"),
|
||||
).rejects.toThrow(/must be an absolute path/u);
|
||||
// Rooted on the current drive, so it names a different file per drive.
|
||||
await expect(
|
||||
resolveAdbBinary({ [ADB_BINARY_ENV]: "\\tools\\adb.exe" }, "win32"),
|
||||
).rejects.toThrow(/must be an absolute path/u);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
cancelledBy,
|
||||
carriedReason,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { isAbsolute, win32 } from "node:path";
|
||||
import { isQualifiedCallerPath } from "../filesystem/callerPath.js";
|
||||
import { findWindowsExecutable } from "../process/WindowsExecutable.js";
|
||||
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
||||
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
||||
@@ -65,11 +65,7 @@ export const resolveAdbBinary = async (
|
||||
pathSource: "path",
|
||||
environment: selectedEnvironment,
|
||||
};
|
||||
if (
|
||||
!(platform === "win32"
|
||||
? win32.isAbsolute(configured)
|
||||
: isAbsolute(configured))
|
||||
)
|
||||
if (!isQualifiedCallerPath(configured, platform))
|
||||
throw new AdbConfigurationFailure(
|
||||
"not_absolute",
|
||||
`${ADB_BINARY_ENV} must be an absolute path to the adb binary`,
|
||||
|
||||
@@ -2727,7 +2727,8 @@ it("refuses a device file name the selected Windows host cannot store before pul
|
||||
exit 0`,
|
||||
);
|
||||
const provider = new AdbProvider({
|
||||
environment: { REA_ADB_PATH: binary },
|
||||
// A double leading slash gives Win32 a UNC root and still opens the POSIX fixture.
|
||||
environment: { REA_ADB_PATH: `/${binary}` },
|
||||
platform: "win32",
|
||||
});
|
||||
const result = await provider.execute({
|
||||
|
||||
@@ -62,6 +62,12 @@ describe("resolveApktoolCommand", () => {
|
||||
await expect(
|
||||
resolveApktoolCommand({ [APKTOOL_COMMAND_ENV]: "bin/apktool" }, "linux"),
|
||||
).rejects.toThrow(/must be an absolute path/u);
|
||||
await expect(
|
||||
resolveApktoolCommand(
|
||||
{ [APKTOOL_COMMAND_ENV]: "\\tools\\apktool.bat" },
|
||||
"win32",
|
||||
),
|
||||
).rejects.toThrow(/must be an absolute path/u);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
cancelledBy,
|
||||
carriedReason,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { isAbsolute, win32 } from "node:path";
|
||||
import { isQualifiedCallerPath } from "../filesystem/callerPath.js";
|
||||
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
||||
import { findWindowsExecutable } from "../process/WindowsExecutable.js";
|
||||
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
||||
@@ -81,11 +81,7 @@ export const resolveApktoolCommand = async (
|
||||
const selectedEnvironment = snapshotEnvironment(environment, platform);
|
||||
const configured = selectedEnvironment[APKTOOL_COMMAND_ENV];
|
||||
if (configured !== undefined && configured.trim() !== "") {
|
||||
if (
|
||||
!(platform === "win32"
|
||||
? win32.isAbsolute(configured)
|
||||
: isAbsolute(configured))
|
||||
)
|
||||
if (!isQualifiedCallerPath(configured, platform))
|
||||
throw new ApktoolConfigurationFailure(
|
||||
"not_absolute",
|
||||
`${APKTOOL_COMMAND_ENV} must be an absolute path to the apktool launcher`,
|
||||
|
||||
@@ -1787,20 +1787,22 @@ it("reports the owned-process prerequisite as Apktool unavailability, not a prob
|
||||
|
||||
it("matches the launcher variable by the selected host's environment rules", async () => {
|
||||
const stub = await writeApktoolStub(() => VERSION_BLOCK);
|
||||
// A double leading slash gives Win32 a UNC root and still opens the POSIX fixture.
|
||||
const command = `/${stub.command}`;
|
||||
// Windows environment names are case-insensitive; POSIX names are exact.
|
||||
const environment = { rea_apktool_command: stub.command, PATH: "" };
|
||||
const environment = { rea_apktool_command: command, PATH: "" };
|
||||
const windows = new ApktoolProvider({ environment, platform: "win32" });
|
||||
onTestFinished(() => windows.close());
|
||||
expect(await windows.inspectAvailability()).toMatchObject({
|
||||
status: "available",
|
||||
diagnostics: {
|
||||
selected_command: stub.command,
|
||||
selected_command: command,
|
||||
command_source: "environment",
|
||||
},
|
||||
});
|
||||
const posix = new ApktoolProvider({ environment, platform: "linux" });
|
||||
onTestFinished(() => posix.close());
|
||||
expect(await posix.inspectAvailability()).not.toMatchObject({
|
||||
diagnostics: { selected_command: stub.command },
|
||||
diagnostics: { selected_command: command },
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,22 @@ 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
|
||||
|
||||
@@ -80,6 +80,13 @@ describe("resolvePcapTools", () => {
|
||||
expect(() =>
|
||||
resolvePcapTools({ [TSHARK_COMMAND_ENV]: "bin/tshark" }, "linux"),
|
||||
).toThrow(/must be an absolute path/u);
|
||||
// Not absolute on POSIX either; on Windows it is rooted on the current drive.
|
||||
expect(() =>
|
||||
resolvePcapTools(
|
||||
{ [TSHARK_COMMAND_ENV]: "\\tools\\tshark.exe" },
|
||||
"win32",
|
||||
),
|
||||
).toThrow(/must be an absolute path/u);
|
||||
});
|
||||
|
||||
it("rejects a relative capinfos path explicitly", () => {
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
AnalysisArtifactChangedError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { isQualifiedCallerPath } from "../filesystem/callerPath.js";
|
||||
|
||||
export interface WiresharkExecutableIdentity {
|
||||
readonly bytes: number;
|
||||
@@ -91,12 +92,13 @@ const TOOL_COMMAND_ENV = {
|
||||
export const selectWiresharkTool = (
|
||||
environment: Readonly<Record<string, string | undefined>>,
|
||||
tool: "tshark" | "capinfos",
|
||||
platform?: NodeJS.Platform,
|
||||
): WiresharkToolSelection => {
|
||||
const key = TOOL_COMMAND_ENV[tool];
|
||||
const configured = environment[key];
|
||||
if (configured === undefined || configured.trim() === "")
|
||||
return { command: tool, source: "path" };
|
||||
if (!isAbsolute(configured))
|
||||
if (!isQualifiedCallerPath(configured, platform))
|
||||
throw new PcapConfigurationFailure(
|
||||
`${tool}_not_absolute`,
|
||||
`${key} must be an absolute path to the ${tool} binary`,
|
||||
@@ -116,8 +118,8 @@ export const resolvePcapTools = (
|
||||
): PcapToolSelection => {
|
||||
// Report every configuration error before consulting PATH for either tool.
|
||||
const selected = {
|
||||
tshark: selectWiresharkTool(environment, "tshark"),
|
||||
capinfos: selectWiresharkTool(environment, "capinfos"),
|
||||
tshark: selectWiresharkTool(environment, "tshark", platform),
|
||||
capinfos: selectWiresharkTool(environment, "capinfos", platform),
|
||||
};
|
||||
const locate = (tool: "tshark" | "capinfos"): WiresharkToolSelection => {
|
||||
const selection = selected[tool];
|
||||
|
||||
@@ -1,6 +1,31 @@
|
||||
import { expect, it, vi } from "vitest";
|
||||
|
||||
import { absoluteCallerPath } from "../../../src/filesystem/callerPath.js";
|
||||
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(() => {
|
||||
|
||||
@@ -597,9 +597,10 @@ it.skipIf(isWindows).each([
|
||||
"decides capture-host support from the selected %s host, not the running one",
|
||||
async (platform, expected) => {
|
||||
const provider = new PcapProvider({
|
||||
// A double leading slash gives Win32 a UNC root and still opens the POSIX fixtures.
|
||||
environment: {
|
||||
REA_TSHARK_COMMAND: tsharkStub,
|
||||
REA_CAPINFOS_COMMAND: capinfosStub,
|
||||
REA_TSHARK_COMMAND: `/${tsharkStub}`,
|
||||
REA_CAPINFOS_COMMAND: `/${capinfosStub}`,
|
||||
},
|
||||
platform,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user