Skip to content

Truly independent web browser

LibWeb: Validate ChaCha20-Poly1305 JWK key lengths

Shannon Booth25a0faf872f2
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
index 9524ad11c9e..ab0fb4bef1d 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
@@ -5702,7 +5702,10 @@ impl AlgorithmMethods for ChaCha20Poly1305 {
// 4. Let data be the byte sequence obtained by decoding the k field of jwk.
data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;
- // FIXME: 5. If the length in bits of data is not 256, then throw a DataError.
+ // 5. If the length in bits of data is not 256, then throw a DataError.
+ if data.as_slice().len() != 32 {
+ return Err(dom_error("DataError", b"Key data must be 256 bits"));
+ }
// 6. If the alg field of jwk is present, and is not "C20P", then throw a DataError.
if jwk
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.txt
new file mode 100644
index 00000000000..625f1cb2fd8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.txt
@@ -0,0 +1,3 @@
+31: DataError
+32: imported
+33: DataError
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.html
new file mode 100644
index 00000000000..20a6d0f0973
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ChaCha20Poly1305-jwk-key-length.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ for (const length of [31, 32, 33]) {
+ const jwk = { kty: "oct", k: btoa("\0".repeat(length)).replace(/=+$/, "") };
+ try {
+ await crypto.subtle.importKey("jwk", jwk, "ChaCha20-Poly1305", true, ["encrypt"]);
+ println(`${length}: imported`);
+ } catch (error) {
+ println(`${length}: ${error.name}`);
+ }
+ }
+ });
+</script>