Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): exempt bundled datastore aliases from the secret guard

Yudhi Armyndharis872675af8679
diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.ts
index ad614c4f6..2dea9297d 100644
--- a/src/config/bootstrap-security.spec.ts
+++ b/src/config/bootstrap-security.spec.ts
@@ -304,6 +304,74 @@ describe('assertNoDefaultSecretsInProduction', () => {
).not.toThrow();
});
+ // The same aliases the rest of the app resolves to the bundled container: its network alias or its
+ // container name, in any letter case, with or without the trailing dot of an absolute DNS name.
+ it.each(['Postgres', 'postgres.', 'openwa-postgres', ' OPENWA-POSTGRES. '])(
+ 'exempts the built-in Postgres default for the bundled host %s',
+ databaseHost => {
+ const env = {
+ nodeEnv: 'production',
+ databaseType: 'postgres',
+ databasePassword: 'openwa',
+ postgresBuiltIn: 'true',
+ databaseHost,
+ };
+ expect(() => assertNoDefaultSecretsInProduction(env)).not.toThrow();
+ expect(() => assertNoDefaultSecretsInProduction({ ...env, postgresBuiltIn: 'false' })).toThrow(
+ /DATABASE_PASSWORD/,
+ );
+ },
+ );
+
+ it.each(['http://minio.:9000', 'http://openwa-minio:9000', 'https://OPENWA-MINIO.:9000'])(
+ 'exempts the built-in MinIO defaults for the bundled endpoint %s',
+ s3Endpoint => {
+ const env = {
+ nodeEnv: 'production',
+ storageType: 's3',
+ s3AccessKey: 'minioadmin',
+ s3SecretKey: 'minioadmin',
+ minioBuiltIn: 'true',
+ s3Endpoint,
+ };
+ expect(() => assertNoDefaultSecretsInProduction(env)).not.toThrow();
+ expect(() => assertNoDefaultSecretsInProduction({ ...env, minioBuiltIn: 'false' })).toThrow(
+ /S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY/,
+ );
+ },
+ );
+
+ it.each(['postgres.example.invalid', 'evil-postgres', 'openwa-postgres-evil'])(
+ 'still refuses the default Postgres password for the external host %s',
+ databaseHost => {
+ expect(() =>
+ assertNoDefaultSecretsInProduction({
+ nodeEnv: 'production',
+ databaseType: 'postgres',
+ databasePassword: 'openwa',
+ postgresBuiltIn: 'true',
+ databaseHost,
+ }),
+ ).toThrow(/DATABASE_PASSWORD/);
+ },
+ );
+
+ it.each(['http://minio.example.invalid:9000', 'http://evil-minio:9000', 'http://openwa-minio-evil:9000'])(
+ 'still refuses the default S3 credentials for the external endpoint %s',
+ s3Endpoint => {
+ expect(() =>
+ assertNoDefaultSecretsInProduction({
+ nodeEnv: 'production',
+ storageType: 's3',
+ s3AccessKey: 'minioadmin',
+ s3SecretKey: 'minioadmin',
+ minioBuiltIn: 'true',
+ s3Endpoint,
+ }),
+ ).toThrow(/S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY/);
+ },
+ );
+
it('refuses prod with default MinIO/S3 credentials', () => {
expect(() =>
assertNoDefaultSecretsInProduction({
diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.ts
index 4efe5d14f..fd837250a 100644
--- a/src/config/bootstrap-security.ts
+++ b/src/config/bootstrap-security.ts
@@ -1,3 +1,5 @@
+import { namesBundledContainer } from '../modules/docker/bundled-host';
+
export interface CorsPolicy {
/** Explicit origin allowlist (empty when none / wildcard blocked). */
origins: string[];
@@ -185,18 +187,6 @@ export function isNodeEnvUnset(nodeEnv?: string): boolean {
return !nodeEnv?.trim();
}
-/** A built-in S3 endpoint is the bundled MinIO (host `minio`). An unset endpoint means the standard
- * AWS regional endpoint, so it is external and its credentials must never be exempted. */
-function isInternalS3Endpoint(endpoint?: string): boolean {
- const e = endpoint?.trim();
- if (!e) return false;
- try {
- return new URL(e).hostname === 'minio';
- } catch {
- return false;
- }
-}
-
export interface SecretCheckEnv {
nodeEnv?: string;
databaseType?: string;
@@ -257,11 +247,11 @@ export function assertNoDefaultSecretsInProduction(env: SecretCheckEnv): void {
// flag AND an internal host: a host-pinned EXTERNAL datastore (even with the built-in flag set) is
// reachable, so its weak credential is still enforced.
const dbHost = env.databaseHost?.trim();
- const dbExempt = env.postgresBuiltIn === 'true' && (!dbHost || dbHost === 'postgres');
+ const dbExempt = env.postgresBuiltIn === 'true' && (!dbHost || namesBundledContainer('postgres', dbHost));
if (env.databaseType === 'postgres' && !dbExempt && isWeak(env.databasePassword)) {
problems.push('DATABASE_PASSWORD');
}
- const s3Exempt = env.minioBuiltIn === 'true' && isInternalS3Endpoint(env.s3Endpoint);
+ const s3Exempt = env.minioBuiltIn === 'true' && namesBundledContainer('minio', env.s3Endpoint);
if (env.storageType === 's3' && !s3Exempt) {
// Name the canonical variables even when the value came from the legacy S3_ACCESS_KEY /
// S3_SECRET_KEY fallback: they are what the docs and .env.example tell an operator to set.