Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): refuse a saved port or pool size the next boot rejects

Yudhi Armyndharis639a74b1cf53
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index 75da7030a..77b8435e3 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -20,6 +20,16 @@ function setSecret(updates: Record<string, string>, key: string, value: string |
if (value) updates[key] = value;
}
+// What the next boot's validation (env.validation.ts) reads from these keys, refused here instead: the saved
+// file is loaded at boot, and a value it rejects crash-loops the instance and the dashboard that could undo it.
+function portValue(label: string, value: string): string {
+ const port = value.trim();
+ if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535) {
+ throw new BadRequestException(`${label} must be an integer port in [1, 65535] (got ${JSON.stringify(value)})`);
+ }
+ return port;
+}
+
// Database. NOTE: these keys must match what src/config/configuration.ts reads.
export function applyDatabaseSection(
database: DatabaseConfigDto,
@@ -63,9 +73,13 @@ export function applyDatabaseSection(
if (database.builtIn === false && existing.POSTGRES_BUILTIN === 'true' && !database.password) {
staleKeys.add('DATABASE_PASSWORD');
}
- if (database.host !== undefined) updates.DATABASE_HOST = database.host || 'localhost';
- if (database.port !== undefined) updates.DATABASE_PORT = database.port || '5432';
- if (database.username !== undefined) updates.DATABASE_USERNAME = database.username || 'postgres';
+ // A blank host or user is what boot reads as unset, which it refuses for PostgreSQL, so a padded or
+ // whitespace-only value falls back to the default like an empty one.
+ if (database.host !== undefined) updates.DATABASE_HOST = database.host.trim() || 'localhost';
+ if (database.port !== undefined) {
+ updates.DATABASE_PORT = database.port.trim() ? portValue('database.port', database.port) : '5432';
+ }
+ if (database.username !== undefined) updates.DATABASE_USERNAME = database.username.trim() || 'postgres';
setSecret(updates, 'DATABASE_PASSWORD', database.password);
if (database.database !== undefined) updates.DATABASE_NAME = database.database || 'openwa';
if (database.schema !== undefined) {
@@ -76,6 +90,11 @@ export function applyDatabaseSection(
}
}
if (database.poolSize !== undefined) {
+ if (!Number.isInteger(database.poolSize) || database.poolSize < 0) {
+ throw new BadRequestException(
+ `database.poolSize must be a non-negative integer (got ${JSON.stringify(database.poolSize)})`,
+ );
+ }
updates.DATABASE_POOL_SIZE = String(database.poolSize || 10);
}
if (database.sslEnabled !== undefined) {
@@ -123,8 +142,10 @@ export function applyRedisSection(
if (!redis.password) staleKeys.add('REDIS_PASSWORD');
} else {
// External Redis (explicit, or inherited when builtIn is absent)
- if (redis.host !== undefined) updates.REDIS_HOST = redis.host || 'localhost';
- if (redis.port !== undefined) updates.REDIS_PORT = redis.port || '6379';
+ if (redis.host !== undefined) updates.REDIS_HOST = redis.host.trim() || 'localhost';
+ if (redis.port !== undefined) {
+ updates.REDIS_PORT = redis.port.trim() ? portValue('redis.port', redis.port) : '6379';
+ }
}
setSecret(updates, 'REDIS_PASSWORD', redis.password);
const redisEnabled = redis.enabled ?? existing.REDIS_ENABLED === 'true';
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 764858d64..fe25127c5 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -69,6 +69,47 @@ describe('InfraConfigController.saveConfig SSL reject-unauthorized', () => {
});
});
+// The saved file is loaded by the next boot, whose validation refuses these values and crash-loops the
+// instance, with the dashboard that could undo the save down.
+describe('InfraConfigController.saveConfig values the next boot refuses', () => {
+ const newController = () => new InfraConfigController({} as never, {} as never, {} as never);
+ const external = { type: 'postgres', builtIn: false, host: 'db', username: 'u', password: 'unit-test-pw' };
+
+ it.each([
+ ['a database port that is not a number', { database: { ...external, port: 'abc' } }],
+ ['a database port above 65535', { database: { ...external, port: '99999' } }],
+ ['a database port of zero', { database: { ...external, port: '0' } }],
+ ['a negative pool size', { database: { ...external, poolSize: -5 } }],
+ ['a fractional pool size', { database: { ...external, poolSize: 1.5 } }],
+ ['a redis port that is not a number', { redis: { enabled: true, host: 'cache', port: 'x' } }],
+ ])('refuses %s instead of writing it', (_label, config) => {
+ (fs.writeFileSync as jest.Mock).mockClear();
+
+ expect(() => newController().saveConfig(config as never)).toThrow(BadRequestException);
+ expect(fs.writeFileSync).not.toHaveBeenCalled();
+ });
+
+ it('writes a blank host and user as the defaults and trims a padded port', () => {
+ (fs.writeFileSync as jest.Mock).mockClear();
+
+ newController().saveConfig({
+ database: {
+ type: 'postgres',
+ builtIn: false,
+ host: ' ',
+ port: ' 5433 ',
+ username: ' ',
+ password: 'unit-test-pw',
+ },
+ } as never);
+
+ const content = ((fs.writeFileSync as jest.Mock).mock.calls as Array<[string, string]>)[0][1];
+ expect(content).toContain('DATABASE_HOST=localhost');
+ expect(content).toContain('DATABASE_PORT=5433');
+ expect(content).toContain('DATABASE_USERNAME=postgres');
+ });
+});
+
describe('InfraConfigController PostgreSQL schema (POSTGRES_SCHEMA)', () => {
const newController = () => new InfraConfigController({} as never, {} as never, {} as never);