From fbd137cf836e51a395c352a2992aea73bd1d101f Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 30 May 2026 23:07:37 -0700 Subject: [PATCH] Narrow `ArrayBufferView` to `Uint8Array` `ArrayBufferView` includes `DataView` which causes a type error when passed to the Web Crypto API. Although any typed array would work here, it is most common to use `Uint8Array` for this kind of data, so it should not cause any problems narrowing the required type. --- src/crypto.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/crypto.js b/src/crypto.js index e81b4b0..2dd5146 100644 --- a/src/crypto.js +++ b/src/crypto.js @@ -2,9 +2,9 @@ /** * Create a `CryptoKey` from a high-entropy random value using HKDF. - * @param {ArrayBuffer|ArrayBufferView} rand A high-entropy random value. - * @param {ArrayBuffer|ArrayBufferView} salt A cryptographic salt value. - * @param {ArrayBuffer|ArrayBufferView} info Additional contextual information. + * @param {ArrayBuffer|Uint8Array} rand A high-entropy random value. + * @param {ArrayBuffer|Uint8Array} salt A cryptographic salt value. + * @param {ArrayBuffer|Uint8Array} info Additional contextual information. * @returns {Promise} A `CryptoKey` that can be used for `"encrypt"` or `"decrypt"`. */ async function getKey(rand, salt, info) { @@ -36,7 +36,7 @@ async function getKey(rand, salt, info) { * @typedef {Object} EncryptRawOptions * @property {EnvpassVersion} [version] Encryption version to use. Currently, only `'v1'` is supported. The default * value is `'v1'`. - * @property {ArrayBuffer|ArrayBufferView} prf Value returned by the passkey's pseudo-random function, + * @property {ArrayBuffer|Uint8Array} prf Value returned by the passkey's pseudo-random function, * which will be used to derive the cryptographic key to encrypt the variables. * @property {string} value Plaintext value to encrypt. */ @@ -51,7 +51,7 @@ export async function encryptRaw(options) { const { version = 'v1', prf, value } = options; if (!ArrayBuffer.isView(prf) && !(prf instanceof ArrayBuffer)) { - throw new TypeError(`prf "${prf}" is not an ArrayBuffer or ArrayBufferView.`); + throw new TypeError(`prf "${prf}" is not an ArrayBuffer or Uint8Array.`); } if (typeof value !== 'string') { @@ -95,7 +95,7 @@ export async function encryptRaw(options) { /** * @typedef {Object} DecryptRawOptions - * @property {ArrayBuffer|ArrayBufferView} prf Value returned by the passkey's pseudo-random function, + * @property {ArrayBuffer|Uint8Array} prf Value returned by the passkey's pseudo-random function, * which will be used to derive the cryptographic key to decrypt the variables. * @property {EnvpassEncryptedValue} value Encrypted ciphertext value to decrypt. */ @@ -111,7 +111,7 @@ export async function decryptRaw(options) { const [magic, version, valueBase64] = value.split(':'); if (!ArrayBuffer.isView(prf) && !(prf instanceof ArrayBuffer)) { - throw new TypeError(`prf "${prf}" is not an ArrayBuffer or ArrayBufferView.`); + throw new TypeError(`prf "${prf}" is not an ArrayBuffer or Uint8Array.`); } if (magic !== 'envpass') { -- 2.47.3