From 236ad90a8424eb544e5cec27fbdc016d7d30be80 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 28 Oct 2015 23:06:40 +0100 Subject: [PATCH] child_process: don't fork bomb ourselves from -e Remove the `-e` argument from process.execArgv in child_process.fork() to keep `node -e 'require("child_process").fork("empty.js")'` from spawning itself recursively. Fixes: https://github.com/nodejs/node/issues/3574 PR-URL: https://github.com/nodejs/node/pull/3575 Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott --- lib/child_process.js | 10 ++++++++++ test/parallel/test-cli-eval.js | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/child_process.js b/lib/child_process.js index 0fe9ca75c7794a..151fb51fc8b0f4 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -32,6 +32,16 @@ exports.fork = function(modulePath /*, args, options*/) { // Prepare arguments for fork: execArgv = options.execArgv || process.execArgv; + + if (execArgv === process.execArgv && process._eval != null) { + const index = execArgv.lastIndexOf(process._eval); + if (index > 0) { + // Remove the -e switch to avoid fork bombing ourselves. + execArgv = execArgv.slice(); + execArgv.splice(index - 1, 2); + } + } + args = execArgv.concat([modulePath], args); // Leave stdin open for the IPC channel. stdout and stderr should be the diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index 10a77c4a67b8a4..15f59c8f37717c 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -8,6 +8,7 @@ if (module.parent) { var common = require('../common'), assert = require('assert'), child = require('child_process'), + path = require('path'), nodejs = '"' + process.execPath + '"'; @@ -75,3 +76,11 @@ child.exec(nodejs + ' --use-strict -p process.execArgv', function(status, stdout, stderr) { assert.equal(stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n"); }); + +// Regression test for https://github.com/nodejs/node/issues/3574 +const emptyFile = path.join(common.fixturesDir, 'empty.js'); +child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`, + function(status, stdout, stderr) { + assert.equal(stdout, ''); + assert.equal(stderr, ''); + });