Skip to content

Commit c6e34bc

Browse files
committed
lib: enable global WebCrypto by default
Enables `--experimental-global-webcrypto` by default, and ensures that the classic `node:crypto` core module is still available in `--eval` or `--print` contexts.
1 parent b39dabe commit c6e34bc

File tree

13 files changed

+74
-25
lines changed

13 files changed

+74
-25
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ module.exports = {
317317
'node-core/no-duplicate-requires': 'error',
318318
},
319319
globals: {
320+
crypto: 'readable',
320321
Crypto: 'readable',
321322
CryptoKey: 'readable',
322323
fetch: 'readable',

doc/api/cli.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ effort to report stack traces relative to the original source file.
280280
Overriding `Error.prepareStackTrace` prevents `--enable-source-maps` from
281281
modifying the stack trace.
282282

283-
### `--experimental-global-webcrypto`
284-
285-
<!-- YAML
286-
added: v17.6.0
287-
-->
288-
289-
Expose the [Web Crypto API][] on the global scope.
290-
291283
### `--experimental-import-meta-resolve`
292284

293285
<!-- YAML
@@ -333,6 +325,14 @@ added: REPLACEME
333325

334326
Disable experimental support for the [Fetch API][].
335327

328+
### `--no-experimental-global-webcrypto`
329+
330+
<!-- YAML
331+
added: REPLACEME
332+
-->
333+
334+
Disable exposition of [Web Crypto API][] on the global scope.
335+
336336
### `--no-experimental-repl-await`
337337

338338
<!-- YAML
@@ -1587,7 +1587,6 @@ Node.js options that are allowed are:
15871587
* `--enable-fips`
15881588
* `--enable-source-maps`
15891589
* `--experimental-abortcontroller`
1590-
* `--experimental-global-webcrypto`
15911590
* `--experimental-import-meta-resolve`
15921591
* `--experimental-json-modules`
15931592
* `--experimental-loader`
@@ -1617,6 +1616,7 @@ Node.js options that are allowed are:
16171616
* `--no-addons`
16181617
* `--no-deprecation`
16191618
* `--no-experimental-fetch`
1619+
* `--no-experimental-global-webcrypto`
16201620
* `--no-experimental-repl-await`
16211621
* `--no-extra-info-on-fatal-exception`
16221622
* `--no-force-async-hooks-checks`

doc/api/globals.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,14 @@ Used to print to stdout and stderr. See the [`console`][] section.
311311

312312
<!-- YAML
313313
added: v17.6.0
314+
changes:
315+
- version: REPLACEME
316+
pr-url: https://github.com/nodejs/node/pull/42083
317+
description: No longer behind `--experimental-global-webcrypto` CLI flag.
314318
-->
315319

316-
> Stability: 1 - Experimental. Enable this API with the
317-
> [`--experimental-global-webcrypto`][] CLI flag.
320+
> Stability: 1 - Experimental. Disable this API with the
321+
> [`--no-experimental-global-webcrypto`][] CLI flag.
318322
319323
A browser-compatible implementation of {Crypto}. This global is available
320324
only if the Node.js binary was compiled with including support for the
@@ -324,21 +328,29 @@ only if the Node.js binary was compiled with including support for the
324328

325329
<!-- YAML
326330
added: v17.6.0
331+
changes:
332+
- version: REPLACEME
333+
pr-url: https://github.com/nodejs/node/pull/42083
334+
description: No longer behind `--experimental-global-webcrypto` CLI flag.
327335
-->
328336

329-
> Stability: 1 - Experimental. Enable this API with the
330-
> [`--experimental-global-webcrypto`][] CLI flag.
337+
> Stability: 1 - Experimental. Disable this API with the
338+
> [`--no-experimental-global-webcrypto`][] CLI flag.
331339
332340
A browser-compatible implementation of the [Web Crypto API][].
333341

334342
## `CryptoKey`
335343

336344
<!-- YAML
337345
added: v17.6.0
346+
changes:
347+
- version: REPLACEME
348+
pr-url: https://github.com/nodejs/node/pull/42083
349+
description: No longer behind `--experimental-global-webcrypto` CLI flag.
338350
-->
339351

340-
> Stability: 1 - Experimental. Enable this API with the
341-
> [`--experimental-global-webcrypto`][] CLI flag.
352+
> Stability: 1 - Experimental. Disable this API with the
353+
> [`--no-experimental-global-webcrypto`][] CLI flag.
342354
343355
A browser-compatible implementation of {CryptoKey}. This global is available
344356
only if the Node.js binary was compiled with including support for the
@@ -586,10 +598,14 @@ The WHATWG [`structuredClone`][] method.
586598

587599
<!-- YAML
588600
added: v17.6.0
601+
changes:
602+
- version: REPLACEME
603+
pr-url: https://github.com/nodejs/node/pull/42083
604+
description: No longer behind `--experimental-global-webcrypto` CLI flag.
589605
-->
590606

591-
> Stability: 1 - Experimental. Enable this API with the
592-
> [`--experimental-global-webcrypto`][] CLI flag.
607+
> Stability: 1 - Experimental. Disable this API with the
608+
> [`--no-experimental-global-webcrypto`][] CLI flag.
593609
594610
A browser-compatible implementation of {SubtleCrypto}. This global is available
595611
only if the Node.js binary was compiled with including support for the
@@ -660,8 +676,8 @@ The object that acts as the namespace for all W3C
660676
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
661677

662678
[Web Crypto API]: webcrypto.md
663-
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
664679
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
680+
[`--no-experimental-global-webcrypto`]: cli.md#--no-experimental-global-webcrypto
665681
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
666682
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
667683
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api

doc/node.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ Requires Node.js to be built with
139139
.It Fl -enable-source-maps
140140
Enable Source Map V3 support for stack traces.
141141
.
142-
.It Fl -experimental-global-webcrypto
143-
Expose the Web Crypto API on the global scope.
144-
.
145142
.It Fl -experimental-import-meta-resolve
146143
Enable experimental ES modules support for import.meta.resolve().
147144
.
@@ -159,6 +156,9 @@ Use the specified file as a security policy.
159156
.It Fl -no-experimental-fetch
160157
Disable experimental support for the Fetch API.
161158
.
159+
.It Fl -no-experimental-global-webcrypto
160+
Disable exposition of the Web Crypto API on the global scope.
161+
.
162162
.It Fl -no-experimental-repl-await
163163
Disable top-level await keyword support in REPL.
164164
.

lib/internal/bootstrap/pre_execution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function setupFetch() {
199199
// removed.
200200
function setupWebCrypto() {
201201
if (process.config.variables.node_no_browser_globals ||
202-
!getOptionValue('--experimental-global-webcrypto')) {
202+
getOptionValue('--no-experimental-global-webcrypto')) {
203203
return;
204204
}
205205

lib/internal/main/eval_string.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1616
const { getOptionValue } = require('internal/options');
1717

1818
prepareMainThreadExecution();
19+
// Delete Webcrypto from the global scope for backward compatibility.
20+
delete globalThis.crypto;
1921
addBuiltinLibsToObject(globalThis, '<eval>');
2022
markBootstrapComplete();
2123

src/node_options.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
323323
AddOption("--experimental-global-webcrypto",
324324
"expose experimental Web Crypto API on the global scope",
325325
&EnvironmentOptions::experimental_global_web_crypto,
326-
kAllowedInEnvironment);
326+
kAllowedInEnvironment,
327+
true);
327328
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
328329
AddOption("--experimental-loader",
329330
"use the specified module as a custom loader",

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class EnvironmentOptions : public Options {
108108
std::string dns_result_order;
109109
bool enable_source_maps = false;
110110
bool experimental_fetch = true;
111-
bool experimental_global_web_crypto = false;
111+
bool experimental_global_web_crypto = true;
112112
bool experimental_https_modules = false;
113113
std::string experimental_specifier_resolution;
114114
bool experimental_wasm_modules = false;

test/parallel/test-bootstrap-modules.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const expectedModules = new Set([
1616
'Internal Binding constants',
1717
'Internal Binding contextify',
1818
'Internal Binding credentials',
19+
'Internal Binding crypto',
1920
'Internal Binding errors',
2021
'Internal Binding fs_dir',
2122
'Internal Binding fs_event_wrap',
@@ -55,6 +56,12 @@ const expectedModules = new Set([
5556
'NativeModule internal/console/constructor',
5657
'NativeModule internal/console/global',
5758
'NativeModule internal/constants',
59+
'NativeModule internal/crypto/hash',
60+
'NativeModule internal/crypto/hashnames',
61+
'NativeModule internal/crypto/keys',
62+
'NativeModule internal/crypto/random',
63+
'NativeModule internal/crypto/util',
64+
'NativeModule internal/crypto/webcrypto',
5865
'NativeModule internal/dtrace',
5966
'NativeModule internal/encoding',
6067
'NativeModule internal/errors',
@@ -120,6 +127,7 @@ const expectedModules = new Set([
120127
'NativeModule internal/streams/duplex',
121128
'NativeModule internal/streams/end-of-stream',
122129
'NativeModule internal/streams/from',
130+
'NativeModule internal/streams/lazy_transform',
123131
'NativeModule internal/streams/legacy',
124132
'NativeModule internal/streams/operators',
125133
'NativeModule internal/streams/passthrough',

test/parallel/test-cli-eval.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,13 @@ child.exec(
288288
common.mustSucceed((stdout) => {
289289
assert.strictEqual(stdout, '.mjs file\n');
290290
}));
291+
292+
if (common.hasCrypto) {
293+
// Assert that calls to crypto utils work without require.
294+
child.exec(
295+
`${nodejs} ` +
296+
'-e "console.log(crypto.randomBytes(16).toString(\'hex\'))"',
297+
common.mustSucceed((stdout) => {
298+
assert.match(stdout, /[0-9a-f]{32}/i);
299+
}));
300+
}

0 commit comments

Comments
 (0)