Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): write the env overlay in place when it cannot be staged

Yudhi Armyndharis120767ea6fc7
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 8be603926..d4d8abcdb 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -86,6 +86,9 @@ interface SavedConfigResponse {
engine: { type: string; headless: boolean; sessionDataPath: string; browserArgs: string };
}
+// Errors of replacing the overlay by rename that the in-place write v0.25.0 used can still get past.
+const IN_PLACE_FALLBACK_CODES = ['EBUSY', 'EXDEV', 'EACCES', 'EPERM', 'EROFS'];
+
@ApiTags('infrastructure')
@Controller('infra')
// Every route here is deployment-global (data export/import, infra config, service orchestration),
@@ -426,8 +429,9 @@ export class InfraConfigController {
// 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. 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.
+ // target. A file the filesystem will not replace by rename (a single-file bind mount, another device) or
+ // that cannot be staged beside (a directory the app cannot write, holding a file it can) is written in
+ // place, as before. Any other failure, a full disk included, leaves the previous overlay untouched.
const target = this.overlayTarget(envPath);
if (target === undefined) {
writeSecretFile(envPath, contents);
@@ -442,7 +446,7 @@ export class InfraConfigController {
} catch {
// Keep the original write or rename error if cleanup also fails.
}
- if (!['EBUSY', 'EXDEV'].includes((error as NodeJS.ErrnoException).code ?? '')) throw error;
+ if (!IN_PLACE_FALLBACK_CODES.includes((error as NodeJS.ErrnoException).code ?? '')) throw error;
writeSecretFile(target, contents);
}
}
diff --git a/src/modules/infra/infra-config.persistence.spec.ts b/src/modules/infra/infra-config.persistence.spec.ts
index 7c612e861..01f576739 100644
--- a/src/modules/infra/infra-config.persistence.spec.ts
+++ b/src/modules/infra/infra-config.persistence.spec.ts
@@ -168,16 +168,72 @@ 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 => {
+it.each(['EBUSY', 'EXDEV', 'EACCES', 'EPERM', 'EROFS'])(
+ 'writes in place when the replacement fails 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']);
+ },
+);
+
+it.each(['EACCES', 'EPERM', 'EROFS'])('writes in place when the staged file cannot be created (%s)', async code => {
seed({ ...database, PUPPETEER_HEADLESS: 'true' });
- jest.mocked(fs.renameSync).mockImplementationOnce(() => {
- throw Object.assign(new Error('the file cannot be replaced'), { code });
+ jest.mocked(fs.writeFileSync).mockImplementationOnce(() => {
+ throw Object.assign(new Error('the directory cannot be written'), { 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']);
});
+it('fails the save when the in-place write fails too, leaving the old overlay', async () => {
+ seed({ ...database, PUPPETEER_HEADLESS: 'true' });
+ const before = actualFs.readFileSync(file);
+ jest
+ .mocked(fs.writeFileSync)
+ .mockImplementationOnce(() => {
+ throw Object.assign(new Error('the directory cannot be written'), { code: 'EACCES' });
+ })
+ .mockImplementationOnce(() => {
+ throw Object.assign(new Error('the file cannot be written'), { code: 'EIO' });
+ });
+ expect((await save({ engine: { headless: false } })).saved).toBe(false);
+ expect(actualFs.readFileSync(file)).toEqual(before);
+});
+
+it('never writes in place after the staged copy ran out of space', async () => {
+ seed({ ...database, PUPPETEER_HEADLESS: 'true' });
+ const before = actualFs.readFileSync(file);
+ const writes = jest.mocked(fs.writeFileSync);
+ writes.mockClear();
+ writes.mockImplementationOnce(() => {
+ throw Object.assign(new Error('no space left on device'), { code: 'ENOSPC' });
+ });
+ expect((await save({ engine: { headless: false } })).saved).toBe(false);
+ expect(writes).toHaveBeenCalledTimes(1);
+ expect(actualFs.readFileSync(file)).toEqual(before);
+});
+
+// A directory the app user cannot write that holds an overlay file it can: a real kernel refusal, not a mocked one.
+(process.getuid?.() === 0 ? it.skip : it)(
+ 'saves a writable overlay file inside a directory that cannot be written',
+ async () => {
+ seed({ ...database, PUPPETEER_HEADLESS: 'true' });
+ actualFs.chmodSync(path.dirname(file), 0o555);
+ try {
+ expect((await save({ engine: { headless: false } })).saved).toBe(true);
+ expect(dotenv.parse(actualFs.readFileSync(file)).PUPPETEER_HEADLESS).toBe('false');
+ } finally {
+ actualFs.chmodSync(path.dirname(file), 0o755);
+ }
+ },
+);
+
describe('an overlay that is a symbolic link', () => {
const managed = () => path.join(directory, 'managed', 'overlay.env');