From 9843177197320e0d0ed852592c811e2da6f5a314 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 6 Dec 2018 00:17:44 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/20565 PR-URL: https://github.com/nodejs/node/pull/24874 Refs: https://github.com/nodejs/node/issues/20565 Reviewed-By: Guy Bedford Reviewed-By: Gus Caplan Reviewed-By: Anna Henningsen Reviewed-By: Anto Aravinth Reviewed-By: Joyee Cheung Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat --- lib/internal/bootstrap/node.js | 14 ++++++++++++-- test/common/index.js | 2 -- test/parallel/test-global.js | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index b537e62fe99d83..31ab0f986c3fc5 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -408,7 +408,12 @@ function setupGlobalVariables() { enumerable: false, configurable: true }); - global.process = process; + Object.defineProperty(global, 'process', { + value: process, + enumerable: false, + writable: true, + configurable: true + }); const util = NativeModule.require('util'); function makeGetter(name) { @@ -445,7 +450,12 @@ function setupGlobalVariables() { // and exposes it on `internal/buffer`. NativeModule.require('internal/buffer'); - global.Buffer = NativeModule.require('buffer').Buffer; + Object.defineProperty(global, 'Buffer', { + value: NativeModule.require('buffer').Buffer, + enumerable: false, + writable: true, + configurable: true + }); process.domain = null; process._exiting = false; } diff --git a/test/common/index.js b/test/common/index.js index 3d7a0187ee5cd8..cd0696c6773b51 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -217,12 +217,10 @@ function platformTimeout(ms) { } let knownGlobals = [ - Buffer, clearImmediate, clearInterval, clearTimeout, global, - process, setImmediate, setInterval, setTimeout diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index b139a6287488d2..e8112531fc7f2c 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -27,6 +27,41 @@ const common = require('../common'); const fixtures = require('../common/fixtures'); const assert = require('assert'); +const { builtinModules } = require('module'); + +// Load all modules to actually cover most code parts. +builtinModules.forEach((moduleName) => { + if (!moduleName.includes('/')) { + try { + // This could throw for e.g., crypto if the binary is not compiled + // accordingly. + require(moduleName); + } catch {} + } +}); + +{ + const expected = [ + 'global', + 'clearImmediate', + 'clearInterval', + 'clearTimeout', + 'setImmediate', + 'setInterval', + 'setTimeout' + ]; + if (global.DTRACE_HTTP_SERVER_RESPONSE) { + expected.unshift( + 'DTRACE_HTTP_SERVER_RESPONSE', + 'DTRACE_HTTP_SERVER_REQUEST', + 'DTRACE_HTTP_CLIENT_RESPONSE', + 'DTRACE_HTTP_CLIENT_REQUEST', + 'DTRACE_NET_STREAM_END', + 'DTRACE_NET_SERVER_CONNECTION' + ); + } + assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected)); +} common.allowGlobals('bar', 'foo');