Files
reverse-engineering-anything/src/apktool/ApktoolConfiguration.test.ts
T
Adolanium 9d5c5f687d fix(process): accept legal characters in Windows batch paths (#15128)
* fix(process): quote Windows batch operators cmd.exe keeps literal

cmd.exe with delayed expansion off keeps & | < > ( ) ^ ! literal inside quotes. Empty tokens and %name% spans are still refused, including when that name is unset, because the child environment can define it.

* fix(process): report a refused batch path as invalid configuration

A percent expansion or an unusable COMSPEC is configuration_invalid. Packet capture does not append the Wireshark install remediation, and an apktool decode does not report a capability gap.

* fix(process): refuse percent spans joined across batch tokens

cmd.exe expands a percent span on the joined command line. Tokens that
are legal alone are refused when joining them forms a span. The spawn
regression kills its child if that child is still running after 10 seconds.

* test(process): include the Windows batch regression in CI

The curated Windows lane now runs WindowsBatchCommand.test.ts.

* fix(domain): preserve a configuration error's caller message

ConfigurationError accepted userMessage and dropped it, so the public
projection replaced the caller text with the generic doctor sentence.
The constructor now stores that message.

* fix(pcap): keep a batch refusal in the public configuration message

A Windows batch refusal stays in the configuration error the caller
sees, including the refused path and the percent-expansion reason.

* fix(apktool): report a decode batch refusal as invalid configuration

Inspection still reports not_configured. Decoding reports
configuration_invalid and names the launcher that cmd.exe cannot receive.
A percent span that appears only after the quoted tokens are joined is
refused the same way.

* test(process): own the Windows batch regression in the package lane

The curated Windows job already runs WindowsBatchCommand.test.ts. The package scope did not name that file, so the lane ownership check reported it as unowned.
2026-11-30 22:28:54 +08:00

727 lines
23 KiB
TypeScript

import * as commandOwner from "../process/OwnedCommand.js";
import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { spawnFailure } from "../../tests/support/process/spawnFailure.js";
import {
AnalysisError,
primaryAnalysisError,
} from "../domain/analysisErrorBase.js";
import {
ProcessOwnershipIdentityBatchFailure,
ProcessOwnershipInspectionCancelled,
} from "../process/ProcessInspectionCancellation.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { systemProcessOwnershipHost } from "../process/ProcessOwnershipObservation.js";
import { ApktoolCommandCleanup } from "./ApktoolCommandCleanup.js";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
APKTOOL_COMMAND_ENV,
apktoolConfigurationAvailability,
ApktoolConfigurationFailure,
apktoolInvocation,
inspectApktoolClient,
parseApktoolVersionOutput,
resolveApktoolCommand,
} from "./ApktoolConfiguration.js";
const lookup = vi.hoisted(() => ({ stat: vi.fn() }));
vi.mock("node:fs/promises", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs/promises")>()),
stat: lookup.stat,
}));
beforeEach(() => {
lookup.stat
.mockReset()
.mockRejectedValue(Object.assign(new Error("missing"), { code: "ENOENT" }));
});
describe("resolveApktoolCommand", () => {
it("prefers an absolute REA_APKTOOL_COMMAND", async () => {
expect(
await resolveApktoolCommand(
{
[APKTOOL_COMMAND_ENV]: "/usr/local/bin/apktool",
},
"linux",
),
).toEqual({
command: "/usr/local/bin/apktool",
commandSource: "environment",
});
});
it("leaves POSIX PATH lookup to process creation", async () => {
expect(await resolveApktoolCommand({}, "linux")).toEqual({
command: "apktool",
commandSource: "path",
});
});
it("rejects a relative environment path explicitly", async () => {
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);
});
});
describe("parseApktoolVersionOutput", () => {
it("reads the version from real apktool output", () => {
expect(parseApktoolVersionOutput("2.7.0-dirty\n", "linux")).toMatchObject({
version: "2.7.0-dirty",
});
});
it("reports null when the launcher prints nothing usable", () => {
expect(parseApktoolVersionOutput(" \n", "linux")).toEqual({
version: null,
reportedVersion: null,
});
expect(
parseApktoolVersionOutput("successful but not a version\n", "linux"),
).toEqual({
version: null,
reportedVersion: "successful but not a version",
});
});
it("parses semantic prerelease versions without claiming they meet the minimum", () => {
expect(parseApktoolVersionOutput("2.7.0-beta.1\n", "linux")).toMatchObject({
version: "2.7.0-beta.1",
});
});
it("validates normalized semantic versions while preserving caller spelling", () => {
expect(parseApktoolVersionOutput("v2.7.0\n", "linux")).toMatchObject({
version: "v2.7.0",
});
});
});
describe("reported Apktool version policy", () => {
it.each([
"2.7.0-dirty",
"2.9.1",
"v1.5.2",
"2.9.2-rc.1",
"2.9.2-rc-dirty",
"2.9.1+patched",
])(
"discloses the upstream advisory for %s without claiming engine patch identity",
(version) => {
const identity = parseApktoolVersionOutput(version, "linux");
if (identity.version === null) throw new Error("Expected parsed version");
expect(identity.version).toBe(version);
const warnings = identity.limitations.filter((limitation) =>
limitation.includes("CVE-2024-21633"),
);
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain(
"does not establish engine contents or downstream backports",
);
},
);
it.each([
"2.9.2",
"2.9.2-dirty",
"v2.9.2",
"2.9.2+downstream",
"2.10.0-dirty",
"3.0.1",
])(
"does not attach CVE-2024-21633 to a reported version at or beyond its fix: %s",
(version) => {
const identity = parseApktoolVersionOutput(version, "linux");
if (identity.version === null) throw new Error("Expected parsed version");
expect(identity).toMatchObject({
version,
supportsDocumentedVersion: true,
});
expect(
identity.limitations.filter((limitation) =>
limitation.includes("CVE-2024-21633"),
),
).toEqual([]);
},
);
it.each([
["2.7.0", true],
["2.7.0-dirty", true],
["v2.7.0-dirty", true],
["2.7.0-beta.1", false],
["2.7.0-rc-dirty", false],
["2.7.0-dirty+patched", false],
["2.6.9", false],
] as const)(
"preserves the documented release floor for %s",
(version, supported) => {
expect(parseApktoolVersionOutput(version, "linux")).toMatchObject({
version,
supportsDocumentedVersion: supported,
});
},
);
});
describe("reported advisory applicability (#4818)", () => {
it.each([
["2.7.0-dirty", true],
["2.9.2", true],
["2.9.2+patched", true],
["2.9.3-rc.1", true],
["2.9.3", false],
["2.9.3-dirty", false],
["3.0.1", false],
] as const)(
"binds the Windows advisory for %s to the selected host",
(version, affected) => {
for (const platform of ["win32", "linux", "darwin"] as const) {
const identity = parseApktoolVersionOutput(version, platform);
if (identity.version === null) throw new Error("Expected version");
expect(identity.version).toBe(version);
const warnings = identity.limitations.filter((warning) =>
warning.includes("GHSA-vgwr-4w3p-xmjv"),
);
expect(warnings.length).toBe(affected && platform === "win32" ? 1 : 0);
}
},
);
it.each([
["3.0.0-beta.1", true],
["3.0.0", true],
["v3.0.0-dirty", true],
["3.0.1", true],
["3.0.1+patched", true],
["3.0.2-rc.1", true],
["3.0.2", false],
["3.0.2-dirty", false],
["3.0.3", false],
["3.1.0-beta.1", false],
["2.12.1", false],
["4.0.0", false],
] as const)(
"bounds the 3.x regression advisory for %s",
(version, affected) => {
for (const platform of ["win32", "linux", "darwin"] as const) {
const identity = parseApktoolVersionOutput(version, platform);
if (identity.version === null) throw new Error("Expected version");
const warnings = identity.limitations.filter((warning) =>
warning.includes("CVE-2026-39973"),
);
expect(warnings.length).toBe(affected ? 1 : 0);
if (affected) {
expect(warnings[0]).toContain(
"does not establish engine contents or downstream backports",
);
expect(warnings[0]?.includes("source-derived")).toBe(
version.includes("3.0.0"),
);
}
}
},
);
});
describe("apktoolInvocation", () => {
const windows = { SystemRoot: "C:\\Windows" };
it("runs a Windows batch launcher through an explicit cmd.exe call", () => {
expect(
apktoolInvocation(
{
command: "C:\\ProgramData\\chocolatey\\bin\\apktool.BAT",
commandSource: "environment",
},
["d", "-o", "C:\\Users\\Analyst\\AppData\\Local\\Temp\\rea out"],
windows,
"win32",
),
).toEqual({
command: "C:\\Windows\\System32\\cmd.exe",
arguments: [
"/d",
"/e:on",
"/v:off",
"/s",
"/c",
'""C:\\ProgramData\\chocolatey\\bin\\apktool.BAT" "d" "-o" "C:\\Users\\Analyst\\AppData\\Local\\Temp\\rea out""',
],
windowsVerbatimArguments: true,
});
});
it("runs native executables and POSIX scripts directly", () => {
for (const [command, platform] of [
["C:\\tools\\apktool.exe", "win32"],
["/opt/apktool/apktool.cmd", "linux"],
] as const)
expect(
apktoolInvocation(
{ command, commandSource: "environment" },
["--version"],
windows,
platform,
),
).toEqual({
command,
arguments: ["--version"],
windowsVerbatimArguments: false,
});
});
it("quotes an ampersand in a Windows batch launcher path", () => {
expect(
apktoolInvocation(
{
command: "C:\\tools & more\\apktool.cmd",
commandSource: "environment",
},
["--version"],
windows,
"win32",
).arguments.at(-1),
).toBe('""C:\\tools & more\\apktool.cmd" "--version""');
});
it("reports a batch launcher that cmd.exe would reinterpret before running it", async () => {
const failure = await inspectApktoolClient(
{
command: "C:\\tools\\apk%tool%.cmd",
commandSource: "environment",
},
commands,
{ environment: windows, platform: "win32" },
).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ApktoolConfigurationFailure);
if (!(failure instanceof ApktoolConfigurationFailure)) return;
expect(failure.code).toBe("launcher_unsupported");
expect(apktoolConfigurationAvailability(failure)).toMatchObject({
status: "unavailable",
code: "not_configured",
remediation: expect.stringContaining(APKTOOL_COMMAND_ENV),
diagnostics: {
selected_command: "C:\\tools\\apk%tool%.cmd",
},
});
});
});
describe("Windows PATH launcher selection", () => {
const environment = {
Path: '"C:\\Selected Tools";C:\\Other Tools',
PathExt: ".EXE;.CMD;.BAT",
ComSpec: "C:\\Windows\\System32\\cmd.exe",
NoDefaultCurrentDirectoryInExePath: "",
};
const files = (...paths: string[]) => {
const selected = new Set(paths.map((path) => path.toLowerCase()));
lookup.stat.mockImplementation(async (path: string) => {
if (selected.has(path.toLowerCase())) return { isFile: () => true };
throw Object.assign(new Error("missing"), { code: "ENOENT" });
});
};
it.each([".BAT", ".CMD"])(
"resolves a default %s launcher before choosing cmd.exe for version and decode",
async (extension) => {
const command = `C:\\Selected Tools\\apktool${extension}`;
files(command);
const selection = await resolveApktoolCommand(environment, "win32");
expect(selection).toEqual({ command, commandSource: "path" });
for (const arguments_ of [
["--version"],
["d", "-o", "C:\\output folder", "C:\\target.apk"],
]) {
const invocation = apktoolInvocation(
selection,
arguments_,
environment,
"win32",
);
expect(invocation).toEqual({
command: environment.ComSpec,
arguments: [
"/d",
"/e:on",
"/v:off",
"/s",
"/c",
`"${[command, ...arguments_].map((token) => `"${token}"`).join(" ")}"`,
],
windowsVerbatimArguments: true,
});
}
},
);
it("honors PATH directory order before extension order", async () => {
files("C:\\Selected Tools\\apktool.CMD", "C:\\Other Tools\\apktool.EXE");
expect(await resolveApktoolCommand(environment, "win32")).toMatchObject({
command: "C:\\Selected Tools\\apktool.CMD",
});
});
it("honors the selected PATHEXT ordering", async () => {
files("C:\\Selected Tools\\apktool.CMD", "C:\\Selected Tools\\apktool.EXE");
expect(
await resolveApktoolCommand(
{ ...environment, PathExt: ".CMD;.EXE" },
"win32",
),
).toMatchObject({ command: "C:\\Selected Tools\\apktool.CMD" });
});
it("keeps native executable arguments outside cmd.exe interpretation", async () => {
const command = "C:\\Selected Tools\\apktool.EXE";
files(command);
const selection = await resolveApktoolCommand(environment, "win32");
const arguments_ = ["d", "C:\\input & literal.apk"];
expect(
apktoolInvocation(selection, arguments_, environment, "win32"),
).toEqual({
command,
arguments: arguments_,
windowsVerbatimArguments: false,
});
});
it("preserves explicit launcher spelling without searching PATH", async () => {
const command = "C:\\selected alias\\APKTOOL.cmd";
expect(
await resolveApktoolCommand(
{ ...environment, [APKTOOL_COMMAND_ENV]: command },
"win32",
),
).toEqual({ command, commandSource: "environment" });
expect(lookup.stat).not.toHaveBeenCalled();
});
it("does not inspect the current directory when the caller disables it", async () => {
await expect(
resolveApktoolCommand(environment, "win32"),
).rejects.toMatchObject({ code: "command_missing" });
expect(
lookup.stat.mock.calls.every(
([path]) =>
String(path).startsWith("C:\\Selected Tools\\") ||
String(path).startsWith("C:\\Other Tools\\"),
),
).toBe(true);
});
it("does not turn an explicitly empty PATHEXT into default extensions", async () => {
files("C:\\Selected Tools\\apktool.EXE");
await expect(
resolveApktoolCommand({ ...environment, PathExt: "" }, "win32"),
).rejects.toMatchObject({ code: "command_missing" });
expect(lookup.stat).not.toHaveBeenCalled();
});
it("skips directories without treating them as executable launchers", async () => {
files("C:\\Other Tools\\apktool.EXE");
const stat = lookup.stat.getMockImplementation()!;
lookup.stat.mockImplementation(async (path: string) =>
path === "C:\\Selected Tools\\apktool.EXE"
? { isFile: () => false }
: stat(path),
);
expect(await resolveApktoolCommand(environment, "win32")).toMatchObject({
command: "C:\\Other Tools\\apktool.EXE",
});
});
it("preserves inspection failures instead of reporting a missing command or selecting a later file", async () => {
lookup.stat.mockRejectedValue(
Object.assign(new Error("denied"), { code: "EACCES" }),
);
const failure = await resolveApktoolCommand(environment, "win32").catch(
(cause: unknown) => cause,
);
expect(failure).toBeInstanceOf(ApktoolConfigurationFailure);
if (!(failure instanceof ApktoolConfigurationFailure)) throw failure;
expect(failure).toMatchObject({
code: "command_unresolved",
diagnostics: {
candidate_path: "C:\\Selected Tools\\apktool.EXE",
filesystem_error: "EACCES",
},
});
expect(apktoolConfigurationAvailability(failure)).toMatchObject({
code: "not_configured",
});
expect(lookup.stat).toHaveBeenCalledTimes(1);
});
it("honors cancellation before inspecting launchers", async () => {
const controller = new AbortController();
controller.abort();
await expect(
resolveApktoolCommand(environment, "win32", controller.signal),
).rejects.toMatchObject({ _tag: "AnalysisCancelledError" });
expect(lookup.stat).not.toHaveBeenCalled();
});
});
it("does not select a launcher when cancellation arrives during lookup", async () => {
const controller = new AbortController();
lookup.stat.mockImplementation(async () => {
controller.abort();
return { isFile: () => true };
});
await expect(
resolveApktoolCommand(
{
PATH: "C:\\Tools",
PATHEXT: ".CMD",
NoDefaultCurrentDirectoryInExePath: "1",
},
"win32",
controller.signal,
),
).rejects.toMatchObject({ _tag: "AnalysisCancelledError" });
expect(lookup.stat).toHaveBeenCalledTimes(1);
});
it.each(["EXE", ".CMD\\..\\outside.exe", ".CMD\0"])(
"rejects malformed PATHEXT %j before path lookup",
async (PATHEXT) => {
await expect(
resolveApktoolCommand({ PATH: "C:\\Tools", PATHEXT }, "win32"),
).rejects.toMatchObject({
code: "command_unresolved",
diagnostics: { invalid_extension: PATHEXT },
});
expect(lookup.stat).not.toHaveBeenCalled();
},
);
it("cancels a pending filesystem lookup and owns its eventual rejection", async () => {
const controller = new AbortController();
let started!: () => void;
let rejectLookup!: (cause: Error) => void;
const pendingStat = new Promise((_resolve, reject) => {
rejectLookup = reject;
});
void pendingStat.catch(() => undefined);
const observed = new Promise<void>((resolve) => {
started = resolve;
});
lookup.stat.mockImplementation(() => {
started();
return pendingStat;
});
const pending = resolveApktoolCommand(
{
PATH: "C:\\Tools",
PATHEXT: ".CMD",
NODEFAULTCURRENTDIRECTORYINEXEPATH: "1",
},
"win32",
controller.signal,
);
try {
await awaitAcknowledgement(observed, pending, "apktool filesystem lookup");
controller.abort();
await expect(pending).rejects.toMatchObject({
_tag: "AnalysisCancelledError",
});
rejectLookup(new Error("filesystem completed after cancellation"));
await Promise.resolve();
expect(lookup.stat).toHaveBeenCalledTimes(1);
} finally {
controller.abort();
rejectLookup(new Error("fixture cleanup"));
await pending.catch(() => undefined);
}
});
it("bounds the entire pending PATH lookup by the selection deadline", async () => {
vi.useFakeTimers();
try {
lookup.stat.mockReturnValue(new Promise(() => {}));
const failure = expect(
resolveApktoolCommand(
{
PATH: "C:\\Tools",
PATHEXT: ".CMD",
NODEFAULTCURRENTDIRECTORYINEXEPATH: "1",
},
"win32",
),
).rejects.toMatchObject({
_tag: "AnalysisTimeoutError",
timeoutMs: 20_000,
});
await vi.advanceTimersByTimeAsync(20_000);
await failure;
expect(lookup.stat).toHaveBeenCalledTimes(1);
} finally {
vi.useRealTimers();
}
});
let commands = new ApktoolCommandCleanup();
afterEach(async () => {
await commands.close();
commands = new ApktoolCommandCleanup();
});
it("does not implicitly search CWD when no opt-out flag is present", async () => {
await expect(
resolveApktoolCommand(
{ Path: "C:\\Selected Tools;D:\\Other Tools", PathExt: ".EXE" },
"win32",
),
).rejects.toMatchObject({ code: "command_missing" });
expect(lookup.stat.mock.calls.map(([path]) => path)).toEqual([
"C:\\Selected Tools\\apktool.EXE",
"D:\\Other Tools\\apktool.EXE",
]);
});
it.each([false, true])(
"keeps preparation failure distinct from selected launcher availability (batch: %s)",
async (batch) => {
const descriptor = await failedInspectionDescriptor();
const reason = { source: "inspection caller" };
const cause = batch
? new ProcessOwnershipIdentityBatchFailure(
[
{ pid: 41, requestIndex: 0, cause: descriptor.failure },
{
pid: 42,
requestIndex: 1,
cause: new ProcessOwnershipInspectionCancelled(reason),
},
],
new Map(),
)
: descriptor.failure;
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(cause);
try {
const result: unknown = await inspectApktoolClient(
{ command: "/selected/apktool", commandSource: "environment" },
commands,
{ platform: "linux" },
).catch((cause: unknown) => cause);
expect(result).toBeInstanceOf(AnalysisError);
if (!(result instanceof AnalysisError)) throw result;
const primary = primaryAnalysisError(result);
expect(primary).toMatchObject({
_tag: "ProviderAdapterError",
diagnostics: { stage: "ownership_preparation" },
});
expect(primary.cause).toBe(cause);
expect(primary.capturedOutput).toBeUndefined();
expect(primary.diagnostics).not.toHaveProperty("compiler_outcome");
if (batch)
expect(primary.secondaryFailures).toContainEqual(
expect.objectContaining({
_tag: "AnalysisCancelledError",
cause: reason,
}),
);
expect(result.cleanupResources).toEqual([descriptor.failure.path]);
descriptor.allow();
await commands.close();
expect(result.cleanupIncomplete).toBe(false);
expect(descriptor.failure.owner.handle.fd).toBe(-1);
} finally {
prepare.mockRestore();
descriptor.allow();
await commands.close();
await descriptor.failure.owner.close();
}
},
);
it("keeps a recorded command timeout primary with its actual inspection stop fact", async () => {
const reason = new Error("recorded command deadline");
const inspection = new ProcessOwnershipInspectionCancelled(reason);
const failure = new commandOwner.OwnedCommandFailure(
"timeout",
"Command deadline elapsed during ownership preparation.",
null,
null,
{ preparationFailure: inspection },
);
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockResolvedValue(undefined);
const command = vi
.spyOn(commandOwner, "runOwnedCommand")
.mockRejectedValue(failure);
try {
const result: unknown = await inspectApktoolClient(
{ command: "/selected/apktool", commandSource: "environment" },
commands,
{ platform: "linux" },
).catch((cause: unknown) => cause);
expect(result).toMatchObject({
_tag: "AnalysisTimeoutError",
timeoutMs: 20_000,
});
if (!(result instanceof AnalysisError)) throw result;
expect(result.cause).toBe(failure);
expect(failure.cancellation).toBeUndefined();
expect(result.secondaryFailures).toHaveLength(1);
const stopped = result.secondaryFailures[0];
if (stopped === undefined)
throw new Error("Expected the recorded inspection stop fact");
expect(stopped.cause).toBe(reason);
expect(Reflect.get(stopped, "inspectionFailure")).toBe(inspection);
expect(result.cleanupResources).toEqual([]);
} finally {
command.mockRestore();
prepare.mockRestore();
}
});
it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => {
const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc");
const darwin = new DarwinProcessOwnershipInspectionError(
"helper capacity exhausted",
{ cause: emfile },
);
const failure = new commandOwner.OwnedCommandFailure(
"process",
"Ownership preparation failed",
null,
null,
{ preparationFailure: darwin },
);
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockResolvedValue(undefined);
const command = vi
.spyOn(commandOwner, "runOwnedCommand")
.mockRejectedValue(failure);
try {
const result: unknown = await inspectApktoolClient(
{ command: "/selected/apktool", commandSource: "environment" },
commands,
{ platform: "linux" },
).catch((cause: unknown) => cause);
expect(result).toMatchObject({
_tag: "AnalysisResourceConstraintError",
retryable: true,
diagnostics: {
system_code: "EMFILE",
selected_command: "/selected/apktool",
},
});
} finally {
command.mockRestore();
prepare.mockRestore();
}
});