1
1
'use strict' ;
2
- // Flags: --expose-internals
3
2
const common = require ( '../common' ) ;
4
3
if ( ! common . hasCrypto )
5
4
common . skip ( 'missing crypto' ) ;
6
5
7
6
const assert = require ( 'assert' ) ;
8
7
const { subtle } = globalThis . crypto ;
9
8
const vm = require ( 'vm' ) ;
10
- const { isArrayBuffer } = require ( 'internal/util/types' ) ;
11
9
12
10
// Test with same-realm ArrayBuffer
13
11
{
14
12
const samerealmData = new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) . buffer ;
15
13
16
14
subtle . digest ( 'SHA-256' , samerealmData )
17
15
. then ( common . mustCall ( ( result ) => {
18
- assert ( isArrayBuffer ( result ) ) ;
19
16
assert . strictEqual ( result . byteLength , 32 ) ; // SHA-256 is 32 bytes
20
17
} ) ) ;
21
18
}
@@ -35,11 +32,30 @@ const { isArrayBuffer } = require('internal/util/types');
35
32
// This should still work, since we're checking structural type
36
33
subtle . digest ( 'SHA-256' , crossrealmBuffer )
37
34
. then ( common . mustCall ( ( result ) => {
38
- assert ( isArrayBuffer ( result ) ) ;
39
35
assert . strictEqual ( result . byteLength , 32 ) ; // SHA-256 is 32 bytes
40
36
} ) ) ;
41
37
}
42
38
39
+ // Cross-realm SharedArrayBuffer should be handled like any SharedArrayBuffer
40
+ {
41
+ const context = vm . createContext ( { } ) ;
42
+ const crossrealmSAB = vm . runInContext ( 'new SharedArrayBuffer(4)' , context ) ;
43
+ assert . notStrictEqual (
44
+ Object . getPrototypeOf ( crossrealmSAB ) ,
45
+ SharedArrayBuffer . prototype
46
+ ) ;
47
+ Promise . allSettled ( [
48
+ subtle . digest ( 'SHA-256' , new Uint8Array ( new SharedArrayBuffer ( 4 ) ) ) ,
49
+ subtle . digest ( 'SHA-256' , new Uint8Array ( crossrealmSAB ) ) ,
50
+ ] ) . then ( common . mustCall ( ( r ) => {
51
+ assert . partialDeepStrictEqual ( r , [
52
+ { status : 'rejected' } ,
53
+ { status : 'rejected' } ,
54
+ ] ) ;
55
+ assert . strictEqual ( r [ 1 ] . reason . message , r [ 0 ] . reason . message ) ;
56
+ } ) ) ;
57
+ }
58
+
43
59
// Test with both TypedArray buffer methods
44
60
{
45
61
const context = vm . createContext ( { } ) ;
@@ -48,14 +64,12 @@ const { isArrayBuffer } = require('internal/util/types');
48
64
// Test the .buffer property
49
65
subtle . digest ( 'SHA-256' , crossrealmUint8Array . buffer )
50
66
. then ( common . mustCall ( ( result ) => {
51
- assert ( isArrayBuffer ( result ) ) ;
52
67
assert . strictEqual ( result . byteLength , 32 ) ;
53
68
} ) ) ;
54
69
55
70
// Test passing the TypedArray directly (should work both before and after the fix)
56
71
subtle . digest ( 'SHA-256' , crossrealmUint8Array )
57
72
. then ( common . mustCall ( ( result ) => {
58
- assert ( isArrayBuffer ( result ) ) ;
59
73
assert . strictEqual ( result . byteLength , 32 ) ;
60
74
} ) ) ;
61
75
}
@@ -76,34 +90,32 @@ const { isArrayBuffer } = require('internal/util/types');
76
90
name : 'AES-GCM' ,
77
91
length : 256
78
92
} , true , [ 'encrypt' , 'decrypt' ] )
79
- . then ( common . mustCall ( ( key ) => {
93
+ . then ( async ( key ) => {
80
94
// Create an initialization vector
81
95
const iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
82
96
83
97
// Encrypt using the cross-realm ArrayBuffer
84
- return subtle . encrypt (
98
+ const ciphertext = await subtle . encrypt (
85
99
{ name : 'AES-GCM' , iv } ,
86
100
key ,
87
101
crossRealmBuffer
88
- ) . then ( ( ciphertext ) => {
102
+ ) ;
89
103
// Decrypt
90
- return subtle . decrypt (
91
- { name : 'AES-GCM' , iv } ,
92
- key ,
93
- ciphertext
94
- ) ;
95
- } ) . then ( common . mustCall ( ( plaintext ) => {
104
+ const plaintext = await subtle . decrypt (
105
+ { name : 'AES-GCM' , iv } ,
106
+ key ,
107
+ ciphertext
108
+ ) ;
96
109
// Verify the decrypted content matches original
97
- const decryptedView = new Uint8Array ( plaintext ) ;
98
- for ( let i = 0 ; i < dataView . length ; i ++ ) {
99
- assert . strictEqual (
100
- decryptedView [ i ] ,
101
- dataView [ i ] ,
102
- `Byte at position ${ i } doesn't match`
103
- ) ;
104
- }
105
- } ) ) ;
106
- } ) ) ;
110
+ const decryptedView = new Uint8Array ( plaintext ) ;
111
+ for ( let i = 0 ; i < dataView . length ; i ++ ) {
112
+ assert . strictEqual (
113
+ decryptedView [ i ] ,
114
+ dataView [ i ] ,
115
+ `Byte at position ${ i } doesn't match`
116
+ ) ;
117
+ }
118
+ } ) . then ( common . mustCall ( ) ) ;
107
119
}
108
120
109
121
// Test with AES-GCM using TypedArray view of cross-realm ArrayBuffer
@@ -122,32 +134,31 @@ const { isArrayBuffer } = require('internal/util/types');
122
134
name : 'AES-GCM' ,
123
135
length : 256
124
136
} , true , [ 'encrypt' , 'decrypt' ] )
125
- . then ( common . mustCall ( ( key ) => {
137
+ . then ( async ( key ) => {
126
138
// Create an initialization vector
127
139
const iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
128
140
129
141
// Encrypt using the TypedArray view of cross-realm ArrayBuffer
130
- return subtle . encrypt (
142
+ const ciphertext = await subtle . encrypt (
131
143
{ name : 'AES-GCM' , iv } ,
132
144
key ,
133
145
dataView
134
- ) . then ( ( ciphertext ) => {
146
+ ) ;
135
147
// Decrypt
136
- return subtle . decrypt (
137
- { name : 'AES-GCM' , iv } ,
138
- key ,
139
- ciphertext
148
+ const plaintext = await subtle . decrypt (
149
+ { name : 'AES-GCM' , iv } ,
150
+ key ,
151
+ ciphertext
152
+ ) ;
153
+
154
+ // Verify the decrypted content matches original
155
+ const decryptedView = new Uint8Array ( plaintext ) ;
156
+ for ( let i = 0 ; i < dataView . length ; i ++ ) {
157
+ assert . strictEqual (
158
+ decryptedView [ i ] ,
159
+ dataView [ i ] ,
160
+ `Byte at position ${ i } doesn't match`
140
161
) ;
141
- } ) . then ( common . mustCall ( ( plaintext ) => {
142
- // Verify the decrypted content matches original
143
- const decryptedView = new Uint8Array ( plaintext ) ;
144
- for ( let i = 0 ; i < dataView . length ; i ++ ) {
145
- assert . strictEqual (
146
- decryptedView [ i ] ,
147
- dataView [ i ] ,
148
- `Byte at position ${ i } doesn't match`
149
- ) ;
150
- }
151
- } ) ) ;
152
- } ) ) ;
162
+ }
163
+ } ) . then ( common . mustCall ( ) ) ;
153
164
}
0 commit comments