OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
email: stop logging message contents and require a real unsubscribe secret
Carl-Gerhard Lindesvärdb197f0243880
diff --git a/apps/public/content/docs/self-hosting/environment-variables.mdx b/apps/public/content/docs/self-hosting/environment-variables.mdxindex 860422214..27f8eb5f9 100644--- a/apps/public/content/docs/self-hosting/environment-variables.mdx+++ b/apps/public/content/docs/self-hosting/environment-variables.mdx@@ -188,12 +188,27 @@ API_CORS_ORIGINS=https://app.example.com,https://another-app.comSecret key for encrypting session cookies. Generate a secure random string (32+ characters).+It also keys two HMACs: the cookie that proves a viewer entered a password-protected share's password, and the signature on unsubscribe links. Set the same value on the API and the worker — the worker sends the emails, the API verifies the links. Without it, password-protected shares cannot be unlocked and unsubscribe links cannot be signed.+**Example**:```bash# Generate with: openssl rand -base64 32COOKIE_SECRET=your-random-secret-here```+### UNSUBSCRIBE_SECRET++**Type**: `string`+**Required**: No+**Default**: falls back to `COOKIE_SECRET`++Keys the HMAC on unsubscribe links, if you want it separate from `COOKIE_SECRET`. One of the two must be set or sending an email that carries an unsubscribe link fails; there is no built-in default, because a known default makes every unsubscribe link forgeable.++**Example**:+```bash+UNSUBSCRIBE_SECRET=$(openssl rand -hex 32)+```+<Callout type="warn">Never use the default value in production! Always generate a unique secret.</Callout>diff --git a/packages/core/test/preload.ts b/packages/core/test/preload.tsindex 99190f1f3..5ff697bda 100644--- a/packages/core/test/preload.ts+++ b/packages/core/test/preload.ts@@ -14,3 +14,6 @@ process.env.DATABASE_URL =process.env.CLICKHOUSE_URL = 'http://localhost:23123/openpanel_test';process.env.REDIS_URL = 'redis://localhost:23379';process.env.SELF_HOSTED = 'true';+// Unsubscribe links are HMAC-signed and there is no default secret any more,+// so a test that exercises one needs a key just like a deployment does.+process.env.UNSUBSCRIBE_SECRET ||= 'test-unsubscribe-secret';diff --git a/packages/email/src/index.tsx b/packages/email/src/index.tsxindex 546678db4..9061d75c7 100644--- a/packages/email/src/index.tsx+++ b/packages/email/src/index.tsx@@ -6,6 +6,15 @@ import type { z } from 'zod';import { type TemplateKey, type Templates, templates } from './emails';import { getUnsubscribeUrl } from './unsubscribe';+/** a***@example.com: enough to correlate a log line without exposing the address. */+function redactEmail(email: string): string {+ const at = email.indexOf('@');+ if (at <= 0) {+ return '***';+ }+ return `${email[0]}***${email.slice(at)}`;+}+export * from './unsubscribe';const FROM = process.env.EMAIL_SENDER ?? '[email protected]';@@ -93,11 +102,12 @@ export async function sendEmail<T extends TemplateKey>(}if (!process.env.RESEND_API_KEY) {- console.log('No SMTP_HOST or RESEND_API_KEY found, here is the data');- console.log('Template:', template);- console.log('Subject: ', subject);- console.log('To: ', to);- console.log('Data: ', JSON.stringify(data, null, 2));+ // Never dump the payload: template data carries password-reset and+ // unsubscribe links, and the recipient is personal data+ // (GHSA-xr2x-w49w-hp2c).+ console.warn(+ `Email not sent (email_provider_not_configured): template=${templateKey} to=${redactEmail(to)}`+ );return null;}diff --git a/packages/email/src/unsubscribe.ts b/packages/email/src/unsubscribe.tsindex 69da61ccc..4cd050bad 100644--- a/packages/email/src/unsubscribe.ts+++ b/packages/email/src/unsubscribe.ts@@ -1,17 +1,26 @@import { createHmac, timingSafeEqual } from 'crypto';-const SECRET =- process.env.UNSUBSCRIBE_SECRET ||- process.env.COOKIE_SECRET ||- process.env.SECRET ||- 'default-secret-change-in-production';+/**+ * Read at call time, not import time, so a missing secret fails the one+ * request that needs it with a clear message instead of silently signing with+ * a well-known default that anyone could forge (GHSA-cv3v-4j56-hr88).+ */+function getSecret(): string {+ const secret = process.env.UNSUBSCRIBE_SECRET || process.env.COOKIE_SECRET;+ if (!secret) {+ throw new Error(+ 'UNSUBSCRIBE_SECRET or COOKIE_SECRET must be set to sign unsubscribe links'+ );+ }+ return secret;+}export function generateUnsubscribeToken(email: string,category: string): string {const data = `${email}:${category}`;- return createHmac('sha256', SECRET).update(data).digest('hex');+ return createHmac('sha256', getSecret()).update(data).digest('hex');}export function verifyUnsubscribeToken(diff --git a/self-hosting/coolify.yml b/self-hosting/coolify.ymlindex 7762ade81..f8f65fa7b 100644--- a/self-hosting/coolify.yml+++ b/self-hosting/coolify.yml@@ -226,6 +226,9 @@ services:- API_URL=$SERVICE_FQDN_OPAPI# Must match the API's key: the worker decrypts GSC tokens during sync- ENCRYPTION_KEY=${SERVICE_HEX_64_ENCRYPTIONKEY}+ # Must match the API's secret: unsubscribe links in worker emails are+ # verified by the API, and signing them needs a real secret+ - COOKIE_SECRET=${SERVICE_BASE64_COOKIESECRET}# Optional: Google Search Console sync needs the Google OAuth client- GOOGLE_CLIENT_ID=${OPENPANEL_GOOGLE_CLIENT_ID}- GOOGLE_CLIENT_SECRET=${OPENPANEL_GOOGLE_CLIENT_SECRET}