Skip to content

Commit e074f4b

Browse files
committed
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
1 parent 6b7b0b7 commit e074f4b

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/internal/crypto/random.js

Lines changed: 2 additions & 1 deletion
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/fixtures/wpt/WebCryptoAPI/getRandomValues.any.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ test(function() {
1313
assert_throws_dom("TypeMismatchError", function() {
1414
self.crypto.getRandomValues(new Float64Array(65537))
1515
}, "Float64Array (too long)")
16-
}, "Float arrays")
16+
}, "Float arrays");
17+
18+
test(function() {
19+
assert_throws_dom("TypeMismatchError", function() {
20+
self.crypto.getRandomValues(new DataView(new ArrayBuffer(1)))
21+
}, "DataView")
22+
23+
assert_throws_dom("TypeMismatchError", function() {
24+
self.crypto.getRandomValues(new DataView(new ArrayBuffer(65537)))
25+
}, "DataView (too long)")
26+
}, "DataView");
1727

1828
const arrays = [
1929
'Int8Array',

test/parallel/test-webcrypto-random.js

Lines changed: 3 additions & 1 deletion
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)