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).
data.start = (u_char *) JS_ToCStringLen(cx, &data.length, value);
if (data.start == NULL) {
- JS_ThrowOutOfMemory(cx);
return NULL;
}
|| !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;
}
alg.start = (u_char *) JS_ToCStringLen(cx, &alg.length, ret);
JS_FreeValue(cx, ret);
if (alg.start == NULL) {
- JS_ThrowOutOfMemory(cx);
goto 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)
|| !JS_IsString(dq)
|| !JS_IsString(qi))
{
-fail1:
JS_ThrowTypeError(cx, "Invalid JWK RSA key");
goto fail;
}
|| !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;
}
name.start = (u_char *) JS_ToCStringLen(cx, &name.length, ret);
JS_FreeValue(cx, ret);
if (name.start == NULL) {
- JS_ThrowOutOfMemory(cx);
goto fail0;
}
alg.start = (u_char *) JS_ToCStringLen(cx, &alg.length, val);
JS_FreeValue(cx, val);
if (alg.start == NULL) {
- JS_ThrowOutOfMemory(cx);
return JS_EXCEPTION;
}
s.start = (u_char *) JS_ToCStringLen(cx, &s.length, v);
JS_FreeValue(cx, v);
if (s.start == NULL) {
- JS_ThrowOutOfMemory(cx);
return JS_EXCEPTION;
}
--- /dev/null
+/*---
+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);
--- /dev/null
+/*---
+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);