From 1065fb17b04e2bf4fcc7be80c66b9f8ddf633566 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sun, 25 Jun 2023 16:05:58 +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`. --- 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 678bd05164f99a..e1b90998d3abcd 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -14,6 +14,7 @@ const { const { getOptionValue, refreshOptions, + getEmbedderOptions, } = require('internal/options'); const { reconnectZeroFillToggle } = require('internal/buffer'); const { @@ -220,7 +221,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; } @@ -270,7 +271,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; } @@ -318,7 +319,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 547c8ddbffe243..298234afd430c0 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;