Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(config): pin the secret guard wiring in main.ts

Yudhi Armyndharise66922434cd1
diff --git a/src/config/boot-env-validation.spec.ts b/src/config/boot-env-validation.spec.ts
index 147e387f5..01f6cd5cb 100644
--- a/src/config/boot-env-validation.spec.ts
+++ b/src/config/boot-env-validation.spec.ts
@@ -1,5 +1,5 @@
import { spawnSync } from 'node:child_process';
-import { mkdtempSync, rmSync } from 'node:fs';
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
@@ -48,3 +48,41 @@ describe('main.ts validates the environment before any boot side effect', () =>
expect(output.match(/LOG_LEVEL must be one of/g)).toHaveLength(1);
}, 90_000);
});
+
+/**
+ * The production secret guard only checks what main.ts hands it, and every field of SecretCheckEnv is optional, so a
+ * field that is declared but not passed, or read from the wrong variable, leaves its check dead without any error:
+ * the guard simply never sees the value. TypeScript cannot tell, so the call is read as source.
+ */
+describe('main.ts hands the production secret guard every value it checks', () => {
+ const read = (file: string): string => readFileSync(join(__dirname, '..', file), 'utf8');
+ // The fields whose variable is not the field name in upper snake case.
+ const VARIABLES: Record<string, string> = {
+ postgresBuiltIn: 'POSTGRES_BUILTIN',
+ minioBuiltIn: 'MINIO_BUILTIN',
+ s3AccessKey: 'S3_ACCESS_KEY_ID',
+ s3SecretKey: 'S3_SECRET_ACCESS_KEY',
+ };
+ const variableOf = (field: string): string =>
+ VARIABLES[field] ?? field.replace(/([a-z\d])([A-Z])/g, '$1_$2').toUpperCase();
+
+ const declared = [
+ ...(
+ /export interface SecretCheckEnv \{([\s\S]*?)\n\}/.exec(read('config/bootstrap-security.ts'))?.[1] ?? ''
+ ).matchAll(/^ {2}(\w+)\?:/gm),
+ ].map(match => match[1]);
+ const passed = /assertNoDefaultSecretsInProduction\(\{([\s\S]*?)\n {2}\}\);/.exec(read('main.ts'))?.[1] ?? '';
+
+ it('finds the fields the guard declares and the call that fills them', () => {
+ expect(declared).toEqual(expect.arrayContaining(['nodeEnv', 'databasePassword', 'apiMasterKey', 'redisPassword']));
+ expect(passed).toContain('nodeEnv: process.env.NODE_ENV');
+ });
+
+ it('passes every declared field, each read from its own environment variable', () => {
+ const unwired = declared.filter(
+ field => !new RegExp(`^ {4}${field}: .*process\\.env\\.${variableOf(field)}\\b`, 'm').test(passed),
+ );
+
+ expect(unwired).toEqual([]);
+ });
+});