From e2d0195dcf2591b4c3a3ad3c20ec214a5dde7def Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Wed, 5 Jul 2023 15:02:59 +0800 Subject: [PATCH] bootstrap: hide experimental web globals with flag kNoBrowserGlobals Do not install experimental web globals when the environment is initialized with embedder flag `node::EnvironmentFlags::kNoBrowserGlobals`. PR-URL: https://github.com/nodejs/node/pull/48545 Reviewed-By: Colin Ihrig --- lib/internal/process/pre_execution.js | 7 +++--- src/node_options.cc | 6 +++++ test/cctest/test_environment.cc | 33 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 5f8569b1531c92..cab2eacb8c0723 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -17,6 +17,7 @@ const { const { getOptionValue, refreshOptions, + getEmbedderOptions, } = require('internal/options'); const { reconnectZeroFillToggle } = require('internal/buffer'); const { @@ -233,7 +234,7 @@ function setupWarningHandler() { // https://fetch.spec.whatwg.org/ function setupFetch() { - if (process.config.variables.node_no_browser_globals || + if (getEmbedderOptions().noBrowserGlobals || getOptionValue('--no-experimental-fetch')) { return; } @@ -283,7 +284,7 @@ function setupFetch() { // TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is // removed. function setupWebCrypto() { - if (process.config.variables.node_no_browser_globals || + if (getEmbedderOptions().noBrowserGlobals || getOptionValue('--no-experimental-global-webcrypto')) { return; } @@ -331,7 +332,7 @@ function setupCodeCoverage() { // TODO(daeyeon): move this to internal/bootstrap/web/* when the CLI flag is // removed. function setupCustomEvent() { - if (process.config.variables.node_no_browser_globals || + if (getEmbedderOptions().noBrowserGlobals || getOptionValue('--no-experimental-global-customevent')) { return; } diff --git a/src/node_options.cc b/src/node_options.cc index fc6050877eb8b4..0e7c38722dd860 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -1239,6 +1239,12 @@ void GetEmbedderOptions(const FunctionCallbackInfo& args) { Boolean::New(isolate, env->no_global_search_paths())) .IsNothing()) return; + if (ret->Set(context, + FIXED_ONE_BYTE_STRING(env->isolate(), "noBrowserGlobals"), + Boolean::New(isolate, env->no_browser_globals())) + .IsNothing()) + return; + args.GetReturnValue().Set(ret); } diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index 09dcb1dccc1b28..2e747c7be58922 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -38,6 +38,39 @@ class EnvironmentTest : public EnvironmentTestFixture { } }; +TEST_F(EnvironmentTest, EnvironmentWithoutBrowserGlobals) { + const v8::HandleScope handle_scope(isolate_); + Argv argv; + Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals}; + + SetProcessExitHandler(*env, [&](node::Environment* env_, int exit_code) { + EXPECT_EQ(*env, env_); + EXPECT_EQ(exit_code, 0); + node::Stop(*env); + }); + + node::LoadEnvironment( + *env, + "const assert = require('assert');" + "const path = require('path');" + "const relativeRequire = " + " require('module').createRequire(path.join(process.cwd(), 'stub.js'));" + "const { intrinsics, nodeGlobals } = " + " relativeRequire('./test/common/globals');" + "const items = Object.getOwnPropertyNames(globalThis);" + "const leaks = [];" + "for (const item of items) {" + " if (intrinsics.has(item)) {" + " continue;" + " }" + " if (nodeGlobals.has(item)) {" + " continue;" + " }" + " leaks.push(item);" + "}" + "assert.deepStrictEqual(leaks, []);"); +} + TEST_F(EnvironmentTest, EnvironmentWithESMLoader) { const v8::HandleScope handle_scope(isolate_); Argv argv;