From 92f17bda64219e44585977243ab384a3e2d83045 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Mon, 21 Sep 2026 16:22:24 -0700 Subject: [PATCH] Webcrypto: validate EC JWK curve type --- external/njs_webcrypto_module.c | 17 ++++++++++------- test/webcrypto/import.t.mjs | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/external/njs_webcrypto_module.c b/external/njs_webcrypto_module.c index 9dfc3a7e..0d54a262 100644 --- a/external/njs_webcrypto_module.c +++ b/external/njs_webcrypto_module.c @@ -3584,14 +3584,17 @@ fail0: curve = 0; val = njs_vm_object_prop(vm, jwk, &string_crv, &value); - if (val != NULL && !njs_value_is_undefined(val)) { - njs_value_string_get(vm, val, &name); + if (val == NULL || !njs_value_is_string(val)) { + njs_vm_type_error(vm, "Invalid JWK EC key"); + return NULL; + } - for (e = &njs_webcrypto_curve[0]; e->name.length != 0; e++) { - if (njs_strstr_eq(&name, &e->name)) { - curve = e->value; - break; - } + njs_value_string_get(vm, val, &name); + + for (e = &njs_webcrypto_curve[0]; e->name.length != 0; e++) { + if (njs_strstr_eq(&name, &e->name)) { + curve = e->value; + break; } } diff --git a/test/webcrypto/import.t.mjs b/test/webcrypto/import.t.mjs index 2d1ea70c..9972d68d 100644 --- a/test/webcrypto/import.t.mjs +++ b/test/webcrypto/import.t.mjs @@ -269,15 +269,25 @@ let ec_tsuite = { extractable: true, usage: [ "sign" ] }, exception: "TypeError: Invalid JWK EC key" }, + { key: { fmt: "jwk", + key: { kty: "EC", + x: "cUUsZRGuVYReGiTQl9MqR7ir6dZIgw-8pM-F0phJWdw", + y: "4Nzn_7uNz0AA3U4fhpfVxSD4U5QciGyEoM4r3jC7bjI", + d: "E2sW0_4a3QXaSTJ0JKbSUbieKTD1UFtr7i_2CuetP6A" }, + alg: { name: "ECDSA", namedCurve: "P-256" }, + extractable: true, + usage: [ "sign" ] }, + exception: "TypeError: Invalid JWK EC key" }, { key: { fmt: "jwk", key: { kty: "EC", x: "cUUsZRGuVYReGiTQl9MqR7ir6dZIgw-8pM-F0phJWdw", y: "4Nzn_7uNz0AA3U4fhpfVxSD4U5QciGyEoM4r3jC7bjI", - d: "E2sW0_4a3QXaSTJ0JKbSUbieKTD1UFtr7i_2CuetP6A" }, + d: "E2sW0_4a3QXaSTJ0JKbSUbieKTD1UFtr7i_2CuetP6A", + crv: 256 }, alg: { name: "ECDSA", namedCurve: "P-256" }, extractable: true, usage: [ "sign" ] }, - exception: "JWK EC curve mismatch" }, + exception: "TypeError: Invalid JWK EC key" }, { key: { fmt: "jwk", key: { kty: "EC", x: "cUUsZRGuVYReGiTQl9MqR7ir6dZIgw-8pM-F0phJWdw", -- 2.47.3