LibWeb: Correctly normalize HashAlgorithmIdentifier

Make sure that `HashAlgorithmIdentifier` is passed through
`normalize_an_algorithm` to verify that the hash is valid and supported.

This is required by the spec, but we are not following it very strictly
in `normalize_an_algorithm` because it is pretty convoluted.

Fixes ~60 tests.
This commit is contained in:
devgianlu 2024-12-18 18:40:49 +01:00 committed by Tim Flynn
parent 4c5a40579a
commit 637f934f69
Notes: github-actions[bot] 2024-12-18 20:46:00 +00:00
4 changed files with 107 additions and 95 deletions

View file

@ -33,6 +33,7 @@
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Crypto/CryptoAlgorithms.h>
#include <LibWeb/Crypto/KeyAlgorithms.h>
#include <LibWeb/Crypto/SubtleCrypto.h>
@ -42,13 +43,26 @@ namespace Web::Crypto {
static JS::ThrowCompletionOr<HashAlgorithmIdentifier> hash_algorithm_identifier_from_value(JS::VM& vm, JS::Value hash_value)
{
if (hash_value.is_string()) {
auto hash_string = TRY(hash_value.to_string(vm));
return HashAlgorithmIdentifier { hash_string };
auto* realm = vm.current_realm();
auto maybe_normalized_algorithm = [&]() -> WebIDL::ExceptionOr<NormalizedAlgorithmAndParameter> {
if (hash_value.is_string()) {
auto hash_string = TRY(hash_value.to_string(vm));
return normalize_an_algorithm(*realm, hash_string, "digest"_string);
} else if (hash_value.is_object()) {
auto hash_object = TRY(hash_value.to_object(vm));
auto hash_object_root = GC::make_root(hash_object);
return normalize_an_algorithm(*realm, hash_object_root, "digest"_string);
} else {
VERIFY_NOT_REACHED();
}
}();
if (maybe_normalized_algorithm.is_error()) {
return Bindings::exception_to_throw_completion(vm, maybe_normalized_algorithm.exception());
}
auto hash_object = TRY(hash_value.to_object(vm));
return HashAlgorithmIdentifier { hash_object };
return HashAlgorithmIdentifier { maybe_normalized_algorithm.value().parameter->name };
}
// https://w3c.github.io/webcrypto/#concept-usage-intersection