Skip to content

Commit

Permalink
fix(node): propagate create cipher errors (#20280)
Browse files Browse the repository at this point in the history
Fixes #19002
  • Loading branch information
littledivy authored and bartlomieju committed Aug 31, 2023
1 parent 4dcb410 commit 999bc87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cli/tests/unit_node/crypto/crypto_cipher_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
},
});
6 changes: 6 additions & 0 deletions ext/node/polyfills/internal/crypto/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 999bc87

Please sign in to comment.