Skip to content

Commit 61ed56a

Browse files
committed
lib: be robust when process global is clobbered
Program bugs sometimes happen to overwrite the `process` global object, leading to unpredictable runtime errors. The bug is often hard to track down because the crash usually looks unrelated and happens far away from the bug point. Make core mostly immune to such corruption by ensuring that all core modules close over a direct reference to the process object instead of a dynamic reference.
1 parent aa77b76 commit 61ed56a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/internal/bootstrap_node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
};
505505

506506
NativeModule.wrapper = [
507-
'(function (exports, require, module, __filename, __dirname) { ',
507+
'(function (exports, require, module, __filename, process) { ',
508508
'\n});'
509509
];
510510

@@ -520,7 +520,7 @@
520520
lineOffset: 0,
521521
displayErrors: true
522522
});
523-
fn(this.exports, NativeModule.require, this, this.filename);
523+
fn(this.exports, NativeModule.require, this, this.filename, process);
524524

525525
this.loaded = true;
526526
} finally {

lib/module.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,15 @@ Module._extensions = {};
5454
var modulePaths = [];
5555
Module.globalPaths = [];
5656

57-
Module.wrapper = NativeModule.wrapper;
58-
Module.wrap = NativeModule.wrap;
57+
Module.wrapper = [
58+
'(function (exports, require, module, __filename, __dirname) { ',
59+
'\n});'
60+
];
61+
62+
Module.wrap = function wrap(script) {
63+
return Module.wrapper[0] + script + Module.wrapper[1];
64+
};
65+
5966
Module._debug = util.debuglog('module');
6067

6168
// We use this alias for the preprocessor that filters it out
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-disable no-global-assign */
2+
/* eslint-disable required-modules */
3+
'use strict';
4+
5+
process = null; // Should not bring down program.

0 commit comments

Comments
 (0)