Skip to content

Commit e042d35

Browse files
panvaaduh95
authored andcommitted
crypto: convert importKey data as a union
Choose the BufferSource or JsonWebKey branch from the value, then check the requested format after algorithm normalization. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent ff86606 commit e042d35

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayBufferIsView,
45
ArrayIsArray,
56
ArrayPrototypeSlice,
67
FunctionPrototypeCall,
@@ -39,6 +40,7 @@ const {
3940
const {
4041
codes: {
4142
ERR_ILLEGAL_CONSTRUCTOR,
43+
ERR_INVALID_ARG_TYPE,
4244
ERR_INVALID_THIS,
4345
},
4446
} = require('internal/errors');
@@ -85,6 +87,7 @@ const {
8587
} = require('internal/crypto/random');
8688

8789
const {
90+
isArrayBuffer,
8891
isPromise,
8992
} = require('internal/util/types');
9093

@@ -1114,7 +1117,8 @@ function importKeyImpl(
11141117
const prefix = prepareSubtleMethod(this, 'importKey', arguments.length, 5);
11151118
let i = 0;
11161119
format = convertSubtleArgument(prefix, 'KeyFormat', format, i++);
1117-
const type = format === 'jwk' ? 'JsonWebKey' : 'BufferSource';
1120+
const type = ArrayBufferIsView(keyData) || isArrayBuffer(keyData) ?
1121+
'BufferSource' : 'JsonWebKey';
11181122
keyData = convertSubtleArgument(prefix, type, keyData, i++);
11191123
algorithm = convertSubtleArgument(
11201124
prefix, 'AlgorithmIdentifier', algorithm, i++);
@@ -1124,6 +1128,11 @@ function importKeyImpl(
11241128

11251129
const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'importKey');
11261130

1131+
if ((format === 'jwk') !== (type === 'JsonWebKey')) {
1132+
throw new ERR_INVALID_ARG_TYPE(
1133+
'keyData', format === 'jwk' ? 'JsonWebKey' : 'BufferSource', keyData);
1134+
}
1135+
11271136
return FunctionPrototypeCall(
11281137
importKeySync,
11291138
this,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const { subtle } = globalThis.crypto;
9+
const jwk = { kty: 'oct', k: 'AAAAAAAAAAAAAAAAAAAAAA' };
10+
11+
(async () => {
12+
for (const data of [new ArrayBuffer(16), new Uint8Array(16),
13+
new DataView(new ArrayBuffer(16)), Buffer.alloc(16)]) {
14+
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
15+
TypeError);
16+
Object.assign(data, jwk);
17+
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
18+
TypeError);
19+
await assert.rejects(subtle.importKey('jwk', data, 'unknown', true, ['encrypt']),
20+
{ name: 'NotSupportedError' });
21+
}
22+
await assert.rejects(subtle.importKey('raw', {}, 'unknown', true, ['encrypt']),
23+
{ name: 'NotSupportedError' });
24+
await assert.rejects(subtle.importKey('raw', {}, 'AES-GCM', true, ['encrypt']),
25+
TypeError);
26+
for (const data of [null, new SharedArrayBuffer(16)]) {
27+
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
28+
{ name: 'DataError' });
29+
}
30+
assert.strictEqual((await subtle.importKey('jwk', jwk, 'AES-GCM', true,
31+
['encrypt'])).type, 'secret');
32+
await assert.rejects(subtle.importKey('raw', {
33+
get kty() { throw new Error('converted JWK'); },
34+
}, 'unknown', true, ['encrypt']), { message: 'converted JWK' });
35+
})().then(common.mustCall());

0 commit comments

Comments
 (0)