From 10e8f4ef1908a99ac75870aa9c5f9da4fd904252 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 26 Aug 2024 17:06:15 +0600 Subject: [PATCH] HSB-482 fix: add env missing checks (#4282) fix: add env missing checks --- packages/hoppscotch-backend/src/errors.ts | 6 ++++++ packages/hoppscotch-backend/src/utils.ts | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 1d424c066..67d342ba8 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -49,6 +49,12 @@ export const AUTH_PROVIDER_NOT_CONFIGURED = export const ENV_NOT_FOUND_KEY_AUTH_PROVIDERS = '"VITE_ALLOWED_AUTH_PROVIDERS" is not present in .env file'; +/** + * Environment variable "DATA_ENCRYPTION_KEY" is not present in .env file + */ +export const ENV_NOT_FOUND_KEY_DATA_ENCRYPTION_KEY = + '"DATA_ENCRYPTION_KEY" is not present in .env file'; + /** * Environment variable "VITE_ALLOWED_AUTH_PROVIDERS" is empty in .env file */ diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index 8bc078245..820976cf9 100644 --- a/packages/hoppscotch-backend/src/utils.ts +++ b/packages/hoppscotch-backend/src/utils.ts @@ -12,6 +12,7 @@ import { AuthProvider } from './auth/helper'; import { ENV_EMPTY_AUTH_PROVIDERS, ENV_NOT_FOUND_KEY_AUTH_PROVIDERS, + ENV_NOT_FOUND_KEY_DATA_ENCRYPTION_KEY, ENV_NOT_SUPPORT_AUTH_PROVIDERS, JSON_INVALID, } from './errors'; @@ -328,6 +329,8 @@ const ENCRYPTION_ALGORITHM = 'aes-256-cbc'; * @returns The encrypted text */ export function encrypt(text: string, key = process.env.DATA_ENCRYPTION_KEY) { + if (!key) throw new Error(ENV_NOT_FOUND_KEY_DATA_ENCRYPTION_KEY); + if (text === null || text === undefined) return text; const iv = crypto.randomBytes(16); @@ -351,6 +354,8 @@ export function decrypt( encryptedData: string, key = process.env.DATA_ENCRYPTION_KEY, ) { + if (!key) throw new Error(ENV_NOT_FOUND_KEY_DATA_ENCRYPTION_KEY); + if (encryptedData === null || encryptedData === undefined) { return encryptedData; }