]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: preserve pending exceptions in QuickJS master
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 17 Nov 2026 21:23:52 +0000 (13:23 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 17 Nov 2026 22:09:33 +0000 (14:09 -0800)
Previously, failed string conversions were reported as out of memory,
although toString() can throw or the input can be a Symbol.  A Symbol
in keyUsages therefore raised a false, uncatchable OOM in QuickJS-NG.
RSA and EC JWK import also replaced existing conversion, validation
and native exceptions with a generic "Invalid JWK ... key" error
from shared cleanup.

Keep the exception set by the failed operation.  Throw the generic
JWK error only for invalid key members, not from shared cleanup.

The issues were introduced in 75ca26f8 (0.8.10).

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

index a32b8d715cb7e41d48750c86d7a36ce4633ad417..311e32bf63fd251a1b910adffe6d989b0f63524e 100644 (file)
@@ -3248,7 +3248,6 @@ qjs_import_base64url_bignum(JSContext *cx, JSValue value)
 
     data.start = (u_char *) JS_ToCStringLen(cx, &data.length, value);
     if (data.start == NULL) {
-        JS_ThrowOutOfMemory(cx);
         return NULL;
     }
 
@@ -3309,11 +3308,11 @@ qjs_import_jwk_rsa(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key)
         || !JS_IsString(e)
         || (!JS_IsUndefined(d) && !JS_IsString(d)))
     {
+        JS_ThrowTypeError(cx, "Invalid JWK RSA key");
 fail0:
         JS_FreeValue(cx, n);
         JS_FreeValue(cx, e);
         JS_FreeValue(cx, d);
-        JS_ThrowTypeError(cx, "Invalid JWK RSA key");
         return NULL;
     }
 
@@ -3338,7 +3337,6 @@ fail0:
         alg.start = (u_char *) JS_ToCStringLen(cx, &alg.length, ret);
         JS_FreeValue(cx, ret);
         if (alg.start == NULL) {
-            JS_ThrowOutOfMemory(cx);
             goto fail0;
         }
 
@@ -3402,27 +3400,27 @@ fail0:
 
     p = JS_GetPropertyStr(cx, jwk, "p");
     if (JS_IsException(p)) {
-        goto fail1;
+        goto fail;
     }
 
     q = JS_GetPropertyStr(cx, jwk, "q");
     if (JS_IsException(q)) {
-        goto fail1;
+        goto fail;
     }
 
     dp = JS_GetPropertyStr(cx, jwk, "dp");
     if (JS_IsException(dp)) {
-        goto fail1;
+        goto fail;
     }
 
     dq = JS_GetPropertyStr(cx, jwk, "dq");
     if (JS_IsException(dq)) {
-        goto fail1;
+        goto fail;
     }
 
     qi = JS_GetPropertyStr(cx, jwk, "qi");
     if (JS_IsException(qi)) {
-        goto fail1;
+        goto fail;
     }
 
     if (!JS_IsString(d)
@@ -3432,7 +3430,6 @@ fail0:
         || !JS_IsString(dq)
         || !JS_IsString(qi))
     {
-fail1:
         JS_ThrowTypeError(cx, "Invalid JWK RSA key");
         goto fail;
     }
@@ -3581,11 +3578,11 @@ qjs_import_jwk_ec(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key)
         || !JS_IsString(y)
         || (!JS_IsUndefined(d) && !JS_IsString(d)))
     {
+        JS_ThrowTypeError(cx, "Invalid JWK EC key");
 fail0:
         JS_FreeValue(cx, x);
         JS_FreeValue(cx, y);
         JS_FreeValue(cx, d);
-        JS_ThrowTypeError(cx, "Invalid JWK EC key");
         return NULL;
     }
 
@@ -3626,7 +3623,6 @@ fail0:
         name.start = (u_char *) JS_ToCStringLen(cx, &name.length, ret);
         JS_FreeValue(cx, ret);
         if (name.start == NULL) {
-            JS_ThrowOutOfMemory(cx);
             goto fail0;
         }
 
@@ -3966,7 +3962,6 @@ qjs_import_jwk_oct(JSContext *cx, JSValue jwk, qjs_webcrypto_key_t *key)
     alg.start = (u_char *) JS_ToCStringLen(cx, &alg.length, val);
     JS_FreeValue(cx, val);
     if (alg.start == NULL) {
-        JS_ThrowOutOfMemory(cx);
         return JS_EXCEPTION;
     }
 
@@ -5966,7 +5961,6 @@ qjs_key_usage(JSContext *cx, JSValue value, unsigned *mask)
         s.start = (u_char *) JS_ToCStringLen(cx, &s.length, v);
         JS_FreeValue(cx, v);
         if (s.start == NULL) {
-            JS_ThrowOutOfMemory(cx);
             return JS_EXCEPTION;
         }
 
diff --git a/test/webcrypto/jwk_errors.t.mjs b/test/webcrypto/jwk_errors.t.mjs
new file mode 100644 (file)
index 0000000..25ad4ca
--- /dev/null
@@ -0,0 +1,38 @@
+/*---
+includes: [compatFs.js, compatWebcrypto.js, runTsuite.js, webCryptoUtils.js]
+flags: [async]
+---*/
+
+async function test(params) {
+    let jwk = load_jwk(params.jwk);
+
+    jwk.key_ops = ['foo'];
+
+    try {
+        await crypto.subtle.importKey('jwk', jwk, params.alg, true,
+                                      ['verify']);
+
+    } catch (e) {
+        if (e.message.startsWith('Invalid JWK')) {
+            throw Error(`key_ops error was replaced: ${e}`);
+        }
+
+        return 'SUCCESS';
+    }
+
+    throw Error('invalid key_ops was accepted');
+}
+
+let jwk_errors_tsuite = {
+    name: 'JWK import error reporting',
+    skip: () => !has_webcrypto(),
+    T: test,
+    prepare_args: (args) => args,
+    tests: [
+        { jwk: 'rsa.pub.jwk', alg: {name: 'RSA-PSS', hash: 'SHA-256'} },
+        { jwk: 'ec.pub.jwk', alg: {name: 'ECDSA', namedCurve: 'P-256'} },
+    ],
+};
+
+run([jwk_errors_tsuite])
+.then($DONE, $DONE);
diff --git a/test/webcrypto/key_usages.t.mjs b/test/webcrypto/key_usages.t.mjs
new file mode 100644 (file)
index 0000000..7bca72c
--- /dev/null
@@ -0,0 +1,37 @@
+/*---
+includes: [compatWebcrypto.js, runTsuite.js]
+flags: [async]
+---*/
+
+async function test(params) {
+    let marker = Error('keyUsages conversion');
+    let usage = params.usage ? params.usage
+                             : {toString() { throw marker; }};
+
+    try {
+        await crypto.subtle.importKey('raw', new Uint8Array(16), 'AES-GCM',
+                                      false, ['encrypt', usage]);
+
+    } catch (e) {
+        if (params.usage ? !(e instanceof TypeError) : e !== marker) {
+            throw Error(`unexpected keyUsages conversion error: ${e}`);
+        }
+
+        return 'SUCCESS';
+    }
+
+    throw Error('importKey accepted invalid keyUsages');
+}
+
+let key_usages_tsuite = {
+    name: 'keyUsages conversion failure',
+    skip: () => !has_webcrypto(),
+    T: test,
+    tests: [
+        {},
+        {usage: Symbol('usage')},
+    ],
+};
+
+run([key_usages_tsuite])
+.then($DONE, $DONE);