]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: free QuickJS algorithm parameters on conversion errors
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 17 Nov 2026 20:38:50 +0000 (12:38 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 17 Nov 2026 22:09:33 +0000 (14:09 -0800)
Previously, when converting an algorithm parameter failed, the value
read from the algorithm object was not freed: AES-GCM iv, tagLength
and additionalData, AES-CTR counter and length, AES-CBC iv, the
derived key length and PBKDF2 iterations.  For example, encrypt()
with an additionalData that is not a BufferSource leaked it; with
debug QuickJS this triggered the gc_obj_list assertion in
JS_FreeRuntime().

Free the value before returning the error.

The issue was introduced in 75ca26f8 (0.8.10).

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

index f4e2ca27a2fdcfd8dc8c4e5c544845a62cb9a1ec..a32b8d715cb7e41d48750c86d7a36ce4633ad417 100644 (file)
@@ -857,6 +857,7 @@ qjs_cipher_aes_gcm(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
 
     ret = qjs_typed_array_data(cx, value, &iv);
     if (JS_IsException(ret)) {
+        JS_FreeValue(cx, value);
         return JS_EXCEPTION;
     }
 
@@ -871,6 +872,7 @@ qjs_cipher_aes_gcm(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
 
     if (!JS_IsUndefined(value)) {
         if (JS_ToInt64(cx, &taglen, value) < 0) {
+            JS_FreeValue(cx, value);
             return JS_EXCEPTION;
         }
     }
@@ -933,6 +935,7 @@ qjs_cipher_aes_gcm(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
     if (!JS_IsUndefined(value)) {
         ret = qjs_typed_array_data(cx, value, &aad);
         if (JS_IsException(ret)) {
+            JS_FreeValue(cx, value);
             goto fail;
         }
     }
@@ -1151,6 +1154,7 @@ qjs_cipher_aes_ctr(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
 
     ret = qjs_typed_array_data(cx, value, &iv);
     if (JS_IsException(ret)) {
+        JS_FreeValue(cx, value);
         return JS_EXCEPTION;
     }
 
@@ -1173,6 +1177,7 @@ qjs_cipher_aes_ctr(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
     }
 
     if (JS_ToInt64(cx, &length, value) < 0) {
+        JS_FreeValue(cx, value);
         return JS_EXCEPTION;
     }
 
@@ -1361,6 +1366,7 @@ qjs_cipher_aes_cbc(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
 
     ret = qjs_typed_array_data(cx, value, &iv);
     if (JS_IsException(ret)) {
+        JS_FreeValue(cx, value);
         return JS_EXCEPTION;
     }
 
@@ -2333,6 +2339,7 @@ qjs_webcrypto_derive(JSContext *cx, JSValueConst this_val, int argc,
     }
 
     if (JS_ToInt64(cx, &length, ret) < 0) {
+        JS_FreeValue(cx, ret);
         return JS_EXCEPTION;
     }
 
@@ -2429,6 +2436,7 @@ qjs_webcrypto_derive(JSContext *cx, JSValueConst this_val, int argc,
         }
 
         if (JS_ToInt64(cx, &iterations, ret) < 0) {
+            JS_FreeValue(cx, ret);
             goto fail;
         }
 
diff --git a/test/webcrypto/conversion_errors.t.mjs b/test/webcrypto/conversion_errors.t.mjs
new file mode 100644 (file)
index 0000000..0a1497a
--- /dev/null
@@ -0,0 +1,66 @@
+/*---
+includes: [compatWebcrypto.js, runTsuite.js]
+flags: [async]
+---*/
+
+async function test(params) {
+    let key = await crypto.subtle.importKey('raw', new Uint8Array(16),
+                                            params.key, false, params.usage);
+    let bad = {
+        valueOf() { throw Error('marker'); },
+        toString() { throw Error('marker'); },
+    };
+
+    try {
+        await params.call(key, bad);
+
+    } catch (e) {
+        return 'SUCCESS';
+    }
+
+    throw Error('invalid parameter was accepted');
+}
+
+let data = new Uint8Array(16);
+let salt = new Uint8Array(16);
+
+let conversion_errors_tsuite = {
+    name: 'algorithm parameter conversion failure',
+    skip: () => !has_webcrypto(),
+    T: test,
+    prepare_args: (args) => args,
+    tests: [
+        { key: 'AES-GCM', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-GCM', iv: bad}, key, data) },
+        { key: 'AES-GCM', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-GCM', iv: new Uint8Array(12), tagLength: bad},
+              key, data) },
+        { key: 'AES-GCM', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-GCM', iv: new Uint8Array(12), additionalData: bad},
+              key, data) },
+        { key: 'AES-CTR', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-CTR', counter: bad, length: 64}, key, data) },
+        { key: 'AES-CTR', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-CTR', counter: new Uint8Array(16), length: bad},
+              key, data) },
+        { key: 'AES-CBC', usage: ['encrypt'],
+          call: (key, bad) => crypto.subtle.encrypt(
+              {name: 'AES-CBC', iv: bad}, key, data) },
+        { key: 'PBKDF2', usage: ['deriveKey'],
+          call: (key, bad) => crypto.subtle.deriveKey(
+              {name: 'PBKDF2', hash: 'SHA-256', salt, iterations: 1}, key,
+              {name: 'AES-CBC', length: bad}, false, ['encrypt']) },
+        { key: 'PBKDF2', usage: ['deriveBits'],
+          call: (key, bad) => crypto.subtle.deriveBits(
+              {name: 'PBKDF2', hash: 'SHA-256', salt, iterations: bad},
+              key, 128) },
+    ],
+};
+
+run([conversion_errors_tsuite])
+.then($DONE, $DONE);