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

fix: add env missing checks
This commit is contained in:
Mir Arif Hasan
2024-08-26 17:06:15 +06:00
committed by GitHub
parent a8822badcf
commit 10e8f4ef19
2 changed files with 11 additions and 0 deletions

View File

@@ -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
*/

View File

@@ -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;
}