Skip to content

Truly independent web browser

LibWeb: Convert WebCrypto hash members like WebIDL dictionaries

Shannon Boothad9a5581d421
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
index 90e8878da96..04a0650833a 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
@@ -54,12 +54,14 @@ fn hash_algorithm_identifier_from_value(
) -> WebIdlResult<HashAlgorithmIdentifier> {
let algorithm = if let Some(hash_string) = hash_string {
AlgorithmIdentifier::DOMString(hash_string)
- } else if hash_value.is_string() {
- AlgorithmIdentifier::DOMString(hash_value.to_utf16_string(vm)?)
} else if hash_value.is_object() {
AlgorithmIdentifier::Object(hash_value.to_object(vm)?)
+ } else if hash_value.is_undefined() {
+ return vm
+ .throw_completion(ErrorKind::TypeError, ErrorType::MissingRequiredProperty, &[&"hash"])
+ .map_err(Into::into);
} else {
- unreachable!("the hash is a string or an object")
+ AlgorithmIdentifier::DOMString(hash_value.to_utf16_string(vm)?)
};
let normalized_algorithm = normalize_an_algorithm(vm, realm, &algorithm, "digest")?;
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hash-member-conversion.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hash-member-conversion.txt
new file mode 100644
index 00000000000..a923800ddd4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-hash-member-conversion.txt
@@ -0,0 +1,19 @@
+HMAC generateKey, missing hash: TypeError
+HMAC generateKey, number hash: NotSupportedError
+HMAC generateKey, null hash: NotSupportedError
+HMAC importKey, missing hash: TypeError
+HMAC importKey, number hash: NotSupportedError
+HMAC importKey, null hash: NotSupportedError
+HKDF deriveBits, missing hash: TypeError
+HKDF deriveBits, number hash: NotSupportedError
+HKDF deriveBits, null hash: NotSupportedError
+PBKDF2 deriveBits, missing hash: TypeError
+PBKDF2 deriveBits, number hash: NotSupportedError
+PBKDF2 deriveBits, null hash: NotSupportedError
+RSA-PSS generateKey, missing hash: TypeError
+RSA-PSS generateKey, number hash: NotSupportedError
+RSA-PSS generateKey, null hash: NotSupportedError
+ECDSA sign, missing hash: TypeError
+ECDSA sign, number hash: NotSupportedError
+ECDSA sign, null hash: NotSupportedError
+supports with a missing hash: false
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hash-member-conversion.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hash-member-conversion.html
new file mode 100644
index 00000000000..a6cb39a21b2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-hash-member-conversion.html
@@ -0,0 +1,58 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const hmacKey = await crypto.subtle.generateKey({ name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
+ const hkdfKey = await crypto.subtle.importKey("raw", new Uint8Array(16), "HKDF", false, ["deriveBits"]);
+ const pbkdf2Key = await crypto.subtle.importKey("raw", new Uint8Array(16), "PBKDF2", false, ["deriveBits"]);
+ const ecdsaKeys = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, false, ["sign"]);
+
+ const operations = {
+ "HMAC generateKey": hash => crypto.subtle.generateKey({ name: "HMAC", hash }, false, ["sign"]),
+ "HMAC importKey": hash =>
+ crypto.subtle.importKey("raw", new Uint8Array(16), { name: "HMAC", hash }, false, ["sign"]),
+ "HKDF deriveBits": hash =>
+ crypto.subtle.deriveBits(
+ { name: "HKDF", hash, salt: new Uint8Array(), info: new Uint8Array() },
+ hkdfKey,
+ 256
+ ),
+ "PBKDF2 deriveBits": hash =>
+ crypto.subtle.deriveBits(
+ { name: "PBKDF2", hash, salt: new Uint8Array(), iterations: 1 },
+ pbkdf2Key,
+ 256
+ ),
+ "RSA-PSS generateKey": hash =>
+ crypto.subtle.generateKey(
+ {
+ name: "RSA-PSS",
+ modulusLength: 1024,
+ publicExponent: new Uint8Array([1, 0, 1]),
+ hash,
+ },
+ false,
+ ["sign"]
+ ),
+ "ECDSA sign": hash =>
+ crypto.subtle.sign({ name: "ECDSA", hash }, ecdsaKeys.privateKey, new Uint8Array()),
+ };
+
+ for (const [name, operation] of Object.entries(operations)) {
+ for (const [description, hash] of [
+ ["missing", undefined],
+ ["number", 1],
+ ["null", null],
+ ]) {
+ try {
+ await operation(hash);
+ println(`${name}, ${description} hash: resolved`);
+ } catch (error) {
+ println(`${name}, ${description} hash: ${error.name}`);
+ }
+ }
+ }
+
+ println(`supports with a missing hash: ${SubtleCrypto.supports("importKey", { name: "HMAC" })}`);
+ });
+</script>