]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: reject out-of-range PBKDF2 iterations
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 20 Nov 2026 02:41:52 +0000 (18:41 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 1 Dec 2026 21:42:41 +0000 (13:42 -0800)
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).

external/njs_webcrypto_module.c
external/qjs_webcrypto_module.c
test/webcrypto/derive.t.mjs

index 6a3723cc3b9cd239021801de8f2a140e3a16df87..38357afac8e1d29583b2eb007fd93e6e2ae79220 100644 (file)
@@ -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,
index fbcf07d74aa8fc0c0f6e3b438a90e2dc7dd5ff3f..f27103eede93fc0eb22de80b252a5ea0321438de 100644 (file)
@@ -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,
index 577a0930cf3d8e7a8a9f45d42a42f1bf400d3817..3e53374d7b8246165df1f05bc9e64af57f205af7 100644 (file)
@@ -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);