mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
execFile was called with shell: true and an array of arguments, which Node deprecates in DEP0190 because it concatenates the arguments by space without escaping anything, giving the impression they have been made safe. The warning was printed to the console on every install. The arguments here are already escaped for the shell by escapeArg, so join them into the command instead of handing them to execFile to concatenate. Nothing about what ends up being run changes. Point the escaping test at the same form so it covers what the installer actually does, and stops emitting the warning itself. Its several hundred round trip cases still pass, which is what confirms the escaping is unaffected. Fixes #851 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
"use strict";
|
|
const child_process = require("child_process");
|
|
const util = require("util");
|
|
const assert = require("assert").strict;
|
|
const path = require("path");
|
|
|
|
const { escapeArg } = require("../../packages/create/escape_arg");
|
|
const execFile = util.promisify(child_process.execFile);
|
|
|
|
async function exec(file, args) {
|
|
// Escape the arguments and join them into the command the same way
|
|
// create.js does, rather than passing them to execFile which would only
|
|
// concatenate them by space without escaping (DEP0190).
|
|
const command = [file, ...args.map(escapeArg)].join(" ");
|
|
const { stdout, stderr } = await execFile(
|
|
command,
|
|
{ shell: true, cwd: __dirname, env: { ...process.env, pct: "%"} }
|
|
);
|
|
return { stdout: JSON.parse(stdout), stderr };
|
|
}
|
|
|
|
const strings = [
|
|
'"a"',
|
|
"a sentence of text.",
|
|
" irregular \tspaces ",
|
|
'"C:\\path\\"',
|
|
"C:\\path\\",
|
|
'\\ "\\" \\\\ "\\\\" \\\\\\ "\\\\\\" \\\\\\\\ "\\\\\\\\" \\\\\\\\\\ "\\\\\\\\\\" \\\\\\\\\\\ "\\\\\\\\\\\\"',
|
|
'"C:\\path"',
|
|
"C:\\path",
|
|
"%OS%",
|
|
"!OS!",
|
|
"😮",
|
|
];
|
|
for (let i=1; i < 128; i++) {
|
|
const char = String.fromCodePoint(i);
|
|
// Skip newline and carriage return because those are difficult to escape
|
|
// Skip most alphanumeric characters as they don't behave differently to each other.
|
|
if (/[\n\r1-9B-Zb-z]/.test(char)) {
|
|
continue;
|
|
}
|
|
strings.push(char, `${char}${char}`, `${char}"`);
|
|
}
|
|
|
|
// Test 100 strings at a time.
|
|
const tests = [[]];
|
|
for (const string of strings) {
|
|
if (tests[tests.length - 1].length >= 100) {
|
|
tests.push([]);
|
|
}
|
|
tests[tests.length - 1].push(string);
|
|
}
|
|
|
|
|
|
describe("create/escape_arg", function() {
|
|
it("should correctly escape strings when args are forwarded", async function() {
|
|
const ext = process.platform === "win32" ? ".cmd" : ".sh";
|
|
for (const args of tests) {
|
|
assert.deepEqual(
|
|
await exec(`.${path.sep}forward_args${ext}`, ["echo_args.js", ...args]),
|
|
{ stdout: args, stderr: "" }
|
|
);
|
|
}
|
|
});
|
|
it("should correctly escape strings when invoking node directly", async function() {
|
|
for (const args of tests) {
|
|
assert.deepEqual(
|
|
await exec("node", ["echo_args.js", ...args]),
|
|
{ stdout: args, stderr: "" }
|
|
);
|
|
}
|
|
});
|
|
});
|