Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): bind credentials to saved service hosts
Yudhi Armyndharis295e6866eadc
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.tsindex 993c8cdfa..6c21ee563 100644--- a/src/modules/infra/config-sections.spec.ts+++ b/src/modules/infra/config-sections.spec.ts@@ -182,7 +182,9 @@ it.each(cases)(existing,ctx,);- if (builtIn === false)+ if (builtIn === false) {+ delete expected.S3_ACCESS_KEY;+ delete expected.S3_SECRET_KEY;Object.assign(expected, {S3_ENDPOINT: 'https://new.external.example',S3_BUCKET: 'new-bucket',@@ -190,7 +192,7 @@ it.each(cases)(S3_ACCESS_KEY_ID: 'NewKey123456',S3_SECRET_ACCESS_KEY: 'NewSecret1234567890',});- else if (!(builtIn === undefined && pin === 'external' && !changed))+ } else if (!(builtIn === undefined && pin === 'external' && !changed))Object.assign(expected, {S3_ENDPOINT: 'http://minio:9000',S3_BUCKET: !changed && pin !== 'external' ? 'media' : 'openwa',@@ -269,3 +271,108 @@ it.each(credentialNames)('removes inactive S3 credentials %s and %s when selectiexpect(savedValues(existing, ctx)).toEqual({ STORAGE_TYPE: 'local', MINIO_BUILTIN: 'false' });});++it.each(['true', 'false'])(+ 'keeps only credentials belonging to the selected external database with startup flag %s',+ flag => {+ for (const type of ['postgres', 'sqlite']) {+ for (const host of ['db.external.example', ' OpenWA-POSTGRES. ']) {+ const existing = {+ DATABASE_TYPE: type,+ DATABASE_HOST: host,+ POSTGRES_BUILTIN: flag,+ DATABASE_PASSWORD: 'StoredPassword123456',+ };+ const ctx = context();++ applyDatabaseSection(+ { type: 'postgres', builtIn: false, host: 'db.external.example', password: '' },+ existing,+ ctx,+ );++ expect(savedValues(existing, ctx).DATABASE_PASSWORD).toBe(+ type === 'postgres' && host === 'db.external.example' ? 'StoredPassword123456' : undefined,+ );+ }+ }+ },+);++it.each(credentialNames)(+ 'keeps only credentials %s and %s belonging to selected external S3 storage',+ (access, secret) => {+ for (const flag of ['true', 'false']) {+ for (const type of ['s3', 'local']) {+ for (const endpoint of ['https://s3.external.example', 'http://OpenWA-MINIO.:9000']) {+ const existing = {+ STORAGE_TYPE: type,+ S3_ENDPOINT: endpoint,+ MINIO_BUILTIN: flag,+ [access]: 'StoredKey123456',+ [secret]: 'StoredSecret1234567890',+ };+ const ctx = context();++ applyStorageSection(+ { type: 's3', builtIn: false, s3Endpoint: 'https://s3.external.example', s3AccessKey: '', s3SecretKey: '' },+ existing,+ ctx,+ );++ const saved = savedValues(existing, ctx);+ expect([saved[access], saved[secret]]).toEqual(+ type === 's3' && endpoint === 'https://s3.external.example'+ ? ['StoredKey123456', 'StoredSecret1234567890']+ : [undefined, undefined],+ );+ }+ }+ }+ },+);++it.each(credentialNames)('fresh S3 credentials replace inactive %s and %s', (access, secret) => {+ const existing = { STORAGE_TYPE: 'local', [access]: 'OldKey123456', [secret]: 'OldSecret1234567890' };+ const ctx = context();++ applyStorageSection(+ { type: 's3', builtIn: false, s3AccessKey: 'NewKey123456', s3SecretKey: 'NewSecret1234567890' },+ existing,+ ctx,+ );++ expect(savedValues(existing, ctx)).toMatchObject({+ S3_ACCESS_KEY_ID: 'NewKey123456',+ S3_SECRET_ACCESS_KEY: 'NewSecret1234567890',+ });+ expect(savedValues(existing, ctx).S3_ACCESS_KEY).toBeUndefined();+ expect(savedValues(existing, ctx).S3_SECRET_KEY).toBeUndefined();+});++it('does not carry the bundled S3 endpoint into a newly selected external service', () => {+ const ctx = context();+ const existing = { STORAGE_TYPE: 's3', MINIO_BUILTIN: 'false', S3_ENDPOINT: 'http://OpenWA-MINIO.:9000' };++ applyStorageSection(+ { builtIn: false, s3AccessKey: 'NewKey123456', s3SecretKey: 'NewSecret1234567890' },+ existing,+ ctx,+ );++ expect(savedValues(existing, ctx).S3_ENDPOINT).toBeUndefined();+});++it.each(['true', 'false'])('keeps external Redis credentials independently of startup flag %s', flag => {+ const existing = {+ REDIS_ENABLED: 'true',+ REDIS_BUILTIN: flag,+ REDIS_HOST: 'redis.external.example',+ REDIS_PASSWORD: 'StoredRedis123456',+ };+ const ctx = context();++ applyRedisSection({ enabled: true, builtIn: false, host: existing.REDIS_HOST, password: '' }, existing, ctx);++ expect(savedValues(existing, ctx).REDIS_PASSWORD).toBe('StoredRedis123456');+});diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.tsindex befc28051..0f1650cf7 100644--- a/src/modules/infra/config-sections.ts+++ b/src/modules/infra/config-sections.ts@@ -117,10 +117,12 @@ export function applyDatabaseSection(// A newly initialized bundled database starts in public; an existing one keeps its schema.updates.POSTGRES_SCHEMA = kept.POSTGRES_SCHEMA || 'public';} else if (!dbBuiltIn) {- // External PostgreSQL. Flipping built-in -> external must not carry the bundled- // 'openwa' password into the external config: the production boot guard rejects- // it, so the next boot would crash-loop. A password in the same payload wins.- if (database.builtIn === false && existing.POSTGRES_BUILTIN === 'true' && !database.password) {+ // Only an already selected external PostgreSQL owns the stored password. Startup flags do not+ // identify its server, and inactive SQLite details must not seed a newly selected service.+ if (+ (existing.DATABASE_TYPE !== 'postgres' || namesBundledContainer('postgres', existing.DATABASE_HOST)) &&+ !database.password+ ) {staleKeys.add('DATABASE_PASSWORD');}// A blank host or user is what boot reads as unset, which it refuses for PostgreSQL, so a padded or@@ -288,14 +290,13 @@ export function applyStorageSection(updates.S3_BUCKET = kept.S3_BUCKET || 'openwa';updates.S3_REGION = kept.S3_REGION || 'us-east-1';} else if (!storageBuiltIn) {- // External S3/MinIO. Flipping built-in -> external must not carry the bundled- // 'minioadmin' credentials or the internal endpoint into the external config:- // the production boot guard rejects those credentials (crash-loop), and a stale- // MinIO endpoint would send AWS-bound traffic to the wrong host. Values in the- // same payload win.- if (storage.builtIn === false && existing.MINIO_BUILTIN === 'true') {+ // Only an already selected external S3 service owns the stored credentials and endpoint.+ // A new external selection must not reuse bundled or inactive local-storage details.+ if (existing.STORAGE_TYPE !== 's3' || namesBundledContainer('minio', existing.S3_ENDPOINT)) {if (!storage.s3AccessKey) staleKeys.add('S3_ACCESS_KEY_ID');if (!storage.s3SecretKey) staleKeys.add('S3_SECRET_ACCESS_KEY');+ staleKeys.add('S3_ACCESS_KEY');+ staleKeys.add('S3_SECRET_KEY');if (!storage.s3Endpoint) staleKeys.add('S3_ENDPOINT');}if (storage.s3Bucket !== undefined) updates.S3_BUCKET = storage.s3Bucket;diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.tsindex e410ca1ed..817f7707e 100644--- a/src/modules/infra/infra-config.controller.spec.ts+++ b/src/modules/infra/infra-config.controller.spec.ts@@ -407,7 +407,10 @@ describe('InfraConfigController.saveConfig env-name correctness and merge (#226)});it('does not blank a stored secret when the form submits an empty value', () => {- const env = written({ database: { type: 'postgres', host: 'db', password: '' } }, 'DATABASE_PASSWORD=keepme\n');+ const env = written(+ { database: { type: 'postgres', host: 'db', password: '' } },+ 'DATABASE_TYPE=postgres\nDATABASE_PASSWORD=keepme\n',+ );expect(env).toContain('DATABASE_PASSWORD=keepme');expect(env).toContain('DATABASE_HOST=db');});