From e6756c3e666e33aa9ee650b7a348a41d29cb3160 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 21 Jun 2024 12:25:07 +0200 Subject: fix(ext/node): don't panic on invalid utf-8 in pem (#24303) --- tests/unit_node/crypto/crypto_key_test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests/unit_node') diff --git a/tests/unit_node/crypto/crypto_key_test.ts b/tests/unit_node/crypto/crypto_key_test.ts index 013601572..d3a43b3b8 100644 --- a/tests/unit_node/crypto/crypto_key_test.ts +++ b/tests/unit_node/crypto/crypto_key_test.ts @@ -415,3 +415,27 @@ Deno.test("generate rsa export public key", async function () { const der = publicKey.export({ format: "der", type: "spki" }); assert(der instanceof Uint8Array); }); + +Deno.test("create public key with invalid utf-8 string", function () { + // This is an invalid UTF-8 string because it contains a lone utf-16 surrogate. + const invalidPem = Buffer.from(new Uint8Array([0xE2, 0x28, 0xA1])); + assertThrows( + () => { + createPublicKey(invalidPem); + }, + Error, + "not valid utf8", + ); +}); + +Deno.test("create private key with invalid utf-8 string", function () { + // This is an invalid UTF-8 string because it contains a lone utf-16 surrogate. + const invalidPem = Buffer.from(new Uint8Array([0xE2, 0x28, 0xA1])); + assertThrows( + () => { + createPrivateKey(invalidPem); + }, + Error, + "not valid utf8", + ); +}); -- cgit v1.2.3