Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): propagate create cipher errors #20280

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(node): propagate create cipher errors
  • Loading branch information
littledivy committed Aug 25, 2023
commit 8249f3beb69fd66729725f939867b2e79f750de9
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
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ece from "npm:http_ece";
import { Buffer } from "node:buffer";
import crypto from "node:crypto";

const buf = Buffer.from("Hello, world!");
ece.encrypt(buf, {
version: "aesgcm",
key: crypto.randomBytes(16),
rs: buf.length + 2 - 1,
});