Skip to content
Merged
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: 5 additions & 2 deletions lib/child_process.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -136,7 +136,7 @@ function fork(modulePath, args = [], options){
if (options != null){
validateObject(options, 'options');
}
options ={...options, shell: false };
options ={__proto__: null, ...options, shell: false };
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR, but I wonder if we should use {__proto__: options, shell: false } and let users decide if they want prototype pollution or not.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Wouldn't it make this object affected by subsequent mutations outside?
In fact, I wonder if we should use structuredClone() or at least make copies of object properties like .env to avoid race conditions if multiple fork()s are called with reused options.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make this object affected by subsequent mutations outside?

I guess it depends if we read the properties in the same tick or not (which I expect we do most of the time, but I haven't checked).

In fact, I wonder if we should use structuredClone()

That wouldn't help with inconsistency of our API: sometimes, we dismiss inherited properties, sometimes we don't. But it's clearly not a big deal as I don't think we ever received any bug report regarding that.

or at least make copies of object properties like .env to avoid race conditions if multiple fork()s are called with reused options.

but wouldn't folks expect the same env to be shared if they supply the same object? The fact that no one complains makes me think we're overthinking it 😅

options.execPath = options.execPath || process.execPath;
validateArgumentNullCheck(options.execPath, 'options.execPath');

Expand DownExpand Up@@ -194,7 +194,7 @@ function normalizeExecArgs(command, options, callback){
}

// Make a shallow copy so we don't clobber the user's options object.
options ={...options };
options ={__proto__: null, ...options };
options.shell = typeof options.shell === 'string' ? options.shell : true;

return{
Expand DownExpand Up@@ -327,6 +327,7 @@ function execFile(file, args, options, callback){
({file, args, options, callback } = normalizeExecFileArgs(file, args, options, callback));

options ={
__proto__: null,
encoding: 'utf8',
timeout: 0,
maxBuffer: MAX_BUFFER,
Expand DownExpand Up@@ -701,6 +702,7 @@ function normalizeSpawnArguments(file, args, options){

return{
// Make a shallow copy so we don't clobber the user's options object.
__proto__: null,
...options,
args,
cwd,
Expand DownExpand Up@@ -826,6 +828,7 @@ function spawn(file, args, options){
*/
function spawnSync(file, args, options){
options ={
__proto__: null,
maxBuffer: MAX_BUFFER,
...normalizeSpawnArguments(file, args, options),
};
Expand Down
59 changes: 59 additions & 0 deletions test/parallel/test-child-process-prototype-tampering.mjs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
import*ascommonfrom'../common/index.mjs';
import*asfixturesfrom'../common/fixtures.mjs';
import{EOL}from'node:os';
import{strictEqual}from'node:assert';
importcpfrom'node:child_process';

// TODO(LiviaMedeiros): test on different platforms
if(!common.isLinux)
common.skip();

constexpectedCWD=process.cwd();
constexpectedUID=process.getuid();

for(consttamperedCwdof['','/tmp','/not/existing/malicious/path',42n]){
Object.prototype.cwd=tamperedCwd;

cp.exec('pwd',common.mustSucceed((out)=>{
strictEqual(`${out}`,`${expectedCWD}${EOL}`);
}));
strictEqual(`${cp.execSync('pwd')}`,`${expectedCWD}${EOL}`);
cp.execFile('pwd',common.mustSucceed((out)=>{
strictEqual(`${out}`,`${expectedCWD}${EOL}`);
}));
strictEqual(`${cp.execFileSync('pwd')}`,`${expectedCWD}${EOL}`);
cp.spawn('pwd').stdout.on('data',common.mustCall((out)=>{
strictEqual(`${out}`,`${expectedCWD}${EOL}`);
}));
strictEqual(`${cp.spawnSync('pwd').stdout}`,`${expectedCWD}${EOL}`);

deleteObject.prototype.cwd;
}

for(consttamperedUIDof[0,1,999,1000,0n,'gwak']){
Object.prototype.uid=tamperedUID;

cp.exec('id -u',common.mustSucceed((out)=>{
strictEqual(`${out}`,`${expectedUID}${EOL}`);
}));
strictEqual(`${cp.execSync('id -u')}`,`${expectedUID}${EOL}`);
cp.execFile('id',['-u'],common.mustSucceed((out)=>{
strictEqual(`${out}`,`${expectedUID}${EOL}`);
}));
strictEqual(`${cp.execFileSync('id',['-u'])}`,`${expectedUID}${EOL}`);
cp.spawn('id',['-u']).stdout.on('data',common.mustCall((out)=>{
strictEqual(`${out}`,`${expectedUID}${EOL}`);
}));
strictEqual(`${cp.spawnSync('id',['-u']).stdout}`,`${expectedUID}${EOL}`);

deleteObject.prototype.uid;
}

{
Object.prototype.execPath='/not/existing/malicious/path';

// Does not throw ENOENT
cp.fork(fixtures.path('empty.js'));

deleteObject.prototype.execPath;
}