Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

HSB-482 fix: add env missing checks (#4282)

Mir Arif Hasan10e8f4ef1908
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;
}