From 1cb547d885906d557a63b2670c4b1b95a8626826 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 26 Aug 2023 10:45:37 +0530 Subject: fix(node): propagate create cipher errors (#20280) Fixes https://github.com/denoland/deno/issues/19002 --- cli/tests/unit_node/crypto/crypto_cipher_test.ts | 24 ++++++++++++++++++++++++ ext/node/polyfills/internal/crypto/cipher.ts | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/cli/tests/unit_node/crypto/crypto_cipher_test.ts b/cli/tests/unit_node/crypto/crypto_cipher_test.ts index 417f5035c..0eecaacf0 100644 --- a/cli/tests/unit_node/crypto/crypto_cipher_test.ts +++ b/cli/tests/unit_node/crypto/crypto_cipher_test.ts @@ -199,3 +199,27 @@ Deno.test({ assertEquals(await text(stream), "foo".repeat(15)); }, }); + +Deno.test({ + name: "createCipheriv - invalid algorithm", + fn() { + assertThrows( + () => + crypto.createCipheriv("foo", new Uint8Array(16), new Uint8Array(16)), + TypeError, + "Unknown cipher", + ); + }, +}); + +Deno.test({ + name: "createDecipheriv - invalid algorithm", + fn() { + assertThrows( + () => + crypto.createDecipheriv("foo", new Uint8Array(16), new Uint8Array(16)), + TypeError, + "Unknown cipher", + ); + }, +}); diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts index 3a8b41f06..5622576cd 100644 --- a/ext/node/polyfills/internal/crypto/cipher.ts +++ b/ext/node/polyfills/internal/crypto/cipher.ts @@ -162,6 +162,9 @@ export class Cipheriv extends Transform implements Cipher { }); this.#cache = new BlockModeCache(false); this.#context = ops.op_node_create_cipheriv(cipher, toU8(key), toU8(iv)); + if (this.#context == 0) { + throw new TypeError("Unknown cipher"); + } } final(encoding: string = getDefaultEncoding()): Buffer | string { @@ -278,6 +281,9 @@ export class Decipheriv extends Transform implements Cipher { }); this.#cache = new BlockModeCache(true); this.#context = ops.op_node_create_decipheriv(cipher, toU8(key), toU8(iv)); + if (this.#context == 0) { + throw new TypeError("Unknown cipher"); + } } final(encoding: string = getDefaultEncoding()): Buffer | string { -- cgit v1.2.3