diff --git a/lib/internal/crypto/webidl.js b/lib/internal/crypto/webidl.js index b9c65c91cfb..688a8111bae 100644 --- a/lib/internal/crypto/webidl.js +++ b/lib/internal/crypto/webidl.js @@ -16,6 +16,7 @@ const { kEmptyObject, } = require('internal/util'); const { + isInt32, isUint32, } = require('internal/validators'); const { @@ -513,6 +514,11 @@ converters.Pbkdf2Params = createDictionaryConverter( validator: (V, dict) => { if (V === 0) throw lazyDOMException('iterations cannot be zero', 'OperationError'); + if (!isInt32(V)) { + throw lazyDOMException( + 'iterations exceeds the implementation limit', + 'NotSupportedError'); + } }, required: true, }, diff --git a/test/fixtures/webcrypto/supports-level-2.mjs b/test/fixtures/webcrypto/supports-level-2.mjs index 3d0f8a63039..deec07dda90 100644 --- a/test/fixtures/webcrypto/supports-level-2.mjs +++ b/test/fixtures/webcrypto/supports-level-2.mjs @@ -124,6 +124,9 @@ export const vectors = { [true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, { name: 'HMAC', hash: 'SHA-256' }], + [false, + { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 }, + { name: 'AES-CBC', length: 128 }], [false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 'HKDF'], @@ -183,6 +186,7 @@ export const vectors = { [true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 8], [true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 0], [false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 0 }, 8], + [false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 }, 8], [false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, null], [false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 7], [false, { name: 'PBKDF2', hash: 'Invalid', salt: Buffer.alloc(0), iterations: 1 }, 8], diff --git a/test/parallel/test-webcrypto-derivebits.js b/test/parallel/test-webcrypto-derivebits.js index 6ef2227ab2d..6b0b9d0e36e 100644 --- a/test/parallel/test-webcrypto-derivebits.js +++ b/test/parallel/test-webcrypto-derivebits.js @@ -120,6 +120,28 @@ const rejectsXCurves = hasFIPS(3, 5); } } +// Test PBKDF2 rejects iteration counts beyond the native signed int range +{ + async function test() { + const key = await subtle.importKey( + 'raw', + new Uint8Array([1]), + 'PBKDF2', + false, + ['deriveBits']); + await assert.rejects( + subtle.deriveBits({ + name: 'PBKDF2', + hash: 'SHA-256', + salt: new Uint8Array([2]), + iterations: 2 ** 31, + }, key, 8), + { name: 'NotSupportedError' }); + } + + test().then(common.mustCall()); +} + // Test X25519 and X448 bit derivation { async function test(name) {