Skip to content

Commit b8de7aa

Browse files
LiviaMedeirospanva
authored andcommitted
crypto: adjust types for getRandomValues
prevents Web Crypto API's getRandomValues from accepting DataView Fixes: #41480 Refs: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues PR-URL: #41481 Reviewed-By: Derek Lewis <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2f17004 commit b8de7aa

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

doc/api/webcrypto.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,15 @@ Provides access to the `SubtleCrypto` API.
357357
added: v15.0.0
358358
-->
359359

360-
* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer}
361-
* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`.
360+
* `typedArray` {Buffer|TypedArray}
361+
* Returns: {Buffer|TypedArray}
362362

363363
Generates cryptographically strong random values. The given `typedArray` is
364364
filled with random values, and a reference to `typedArray` is returned.
365365

366+
The given `typedArray` must be an integer-based instance of {TypedArray},
367+
i.e. `Float32Array` and `Float64Array` are not accepted.
368+
366369
An error will be thrown if the given `typedArray` is larger than 65,536 bytes.
367370

368371
### `crypto.randomUUID()`

lib/internal/crypto/random.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
const {
5151
isArrayBufferView,
5252
isAnyArrayBuffer,
53+
isTypedArray,
5354
isFloat32Array,
5455
isFloat64Array,
5556
} = require('internal/util/types');
@@ -307,7 +308,7 @@ function onJobDone(buf, callback, error) {
307308
// not allowed to exceed 65536 bytes, and can only
308309
// be an integer-type TypedArray.
309310
function getRandomValues(data) {
310-
if (!isArrayBufferView(data) ||
311+
if (!isTypedArray(data) ||
311312
isFloat32Array(data) ||
312313
isFloat64Array(data)) {
313314
// Ordinarily this would be an ERR_INVALID_ARG_TYPE. However,

test/parallel/test-webcrypto-random.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { getRandomValues } = require('crypto').webcrypto;
1313
undefined, null, '', 1, {}, [],
1414
new Float32Array(1),
1515
new Float64Array(1),
16+
new DataView(new ArrayBuffer(1)),
1617
].forEach((i) => {
1718
assert.throws(
1819
() => getRandomValues(i),
@@ -32,6 +33,7 @@ const intTypedConstructors = [
3233
Uint8Array,
3334
Uint16Array,
3435
Uint32Array,
36+
Uint8ClampedArray,
3537
BigInt64Array,
3638
BigUint64Array,
3739
];
@@ -47,7 +49,7 @@ for (const ctor of intTypedConstructors) {
4749
{
4850
const buf = new Uint16Array(10);
4951
const before = Buffer.from(buf).toString('hex');
50-
getRandomValues(new DataView(buf.buffer));
52+
getRandomValues(buf);
5153
const after = Buffer.from(buf).toString('hex');
5254
assert.notStrictEqual(before, after);
5355
}

0 commit comments

Comments
 (0)