Lightpanda: the headless browser designed for AI and automation
webapi: unwrapKey checks the unwrapped hash only for HMAC and RSA
orezraeyadabd9cf5a16
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.htmlindex ad75dac42..1597ec8a0 100644--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@ -1686,6 +1686,13 @@unwrapUnknownFirst: await errorName(() => subtle.unwrapKey("raw", wrapped, kw, "foo", {}, true, ["encrypt"])),unwrapBeforeKey: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", {}, true, ["encrypt"])),unwrapHashBeforeKey: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", { name: "HMAC", hash: {} }, true, ["sign"])),+ // For HMAC and RSA, which take a hash, a missing or unknown one is+ // rejected there too; another algorithm's stray hash is ignored.+ unwrapNoHashBeforeKey: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", "HMAC", true, ["sign"])),+ unwrapUnknownHashBeforeKey: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", { name: "HMAC", hash: "SHA-999" }, true, ["sign"])),+ unwrapRsaUnknownHash: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", { name: "RSA-OAEP", hash: "SHA-999" }, true, ["encrypt"])),+ unwrapAesStrayHash: await errorName(() => subtle.unwrapKey("raw", wrapped, kw, "AES-KW", { name: "AES-GCM", hash: {} }, true, ["encrypt"])),+ unwrapAesStrayHashKey: await errorName(() => subtle.unwrapKey("raw", wrapped, aes, "AES-KW", { name: "AES-GCM", hash: {} }, true, ["encrypt"])),// deriveKey normalizes the algorithm before derivedKeyType.deriveHashFirst: await errorName(() => subtle.deriveKey(pbkdf2Params({}), pbkdf2, "AES-FOO", true, ["encrypt"])),// A name that isn't a known algorithm stays NotSupportedError: a@@ -1730,6 +1737,11 @@testing.expectEqual("NotSupportedError", results.unwrapUnknownFirst);testing.expectEqual("TypeError", results.unwrapBeforeKey);testing.expectEqual("TypeError", results.unwrapHashBeforeKey);+ testing.expectEqual("TypeError", results.unwrapNoHashBeforeKey);+ testing.expectEqual("NotSupportedError", results.unwrapUnknownHashBeforeKey);+ testing.expectEqual("NotSupportedError", results.unwrapRsaUnknownHash);+ testing.expectEqual(null, results.unwrapAesStrayHash);+ testing.expectEqual("InvalidAccessError", results.unwrapAesStrayHashKey);testing.expectEqual("TypeError", results.deriveHashFirst);testing.expectEqual("NotSupportedError", results.importFunction);testing.expectEqual("NotSupportedError", results.importNull);diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zigindex 245718fa3..e082c53c8 100644--- a/src/browser/webapi/SubtleCrypto.zig+++ b/src/browser/webapi/SubtleCrypto.zig@@ -565,12 +565,16 @@ pub fn unwrapKey() !js.Promise {const local = exec.js.local.?;const name = try wrapAlgorithmName(algo, local);- // The unwrapped key's algorithm (its name and hash) is normalized next,- // before the key checks.- if (unwrapped_key_algo.algoName() == null) {- return local.typeError(missing_name);+ // The unwrapped key's algorithm is normalized next, before the key checks:+ // its name and, for HMAC and RSA, which take one, a known hash. Another+ // algorithm's stray `hash` is ignored, as importKey does.+ const unwrapped_name = unwrapped_key_algo.algoName() orelse return local.typeError(missing_name);+ if (eqlIgnoreCase(unwrapped_name, "HMAC") or RSA.scheme(unwrapped_name) != null) {+ const hash_name = try importHash(unwrapped_key_algo, local) orelse return local.typeError("the unwrapped key's algorithm requires a hash");+ if (common.canonicalHash(hash_name) == null) {+ return error.NotSupported;+ }}- _ = try importHash(unwrapped_key_algo, local);try checkWrappingKey(name, unwrapping_key, .unwrap);const bytes = try cryptBytes(algo, unwrapping_key, wrapped_key.values, .unwrap, exec);