From ef2d98fe11ffe467a31d2e30e3ae9738147b74e9 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 11 Sep 2024 13:27:07 +0900 Subject: fix(ext/node): validate input lengths in `Cipheriv` and `Decipheriv` (#25570) addresses the first part of #25279 --- tests/unit_node/crypto/crypto_cipher_test.ts | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'tests/unit_node/crypto/crypto_cipher_test.ts') diff --git a/tests/unit_node/crypto/crypto_cipher_test.ts b/tests/unit_node/crypto/crypto_cipher_test.ts index 91227cf00..ad424e5d4 100644 --- a/tests/unit_node/crypto/crypto_cipher_test.ts +++ b/tests/unit_node/crypto/crypto_cipher_test.ts @@ -245,6 +245,44 @@ Deno.test({ }, }); +Deno.test({ + name: "createCipheriv - invalid inputs", + fn() { + assertThrows( + () => + crypto.createCipheriv("aes256", new Uint8Array(31), new Uint8Array(16)), + RangeError, + "Invalid key length", + ); + assertThrows( + () => + crypto.createCipheriv( + "aes-256-cbc", + new Uint8Array(31), + new Uint8Array(16), + ), + RangeError, + "Invalid key length", + ); + assertThrows( + () => + crypto.createCipheriv("aes256", new Uint8Array(32), new Uint8Array(15)), + TypeError, + "Invalid initialization vector", + ); + assertThrows( + () => + crypto.createCipheriv( + "aes-256-cbc", + new Uint8Array(32), + new Uint8Array(15), + ), + TypeError, + "Invalid initialization vector", + ); + }, +}); + Deno.test({ name: "createDecipheriv - invalid algorithm", fn() { @@ -257,6 +295,52 @@ Deno.test({ }, }); +Deno.test({ + name: "createDecipheriv - invalid inputs", + fn() { + assertThrows( + () => + crypto.createDecipheriv( + "aes256", + new Uint8Array(31), + new Uint8Array(16), + ), + RangeError, + "Invalid key length", + ); + assertThrows( + () => + crypto.createDecipheriv( + "aes-256-cbc", + new Uint8Array(31), + new Uint8Array(16), + ), + RangeError, + "Invalid key length", + ); + assertThrows( + () => + crypto.createDecipheriv( + "aes256", + new Uint8Array(32), + new Uint8Array(15), + ), + TypeError, + "Invalid initialization vector", + ); + assertThrows( + () => + crypto.createDecipheriv( + "aes-256-cbc", + new Uint8Array(32), + new Uint8Array(15), + ), + TypeError, + "Invalid initialization vector", + ); + }, +}); + Deno.test({ name: "getCiphers", fn() { -- cgit v1.2.3