Skip to content

Commit af8624f

Browse files
legendecasCeres6
authored andcommitted
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: nodejs#48545 Reviewed-By: Colin Ihrig <[email protected]>
1 parent f175d9d commit af8624f

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

lib/internal/process/pre_execution.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
const {
2626
getOptionValue,
2727
refreshOptions,
28+
getEmbedderOptions,
2829
} = require('internal/options');
2930
const { reconnectZeroFillToggle } = require('internal/buffer');
3031
const {
@@ -241,7 +242,7 @@ function setupWarningHandler() {
241242

242243
// https://fetch.spec.whatwg.org/
243244
function setupFetch() {
244-
if (process.config.variables.node_no_browser_globals ||
245+
if (getEmbedderOptions().noBrowserGlobals ||
245246
getOptionValue('--no-experimental-fetch')) {
246247
return;
247248
}
@@ -291,7 +292,7 @@ function setupFetch() {
291292
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
292293
// removed.
293294
function setupWebCrypto() {
294-
if (process.config.variables.node_no_browser_globals ||
295+
if (getEmbedderOptions().noBrowserGlobals ||
295296
getOptionValue('--no-experimental-global-webcrypto')) {
296297
return;
297298
}
@@ -339,7 +340,7 @@ function setupCodeCoverage() {
339340
// TODO(daeyeon): move this to internal/bootstrap/web/* when the CLI flag is
340341
// removed.
341342
function setupCustomEvent() {
342-
if (process.config.variables.node_no_browser_globals ||
343+
if (getEmbedderOptions().noBrowserGlobals ||
343344
getOptionValue('--no-experimental-global-customevent')) {
344345
return;
345346
}

src/node_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,12 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
12431243
Boolean::New(isolate, env->no_global_search_paths()))
12441244
.IsNothing()) return;
12451245

1246+
if (ret->Set(context,
1247+
FIXED_ONE_BYTE_STRING(env->isolate(), "noBrowserGlobals"),
1248+
Boolean::New(isolate, env->no_browser_globals()))
1249+
.IsNothing())
1250+
return;
1251+
12461252
args.GetReturnValue().Set(ret);
12471253
}
12481254

test/cctest/test_environment.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,39 @@ class EnvironmentTest : public EnvironmentTestFixture {
3838
}
3939
};
4040

41+
TEST_F(EnvironmentTest, EnvironmentWithoutBrowserGlobals) {
42+
const v8::HandleScope handle_scope(isolate_);
43+
Argv argv;
44+
Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals};
45+
46+
SetProcessExitHandler(*env, [&](node::Environment* env_, int exit_code) {
47+
EXPECT_EQ(*env, env_);
48+
EXPECT_EQ(exit_code, 0);
49+
node::Stop(*env);
50+
});
51+
52+
node::LoadEnvironment(
53+
*env,
54+
"const assert = require('assert');"
55+
"const path = require('path');"
56+
"const relativeRequire = "
57+
" require('module').createRequire(path.join(process.cwd(), 'stub.js'));"
58+
"const { intrinsics, nodeGlobals } = "
59+
" relativeRequire('./test/common/globals');"
60+
"const items = Object.getOwnPropertyNames(globalThis);"
61+
"const leaks = [];"
62+
"for (const item of items) {"
63+
" if (intrinsics.has(item)) {"
64+
" continue;"
65+
" }"
66+
" if (nodeGlobals.has(item)) {"
67+
" continue;"
68+
" }"
69+
" leaks.push(item);"
70+
"}"
71+
"assert.deepStrictEqual(leaks, []);");
72+
}
73+
4174
TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
4275
const v8::HandleScope handle_scope(isolate_);
4376
Argv argv;

0 commit comments

Comments
 (0)