Truly independent web browser
LibWeb: Reject empty usages for encapsulated and decapsulated keys
Shannon Boothc785cc87d3f9
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rsindex f177e272838..69f619a3e8e 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs@@ -2093,9 +2093,19 @@ impl SubtleCrypto {queue_crypto_rejection(vm, realm, promise, error);return;}- // FIXME: 14. If usages is empty, then throw a SyntaxError.let shared_key = maybe_shared_key.must();+ // 14. If usages is empty, then throw a SyntaxError.+ if usages.is_empty() {+ queue_crypto_rejection(+ vm,+ realm,+ promise,+ dom_error("SyntaxError", b"usages must not be empty"),+ );+ return;+ }+// 15. Set the [[extractable]] internal slot of sharedKey to extractable.shared_key.set_extractable(extractable);@@ -2368,9 +2378,19 @@ impl SubtleCrypto {queue_crypto_rejection(vm, realm, promise, error);return;}- // FIXME: 15. If usages is empty, then throw a SyntaxError.let shared_key = maybe_shared_key.must();+ // 15. If usages is empty, then throw a SyntaxError.+ if usages.is_empty() {+ queue_crypto_rejection(+ vm,+ realm,+ promise,+ dom_error("SyntaxError", b"usages must not be empty"),+ );+ return;+ }+// 16. Set the [[extractable]] internal slot of sharedKey to extractable.shared_key.set_extractable(extractable);diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.txtnew file mode 100644index 00000000000..1c8b4a28617--- /dev/null+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.txt@@ -0,0 +1,4 @@+encapsulateKey with []: SyntaxError+decapsulateKey with []: SyntaxError+encapsulateKey with [encrypt]: encrypt+decapsulateKey with [encrypt]: encryptdiff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.htmlnew file mode 100644index 00000000000..ed4d25e2a4c--- /dev/null+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLKEM-shared-key-empty-usages.html@@ -0,0 +1,36 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const algorithm = { name: "ML-KEM-768" };+ const { publicKey, privateKey } = await crypto.subtle.generateKey(algorithm, false, [+ "encapsulateKey",+ "encapsulateBits",+ "decapsulateKey",+ ]);+ const { ciphertext } = await crypto.subtle.encapsulateBits(algorithm, publicKey);++ for (const usages of [[], ["encrypt"]]) {+ try {+ const { sharedKey } = await crypto.subtle.encapsulateKey(algorithm, publicKey, "AES-GCM", false, usages);+ println(`encapsulateKey with [${usages}]: ${sharedKey.usages}`);+ } catch (error) {+ println(`encapsulateKey with [${usages}]: ${error.name}`);+ }++ try {+ const sharedKey = await crypto.subtle.decapsulateKey(+ algorithm,+ privateKey,+ ciphertext,+ "AES-GCM",+ false,+ usages+ );+ println(`decapsulateKey with [${usages}]: ${sharedKey.usages}`);+ } catch (error) {+ println(`decapsulateKey with [${usages}]: ${error.name}`);+ }+ }+ });+</script>