From 19ee8ddec2568e9781f6cc85dc926f28a3a48b1b Mon Sep 17 00:00:00 2001 From: stelar7 Date: Thu, 14 Nov 2024 14:49:02 +0100 Subject: [PATCH] LibWeb: Correctly set the key_usages on HMAC export --- Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp | 15 ++++--------- .../expected/Crypto/SubtleCrypto-hmac.txt | 1 + .../Text/input/Crypto/SubtleCrypto-hmac.html | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hmac.txt create mode 100644 Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hmac.html diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp index 05910b6849a..67956559321 100644 --- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp +++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp @@ -3589,8 +3589,6 @@ WebIDL::ExceptionOr> HMAC::import_key(Web::Crypto::A // https://w3c.github.io/webcrypto/#hmac-operations WebIDL::ExceptionOr> HMAC::export_key(Bindings::KeyFormat format, JS::NonnullGCPtr key) { - auto& vm = m_realm->vm(); - // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot // of key cannot be accessed, then throw an OperationError. // NOTE: In our impl this is always accessible @@ -3657,15 +3655,10 @@ WebIDL::ExceptionOr> HMAC::export_key(Bindings::Key } // Set the key_ops attribute of jwk to equal the usages attribute of key. - auto key_usages = verify_cast(key->usages()); - auto key_usages_length = MUST(MUST(key_usages->get(vm.names.length)).to_length(vm)); - for (auto i = 0u; i < key_usages_length; ++i) { - auto usage = key_usages->get(i); - if (!usage.has_value()) - break; - - auto usage_string = TRY(usage.value().to_string(vm)); - jwk.key_ops->append(usage_string); + jwk.key_ops = Vector {}; + jwk.key_ops->ensure_capacity(key->internal_usages().size()); + for (auto const& usage : key->internal_usages()) { + jwk.key_ops->append(Bindings::idl_enum_to_string(usage)); } // Set the ext attribute of jwk to equal the [[extractable]] internal slot of key. diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hmac.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hmac.txt new file mode 100644 index 00000000000..3632537bac6 --- /dev/null +++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hmac.txt @@ -0,0 +1 @@ +Does not crash. diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hmac.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hmac.html new file mode 100644 index 00000000000..9934bd0f062 --- /dev/null +++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hmac.html @@ -0,0 +1,22 @@ + +