Skip to content

Commit c9747f1

Browse files
panvatargos
authored andcommitted
crypto: use globalThis.crypto over require('crypto').webcrypto
PR-URL: #45817 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 53f02cf commit c9747f1

36 files changed

+77
-76
lines changed

.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ module.exports = {
239239
selector: "CallExpression[callee.name='isNaN']",
240240
message: 'Use Number.isNaN() instead of the global isNaN() function.',
241241
},
242+
{
243+
// TODO(@panva): move this to no-restricted-properties
244+
// when https://github.com/eslint/eslint/issues/16412 is fixed
245+
selector: "Identifier[name='webcrypto']",
246+
message: 'Use `globalThis.crypto`.',
247+
},
242248
],
243249
'no-return-await': 'error',
244250
'no-self-compare': 'error',

benchmark/crypto/webcrypto-digest.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
22

33
const common = require('../common.js');
4-
const {
5-
createHash,
6-
webcrypto,
7-
} = require('crypto');
8-
const { subtle } = webcrypto;
4+
const { createHash } = require('crypto');
5+
const { subtle } = globalThis.crypto;
96

107
const bench = common.createBenchmark(main, {
118
sync: ['createHash', 'subtle'],
@@ -48,7 +45,7 @@ function measureSubtle(n, data, method) {
4845
}
4946

5047
function main({ n, sync, data, method }) {
51-
data = webcrypto.getRandomValues(Buffer.alloc(data));
48+
data = globalThis.crypto.getRandomValues(Buffer.alloc(data));
5249
switch (sync) {
5350
case 'createHash': return measureLegacy(n, data, method);
5451
case 'subtle': return measureSubtle(n, data, method);

test/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ rules:
4949
message: Use 'test' as debuglog value in tests.
5050
- selector: CallExpression:matches([callee.object.name="common"][callee.property.name=/^mustCall/],[callee.name="mustCall"],[callee.name="mustCallAtLeast"])>:first-child[type=/FunctionExpression$/][body.body.length=0]
5151
message: Do not use an empty function, omit the parameter altogether.
52+
- selector: Identifier[name='webcrypto']
53+
message: Use `globalThis.crypto`.
5254

5355
# Custom rules in tools/eslint-rules
5456
node-core/prefer-assert-iferror: error

test/parallel/test-crypto-psychic-signatures.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ for (const [encoding, signatures] of Object.entries(vectors)) {
8080
);
8181

8282
// webcrypto
83-
crypto.webcrypto.subtle.importKey(
83+
globalThis.crypto.subtle.importKey(
8484
'spki',
8585
keyPair.publicKey,
8686
{ name: 'ECDSA', namedCurve: 'P-256' },
8787
false,
8888
['verify'],
8989
).then((publicKey) => {
90-
return crypto.webcrypto.subtle.verify(
90+
return globalThis.crypto.subtle.verify(
9191
{ name: 'ECDSA', hash: 'SHA-256' },
9292
publicKey,
9393
signature,

test/parallel/test-crypto-random.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ if (!common.hasCrypto)
2828

2929
const assert = require('assert');
3030
const crypto = require('crypto');
31-
const cryptop = require('crypto').webcrypto;
3231
const { kMaxLength } = require('buffer');
3332

3433
const kMaxInt32 = 2 ** 31 - 1;
@@ -107,7 +106,7 @@ common.expectWarning('DeprecationWarning',
107106
new Uint32Array(10),
108107
].forEach((buf) => {
109108
const before = Buffer.from(buf.buffer).toString('hex');
110-
cryptop.getRandomValues(buf);
109+
globalThis.crypto.getRandomValues(buf);
111110
const after = Buffer.from(buf.buffer).toString('hex');
112111
assert.notStrictEqual(before, after);
113112
});

test/parallel/test-crypto-subtle-zero-length.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const crypto = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
(async () => {
12-
const k = await crypto.subtle.importKey(
12+
const k = await subtle.importKey(
1313
'raw',
1414
new Uint8Array(32),
1515
{ name: 'AES-GCM' },
1616
false,
1717
[ 'encrypt', 'decrypt' ]);
1818
assert(k instanceof CryptoKey);
1919

20-
const e = await crypto.subtle.encrypt({
20+
const e = await subtle.encrypt({
2121
name: 'AES-GCM',
2222
iv: new Uint8Array(12),
2323
}, k, new Uint8Array(0));
@@ -28,7 +28,7 @@ const crypto = require('crypto').webcrypto;
2828
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
2929
0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));
3030

31-
const v = await crypto.subtle.decrypt({
31+
const v = await subtle.decrypt({
3232
name: 'AES-GCM',
3333
iv: new Uint8Array(12),
3434
}, k, e);

test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const crypto = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

11-
crypto.subtle.importKey(
11+
subtle.importKey(
1212
'raw',
1313
new Uint8Array(32),
1414
{
@@ -18,7 +18,7 @@ crypto.subtle.importKey(
1818
[ 'encrypt', 'decrypt' ])
1919
.then((k) => {
2020
assert.rejects(() => {
21-
return crypto.subtle.decrypt({
21+
return subtle.decrypt({
2222
name: 'AES-GCM',
2323
iv: new Uint8Array(12),
2424
}, k, new Uint8Array(0));

test/parallel/test-global-webcrypto-classes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const webcrypto = require('internal/crypto/webcrypto');
109

10+
/* eslint-disable no-restricted-syntax */
11+
const webcrypto = require('internal/crypto/webcrypto');
1112
assert.strictEqual(Crypto, webcrypto.Crypto);
1213
assert.strictEqual(CryptoKey, webcrypto.CryptoKey);
1314
assert.strictEqual(SubtleCrypto, webcrypto.SubtleCrypto);

test/parallel/test-global-webcrypto.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!common.hasCrypto)
77
const assert = require('assert');
88
const crypto = require('crypto');
99

10+
/* eslint-disable no-restricted-syntax */
1011
assert.strictEqual(globalThis.crypto, crypto.webcrypto);
1112
assert.strictEqual(Crypto, crypto.webcrypto.constructor);
1213
assert.strictEqual(SubtleCrypto, crypto.webcrypto.subtle.constructor);

test/parallel/test-webcrypto-constructors.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9+
const { subtle } = globalThis.crypto;
910

1011
// Test CryptoKey constructor
1112
{
@@ -137,15 +138,15 @@ const notSubtle = Reflect.construct(function() {}, [], SubtleCrypto);
137138
}
138139

139140
{
140-
globalThis.crypto.subtle.importKey(
141+
subtle.importKey(
141142
'raw',
142143
globalThis.crypto.getRandomValues(new Uint8Array(4)),
143144
'PBKDF2',
144145
false,
145146
['deriveKey'],
146147
).then((key) => {
147-
globalThis.crypto.subtle.importKey = common.mustNotCall();
148-
return globalThis.crypto.subtle.deriveKey({
148+
subtle.importKey = common.mustNotCall();
149+
return subtle.deriveKey({
149150
name: 'PBKDF2',
150151
hash: 'SHA-512',
151152
salt: globalThis.crypto.getRandomValues(new Uint8Array()),

test/parallel/test-webcrypto-cryptokey-workers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!common.hasCrypto)
88
common.skip('missing crypto');
99

1010
const assert = require('assert');
11-
const { subtle } = require('crypto').webcrypto;
11+
const { subtle } = globalThis.crypto;
1212
const { once } = require('events');
1313

1414
const {

test/parallel/test-webcrypto-derivebits-cfrg.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -196,7 +195,7 @@ async function prepareKeys() {
196195

197196
{
198197
// Public is a secret key
199-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
198+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
200199
const key = await subtle.importKey(
201200
'raw',
202201
keyData,

test/parallel/test-webcrypto-derivebits-ecdh.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -251,7 +250,7 @@ async function prepareKeys() {
251250

252251
{
253252
// Public is a secret key
254-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
253+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
255254
const key = await subtle.importKey(
256255
'raw',
257256
keyData,

test/parallel/test-webcrypto-derivebits-hkdf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle } = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
function getDeriveKeyInfo(name, length, hash, ...usages) {
1212
return [{ name, length, hash }, usages];

test/parallel/test-webcrypto-derivebits.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (!common.hasCrypto)
77
common.skip('missing crypto');
88

99
const assert = require('assert');
10-
const { subtle } = require('crypto').webcrypto;
10+
const { subtle } = globalThis.crypto;
1111

1212
// This is only a partial test. The WebCrypto Web Platform Tests
1313
// will provide much greater coverage.

test/parallel/test-webcrypto-derivekey-cfrg.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -168,7 +167,7 @@ async function prepareKeys() {
168167

169168
{
170169
// Public is a secret key
171-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
170+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
172171
const key = await subtle.importKey(
173172
'raw',
174173
keyData,

test/parallel/test-webcrypto-derivekey-ecdh.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -227,7 +226,7 @@ async function prepareKeys() {
227226

228227
{
229228
// Public is a secret key
230-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
229+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
231230
const key = await subtle.importKey(
232231
'raw',
233232
keyData,

test/parallel/test-webcrypto-derivekey.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ if (!common.hasCrypto)
77
common.skip('missing crypto');
88

99
const assert = require('assert');
10-
const { webcrypto: { subtle }, KeyObject } = require('crypto');
10+
const { subtle } = globalThis.crypto;
11+
const { KeyObject } = require('crypto');
1112

1213
// This is only a partial test. The WebCrypto Web Platform Tests
1314
// will provide much greater coverage.

test/parallel/test-webcrypto-digest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (!common.hasCrypto)
77

88
const assert = require('assert');
99
const { Buffer } = require('buffer');
10-
const { subtle } = require('crypto').webcrypto;
10+
const { subtle } = globalThis.crypto;
1111
const { createHash } = require('crypto');
1212

1313
const kTests = [

test/parallel/test-webcrypto-encrypt-decrypt-aes.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
1312
// Using a copy of plaintext to prevent tampering of the original
@@ -214,8 +213,8 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
214213
['encrypt', 'decrypt'],
215214
);
216215

217-
const iv = webcrypto.getRandomValues(new Uint8Array(12));
218-
const aad = webcrypto.getRandomValues(new Uint8Array(32));
216+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
217+
const aad = globalThis.crypto.getRandomValues(new Uint8Array(32));
219218

220219
const encrypted = await subtle.encrypt(
221220
{
@@ -225,7 +224,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
225224
tagLength: 128
226225
},
227226
secretKey,
228-
webcrypto.getRandomValues(new Uint8Array(32))
227+
globalThis.crypto.getRandomValues(new Uint8Array(32))
229228
);
230229

231230
await subtle.decrypt(

test/parallel/test-webcrypto-encrypt-decrypt-rsa.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle } = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
const {
1212
passing

test/parallel/test-webcrypto-encrypt-decrypt.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
// This is only a partial test. The WebCrypto Web Platform Tests
1312
// will provide much greater coverage.
1413

1514
// Test Encrypt/Decrypt RSA-OAEP
1615
{
17-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
16+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
1817

1918
async function test() {
2019
const ec = new TextEncoder();
@@ -45,8 +44,8 @@ const { subtle } = webcrypto;
4544

4645
// Test Encrypt/Decrypt AES-CTR
4746
{
48-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
49-
const counter = webcrypto.getRandomValues(new Uint8Array(16));
47+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
48+
const counter = globalThis.crypto.getRandomValues(new Uint8Array(16));
5049

5150
async function test() {
5251
const key = await subtle.generateKey({
@@ -72,8 +71,8 @@ const { subtle } = webcrypto;
7271

7372
// Test Encrypt/Decrypt AES-CBC
7473
{
75-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
76-
const iv = webcrypto.getRandomValues(new Uint8Array(16));
74+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
75+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(16));
7776

7877
async function test() {
7978
const key = await subtle.generateKey({
@@ -99,8 +98,8 @@ const { subtle } = webcrypto;
9998

10099
// Test Encrypt/Decrypt AES-GCM
101100
{
102-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
103-
const iv = webcrypto.getRandomValues(new Uint8Array(12));
101+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
102+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
104103

105104
async function test() {
106105
const key = await subtle.generateKey({

test/parallel/test-webcrypto-export-import-cfrg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!common.hasCrypto)
88

99
const assert = require('assert');
1010
const crypto = require('crypto');
11-
const { subtle } = crypto.webcrypto;
11+
const { subtle } = globalThis.crypto;
1212

1313
const keyData = {
1414
'Ed25519': {

0 commit comments

Comments
 (0)