Skip to content

Truly independent web browser

LibWeb: Apply [EnforceRange] to the KMAC key lengths

Shannon Booth44fe0291b284
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
index 431dce29c29..445ed6e11ce 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
@@ -827,7 +827,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> KmacKeyGenParams::from_val
auto maybe_length = Optional<WebIDL::UnsignedLong> {};
if (MUST(object.has_property("length"_utf16_fly_string))) {
auto length_value = TRY(object.get("length"_utf16_fly_string));
- maybe_length = TRY(length_value.to_u32(vm));
+ maybe_length = TRY(WebIDL::convert_to_int<WebIDL::UnsignedLong>(vm, length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));
}
return adopt_own<AlgorithmParams>(*new KmacKeyGenParams { maybe_length });
@@ -846,7 +846,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> KmacImportParams::from_val
auto maybe_length = Optional<WebIDL::UnsignedLong> {};
if (MUST(object.has_property("length"_utf16_fly_string))) {
auto length_value = TRY(object.get("length"_utf16_fly_string));
- maybe_length = TRY(length_value.to_u32(vm));
+ maybe_length = TRY(WebIDL::convert_to_int<WebIDL::UnsignedLong>(vm, length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));
}
return adopt_own<AlgorithmParams>(*new KmacImportParams { maybe_length });
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-length-enforce-range.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-length-enforce-range.txt
new file mode 100644
index 00000000000..027ff84fe2f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-length-enforce-range.txt
@@ -0,0 +1,2 @@
+KmacKeyGenParams length: TypeError
+KmacImportParams length: TypeError
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-length-enforce-range.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-length-enforce-range.html
new file mode 100644
index 00000000000..9e3dc3a18df
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-length-enforce-range.html
@@ -0,0 +1,19 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ async function check(label, promise) {
+ try {
+ await promise;
+ println(`${label}: ok`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ const algorithm = { name: "KMAC128", length: 2 ** 32 + 128 };
+
+ await check("KmacKeyGenParams length", crypto.subtle.generateKey(algorithm, false, ["sign"]));
+ await check("KmacImportParams length", crypto.subtle.importKey("raw-secret", new Uint8Array(16), algorithm, false, ["sign"]));
+ });
+</script>