Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): refuse an unusable S3 endpoint only when the save changes it

Yudhi Armyndharis2455351714d0
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 1043634cc..f28173c2a 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -199,36 +199,86 @@ describe('InfraConfigController.saveConfig values the next boot refuses', () =>
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
- // The boot only reports an S3 endpoint the client cannot use, so a deployment running on its local fallback must
- // still be able to save other settings; only a save that names the storage type or the endpoint is refused.
- describe('an S3 endpoint without a scheme already stored', () => {
- const STORED =
- 'STORAGE_TYPE=s3\nS3_ENDPOINT=minio:9000\nS3_ACCESS_KEY_ID=AccessKey1234\nS3_SECRET_ACCESS_KEY=SecretKey1234567\n';
- beforeEach(() => {
+ // The boot only reports an S3 endpoint the client cannot use, so an instance that stored one keeps starting, and the
+ // dashboard, which sends the storage type and endpoint with every save, must keep saving. The endpoint is refused
+ // only when the save changes what the boot reads for it or newly selects S3.
+ describe('an S3 endpoint without a scheme', () => {
+ const KEYS = 'S3_ACCESS_KEY_ID=AccessKey1234\nS3_SECRET_ACCESS_KEY=SecretKey1234567\n';
+ const S3_STORED = `STORAGE_TYPE=s3\nS3_ENDPOINT=minio:9000\n${KEYS}`;
+ const NEW_KEYS = { s3AccessKey: 'AccessKey1234', s3SecretKey: 'SecretKey1234567' };
+ const LOCAL_STORED = `STORAGE_TYPE=local\nS3_ENDPOINT=minio:9000\n${KEYS}`;
+ const store = (text: string) => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
- (fs.readFileSync as jest.Mock).mockReturnValue(STORED);
+ (fs.readFileSync as jest.Mock).mockReturnValue(text);
(fs.writeFileSync as jest.Mock).mockClear();
- });
+ };
+ // What the dashboard sends after a reload: the storage section is always present, with the stored endpoint.
+ const dashboardSave = (storage: object) =>
+ ({
+ engine: { headless: false },
+ storage: { type: 's3', s3Bucket: 'media', s3Endpoint: 'minio:9000', ...storage },
+ }) as never;
afterEach(() => {
(fs.existsSync as jest.Mock).mockReturnValue(false);
(fs.readFileSync as jest.Mock).mockReturnValue('');
+ delete process.env.S3_ENDPOINT;
+ delete process.env.STORAGE_TYPE;
+ recordPinnedEnvKeys({});
});
it('does not stop a save of another section', () => {
+ store(S3_STORED);
expect(newController().saveConfig({ engine: { headless: false } }).saved).toBe(true);
});
- it('refuses a save that names the storage type again, with the boot text', () => {
- expect(() => newController().saveConfig({ storage: { type: 's3', s3Bucket: 'media' } } as never)).toThrow(
+ it('does not stop a dashboard save that resubmits the stored endpoint', () => {
+ store(S3_STORED);
+ expect(newController().saveConfig(dashboardSave({})).saved).toBe(true);
+ });
+
+ it('refuses a save that changes it to another unusable value, with the boot text', () => {
+ store(S3_STORED);
+ expect(() => newController().saveConfig(dashboardSave({ s3Endpoint: 'other:9000' }))).toThrow(
/fails boot validation\. S3_ENDPOINT must be an absolute http\(s\) URL/,
);
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
+ it('refuses a save that newly selects S3 beside it', () => {
+ store(LOCAL_STORED);
+ expect(() =>
+ newController().saveConfig({ storage: { type: 's3', s3Endpoint: 'minio:9000', ...NEW_KEYS } } as never),
+ ).toThrow(/S3_ENDPOINT must be an absolute http\(s\) URL/);
+ expect(fs.writeFileSync).not.toHaveBeenCalled();
+ });
+
+ it('refuses newly selecting S3 while the environment pins it', () => {
+ store(LOCAL_STORED);
+ process.env.S3_ENDPOINT = 'minio:9000';
+ recordPinnedEnvKeys({ S3_ENDPOINT: 'minio:9000' });
+ expect(() => newController().saveConfig({ storage: { type: 's3', ...NEW_KEYS } } as never)).toThrow(
+ /S3_ENDPOINT must be an absolute http\(s\) URL/,
+ );
+ });
+
+ it('saves a switch to local storage', () => {
+ store(S3_STORED);
+ expect(newController().saveConfig({ storage: { type: 'local', localPath: './data/media' } } as never).saved).toBe(
+ true,
+ );
+ });
+
it('accepts the endpoint once it carries the scheme', () => {
- expect(
- newController().saveConfig({ storage: { type: 's3', s3Endpoint: 'http://minio:9000' } } as never).saved,
- ).toBe(true);
+ store(S3_STORED);
+ expect(newController().saveConfig(dashboardSave({ s3Endpoint: 'http://minio:9000' })).saved).toBe(true);
+ });
+
+ it('does not block a save while the environment pins it, which the page cannot change', () => {
+ store(S3_STORED);
+ Object.assign(process.env, { S3_ENDPOINT: 'minio:9000', STORAGE_TYPE: 's3' });
+ recordPinnedEnvKeys({ S3_ENDPOINT: 'minio:9000', STORAGE_TYPE: 's3' });
+ expect(newController().saveConfig(dashboardSave({})).saved).toBe(true);
+ expect(newController().saveConfig(dashboardSave({ s3Endpoint: 'http://minio:9000' })).saved).toBe(true);
});
});
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 8451e7a9e..8be603926 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -216,7 +216,7 @@ export class InfraConfigController {
this.assertNoLineBreakValues(updates);
const merged = this.mergeWithExisting(existing, ctx);
this.assertProductionBootable(merged);
- this.assertBootValidates(updates, merged);
+ this.assertBootValidates(existing, merged);
this.assertStorageRootWritable(updates, merged);
this.persistGeneratedEnv(envPath, merged);
this.auditConfigSaved(config, profiles);
@@ -355,14 +355,19 @@ export class InfraConfigController {
// The environment validation the next boot runs (env.validation.ts), applied to the same view of the merged result.
// Without it a save the dashboard accepts, a PostgreSQL selection with no host or a pool size that is not a plain
// integer, crash-loops the instance and the dashboard that could undo it.
- // The boot only reports an S3 endpoint the client cannot use, so a deployment on its local fallback keeps starting
- // and keeps saving other settings; a save that names the storage type or the endpoint is refused instead.
- private assertBootValidates(updates: Record<string, string>, merged: Record<string, string>): void {
+ // The boot only reports an S3 endpoint the client cannot use, so an instance that stored one keeps starting and keeps
+ // saving. The dashboard sends the storage type and endpoint with every save, so the endpoint is refused only when this
+ // save changes what the boot reads for it or newly selects S3; resubmitting it, an environment pin the page cannot
+ // change, or a switch to local storage goes through.
+ private assertBootValidates(existing: Record<string, string>, merged: Record<string, string>): void {
const bootValue = this.bootValueReader(merged);
+ const before = this.bootValueReader(existing);
try {
const env = Object.fromEntries(Object.keys({ ...process.env, ...merged }).map(key => [key, bootValue(key)]));
validateEnv(env);
- const s3Problem = 'STORAGE_TYPE' in updates || 'S3_ENDPOINT' in updates ? s3EndpointError(env) : null;
+ const endpointChanged = (before('S3_ENDPOINT') ?? '').trim() !== (bootValue('S3_ENDPOINT') ?? '').trim();
+ const newlyS3 = before('STORAGE_TYPE') !== 's3' && bootValue('STORAGE_TYPE') === 's3';
+ const s3Problem = endpointChanged || newlyS3 ? s3EndpointError(env) : null;
if (s3Problem) throw new Error(s3Problem);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);