GeezerVison is showing github.com — crypto: copy parameters without array species · nodejs/node@7db05b5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +6-9Lines changed: 6 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ const { | 12 | 12 | DataViewPrototypeGetByteLength, | 13 | 13 | DataViewPrototypeGetByteOffset, | 14 | 14 | MathFloor, | | 15 | + MathMin, | 15 | 16 | Number, | 16 | 17 | ObjectDefineProperty, | 17 | 18 | ObjectEntries, | @@ -31,7 +32,6 @@ const { | 31 | 32 | TypedArrayPrototypeGetByteLength, | 32 | 33 | TypedArrayPrototypeGetByteOffset, | 33 | 34 | TypedArrayPrototypeGetLength, | 34 | | - TypedArrayPrototypeSlice, | 35 | 35 | Uint8Array, | 36 | 36 | } = primordials; | 37 | 37 |
| @@ -712,14 +712,11 @@ function truncateToBitLength(length, bytes) { | 712 | 712 | new Uint8Array( | 713 | 713 | getDataViewOrTypedArrayBuffer(bytes), | 714 | 714 | getDataViewOrTypedArrayByteOffset(bytes), | 715 | | - getDataViewOrTypedArrayByteLength(bytes), | | 715 | + MathMin(lengthBytes, getDataViewOrTypedArrayByteLength(bytes)), | 716 | 716 | ) : | 717 | | - new Uint8Array(bytes, 0, ArrayBufferPrototypeGetByteLength(bytes)); | 718 | | - const result = TypedArrayPrototypeSlice( | 719 | | - byteView, | 720 | | - 0, | 721 | | - lengthBytes, | 722 | | - ); | | 717 | + new Uint8Array(bytes, 0, | | 718 | + MathMin(lengthBytes, ArrayBufferPrototypeGetByteLength(bytes))); | | 719 | + const result = new Uint8Array(byteView); | 723 | 720 |
| 724 | 721 | const remainder = length % 8; | 725 | 722 | if (remainder !== 0) | @@ -795,7 +792,7 @@ function normalizeAlgorithm(algorithm, op) { | 795 | 792 | const idlValue = normalizedAlgorithm[member]; | 796 | 793 | // 3. | 797 | 794 | if (idlType === 'BufferSource' && idlValue) { | 798 | | - normalizedAlgorithm[member] = TypedArrayPrototypeSlice( | | 795 | + normalizedAlgorithm[member] = new Uint8Array( | 799 | 796 | getBufferSourceBytes(idlValue), | 800 | 797 | ); | 801 | 798 | } else if (idlType === 'HashAlgorithmIdentifier') { | |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +const { subtle } = globalThis.crypto; | | 9 | + | | 10 | +function withSpecies(callback) { | | 11 | + Object.defineProperty(Uint8Array, Symbol.species, { | | 12 | + configurable: true, | | 13 | + value: Float64Array, | | 14 | + }); | | 15 | + try { | | 16 | + return callback(); | | 17 | + } finally { | | 18 | + delete Uint8Array[Symbol.species]; | | 19 | + } | | 20 | +} | | 21 | + | | 22 | +(async () => { | | 23 | + const key = await subtle.importKey('raw', new Uint8Array(16), | | 24 | + 'AES-GCM', false, ['encrypt']); | | 25 | + const algorithm = { name: 'AES-GCM', iv: new Uint8Array(12).fill(1) }; | | 26 | + const data = new Uint8Array(16); | | 27 | + const expected = await subtle.encrypt(algorithm, key, data); | | 28 | + assert.deepStrictEqual(await withSpecies(() => subtle.encrypt(algorithm, key, data)), expected); | | 29 | + | | 30 | + const hmac = { name: 'HMAC', hash: 'SHA-256', length: 100 }; | | 31 | + const secret = new Uint8Array(13).fill(1); | | 32 | + const normal = await subtle.importKey('raw', secret, hmac, true, ['sign']); | | 33 | + const tampered = await withSpecies(() => subtle.importKey('raw', secret, hmac, true, ['sign'])); | | 34 | + assert.deepStrictEqual(await subtle.exportKey('raw', tampered), await subtle.exportKey('raw', normal)); | | 35 | + assert.deepStrictEqual(await subtle.sign('HMAC', tampered, data), await subtle.sign('HMAC', normal, data)); | | 36 | +})().then(common.mustCall()); |
|