crypto: convert getRandomValues views · nodejs/node@6bfd392 · GitHub
Skip to content

Commit 6bfd392

Browse files
panvaaduh95
authored andcommitted
crypto: convert getRandomValues views
Apply Web IDL view conversion before checking the integer array type, including shared and resizable backing-buffer restrictions. 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 529bd18 commit 6bfd392

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 2 additions & 1 deletion

‎lib/internal/webidl.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,14 @@ function getViewedArrayBuffer(V) {
904904
TypedArrayPrototypeGetBuffer(V) : DataViewPrototypeGetBuffer(V);
905905
}
906906

907+
converters.ArrayBufferView = (V, options = kEmptyObject) => {
908+
if (!ArrayBufferIsView(V)) {
909+
throw makeException('is not an ArrayBufferView.', options);
910+
}
911+
validateBufferSourceBacking(getViewedArrayBuffer(V), options);
912+
return V;
913+
};
914+
907915
/**
908916
* Validates [AllowShared] and [AllowResizable] backing-store constraints.
909917
* @param {ArrayBuffer|SharedArrayBuffer} buffer Backing buffer.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
9+
for (const buffer of [
10+
new SharedArrayBuffer(8),
11+
new SharedArrayBuffer(8, { maxByteLength: 16 }),
12+
new ArrayBuffer(8, { maxByteLength: 16 }),
13+
]) {
14+
for (const Type of [Uint8Array, Float32Array, DataView]) {
15+
assert.throws(() => crypto.getRandomValues(new Type(buffer)), TypeError);
16+
}
17+
}
18+
19+
assert.throws(() => crypto.getRandomValues(new Proxy(new Uint8Array(8), {})),
20+
TypeError);

‎test/parallel/test-webcrypto-random.js‎

Lines changed: 4 additions & 1 deletion

0 commit comments

Comments
 (0)