mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
* 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.
128 lines
3.5 KiB
TypeScript
128 lines
3.5 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
quoteWindowsBatchToken,
|
|
windowsBatchCommand,
|
|
WindowsBatchCommandError,
|
|
} from "./WindowsBatchCommand.js";
|
|
|
|
const refusal = (value: string): WindowsBatchCommandError => {
|
|
try {
|
|
quoteWindowsBatchToken(value);
|
|
} catch (cause) {
|
|
if (cause instanceof WindowsBatchCommandError) return cause;
|
|
throw cause;
|
|
}
|
|
throw new Error(`expected ${JSON.stringify(value)} to be refused`);
|
|
};
|
|
|
|
describe("quoteWindowsBatchToken", () => {
|
|
it.each([
|
|
"C:\\Users\\R&D",
|
|
"caret^name",
|
|
"bang!name",
|
|
"pipe|name",
|
|
"lt<name",
|
|
"gt>name",
|
|
"paren(name)",
|
|
"100%",
|
|
"percent%only",
|
|
"%%",
|
|
])("quotes %s", (value) => {
|
|
expect(quoteWindowsBatchToken(value)).toBe(`"${value}"`);
|
|
});
|
|
|
|
it.each(["%PATH%", "%%PATH%%", "a%b%c", "file%name%.txt"])(
|
|
"refuses a percent expansion %s",
|
|
(value) => {
|
|
expect(refusal(value)).toMatchObject({ reason: "metacharacter" });
|
|
},
|
|
);
|
|
|
|
it.each(['say "hi"', "line\n", "line\r", "nul\0"])(
|
|
"refuses a quote or control character %s",
|
|
(value) => {
|
|
expect(refusal(value).reason).toBe("metacharacter");
|
|
},
|
|
);
|
|
|
|
it("refuses an empty argument with its own reason", () => {
|
|
expect(refusal("")).toMatchObject({
|
|
reason: "empty",
|
|
message: "Windows batch arguments cannot be empty",
|
|
});
|
|
});
|
|
});
|
|
|
|
const batchEnvironment = {
|
|
SYSTEMROOT: "C:\\Windows",
|
|
COMSPEC: "C:\\Windows\\System32\\cmd.exe",
|
|
};
|
|
|
|
it("refuses a percent span that appears only after quoted tokens are joined", () => {
|
|
expect(quoteWindowsBatchToken("%PATH:__REA_NO_MATCH__")).toBe(
|
|
'"%PATH:__REA_NO_MATCH__"',
|
|
);
|
|
expect(quoteWindowsBatchToken("=ignored%")).toBe('"=ignored%"');
|
|
expect(() =>
|
|
windowsBatchCommand({
|
|
environment: batchEnvironment,
|
|
batchPath: "C:\\tools\\tool.cmd",
|
|
arguments: ["%PATH:__REA_NO_MATCH__", "=ignored%"],
|
|
}),
|
|
).toThrow(WindowsBatchCommandError);
|
|
});
|
|
|
|
it("keeps a trailing percent when the following token has none", () => {
|
|
expect(
|
|
windowsBatchCommand({
|
|
environment: batchEnvironment,
|
|
batchPath: "C:\\tools\\tool.cmd",
|
|
arguments: ["100%", "done"],
|
|
}).arguments.at(-1),
|
|
).toBe(`""C:\\tools\\tool.cmd" "100%" "done""`);
|
|
});
|
|
|
|
it.skipIf(process.platform !== "win32")(
|
|
"cmd.exe receives an ampersand path as one argument",
|
|
async () => {
|
|
const invocation = windowsBatchCommand({
|
|
environment: process.env,
|
|
batchPath: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
"process.stdout.write(process.argv[1])",
|
|
"C:\\Users\\R&D",
|
|
],
|
|
});
|
|
const output = await new Promise<string>((resolve, reject) => {
|
|
const child = spawn(invocation.command, [...invocation.arguments], {
|
|
windowsVerbatimArguments: true,
|
|
});
|
|
const timer = setTimeout(() => {
|
|
child.kill();
|
|
reject(new Error("cmd.exe did not exit"));
|
|
}, 10_000);
|
|
let out = "";
|
|
let err = "";
|
|
child.stdout.on("data", (chunk: Buffer) => {
|
|
out += chunk.toString("utf8");
|
|
});
|
|
child.stderr.on("data", (chunk: Buffer) => {
|
|
err += chunk.toString("utf8");
|
|
});
|
|
child.on("error", (error) => {
|
|
clearTimeout(timer);
|
|
reject(error);
|
|
});
|
|
child.on("close", (code) => {
|
|
clearTimeout(timer);
|
|
if (code === 0) resolve(out);
|
|
else reject(new Error(`exit ${String(code)}: ${err}`));
|
|
});
|
|
});
|
|
expect(output).toBe("C:\\Users\\R&D");
|
|
},
|
|
);
|