Skip to content

Commit 1f93913

Browse files
Renegade334aduh95
authored andcommitted
crypto: use return await when returning Promises from async functions
This offers _some_ resistance to `%Promise.prototype%` pollution. Refs: #59699 PR-URL: #59841 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent f488b2f commit 1f93913

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

lib/internal/crypto/aes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async function asyncAesGcmCipher(mode, key, data, algorithm) {
160160
break;
161161
}
162162

163-
return jobPromise(() => new AESCipherJob(
163+
return await jobPromise(() => new AESCipherJob(
164164
kCryptoJobAsync,
165165
mode,
166166
key[kKeyObject][kHandle],

lib/internal/crypto/cfrg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ async function eddsaSignVerify(key, data, algorithm, signature) {
349349
if (key.type !== type)
350350
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
351351

352-
return jobPromise(() => new SignJob(
352+
return await jobPromise(() => new SignJob(
353353
kCryptoJobAsync,
354354
mode,
355355
key[kKeyObject][kHandle],

lib/internal/crypto/ec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
9999
let keyPair;
100100
try {
101101
keyPair = await generateKeyPair('ec', { namedCurve });
102-
} catch(err) {
102+
} catch (err) {
103103
throw lazyDOMException(
104104
'The operation failed for an operation-specific reason',
105105
{ name: 'OperationError', cause: err });
@@ -292,7 +292,7 @@ async function ecdsaSignVerify(key, data, { name, hash }, signature) {
292292

293293
const hashname = normalizeHashName(hash.name);
294294

295-
return jobPromise(() => new SignJob(
295+
return await jobPromise(() => new SignJob(
296296
kCryptoJobAsync,
297297
mode,
298298
key[kKeyObject][kHandle],

lib/internal/crypto/rsa.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async function rsaOaepCipher(mode, key, data, algorithm) {
101101
'InvalidAccessError');
102102
}
103103

104-
return jobPromise(() => new RSACipherJob(
104+
return await jobPromise(() => new RSACipherJob(
105105
kCryptoJobAsync,
106106
mode,
107107
key[kKeyObject][kHandle],
@@ -335,7 +335,7 @@ async function rsaSignVerify(key, data, { saltLength }, signature) {
335335
if (key.type !== type)
336336
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
337337

338-
return jobPromise(() => {
338+
return await jobPromise(() => {
339339
if (key.algorithm.name === 'RSA-PSS') {
340340
validateInt32(
341341
saltLength,

lib/internal/crypto/webcrypto.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async function digest(algorithm, data) {
8080

8181
algorithm = normalizeAlgorithm(algorithm, 'digest');
8282

83-
return ReflectApply(asyncDigest, this, [algorithm, data]);
83+
return await ReflectApply(asyncDigest, this, [algorithm, data]);
8484
}
8585

8686
function randomUUID() {
@@ -210,13 +210,13 @@ async function deriveBits(algorithm, baseKey, length = null) {
210210
case 'X448':
211211
// Fall through
212212
case 'ECDH':
213-
return require('internal/crypto/diffiehellman')
213+
return await require('internal/crypto/diffiehellman')
214214
.ecdhDeriveBits(algorithm, baseKey, length);
215215
case 'HKDF':
216-
return require('internal/crypto/hkdf')
216+
return await require('internal/crypto/hkdf')
217217
.hkdfDeriveBits(algorithm, baseKey, length);
218218
case 'PBKDF2':
219-
return require('internal/crypto/pbkdf2')
219+
return await require('internal/crypto/pbkdf2')
220220
.pbkdf2DeriveBits(algorithm, baseKey, length);
221221
}
222222
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
@@ -327,12 +327,12 @@ async function exportKeySpki(key) {
327327
case 'RSA-PSS':
328328
// Fall through
329329
case 'RSA-OAEP':
330-
return require('internal/crypto/rsa')
330+
return await require('internal/crypto/rsa')
331331
.rsaExportKey(key, kWebCryptoKeyFormatSPKI);
332332
case 'ECDSA':
333333
// Fall through
334334
case 'ECDH':
335-
return require('internal/crypto/ec')
335+
return await require('internal/crypto/ec')
336336
.ecExportKey(key, kWebCryptoKeyFormatSPKI);
337337
case 'Ed25519':
338338
// Fall through
@@ -341,7 +341,7 @@ async function exportKeySpki(key) {
341341
case 'X25519':
342342
// Fall through
343343
case 'X448':
344-
return require('internal/crypto/cfrg')
344+
return await require('internal/crypto/cfrg')
345345
.cfrgExportKey(key, kWebCryptoKeyFormatSPKI);
346346
default:
347347
return undefined;
@@ -355,12 +355,12 @@ async function exportKeyPkcs8(key) {
355355
case 'RSA-PSS':
356356
// Fall through
357357
case 'RSA-OAEP':
358-
return require('internal/crypto/rsa')
358+
return await require('internal/crypto/rsa')
359359
.rsaExportKey(key, kWebCryptoKeyFormatPKCS8);
360360
case 'ECDSA':
361361
// Fall through
362362
case 'ECDH':
363-
return require('internal/crypto/ec')
363+
return await require('internal/crypto/ec')
364364
.ecExportKey(key, kWebCryptoKeyFormatPKCS8);
365365
case 'Ed25519':
366366
// Fall through
@@ -369,7 +369,7 @@ async function exportKeyPkcs8(key) {
369369
case 'X25519':
370370
// Fall through
371371
case 'X448':
372-
return require('internal/crypto/cfrg')
372+
return await require('internal/crypto/cfrg')
373373
.cfrgExportKey(key, kWebCryptoKeyFormatPKCS8);
374374
default:
375375
return undefined;
@@ -381,7 +381,7 @@ async function exportKeyRawPublic(key) {
381381
case 'ECDSA':
382382
// Fall through
383383
case 'ECDH':
384-
return require('internal/crypto/ec')
384+
return await require('internal/crypto/ec')
385385
.ecExportKey(key, kWebCryptoKeyFormatRaw);
386386
case 'Ed25519':
387387
// Fall through
@@ -390,7 +390,7 @@ async function exportKeyRawPublic(key) {
390390
case 'X25519':
391391
// Fall through
392392
case 'X448':
393-
return require('internal/crypto/cfrg')
393+
return await require('internal/crypto/cfrg')
394394
.cfrgExportKey(key, kWebCryptoKeyFormatRaw);
395395
default:
396396
return undefined;
@@ -682,7 +682,7 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
682682
}
683683
}
684684

685-
return cipherOrWrap(
685+
return await cipherOrWrap(
686686
kWebCryptoCipherEncrypt,
687687
algorithm,
688688
wrappingKey,
@@ -787,19 +787,19 @@ async function signVerify(algorithm, key, data, signature) {
787787
case 'RSA-PSS':
788788
// Fall through
789789
case 'RSASSA-PKCS1-v1_5':
790-
return require('internal/crypto/rsa')
790+
return await require('internal/crypto/rsa')
791791
.rsaSignVerify(key, data, algorithm, signature);
792792
case 'ECDSA':
793-
return require('internal/crypto/ec')
793+
return await require('internal/crypto/ec')
794794
.ecdsaSignVerify(key, data, algorithm, signature);
795795
case 'Ed25519':
796796
// Fall through
797797
case 'Ed448':
798798
// Fall through
799-
return require('internal/crypto/cfrg')
799+
return await require('internal/crypto/cfrg')
800800
.eddsaSignVerify(key, data, algorithm, signature);
801801
case 'HMAC':
802-
return require('internal/crypto/mac')
802+
return await require('internal/crypto/mac')
803803
.hmacSignVerify(key, data, algorithm, signature);
804804
}
805805
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
@@ -824,7 +824,7 @@ async function sign(algorithm, key, data) {
824824
context: '3rd argument',
825825
});
826826

827-
return signVerify(algorithm, key, data);
827+
return await signVerify(algorithm, key, data);
828828
}
829829

830830
async function verify(algorithm, key, signature, data) {
@@ -850,7 +850,7 @@ async function verify(algorithm, key, signature, data) {
850850
context: '4th argument',
851851
});
852852

853-
return signVerify(algorithm, key, data, signature);
853+
return await signVerify(algorithm, key, data, signature);
854854
}
855855

856856
async function cipherOrWrap(mode, algorithm, key, data, op) {
@@ -873,18 +873,18 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {
873873

874874
switch (algorithm.name) {
875875
case 'RSA-OAEP':
876-
return require('internal/crypto/rsa')
876+
return await require('internal/crypto/rsa')
877877
.rsaCipher(mode, key, data, algorithm);
878878
case 'AES-CTR':
879879
// Fall through
880880
case 'AES-CBC':
881881
// Fall through
882882
case 'AES-GCM':
883-
return require('internal/crypto/aes')
883+
return await require('internal/crypto/aes')
884884
.aesCipher(mode, key, data, algorithm);
885885
case 'AES-KW':
886886
if (op === 'wrapKey' || op === 'unwrapKey') {
887-
return require('internal/crypto/aes')
887+
return await require('internal/crypto/aes')
888888
.aesCipher(mode, key, data, algorithm);
889889
}
890890
}
@@ -911,7 +911,13 @@ async function encrypt(algorithm, key, data) {
911911
});
912912

913913
algorithm = normalizeAlgorithm(algorithm, 'encrypt');
914-
return cipherOrWrap(kWebCryptoCipherEncrypt, algorithm, key, data, 'encrypt');
914+
return await cipherOrWrap(
915+
kWebCryptoCipherEncrypt,
916+
algorithm,
917+
key,
918+
data,
919+
'encrypt',
920+
);
915921
}
916922

917923
async function decrypt(algorithm, key, data) {
@@ -934,7 +940,13 @@ async function decrypt(algorithm, key, data) {
934940
});
935941

936942
algorithm = normalizeAlgorithm(algorithm, 'decrypt');
937-
return cipherOrWrap(kWebCryptoCipherDecrypt, algorithm, key, data, 'decrypt');
943+
return await cipherOrWrap(
944+
kWebCryptoCipherDecrypt,
945+
algorithm,
946+
key,
947+
data,
948+
'decrypt',
949+
);
938950
}
939951

940952
// The SubtleCrypto and Crypto classes are defined as part of the

0 commit comments

Comments
 (0)