Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use env variable to pass value containing user controlled value #49134

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions test/abort/test-abort-fatal-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@ if (common.isWindows)
const assert = require('assert');
const exec = require('child_process').exec;

let cmdline = `ulimit -c 0; ${process.execPath}`;
cmdline += ' --max-old-space-size=16 --max-semi-space-size=4';
cmdline += ' -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"';
const cmdline =
'ulimit -c 0; "$NODE" --max-old-space-size=16 --max-semi-space-size=4' +
' -e "a = []; for (i = 0; i < 1e9; i++) { a.push({}) }"';

exec(cmdline, function(err, stdout, stderr) {
if (!err) {
console.log(stdout);
console.log(stderr);
assert(false, 'this test should fail');
exec(cmdline, { env: { NODE: process.execPath } }, common.mustCall((err, stdout, stderr) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to extend env otherwise the child process will fail to run in , e.g. the shared library CI configurations.

if (err?.code !== 134 && err?.signal !== 'SIGABRT') {
console.log({ err, stdout, stderr });
assert.fail(err?.message);
}

if (err.code !== 134 && err.signal !== 'SIGABRT') {
console.log(stdout);
console.log(stderr);
console.log(err);
assert(false, err);
}
});
}));
3 changes: 2 additions & 1 deletion test/async-hooks/test-callback-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ assert.ok(!arg);
'--abort-on-uncaught-exception', __filename, 'test_callback_abort' ];
const options = { encoding: 'utf8' };
if (!common.isWindows) {
program = `ulimit -c 0 && exec ${program} ${args.join(' ')}`;
program = `ulimit -c 0 && exec "$NODE" ${args[0]} "$FILE" ${args[2]}`;
args = [];
options.shell = true;
options.env = { NODE: process.execPath, FILE: __filename };
}
const child = spawnSync(program, args, options);
if (common.isWindows) {
Expand Down
15 changes: 12 additions & 3 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ function childShouldThrowAndAbort() {
// continuous testing and developers' machines
testCmd += 'ulimit -c 0 && ';
}
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
testCmd += `"${process.argv[1]}" child`;
const child = exec(testCmd);
testCmd += '"$NODE" --abort-on-uncaught-exception ';
testCmd += '"$FILE" child';
const child = exec(testCmd, { env: { NODE: process.argv[0], FILE: process.argv[1] } });
child.on('exit', function onExit(exitCode, signal) {
const errMsg = 'Test should have aborted ' +
`but instead exited with exit code ${exitCode}` +
Expand Down Expand Up @@ -1063,6 +1063,15 @@ const common = {
get checkoutEOL() {
return fs.readFileSync(__filename).includes('\r\n') ? '\r\n' : '\n';
},

get isInsideCWDWithUnusualChars() {
const cwd = process.cwd();
return cwd.includes('%') ||
(!isWindows && cwd.includes('\\')) ||
cwd.includes('\n') ||
cwd.includes('\r') ||
cwd.includes('\t');
},
};

const validProperties = new Set(Object.keys(common));
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-bad-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ ChildProcess.prototype.spawn = function() {
};

function createChild(options, callback) {
const cmd = `"${process.execPath}" "${__filename}" child`;
const cmd = '"$NODE" "$FILE" child';
options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } };

return cp.exec(cmd, options, common.mustCall(callback));
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-exec-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ if (process.argv[2] === 'child') {
const expectedStdout = `${stdoutData}\n`;
const expectedStderr = `${stderrData}\n`;
function run(options, callback) {
const cmd = `"${process.execPath}" "${__filename}" child`;
const cmd = '"$NODE" "$FILE" child';
options = { ...options, env: { ...options.env, NODE: process.execPath, FILE: __filename } };

cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => {
callback(stdout, stderr);
Expand Down
41 changes: 22 additions & 19 deletions test/parallel/test-child-process-exec-maxbuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ function runChecks(err, stdio, streamName, expected) {
assert.deepStrictEqual(stdio[streamName], expected);
}

const env = { NODE: process.execPath };

// default value
{
const cmd =
`"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"';

cp.exec(cmd, common.mustCall((err) => {
cp.exec(cmd, { env }, common.mustCall((err) => {
assert(err instanceof RangeError);
assert.strictEqual(err.message, 'stdout maxBuffer length exceeded');
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
Expand All @@ -25,17 +27,17 @@ function runChecks(err, stdio, streamName, expected) {
// default value
{
const cmd =
`${process.execPath} -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"';

cp.exec(cmd, common.mustSucceed((stdout, stderr) => {
cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
assert.strictEqual(stderr, '');
}));
}

{
const cmd = `"${process.execPath}" -e "console.log('hello world');"`;
const options = { maxBuffer: Infinity };
const cmd = '"$NODE" -e "console.log(\'hello world\');"';
const options = { env, maxBuffer: Infinity };

cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout.trim(), 'hello world');
Expand All @@ -58,10 +60,11 @@ function runChecks(err, stdio, streamName, expected) {
// default value
{
const cmd =
`"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"';

cp.exec(
cmd,
{ env },
common.mustCall((err, stdout, stderr) => {
runChecks(
err,
Expand All @@ -76,9 +79,9 @@ function runChecks(err, stdio, streamName, expected) {
// default value
{
const cmd =
`"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"';

cp.exec(cmd, common.mustSucceed((stdout, stderr) => {
cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
assert.strictEqual(stderr, '');
}));
Expand All @@ -87,35 +90,35 @@ function runChecks(err, stdio, streamName, expected) {
const unicode = '中文测试'; // length = 4, byte length = 12

{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
const cmd = `"$NODE" -e "console.log('${unicode}');"`;

cp.exec(
cmd,
{ maxBuffer: 10 },
{ env, maxBuffer: 10 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
})
);
}

{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
const cmd = `"$NODE" -e "console.error('${unicode}');"`;

cp.exec(
cmd,
{ maxBuffer: 3 },
{ env, maxBuffer: 3 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stderr', '中文测');
})
);
}

{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
const cmd = `"$NODE" -e "console.log('${unicode}');"`;

const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 10 },
{ encoding: null, env, maxBuffer: 10 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
})
Expand All @@ -125,11 +128,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12
}

{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
const cmd = `"$NODE" -e "console.error('${unicode}');"`;

const child = cp.exec(
cmd,
{ encoding: null, maxBuffer: 3 },
{ encoding: null, env, maxBuffer: 3 },
common.mustCall((err, stdout, stderr) => {
runChecks(err, { stdout, stderr }, 'stderr', '中文测');
})
Expand All @@ -139,11 +142,11 @@ const unicode = '中文测试'; // length = 4, byte length = 12
}

{
const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
const cmd = `"$NODE" -e "console.error('${unicode}');"`;

cp.exec(
cmd,
{ encoding: null, maxBuffer: 5 },
{ encoding: null, env, maxBuffer: 5 },
common.mustCall((err, stdout, stderr) => {
const buf = Buffer.from(unicode).slice(0, 5);
runChecks(err, { stdout, stderr }, 'stderr', buf);
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-child-process-exec-std-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ if (process.argv[2] === 'child') {
console.log(stdoutData);
console.error(stderrData);
} else {
const cmd = `"${process.execPath}" "${__filename}" child`;
const child = cp.exec(cmd, common.mustSucceed((stdout, stderr) => {
const cmd = '"$NODE" "$FILE" child';
const env = { NODE: process.execPath, FILE: __filename };
const child = cp.exec(cmd, { env }, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout, expectedStdout);
assert.strictEqual(stderr, expectedStderr);
}));
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-exec-timeout-expire.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ if (process.argv[2] === 'child') {
return;
}

const cmd = `"${process.execPath}" "${__filename}" child`;
const cmd = '"$NODE" "$FILE" child';

cp.exec(cmd, {
env: { NODE: process.execPath, FILE: __filename },
timeout: kExpiringParentTimer,
}, common.mustCall((err, stdout, stderr) => {
console.log('[stdout]', stdout.trim());
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-exec-timeout-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ if (process.argv[2] === 'child') {
return;
}

const cmd = `"${process.execPath}" "${__filename}" child`;
const cmd = '"$NODE" "$FILE" child';

// Test with a different kill signal.
cp.exec(cmd, {
env: { NODE: process.execPath, FILE: __filename },
timeout: kExpiringParentTimer,
killSignal: 'SIGKILL'
}, common.mustCall((err, stdout, stderr) => {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-child-process-exec-timeout-not-expired.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ if (process.argv[2] === 'child') {
return;
}

const cmd = `"${process.execPath}" "${__filename}" child`;
const cmd = '"$NODE" "$FILE" child';

cp.exec(cmd, {
env: { NODE: process.execPath, FILE: __filename },
timeout: kTimeoutNotSupposedToExpire
}, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout.trim(), 'child stdout');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const os = require('os');

const fixture = fixtures.path('exit.js');
const echoFixture = fixtures.path('echo.js');
const execOpts = { encoding: 'utf8', shell: true };
const execOpts = { encoding: 'utf8', shell: true, env: { NODE: process.execPath, FIXTURE: fixture } };

{
execFile(
Expand Down Expand Up @@ -46,7 +46,7 @@ const execOpts = { encoding: 'utf8', shell: true };

{
// Verify the shell option works properly
execFile(process.execPath, [fixture, 0], execOpts, common.mustSucceed());
execFile('"$NODE"', ['"$FIXTURE"', 0], execOpts, common.mustSucceed());
}

{
Expand Down
12 changes: 7 additions & 5 deletions test/parallel/test-child-process-execsync-maxbuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const args = [
// Verify that an error is returned if maxBuffer is surpassed.
{
assert.throws(() => {
execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 });
execSync(`"$NODE" ${args.join(' ')}`, { env: { NODE: process.execPath }, maxBuffer: 1 });
}, (e) => {
assert.ok(e, 'maxBuffer should error');
assert.strictEqual(e.code, 'ENOBUFS');
Expand All @@ -33,8 +33,8 @@ const args = [
// Verify that a maxBuffer size of Infinity works.
{
const ret = execSync(
`"${process.execPath}" ${args.join(' ')}`,
{ maxBuffer: Infinity }
`"$NODE" ${args.join(' ')}`,
{ env: { NODE: process.execPath }, maxBuffer: Infinity },
);

assert.deepStrictEqual(ret, msgOutBuf);
Expand All @@ -44,7 +44,8 @@ const args = [
{
assert.throws(() => {
execSync(
`"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024))"',
{ env: { NODE: process.execPath } },
);
}, (e) => {
assert.ok(e, 'maxBuffer should error');
Expand All @@ -57,7 +58,8 @@ const args = [
// Default maxBuffer size is 1024 * 1024.
{
const ret = execSync(
`"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`
'"$NODE" -e "console.log(\'a\'.repeat(1024 * 1024 - 1))"',
{ env: { NODE: process.execPath } },
);

assert.deepStrictEqual(
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const exec = promisify(child_process.exec);
const execFile = promisify(child_process.execFile);

{
const promise = exec(`${process.execPath} -p 42`);
const promise = exec('"$NODE" -p 42', { env: { NODE: process.execPath } });

assert(promise.child instanceof child_process.ChildProcess);
promise.then(common.mustCall((obj) => {
Expand Down Expand Up @@ -45,7 +45,7 @@ const execFile = promisify(child_process.execFile);
const failingCodeWithStdoutErr =
'console.log(42);console.error(43);process.exit(1)';
{
exec(`${process.execPath} -e "${failingCodeWithStdoutErr}"`)
exec(`"$NODE" -e "${failingCodeWithStdoutErr}"`, { env: { NODE: process.execPath } })
.catch(common.mustCall((err) => {
assert.strictEqual(err.code, 1);
assert.strictEqual(err.stdout, '42\n');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-spawn-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ command.on('close', common.mustCall((code, signal) => {
}));

// Verify that the environment is properly inherited
const env = cp.spawn(`"${process.execPath}" -pe process.env.BAZ`, {
env: { ...process.env, BAZ: 'buzz' },
const env = cp.spawn('"$NODE" -pe process.env.BAZ', {
env: { ...process.env, NODE: process.execPath, BAZ: 'buzz' },
encoding: 'utf8',
shell: true
});
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-spawnsync-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const command = cp.spawnSync(cmd, { shell: true });
assert.strictEqual(command.stdout.toString().trim(), 'bar');

// Verify that the environment is properly inherited
const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, {
env: { ...process.env, BAZ: 'buzz' },
const env = cp.spawnSync('"$NODE" -pe process.env.BAZ', {
env: { ...process.env, NODE: process.execPath, BAZ: 'buzz' },
shell: true
});

Expand Down
Loading
Loading