Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): refuse a placeholder METRICS_TOKEN

Yudhi Armyndharis4aeab9fe3c84
diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.ts
index 780336ad9..ad614c4f6 100644
--- a/src/config/bootstrap-security.spec.ts
+++ b/src/config/bootstrap-security.spec.ts
@@ -1,3 +1,5 @@
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
import {
resolveCorsPolicy,
isSwaggerEnabled,
@@ -385,6 +387,30 @@ describe('assertNoDefaultSecretsInProduction', () => {
).not.toThrow();
});
+ // The scrape endpoint is public and this token is its only credential, so the value .env.example ships
+ // (a copy-paste away from production) and the known weak values must not boot. There is no length floor:
+ // a short token that is not a known default keeps booting.
+ it('refuses the METRICS_TOKEN that .env.example ships, and known weak values, in production', () => {
+ const example = readFileSync(join(__dirname, '..', '..', '.env.example'), 'utf8');
+ const shipped = /^#?\s*METRICS_TOKEN=(.+)$/m.exec(example)?.[1];
+ expect(shipped).toBeTruthy();
+ for (const metricsToken of [shipped, ` ${shipped?.toUpperCase()} `, 'change-me', 'test', 'Secret']) {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv: 'production', metricsToken })).toThrow(
+ /METRICS_TOKEN/,
+ );
+ }
+ });
+
+ it('allows an unset, blank or non-default METRICS_TOKEN in production, and checks none outside it', () => {
+ // Unset or blank leaves the endpoint disabled, so there is no credential to be weak.
+ for (const metricsToken of [undefined, '', ' ', 'prom-scrape', 'k'.repeat(40)]) {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv: 'production', metricsToken })).not.toThrow();
+ }
+ for (const nodeEnv of ['development', 'test', undefined]) {
+ expect(() => assertNoDefaultSecretsInProduction({ nodeEnv, metricsToken: 'test' })).not.toThrow();
+ }
+ });
+
it('allows the default sqlite + local-storage prod setup (no secrets needed)', () => {
expect(() =>
assertNoDefaultSecretsInProduction({ nodeEnv: 'production', databaseType: 'sqlite', storageType: 'local' }),
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index ef20d2c1d..4efe5d14f 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -150,6 +150,13 @@ const FORBIDDEN_PROD_SECRETS = new Set([
'demo',
]);
+/**
+ * The METRICS_TOKEN value .env.example ships (commented out). Kept apart from the list above, which is
+ * shared by every secret: this string is a placeholder only for the scrape token, and is a publicly known
+ * credential for the endpoint once someone uncomments the line unchanged.
+ */
+const METRICS_TOKEN_PLACEHOLDER = 'change-me-to-a-long-random-string';
+
/**
* Whether to warn that API_KEY_PEPPER is unset in production. Without a pepper, stored API-key hashes
* fall back to plain SHA-256 (still functional). Advisory only: enabling a pepper on an install with keys
@@ -210,6 +217,8 @@ export interface SecretCheckEnv {
allowDevApiKey?: string;
/** REDIS_PASSWORD — optional; passwordless private-network Redis is supported, so only a known placeholder is rejected. */
redisPassword?: string;
+ /** METRICS_TOKEN: optional (unset disables the scrape endpoint), so only a known placeholder is rejected. */
+ metricsToken?: string;
}
/**
@@ -275,6 +284,13 @@ export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {
if (env.redisPassword && FORBIDDEN_PROD_SECRETS.has(env.redisPassword.trim().toLowerCase())) {
problems.push('REDIS_PASSWORD');
}
+ // METRICS_TOKEN is optional too (unset leaves the public scrape endpoint disabled) and has no length
+ // floor, so only a known placeholder is refused: the .env.example value or a shared weak value. Trimmed
+ // like MetricsService trims it before comparing.
+ const metricsToken = env.metricsToken?.trim().toLowerCase();
+ if (metricsToken && (metricsToken === METRICS_TOKEN_PLACEHOLDER || FORBIDDEN_PROD_SECRETS.has(metricsToken))) {
+ problems.push('METRICS_TOKEN');
+ }
// ALLOW_DEV_API_KEY=true seeds the publicly-documented `dev-admin-key` as an ADMIN credential
// (when no API_MASTER_KEY is set) — never allow that opt-in to be carried into production.
if (env.allowDevApiKey === 'true') {
diff --git a/src/main.ts b/src/main.ts
index 1ab2bb45c..44f7646fe 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -96,6 +96,7 @@ async function bootstrap() {
apiMasterKey: process.env.API_MASTER_KEY,
allowDevApiKey: process.env.ALLOW_DEV_API_KEY,
redisPassword: process.env.REDIS_PASSWORD,
+ metricsToken: process.env.METRICS_TOKEN,
});
// Advisory (not enforced): without API_KEY_PEPPER, stored API-key hashes use plain SHA-256. Enabling