diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index caeca416836..c0a50a1b4be 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -2027,7 +2027,8 @@ function getRandomValues(array) { const prefix = "Failed to execute 'getRandomValues' on 'Crypto'"; webidl.requiredArguments(arguments.length, 1, { prefix }); - return ReflectApply(_getRandomValues, this, arguments); + array = convertSubtleArgument(prefix, 'ArrayBufferView', array, 0); + return _getRandomValues(array); } ObjectDefineProperties( diff --git a/lib/internal/webidl.js b/lib/internal/webidl.js index ee0388b70e9..88f4205a1c0 100644 --- a/lib/internal/webidl.js +++ b/lib/internal/webidl.js @@ -904,6 +904,14 @@ function getViewedArrayBuffer(V) { TypedArrayPrototypeGetBuffer(V) : DataViewPrototypeGetBuffer(V); } +converters.ArrayBufferView = (V, options = kEmptyObject) => { + if (!ArrayBufferIsView(V)) { + throw makeException('is not an ArrayBufferView.', options); + } + validateBufferSourceBacking(getViewedArrayBuffer(V), options); + return V; +}; + /** * Validates [AllowShared] and [AllowResizable] backing-store constraints. * @param {ArrayBuffer|SharedArrayBuffer} buffer Backing buffer. diff --git a/test/parallel/test-webcrypto-random-backing-buffer.js b/test/parallel/test-webcrypto-random-backing-buffer.js new file mode 100644 index 00000000000..607b5b3e509 --- /dev/null +++ b/test/parallel/test-webcrypto-random-backing-buffer.js @@ -0,0 +1,20 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); + +for (const buffer of [ + new SharedArrayBuffer(8), + new SharedArrayBuffer(8, { maxByteLength: 16 }), + new ArrayBuffer(8, { maxByteLength: 16 }), +]) { + for (const Type of [Uint8Array, Float32Array, DataView]) { + assert.throws(() => crypto.getRandomValues(new Type(buffer)), TypeError); + } +} + +assert.throws(() => crypto.getRandomValues(new Proxy(new Uint8Array(8), {})), + TypeError); diff --git a/test/parallel/test-webcrypto-random.js b/test/parallel/test-webcrypto-random.js index 20639d2b8e1..abc0f86f671 100644 --- a/test/parallel/test-webcrypto-random.js +++ b/test/parallel/test-webcrypto-random.js @@ -9,8 +9,11 @@ const { Buffer } = require('buffer'); const assert = require('assert'); const { crypto } = globalThis; +for (const value of [undefined, null, '', 1, {}, [], new ArrayBuffer(1)]) { + assert.throws(() => crypto.getRandomValues(value), TypeError); +} + [ - undefined, null, '', 1, {}, [], new Float32Array(1), new Float64Array(1), new DataView(new ArrayBuffer(1)),