Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): write the env overlay in place where it cannot be replaced

Yudhi Armyndharisbce3abc3f563
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 904d5c23f..129781b80 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -413,22 +413,47 @@ export class InfraConfigController {
// Write to data/ so it persists across container restarts. Owner-only (0600): this file holds
// the DB/S3/Redis credentials, so it must not be world-readable between save and next restart.
// Staged beside the target and renamed over it: a write that fails part-way (disk full) would
- // otherwise leave a truncated overlay for the next boot to load.
- const staged = `${envPath}.${randomUUID()}.tmp`;
- try {
- writeSecretFile(staged, contents);
- fs.renameSync(staged, envPath);
- } catch (error) {
+ // otherwise leave a truncated overlay for the next boot to load. A symlink is written through to its
+ // target, and a file the filesystem will not replace by rename (a single-file bind mount, another
+ // device) is written in place, as before.
+ const target = this.overlayTarget(envPath);
+ if (target === undefined) {
+ writeSecretFile(envPath, contents);
+ } else {
+ const staged = `${target}.${randomUUID()}.tmp`;
try {
- fs.rmSync(staged, { force: true });
- } catch {
- // Keep the original write or rename error if cleanup also fails.
+ writeSecretFile(staged, contents);
+ fs.renameSync(staged, target);
+ } catch (error) {
+ try {
+ fs.rmSync(staged, { force: true });
+ } catch {
+ // Keep the original write or rename error if cleanup also fails.
+ }
+ if (!['EBUSY', 'EXDEV'].includes((error as NodeJS.ErrnoException).code ?? '')) throw error;
+ writeSecretFile(target, contents);
}
- throw error;
}
this.logger.log('Configuration saved', { envPath });
}
+ // The file a save replaces: the overlay itself, or what a symlink at its path points to. Undefined for a link
+ // that points nowhere, which only an in-place write through it can create.
+ private overlayTarget(envPath: string): string | undefined {
+ let isLink: boolean;
+ try {
+ isLink = fs.lstatSync(envPath).isSymbolicLink();
+ } catch {
+ return envPath;
+ }
+ if (!isLink) return envPath;
+ try {
+ return fs.realpathSync(envPath);
+ } catch {
+ return undefined;
+ }
+ }
+
private auditConfigSaved(config: SaveConfigDto, profiles: string[]): void {
// Audit the credential-bearing env mutation. Fire-and-forget (not awaited) so saveConfig stays
// synchronous — its validation rejections must remain synchronous throws the tests assert via
diff --git a/src/modules/infra/infra-config.persistence.spec.ts b/src/modules/infra/infra-config.persistence.spec.ts
index 563b9190e..62d10dde0 100644
--- a/src/modules/infra/infra-config.persistence.spec.ts
+++ b/src/modules/infra/infra-config.persistence.spec.ts
@@ -132,6 +132,41 @@ it('preserves the old overlay and removes staging after a replacement failure',
expect(actualFs.readdirSync(path.dirname(file))).toEqual(['.env.generated']);
});
+it.each(['EBUSY', 'EXDEV'])('writes in place when the filesystem refuses the replacement with %s', async code => {
+ seed({ ...database, PUPPETEER_HEADLESS: 'true' });
+ jest.mocked(fs.renameSync).mockImplementationOnce(() => {
+ throw Object.assign(new Error('the file cannot be replaced'), { code });
+ });
+ expect((await save({ engine: { headless: false } })).saved).toBe(true);
+ expect(dotenv.parse(actualFs.readFileSync(file)).PUPPETEER_HEADLESS).toBe('false');
+ expect(actualFs.readdirSync(path.dirname(file))).toEqual(['.env.generated']);
+});
+
+describe('an overlay that is a symbolic link', () => {
+ const managed = () => path.join(directory, 'managed', 'overlay.env');
+
+ it('is written through to its target and stays a link', async () => {
+ actualFs.mkdirSync(path.dirname(managed()));
+ actualFs.renameSync(file, managed());
+ actualFs.symlinkSync(managed(), file);
+ expect((await save({ engine: { headless: false } })).saved).toBe(true);
+ expect(actualFs.lstatSync(file).isSymbolicLink()).toBe(true);
+ expect(dotenv.parse(actualFs.readFileSync(managed())).PUPPETEER_HEADLESS).toBe('false');
+ expect(actualFs.readdirSync(path.dirname(file)).sort()).toEqual(['.env.generated']);
+ expect(actualFs.readdirSync(path.dirname(managed()))).toEqual(['overlay.env']);
+ });
+
+ it('is created at its target when the link points nowhere', async () => {
+ actualFs.unlinkSync(file);
+ actualFs.symlinkSync(path.join(directory, 'data', 'created.env'), file);
+ expect((await save({ engine: { headless: false } })).saved).toBe(true);
+ expect(actualFs.lstatSync(file).isSymbolicLink()).toBe(true);
+ expect(dotenv.parse(actualFs.readFileSync(path.join(directory, 'data', 'created.env'))).PUPPETEER_HEADLESS).toBe(
+ 'false',
+ );
+ });
+});
+
it('creates and replaces the overlay owner-only', async () => {
actualFs.unlinkSync(file);
expect((await save({ engine: { headless: false } })).saved).toBe(true);