diff --git a/src/adb/AdbConfiguration.test.ts b/src/adb/AdbConfiguration.test.ts index 43aeeaa14f..b7d28b346c 100644 --- a/src/adb/AdbConfiguration.test.ts +++ b/src/adb/AdbConfiguration.test.ts @@ -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); }); }); diff --git a/src/adb/AdbConfiguration.ts b/src/adb/AdbConfiguration.ts index 007c5a0973..26787ff3b7 100644 --- a/src/adb/AdbConfiguration.ts +++ b/src/adb/AdbConfiguration.ts @@ -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`, diff --git a/src/adb/AdbProvider.test.ts b/src/adb/AdbProvider.test.ts index 84e620bff1..193e4b737f 100644 --- a/src/adb/AdbProvider.test.ts +++ b/src/adb/AdbProvider.test.ts @@ -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({ diff --git a/src/apktool/ApktoolConfiguration.test.ts b/src/apktool/ApktoolConfiguration.test.ts index 845db4a5e1..d92a729a1c 100644 --- a/src/apktool/ApktoolConfiguration.test.ts +++ b/src/apktool/ApktoolConfiguration.test.ts @@ -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); }); }); diff --git a/src/apktool/ApktoolConfiguration.ts b/src/apktool/ApktoolConfiguration.ts index c5652f43cd..74495f6b18 100644 --- a/src/apktool/ApktoolConfiguration.ts +++ b/src/apktool/ApktoolConfiguration.ts @@ -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`, diff --git a/src/apktool/ApktoolProvider.test.ts b/src/apktool/ApktoolProvider.test.ts index 9ae307867a..183564faf4 100644 --- a/src/apktool/ApktoolProvider.test.ts +++ b/src/apktool/ApktoolProvider.test.ts @@ -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 }, }); }); diff --git a/src/filesystem/callerPath.ts b/src/filesystem/callerPath.ts index 8ec8e69556..b7736707a1 100644 --- a/src/filesystem/callerPath.ts +++ b/src/filesystem/callerPath.ts @@ -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 diff --git a/src/pcap/PcapConfiguration.test.ts b/src/pcap/PcapConfiguration.test.ts index 155b8e7f7f..3628f763f3 100644 --- a/src/pcap/PcapConfiguration.test.ts +++ b/src/pcap/PcapConfiguration.test.ts @@ -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", () => { diff --git a/src/pcap/PcapConfiguration.ts b/src/pcap/PcapConfiguration.ts index 8fd856d8d8..a9df3a58a3 100644 --- a/src/pcap/PcapConfiguration.ts +++ b/src/pcap/PcapConfiguration.ts @@ -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>, 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]; diff --git a/tests/boundary/filesystem/callerPath.test.ts b/tests/boundary/filesystem/callerPath.test.ts index f22395196c..bc4a69c7d0 100644 --- a/tests/boundary/filesystem/callerPath.test.ts +++ b/tests/boundary/filesystem/callerPath.test.ts @@ -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(() => { diff --git a/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts b/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts index 3b26483f42..3dc71d6b8d 100644 --- a/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts +++ b/tests/boundary/pcap/pcapProviderCrossPlatform.test.ts @@ -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, });