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

src,lib: handle invalid stdio configuration gracefully #55942

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,13 @@ ChildProcess.prototype.spawn = function(options) {

for (i = 0; i < stdio.length; i++) {
const stream = stdio[i];
// Never trust the user input/runtime
// Refs: https://github.com/nodejs/node/issues/55932
if (!stream) {
process.nextTick(onErrorNT, this, UV_EINVAL);
return UV_EINVAL;
}

if (stream.type === 'ignore') continue;

if (stream.ipc) {
Expand Down
11 changes: 11 additions & 0 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "stream_wrap.h"
#include "util-inl.h"

#include <stdio.h>
#include <climits>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -122,6 +123,16 @@ class ProcessWrap : public HandleWrap {
for (uint32_t i = 0; i < len; i++) {
Local<Object> stdio =
stdios->Get(context, i).ToLocalChecked().As<Object>();

// Never trust the user.
// Refs: https://github.com/nodejs/node/issues/55932
if (!stdio->IsObject()) {
stdio = Object::New(env->isolate());
stdio->Set(context,
env->type_string(),
env->ignore_string()).FromJust();
}

Local<Value> type =
stdio->Get(context, env->type_string()).ToLocalChecked();

Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-child-process-muted-array-proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const assert = require('node:assert');
const { spawn } = require('node:child_process');

// The UNIX version of this test is enough.
if (common.isWindows) {
common.skip('This test is enough for Unix-like systems');
return;
}

// Refs: https://github.com/nodejs/node/issues/55932
Copy link
Member

Choose a reason for hiding this comment

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

In addition to the link, a short description of the issue being tested would be helpful so folks don't have to actually follow the link to see what this is about.

Object.defineProperty(Array.prototype, '0', {
set() {
console.log(123);
},
get() {
return 123;
}
}, { configurable: true, writable: true });

const cp = spawn('ls');

// Can't use common.mustCall() here because array prototype is mutated.
cp.on('error', (err) => {
assert.strictEqual(err.code, 'EINVAL');
});
Loading