From: Dmitry Volyntsev Date: Tue, 17 Nov 2026 20:38:50 +0000 (-0800) Subject: WebCrypto: free QuickJS algorithm parameters on conversion errors X-Git-Url: https://git.kaiwu.me/http/ngx_http_auth_basic_module.html?a=commitdiff_plain;h=932aadb95b9a444b4f79e14208c20cbc745183f5;p=njs.git WebCrypto: free QuickJS algorithm parameters on conversion errors 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). --- diff --git a/external/qjs_webcrypto_module.c b/external/qjs_webcrypto_module.c index f4e2ca27..a32b8d71 100644 --- a/external/qjs_webcrypto_module.c +++ b/external/qjs_webcrypto_module.c @@ -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 index 00000000..0a1497ac --- /dev/null +++ b/test/webcrypto/conversion_errors.t.mjs @@ -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);