Skip to content

Commit abea0af

Browse files
panvaaduh95
authored andcommitted
test: add WebCrypto Promise.prototype.then pollution regression tests
PR-URL: #62226 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 92ef2ad commit abea0af

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import*ascommonfrom'../common/index.mjs';
2+
3+
if(!common.hasCrypto)common.skip('missing crypto');
4+
5+
// WebCrypto subtle methods must not leak intermediate values
6+
// through Promise.prototype.then pollution.
7+
// Regression test for https://github.com/nodejs/node/pull/61492
8+
// and https://github.com/nodejs/node/issues/59699.
9+
10+
import{hasOpenSSL}from'../common/crypto.js';
11+
12+
const{ subtle }=globalThis.crypto;
13+
14+
Promise.prototype.then=common.mustNotCall('Promise.prototype.then');
15+
16+
awaitsubtle.digest('SHA-256',newUint8Array([1,2,3]));
17+
18+
awaitsubtle.generateKey({name: 'AES-CBC',length: 256},true,['encrypt','decrypt']);
19+
20+
awaitsubtle.generateKey({name: 'ECDSA',namedCurve: 'P-256'},true,['sign','verify']);
21+
22+
constrawKey=globalThis.crypto.getRandomValues(newUint8Array(32));
23+
24+
constimportedKey=awaitsubtle.importKey(
25+
'raw',rawKey,{name: 'AES-CBC',length: 256},false,['encrypt','decrypt']);
26+
27+
constexportableKey=awaitsubtle.importKey(
28+
'raw',rawKey,{name: 'AES-CBC',length: 256},true,['encrypt','decrypt']);
29+
30+
awaitsubtle.exportKey('raw',exportableKey);
31+
32+
constiv=globalThis.crypto.getRandomValues(newUint8Array(16));
33+
constplaintext=newTextEncoder().encode('Hello, world!');
34+
35+
constciphertext=awaitsubtle.encrypt({name: 'AES-CBC', iv },importedKey,plaintext);
36+
37+
awaitsubtle.decrypt({name: 'AES-CBC', iv },importedKey,ciphertext);
38+
39+
constsigningKey=awaitsubtle.generateKey(
40+
{name: 'HMAC',hash: 'SHA-256'},false,['sign','verify']);
41+
42+
constdata=newTextEncoder().encode('test data');
43+
44+
constsignature=awaitsubtle.sign('HMAC',signingKey,data);
45+
46+
awaitsubtle.verify('HMAC',signingKey,signature,data);
47+
48+
constpbkdf2Key=awaitsubtle.importKey(
49+
'raw',rawKey,'PBKDF2',false,['deriveBits','deriveKey']);
50+
51+
awaitsubtle.deriveBits(
52+
{name: 'PBKDF2',salt: rawKey,iterations: 1000,hash: 'SHA-256'},
53+
pbkdf2Key,256);
54+
55+
awaitsubtle.deriveKey(
56+
{name: 'PBKDF2',salt: rawKey,iterations: 1000,hash: 'SHA-256'},
57+
pbkdf2Key,
58+
{name: 'AES-CBC',length: 256},
59+
true,
60+
['encrypt','decrypt']);
61+
62+
constwrappingKey=awaitsubtle.generateKey(
63+
{name: 'AES-KW',length: 256},true,['wrapKey','unwrapKey']);
64+
65+
constkeyToWrap=awaitsubtle.generateKey(
66+
{name: 'AES-CBC',length: 256},true,['encrypt','decrypt']);
67+
68+
constwrapped=awaitsubtle.wrapKey('raw',keyToWrap,wrappingKey,'AES-KW');
69+
70+
awaitsubtle.unwrapKey(
71+
'raw',wrapped,wrappingKey,'AES-KW',
72+
{name: 'AES-CBC',length: 256},true,['encrypt','decrypt']);
73+
74+
const{ privateKey }=awaitsubtle.generateKey(
75+
{name: 'ECDSA',namedCurve: 'P-256'},true,['sign','verify']);
76+
77+
awaitsubtle.getPublicKey(privateKey,['verify']);
78+
79+
if(hasOpenSSL(3,5)){
80+
constkemPair=awaitsubtle.generateKey(
81+
{name: 'ML-KEM-768'},false,
82+
['encapsulateKey','encapsulateBits','decapsulateKey','decapsulateBits']);
83+
84+
const{ciphertext: ct1}=awaitsubtle.encapsulateKey(
85+
{name: 'ML-KEM-768'},kemPair.publicKey,'HKDF',false,['deriveBits']);
86+
87+
awaitsubtle.decapsulateKey(
88+
{name: 'ML-KEM-768'},kemPair.privateKey,ct1,'HKDF',false,['deriveBits']);
89+
90+
const{ciphertext: ct2}=awaitsubtle.encapsulateBits(
91+
{name: 'ML-KEM-768'},kemPair.publicKey);
92+
93+
awaitsubtle.decapsulateBits(
94+
{name: 'ML-KEM-768'},kemPair.privateKey,ct2);
95+
}

0 commit comments

Comments
 (0)