Skip to content

Commit c992639

Browse files
committed
bootstrap: make Buffer and process non-enumerable
This makes sure these two properties are non-enumerable. This aligns them with all other globals that are not enumerable by spec. Refs: #20565 PR-URL: #24874 Refs: #20565 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent a3801e9 commit c992639

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

lib/internal/bootstrap/node.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,12 @@ function setupGlobalVariables() {
408408
enumerable: false,
409409
configurable: true
410410
});
411-
global.process = process;
411+
Object.defineProperty(global, 'process', {
412+
value: process,
413+
enumerable: false,
414+
writable: true,
415+
configurable: true
416+
});
412417
const util = NativeModule.require('util');
413418

414419
function makeGetter(name) {
@@ -445,7 +450,12 @@ function setupGlobalVariables() {
445450
// and exposes it on `internal/buffer`.
446451
NativeModule.require('internal/buffer');
447452

448-
global.Buffer = NativeModule.require('buffer').Buffer;
453+
Object.defineProperty(global, 'Buffer', {
454+
value: NativeModule.require('buffer').Buffer,
455+
enumerable: false,
456+
writable: true,
457+
configurable: true
458+
});
449459
process.domain = null;
450460
process._exiting = false;
451461
}

test/common/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,10 @@ function platformTimeout(ms) {
217217
}
218218

219219
let knownGlobals = [
220-
Buffer,
221220
clearImmediate,
222221
clearInterval,
223222
clearTimeout,
224223
global,
225-
process,
226224
setImmediate,
227225
setInterval,
228226
setTimeout

test/parallel/test-global.js

+35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ const common = require('../common');
2727
const fixtures = require('../common/fixtures');
2828

2929
const assert = require('assert');
30+
const { builtinModules } = require('module');
31+
32+
// Load all modules to actually cover most code parts.
33+
builtinModules.forEach((moduleName) => {
34+
if (!moduleName.includes('/')) {
35+
try {
36+
// This could throw for e.g., crypto if the binary is not compiled
37+
// accordingly.
38+
require(moduleName);
39+
} catch {}
40+
}
41+
});
42+
43+
{
44+
const expected = [
45+
'global',
46+
'clearImmediate',
47+
'clearInterval',
48+
'clearTimeout',
49+
'setImmediate',
50+
'setInterval',
51+
'setTimeout'
52+
];
53+
if (global.DTRACE_HTTP_SERVER_RESPONSE) {
54+
expected.unshift(
55+
'DTRACE_HTTP_SERVER_RESPONSE',
56+
'DTRACE_HTTP_SERVER_REQUEST',
57+
'DTRACE_HTTP_CLIENT_RESPONSE',
58+
'DTRACE_HTTP_CLIENT_REQUEST',
59+
'DTRACE_NET_STREAM_END',
60+
'DTRACE_NET_SERVER_CONNECTION'
61+
);
62+
}
63+
assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
64+
}
3065

3166
common.allowGlobals('bar', 'foo');
3267

0 commit comments

Comments
 (0)