Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(ee): add ENCRYPTION_KEY to default environment variables + improve error messages (#2853)

Marc Klingen9f3ac3ab9fac
diff --git a/.env.dev.example b/.env.dev.example
index 555b8955ba..deff9fefc7 100644
--- a/.env.dev.example
+++ b/.env.dev.example
@@ -50,5 +50,5 @@ REDIS_AUTH="myredissecret"
LANGFUSE_WORKER_PASSWORD=mybasicauthsecret
# openssl rand -hex 32 used only here
-ENCRYPTION_KEY=6c16874e5c0f0cc74ddec00425fa99fbe9ffbe412b7d5d906a4b00005df91403
+ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
diff --git a/.env.local.example b/.env.local.example
index d3e03072bd..4bec1e2212 100644
--- a/.env.local.example
+++ b/.env.local.example
@@ -18,4 +18,7 @@ REDIS_HOST="127.0.0.1"
REDIS_PORT=6379
REDIS_AUTH="myredissecret"
-LANGFUSE_WORKER_PASSWORD=myworkerpassword
\ No newline at end of file
+LANGFUSE_WORKER_PASSWORD=myworkerpassword
+
+# openssl rand -hex 32 used only here
+ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
\ No newline at end of file
diff --git a/.env.prod.example b/.env.prod.example
index a8ab9b3abc..10e05b0aea 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -16,14 +16,17 @@ DATABASE_URL="postgresql://postgres:postgres@db:5432/postgres"
# NEXTAUTH_URL does not need to be set when deploying on Vercel
NEXTAUTH_URL="http://localhost:3000"
-# You can generate a new secret on the command line with:
+# For each of these, you can generate a new secret on the command line with:
# openssl rand -base64 32
-# https://next-auth.js.org/configuration/options#secret
-NEXTAUTH_SECRET="secret"
-SALT="salt"
+NEXTAUTH_SECRET="secret" # https://next-auth.js.org/configuration/options#secret
+SALT="salt" # salt used to hash api keys
+
+# API level encryption for sensitive data
+# Must be 256 bits, 64 string characters in hex format, generate via: openssl rand -hex 32
+ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000"
# Use CSP headers to enforce HTTPS, optional
-LANGFUSE_CSP_ENFORCE_HTTPS="true"
+# LANGFUSE_CSP_ENFORCE_HTTPS="true"
# Docker only, optional
@@ -143,9 +146,6 @@ LANGFUSE_CSP_ENFORCE_HTTPS="true"
# Crisp chat
# NEXT_PUBLIC_CRISP_WEBSITE_ID=
-# Encryption
-# ENCRYPTION_KEY=
-
# Admin API
# ADMIN_API_KEY=
diff --git a/docker-compose.build.yml b/docker-compose.build.yml
index 2d89c07ac0..ec1bedf900 100644
--- a/docker-compose.build.yml
+++ b/docker-compose.build.yml
@@ -16,6 +16,7 @@ services:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
- NEXTAUTH_SECRET=mysecret
- SALT=mysalt
+ - ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000 # generate via `openssl rand -hex 32`
- NEXTAUTH_URL=http://localhost:3000
- TELEMETRY_ENABLED=${TELEMETRY_ENABLED:-true}
- LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES=${LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES:-false}
diff --git a/docker-compose.yml b/docker-compose.yml
index 215ec2cb08..a3601dfb72 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -10,6 +10,7 @@ services:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
- NEXTAUTH_SECRET=mysecret
- SALT=mysalt
+ - ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000 # generate via `openssl rand -hex 32`
- NEXTAUTH_URL=http://localhost:3000
- TELEMETRY_ENABLED=${TELEMETRY_ENABLED:-true}
- LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES=${LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES:-false}
diff --git a/packages/shared/src/encryption/index.ts b/packages/shared/src/encryption/index.ts
index f4188be44e..6a8c58e8d2 100644
--- a/packages/shared/src/encryption/index.ts
+++ b/packages/shared/src/encryption/index.ts
@@ -1,7 +1,7 @@
import crypto from "crypto";
import { env } from "../env";
-const ENCRYPTION_KEY: string = env.ENCRYPTION_KEY || ""; // Must be 256 bits (32 bytes, 64 hex characters)
+const ENCRYPTION_KEY: string | undefined = env.ENCRYPTION_KEY; // Must be 256 bits (32 bytes, 64 hex characters)
const IV_LENGTH: number = 16; // For AES, this is always 16
// Alternatively: openssl rand -hex 32
@@ -13,17 +13,16 @@ export function keyGen() {
* Encrypts the given plain text using AES-256-GCM algorithm.
*
* @param {string} plainText - The text to encrypt.
- * @param {string} [keyHex=ENCRYPTION_KEY] - The encryption key in hexadecimal format.
* @returns {string} The encrypted data in hex format, including IV and authentication tag.
*/
-export function encrypt(
- plainText: string,
- keyHex: string = ENCRYPTION_KEY
-): string {
+export function encrypt(plainText: string): string {
+ if (!ENCRYPTION_KEY) {
+ throw new Error("Missing environment variable: `ENCRYPTION_KEY`");
+ }
const iv = crypto.randomBytes(IV_LENGTH); // Directly use Buffer returned by randomBytes
const cipher = crypto.createCipheriv(
"aes-256-gcm",
- Buffer.from(keyHex, "hex"),
+ Buffer.from(ENCRYPTION_KEY, "hex"),
iv
);
let encrypted = cipher.update(plainText, "utf8", "hex");
@@ -34,7 +33,10 @@ export function encrypt(
return iv.toString("hex") + ":" + encrypted + ":" + authTag.toString("hex");
}
-export function decrypt(text: string, keyHex: string = ENCRYPTION_KEY): string {
+export function decrypt(text: string): string {
+ if (!ENCRYPTION_KEY) {
+ throw new Error("Missing environment variable: `ENCRYPTION_KEY`");
+ }
const [ivHex, encryptedHex, authTagHex] = text.split(":");
if (!ivHex || !encryptedHex || !authTagHex) {
throw new Error("Invalid or corrupted cipher format");
@@ -46,7 +48,7 @@ export function decrypt(text: string, keyHex: string = ENCRYPTION_KEY): string {
const decipher = crypto.createDecipheriv(
"aes-256-gcm",
- Buffer.from(keyHex, "hex"),
+ Buffer.from(ENCRYPTION_KEY, "hex"),
iv
);
decipher.setAuthTag(authTag);