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

child_process: add safety checks on stdio access #3799

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
81 changes: 47 additions & 34 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,22 @@ exports.execFile = function(file /*, args, options, callback*/) {

function errorhandler(e) {
ex = e;
child.stdout.destroy();
child.stderr.destroy();

if (child.stdout)
child.stdout.destroy();

if (child.stderr)
child.stderr.destroy();

exithandler();
}

function kill() {
child.stdout.destroy();
child.stderr.destroy();
if (child.stdout)
child.stdout.destroy();

if (child.stderr)
child.stderr.destroy();

killed = true;
try {
Expand All @@ -247,37 +255,42 @@ exports.execFile = function(file /*, args, options, callback*/) {
}, options.timeout);
}

child.stdout.addListener('data', function(chunk) {
stdoutLen += chunk.length;

if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded');
kill();
} else {
if (!encoding)
_stdout.push(chunk);
else
_stdout += chunk;
}
});

child.stderr.addListener('data', function(chunk) {
stderrLen += chunk.length;

if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded');
kill();
} else {
if (!encoding)
_stderr.push(chunk);
else
_stderr += chunk;
}
});
if (child.stdout) {
if (encoding)
child.stdout.setEncoding(encoding);
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a behavior change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. I just moved the code up a few lines to avoid another if (child.stdout) check.

Copy link
Contributor

Choose a reason for hiding this comment

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

ahhh sorry, missed that.


child.stdout.addListener('data', function(chunk) {
stdoutLen += chunk.length;

if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded');
kill();
} else {
if (!encoding)
_stdout.push(chunk);
else
_stdout += chunk;
}
});
}

if (encoding) {
child.stderr.setEncoding(encoding);
child.stdout.setEncoding(encoding);
if (child.stderr) {
if (encoding)
child.stderr.setEncoding(encoding);

child.stderr.addListener('data', function(chunk) {
stderrLen += chunk.length;

if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded');
kill();
} else {
if (!encoding)
_stderr.push(chunk);
else
_stderr += chunk;
}
});
}

child.addListener('close', exithandler);
Expand Down