]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: release invalid oct JWK key values in QuickJS
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 18 Nov 2026 01:17:09 +0000 (17:17 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 19 Nov 2026 06:07:22 +0000 (22:07 -0800)
Reading the k property acquires a reference even when the value is
not a string. Release it on rejection. If the property access throws,
propagate its exception rather than replacing it with a TypeError.

The issue was introduced in 75ca26f8 (0.8.10).

external/qjs_webcrypto_module.c
test/webcrypto/jwk_errors.t.mjs

index 102197eae7c0a43d47663a47ac0b12511b806ce6..483e84c29d0cc7557000fb827fc921739e0ebf69 100644 (file)
@@ -3930,7 +3930,12 @@ qjs_import_jwk_oct(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key)
     };
 
     val = JS_GetPropertyStr(cx, jwk, "k");
-    if (JS_IsException(val) || !JS_IsString(val)) {
+    if (JS_IsException(val)) {
+        return JS_EXCEPTION;
+    }
+
+    if (!JS_IsString(val)) {
+        JS_FreeValue(cx, val);
         JS_ThrowTypeError(cx, "Invalid JWK oct key");
         return JS_EXCEPTION;
     }
index 25ad4ca7b32668c4c39dcab0f49c799cffd222f1..f4bf607f11484736e27c7cff6543dac996dca0e3 100644 (file)
@@ -34,5 +34,32 @@ let jwk_errors_tsuite = {
     ],
 };
 
-run([jwk_errors_tsuite])
+/*
+ * Implementations may reject a non-string "k" or convert it to a string.
+ * The test only checks that the attempt does not leak "k".
+ */
+async function test_oct(params) {
+    try {
+        await crypto.subtle.importKey('jwk',
+                                      { kty: 'oct', alg: 'HS256', k: params.k },
+                                      { name: 'HMAC', hash: 'SHA-256' },
+                                      true, ['sign']);
+
+    } catch (e) {
+    }
+
+    return 'SUCCESS';
+}
+
+let jwk_oct_tsuite = {
+    name: 'JWK oct key',
+    skip: () => !has_webcrypto(),
+    T: test_oct,
+    prepare_args: (args) => args,
+    tests: [
+        { k: {} },
+    ],
+};
+
+run([jwk_errors_tsuite, jwk_oct_tsuite])
 .then($DONE, $DONE);