From: Dmitry Volyntsev Date: Fri, 20 Nov 2026 02:41:52 +0000 (-0800) Subject: WebCrypto: reject out-of-range PBKDF2 iterations X-Git-Url: https://git.kaiwu.me/web/http/static/gitweb.js?a=commitdiff_plain;h=13fa137fe3c321ea8006122a46a18ddfe4a2f907;p=njs.git WebCrypto: reject out-of-range PBKDF2 iterations Previously, PBKDF2 converted the iteration count to int64_t and passed it to PKCS5_PBKDF2_HMAC(), which takes int. Counts such as 2^32 + 1 were silently narrowed to one iteration, producing a weaker derivation than requested. The same conversion could turn negative counts into valid positive counts, and JS_ToInt64() in QuickJS also wrapped counts modulo 2^64. Convert QuickJS iteration counts with saturation and check that the converted count is in 1..INT_MAX before the OpenSSL call. Reject unsupported counts explicitly instead of narrowing them. The int limit is imposed by the OpenSSL interface; WebCrypto defines iterations as an unsigned long and rejects zero (section 34.4.1). The issue was introduced in 7b2b7612 (0.7.0) and inherited by the QuickJS implementation in 75ca26f8 (0.8.10). --- diff --git a/external/njs_webcrypto_module.c b/external/njs_webcrypto_module.c index 6a3723cc..38357afa 100644 --- a/external/njs_webcrypto_module.c +++ b/external/njs_webcrypto_module.c @@ -2081,6 +2081,11 @@ njs_ext_derive(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, goto fail; } + if (njs_slow_path(iterations < 1 || iterations > INT_MAX)) { + njs_vm_type_error(vm, "PBKDF2 iterations is out of range"); + goto fail; + } + md = njs_algorithm_hash_digest(hash); ret = PKCS5_PBKDF2_HMAC((char *) key->u.s.raw.start, key->u.s.raw.length, diff --git a/external/qjs_webcrypto_module.c b/external/qjs_webcrypto_module.c index fbcf07d7..f27103ee 100644 --- a/external/qjs_webcrypto_module.c +++ b/external/qjs_webcrypto_module.c @@ -2536,13 +2536,18 @@ qjs_webcrypto_derive(JSContext *cx, JSValueConst this_val, int argc, goto fail; } - if (JS_ToInt64(cx, &iterations, ret) < 0) { + if (qjs_value_to_integer(cx, ret, &iterations) < 0) { JS_FreeValue(cx, ret); goto fail; } JS_FreeValue(cx, ret); + if (iterations < 1 || iterations > INT_MAX) { + JS_ThrowTypeError(cx, "PBKDF2 iterations is out of range"); + goto fail; + } + md = qjs_algorithm_hash_digest(hash); rc = PKCS5_PBKDF2_HMAC((char *) key->u.s.raw.start, key->u.s.raw.length, diff --git a/test/webcrypto/derive.t.mjs b/test/webcrypto/derive.t.mjs index 577a0930..3e53374d 100644 --- a/test/webcrypto/derive.t.mjs +++ b/test/webcrypto/derive.t.mjs @@ -199,5 +199,24 @@ let derive_length_tsuite = { ], }; -run([derive_tsuite, derive_length_tsuite]) +let derive_iterations_tsuite = { + name: "PBKDF2 iteration validation", + skip: () => (!has_buffer() || !has_webcrypto()), + T: testDeriveParameters, + opts: { name: "PBKDF2", length: 128, derive: "bits", reject: true }, + tests: [ + { iterations: 0 }, + { iterations: -1 }, + { iterations: 0x80000000 }, + { iterations: 0x100000001 }, + { iterations: 2 ** 64 + 4096 }, + { iterations: -0xffffffff }, + { iterations: NaN }, + { iterations: Infinity }, + { iterations: 0x100000001, derive: "key" }, + { iterations: 1, reject: false, bytes: 16 }, + ], +}; + +run([derive_tsuite, derive_length_tsuite, derive_iterations_tsuite]) .then($DONE, $DONE);