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 <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 53f02cf commit c9747f1

36 files changed

+77
-76
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 3 deletions
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()),

0 commit comments

Comments
 (0)