]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: fix HKDF and PBKDF2 key algorithm in QuickJS
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 19 Nov 2026 01:05:29 +0000 (17:05 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 19 Nov 2026 06:07:22 +0000 (22:07 -0800)
Previously, reading key.algorithm on an HKDF or PBKDF2 key passed a
NULL hash name to JS_NewString() and crashed the process. These keys
went through the HMAC branch, although they have no hash. Report only
the algorithm name for them.

The issue was introduced in 75ca26f8 (0.8.10).

external/qjs_webcrypto_module.c
test/webcrypto/key_algorithm.t.mjs [new file with mode: 0644]

index 311e32bf63fd251a1b910adffe6d989b0f63524e..48b91bf67883f90db767c7ae67c36710f02e4bf3 100644 (file)
@@ -5467,6 +5467,8 @@ qjs_webcrypto_key_algorithm(JSContext *cx, JSValueConst this_val)
 
     case QJS_ALGORITHM_ED25519:
     case QJS_ALGORITHM_X25519:
+    case QJS_ALGORITHM_PBKDF2:
+    case QJS_ALGORITHM_HKDF:
         break;
 
     case QJS_ALGORITHM_HMAC:
diff --git a/test/webcrypto/key_algorithm.t.mjs b/test/webcrypto/key_algorithm.t.mjs
new file mode 100644 (file)
index 0000000..0285e71
--- /dev/null
@@ -0,0 +1,30 @@
+/*---
+includes: [compatWebcrypto.js, runTsuite.js]
+flags: [async]
+---*/
+
+async function test(params) {
+    let key = await crypto.subtle.importKey('raw', new Uint8Array(16),
+                                            params.name, false,
+                                            ['deriveBits']);
+    let algorithm = key.algorithm;
+
+    if (algorithm.name !== params.name) {
+        throw Error(`unexpected algorithm: ${JSON.stringify(algorithm)}`);
+    }
+
+    return 'SUCCESS';
+}
+
+let key_algorithm_tsuite = {
+    name: 'derivation key algorithm',
+    skip: () => !has_webcrypto(),
+    T: test,
+    tests: [
+        { name: 'HKDF' },
+        { name: 'PBKDF2' },
+    ],
+};
+
+run([key_algorithm_tsuite])
+.then($DONE, $DONE);