]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: reject empty HMAC keys in importKey() master
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 13 Nov 2026 02:26:58 +0000 (18:26 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 16 Nov 2026 18:13:27 +0000 (10:13 -0800)
Previously, importKey() accepted an empty raw or JWK HMAC key.  The
HMAC import key operation requires a DataError when the key data
length is zero.  It is reported as TypeError, as other invalid key
data in njs.

The issue was introduced in 7b2b7612 (0.7.0) and the QuickJS
counterpart in 75ca26f8 (0.8.10).

external/njs_webcrypto_module.c
external/qjs_webcrypto_module.c
test/webcrypto/import.t.mjs

index 61b3e6c96316614fc5c202ebce8ee2c8c4eae847..f12244ead3556ba4138599905708d72700929639 100644 (file)
@@ -4298,6 +4298,11 @@ njs_webcrypto_import_key_internal(njs_vm_t *vm, njs_webcrypto_key_format_t fmt,
             }
         }
 
             }
         }
 
+        if (key->u.s.raw.length == 0) {
+            njs_vm_type_error(vm, "HMAC key cannot be empty");
+            goto fail;
+        }
+
         break;
 
     case NJS_ALGORITHM_AES_GCM:
         break;
 
     case NJS_ALGORITHM_AES_GCM:
index 5defe8c989b07c9589d169e190a61af0f9ae410d..f4b22b699f6e9007275044a3ba1ae8ef638d50e0 100644 (file)
@@ -4294,6 +4294,11 @@ qjs_webcrypto_import_key_internal(JSContext *cx, qjs_webcrypto_key_format_t fmt,
             }
         }
 
             }
         }
 
+        if (wkey->u.s.raw.length == 0) {
+            JS_ThrowTypeError(cx, "HMAC key cannot be empty");
+            goto fail;
+        }
+
         break;
 
     case QJS_ALGORITHM_AES_GCM:
         break;
 
     case QJS_ALGORITHM_AES_GCM:
index 9972d68d301c64e98c236428cf6e74ec156eca70..0160d7c423e1117a9ed21e501cb0ada16df64be5 100644 (file)
@@ -424,6 +424,12 @@ let hmac_tsuite = {
                extractable: true,
                usage: [ "sign", "verify" ] },
         exception: "TypeError: unknown hash name: \"SHA-385\"" },
                extractable: true,
                usage: [ "sign", "verify" ] },
         exception: "TypeError: unknown hash name: \"SHA-385\"" },
+      { key: { fmt: "raw",
+               key: "",
+               alg: { name: "HMAC", hash: "SHA-256" },
+               extractable: true,
+               usage: [ "sign", "verify" ] },
+        exception: "TypeError: HMAC key cannot be empty" },
       { key: { fmt: "spki",
                key: "ec.spki",
                alg: { name: "HMAC" },
       { key: { fmt: "spki",
                key: "ec.spki",
                alg: { name: "HMAC" },
@@ -489,6 +495,12 @@ let hmac_tsuite = {
                extractable: true,
                usage: [ "sign" ]},
         exception: "TypeError: Invalid JWK oct key" },
                extractable: true,
                usage: [ "sign" ]},
         exception: "TypeError: Invalid JWK oct key" },
+      { key: { fmt: "jwk",
+               key: { alg: 'HS256', ext: true, k: '', kty: 'oct' },
+               alg: { name: "HMAC", hash: "SHA-256" },
+               extractable: true,
+               usage: [ "sign" ]},
+        exception: "TypeError: HMAC key cannot be empty" },
       { key: { fmt: "jwk",
                key: { alg: 'HS256', k: 'AA', key_ops: [ 'sign' ], kty: 'oct' },
                alg: { name: "HMAC", hash: "SHA-256" },
       { key: { fmt: "jwk",
                key: { alg: 'HS256', k: 'AA', key_ops: [ 'sign' ], kty: 'oct' },
                alg: { name: "HMAC", hash: "SHA-256" },