diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 8812120091f930..ecef7ad0048991 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -307,7 +307,7 @@ function onJobDone(buf, callback, error) { // not allowed to exceed 65536 bytes, and can only // be an integer-type TypedArray. function getRandomValues(data) { - if (!isArrayBufferView(data) || + if (!isTypedArray(data) || isFloat32Array(data) || isFloat64Array(data)) { // Ordinarily this would be an ERR_INVALID_ARG_TYPE. However, diff --git a/test/fixtures/wpt/WebCryptoAPI/getRandomValues.any.js b/test/fixtures/wpt/WebCryptoAPI/getRandomValues.any.js index a6762353798f43..dc86f4a1c2295a 100644 --- a/test/fixtures/wpt/WebCryptoAPI/getRandomValues.any.js +++ b/test/fixtures/wpt/WebCryptoAPI/getRandomValues.any.js @@ -13,7 +13,17 @@ test(function() { assert_throws_dom("TypeMismatchError", function() { self.crypto.getRandomValues(new Float64Array(65537)) }, "Float64Array (too long)") -}, "Float arrays") +}, "Float arrays"); + +test(function() { + assert_throws_dom("TypeMismatchError", function() { + self.crypto.getRandomValues(new DataView(new ArrayBuffer(1))) + }, "DataView") + + assert_throws_dom("TypeMismatchError", function() { + self.crypto.getRandomValues(new DataView(new ArrayBuffer(65537))) + }, "DataView (too long)") +}, "DataView"); const arrays = [ 'Int8Array', diff --git a/test/parallel/test-webcrypto-random.js b/test/parallel/test-webcrypto-random.js index f2bf2c396fd20a..e17cc834b6c2bf 100644 --- a/test/parallel/test-webcrypto-random.js +++ b/test/parallel/test-webcrypto-random.js @@ -13,6 +13,7 @@ const { getRandomValues } = require('crypto').webcrypto; undefined, null, '', 1, {}, [], new Float32Array(1), new Float64Array(1), + new DataView(new ArrayBuffer(1)), ].forEach((i) => { assert.throws( () => getRandomValues(i), @@ -32,6 +33,7 @@ const intTypedConstructors = [ Uint8Array, Uint16Array, Uint32Array, + Uint8ClampedArray, BigInt64Array, BigUint64Array, ]; @@ -47,7 +49,7 @@ for (const ctor of intTypedConstructors) { { const buf = new Uint16Array(10); const before = Buffer.from(buf).toString('hex'); - getRandomValues(new DataView(buf.buffer)); + getRandomValues(buf); const after = Buffer.from(buf).toString('hex'); assert.notStrictEqual(before, after); }