From 302c6a90c092022ff2d70b57bcd6abbbf447fe25 Mon Sep 17 00:00:00 2001 From: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Date: Mon, 21 Apr 2025 11:25:48 +0530 Subject: [PATCH] chore: removes formbricks_encryption_key-environment-variable (#5426) --- apps/web/app/lib/singleUseSurveys.test.ts | 18 ----------- apps/web/app/lib/singleUseSurveys.ts | 30 ++++++------------- .../question-form-input/index.test.tsx | 2 -- packages/lib/constants.ts | 1 - packages/lib/env.ts | 2 -- packages/lib/utils/singleUseSurveys.ts | 12 ++------ turbo.json | 1 - 7 files changed, 11 insertions(+), 55 deletions(-) diff --git a/apps/web/app/lib/singleUseSurveys.test.ts b/apps/web/app/lib/singleUseSurveys.test.ts index 53a0c49957..8a792f40a4 100644 --- a/apps/web/app/lib/singleUseSurveys.test.ts +++ b/apps/web/app/lib/singleUseSurveys.test.ts @@ -13,7 +13,6 @@ vi.mock("@formbricks/lib/crypto", () => ({ // Mock constants vi.mock("@formbricks/lib/constants", () => ({ ENCRYPTION_KEY: "test-encryption-key", - FORMBRICKS_ENCRYPTION_KEY: "test-formbricks-encryption-key", })); // Mock cuid2 @@ -81,7 +80,6 @@ describe("generateSurveySingleUseId", () => { // Temporarily mock ENCRYPTION_KEY as undefined vi.doMock("@formbricks/lib/constants", () => ({ ENCRYPTION_KEY: undefined, - FORMBRICKS_ENCRYPTION_KEY: "test-formbricks-encryption-key", })); // Re-import to get the new mock values @@ -94,7 +92,6 @@ describe("generateSurveySingleUseId", () => { // Temporarily mock ENCRYPTION_KEY as undefined vi.doMock("@formbricks/lib/constants", () => ({ ENCRYPTION_KEY: undefined, - FORMBRICKS_ENCRYPTION_KEY: "test-formbricks-encryption-key", })); // Re-import to get the new mock values @@ -102,19 +99,4 @@ describe("generateSurveySingleUseId", () => { expect(() => validateSurveySingleUseIdNoKey(mockEncryptedCuid)).toThrow("ENCRYPTION_KEY is not set"); }); - - test("throws error when FORMBRICKS_ENCRYPTION_KEY is not set in validateSurveySingleUseId for AES128", async () => { - // Temporarily mock FORMBRICKS_ENCRYPTION_KEY as undefined - vi.doMock("@formbricks/lib/constants", () => ({ - ENCRYPTION_KEY: "test-encryption-key", - FORMBRICKS_ENCRYPTION_KEY: undefined, - })); - - // Re-import to get the new mock values - const { validateSurveySingleUseId: validateSurveySingleUseIdNoKey } = await import("./singleUseSurveys"); - - expect(() => - validateSurveySingleUseIdNoKey("M(.Bob=dS1!wUSH2lb,E7hxO=He1cnnitmXrG|Su/DKYZrPy~zgS)u?dgI53sfs/") - ).toThrow("FORMBRICKS_ENCRYPTION_KEY is not defined"); - }); }); diff --git a/apps/web/app/lib/singleUseSurveys.ts b/apps/web/app/lib/singleUseSurveys.ts index aaceacd6d9..2844ef9762 100644 --- a/apps/web/app/lib/singleUseSurveys.ts +++ b/apps/web/app/lib/singleUseSurveys.ts @@ -1,6 +1,6 @@ import cuid2 from "@paralleldrive/cuid2"; -import { ENCRYPTION_KEY, FORMBRICKS_ENCRYPTION_KEY } from "@formbricks/lib/constants"; -import { decryptAES128, symmetricDecrypt, symmetricEncrypt } from "@formbricks/lib/crypto"; +import { ENCRYPTION_KEY } from "@formbricks/lib/constants"; +import { symmetricDecrypt, symmetricEncrypt } from "@formbricks/lib/crypto"; // generate encrypted single use id for the survey export const generateSurveySingleUseId = (isEncrypted: boolean): string => { @@ -21,25 +21,13 @@ export const generateSurveySingleUseId = (isEncrypted: boolean): string => { export const validateSurveySingleUseId = (surveySingleUseId: string): string | undefined => { let decryptedCuid: string | null = null; - if (surveySingleUseId.length === 64) { - if (!FORMBRICKS_ENCRYPTION_KEY) { - throw new Error("FORMBRICKS_ENCRYPTION_KEY is not defined"); - } - - try { - decryptedCuid = decryptAES128(FORMBRICKS_ENCRYPTION_KEY, surveySingleUseId); - } catch (error) { - return undefined; - } - } else { - if (!ENCRYPTION_KEY) { - throw new Error("ENCRYPTION_KEY is not set"); - } - try { - decryptedCuid = symmetricDecrypt(surveySingleUseId, ENCRYPTION_KEY); - } catch (error) { - return undefined; - } + if (!ENCRYPTION_KEY) { + throw new Error("ENCRYPTION_KEY is not set"); + } + try { + decryptedCuid = symmetricDecrypt(surveySingleUseId, ENCRYPTION_KEY); + } catch (error) { + return undefined; } if (cuid2.isCuid(decryptedCuid)) { diff --git a/apps/web/modules/survey/components/question-form-input/index.test.tsx b/apps/web/modules/survey/components/question-form-input/index.test.tsx index 2176eda07e..fe8ab64878 100644 --- a/apps/web/modules/survey/components/question-form-input/index.test.tsx +++ b/apps/web/modules/survey/components/question-form-input/index.test.tsx @@ -12,7 +12,6 @@ vi.mock("@formbricks/lib/constants", () => ({ FORMBRICKS_API_HOST: "http://localhost:3000", FORMBRICKS_ENVIRONMENT_ID: "test-env-id", ENCRYPTION_KEY: "test-encryption-key", - FORMBRICKS_ENCRYPTION_KEY: "test-fb-encryption-key", WEBAPP_URL: "http://localhost:3000", DEFAULT_BRAND_COLOR: "#64748b", AVAILABLE_LOCALES: ["en-US", "de-DE", "pt-BR", "fr-FR", "zh-Hant-TW", "pt-PT"], @@ -49,7 +48,6 @@ vi.mock("@formbricks/lib/env", () => ({ FORMBRICKS_API_HOST: "http://localhost:3000", FORMBRICKS_ENVIRONMENT_ID: "test-env-id", ENCRYPTION_KEY: "test-encryption-key", - FORMBRICKS_ENCRYPTION_KEY: "test-fb-encryption-key", NODE_ENV: "test", ENTERPRISE_LICENSE_KEY: "test-license-key", }, diff --git a/packages/lib/constants.ts b/packages/lib/constants.ts index aa7da4b99d..3ea031cbb4 100644 --- a/packages/lib/constants.ts +++ b/packages/lib/constants.ts @@ -14,7 +14,6 @@ export const WEBAPP_URL = export const SURVEY_URL = env.SURVEY_URL; // encryption keys -export const FORMBRICKS_ENCRYPTION_KEY = env.FORMBRICKS_ENCRYPTION_KEY || undefined; export const ENCRYPTION_KEY = env.ENCRYPTION_KEY; // Other diff --git a/packages/lib/env.ts b/packages/lib/env.ts index d8017affa7..f3037d1dd8 100644 --- a/packages/lib/env.ts +++ b/packages/lib/env.ts @@ -30,7 +30,6 @@ export const env = createEnv({ EMAIL_VERIFICATION_DISABLED: z.enum(["1", "0"]).optional(), ENCRYPTION_KEY: z.string(), ENTERPRISE_LICENSE_KEY: z.string().optional(), - FORMBRICKS_ENCRYPTION_KEY: z.string().length(24).or(z.string().length(0)).optional(), FORMBRICKS_API_HOST: z .string() .url() @@ -155,7 +154,6 @@ export const env = createEnv({ EMAIL_VERIFICATION_DISABLED: process.env.EMAIL_VERIFICATION_DISABLED, ENCRYPTION_KEY: process.env.ENCRYPTION_KEY, ENTERPRISE_LICENSE_KEY: process.env.ENTERPRISE_LICENSE_KEY, - FORMBRICKS_ENCRYPTION_KEY: process.env.FORMBRICKS_ENCRYPTION_KEY, FORMBRICKS_API_HOST: process.env.FORMBRICKS_API_HOST, FORMBRICKS_ENVIRONMENT_ID: process.env.FORMBRICKS_ENVIRONMENT_ID, GITHUB_ID: process.env.GITHUB_ID, diff --git a/packages/lib/utils/singleUseSurveys.ts b/packages/lib/utils/singleUseSurveys.ts index fe3c0cfe6c..88bead7059 100644 --- a/packages/lib/utils/singleUseSurveys.ts +++ b/packages/lib/utils/singleUseSurveys.ts @@ -1,5 +1,5 @@ import cuid2 from "@paralleldrive/cuid2"; -import { decryptAES128, symmetricDecrypt, symmetricEncrypt } from "../../lib/crypto"; +import { symmetricDecrypt, symmetricEncrypt } from "../../lib/crypto"; import { env } from "../../lib/env"; // generate encrypted single use id for the survey @@ -36,15 +36,7 @@ export const validateSurveySingleUseId = (surveySingleUseId: string): string | u throw new Error("ENCRYPTION_KEY is not set"); } - if (surveySingleUseId.length === 64) { - if (!env.FORMBRICKS_ENCRYPTION_KEY) { - throw new Error("FORMBRICKS_ENCRYPTION_KEY is not defined"); - } - - decryptedCuid = decryptAES128(env.FORMBRICKS_ENCRYPTION_KEY!, surveySingleUseId); - } else { - decryptedCuid = symmetricDecrypt(surveySingleUseId, env.ENCRYPTION_KEY); - } + decryptedCuid = symmetricDecrypt(surveySingleUseId, env.ENCRYPTION_KEY); if (cuid2.isCuid(decryptedCuid)) { return decryptedCuid; diff --git a/turbo.json b/turbo.json index 0b5762a44a..28751aef83 100644 --- a/turbo.json +++ b/turbo.json @@ -114,7 +114,6 @@ "EMAIL_VERIFICATION_DISABLED", "ENCRYPTION_KEY", "ENTERPRISE_LICENSE_KEY", - "FORMBRICKS_ENCRYPTION_KEY", "FORMBRICKS_API_HOST", "FORMBRICKS_ENVIRONMENT_ID", "GITHUB_ID",