From 4948164aa195e8f7658b937de5ce6141f635c785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 25 Oct 2024 09:14:23 +0100 Subject: [PATCH] wip: Test most basic error handling --- src/utils/encryption.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/utils/encryption.ts b/src/utils/encryption.ts index c1bd723d..935b87cf 100644 --- a/src/utils/encryption.ts +++ b/src/utils/encryption.ts @@ -31,14 +31,19 @@ export function decrypt( ): string | undefined { if (!secret) return undefined; - const [encryptedToken, iv] = secret.split(":"); - const decipher = crypto.createDecipheriv( - "AES-256-CBC", - Buffer.from(key, "utf-8"), - Buffer.from(iv, "hex"), - ); - let decryptedToken = decipher.update(encryptedToken, "hex", "utf-8"); - decryptedToken += decipher.final("utf-8"); + try { + const [encryptedToken, iv] = secret.split(":"); + const decipher = crypto.createDecipheriv( + "AES-256-CBC", + Buffer.from(key, "utf-8"), + Buffer.from(iv, "hex"), + ); + let decryptedToken = decipher.update(encryptedToken, "hex", "utf-8"); + decryptedToken += decipher.final("utf-8"); - return decryptedToken; + return decryptedToken; + } catch (error) { + console.error("Failed to decrypt secret"); + return undefined; + } }