From: Dmitry Volyntsev Date: Wed, 18 Nov 2026 01:17:09 +0000 (-0800) Subject: WebCrypto: release invalid oct JWK key values in QuickJS X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=8ed912938127f28d06fbf22f8be24c4f44e15bad;p=njs.git WebCrypto: release invalid oct JWK key values in QuickJS 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). --- diff --git a/external/qjs_webcrypto_module.c b/external/qjs_webcrypto_module.c index 102197ea..483e84c2 100644 --- a/external/qjs_webcrypto_module.c +++ b/external/qjs_webcrypto_module.c @@ -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; } diff --git a/test/webcrypto/jwk_errors.t.mjs b/test/webcrypto/jwk_errors.t.mjs index 25ad4ca7..f4bf607f 100644 --- a/test/webcrypto/jwk_errors.t.mjs +++ b/test/webcrypto/jwk_errors.t.mjs @@ -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);