Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): stop warning about a missing file before a secret write

Yudhi Armyndharis2551d7fb9d2a
diff --git a/src/common/utils/secret-file.spec.ts b/src/common/utils/secret-file.spec.ts
index aa38440f5..23137ec23 100644
--- a/src/common/utils/secret-file.spec.ts
+++ b/src/common/utils/secret-file.spec.ts
@@ -1,8 +1,16 @@
import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
+import * as fs from 'fs';
import { writeSecretFile } from './secret-file';
+// A named import of chmodSync cannot be spied on after the fact, so route it through a mock that
+// defaults to the real call.
+jest.mock('fs', () => {
+ const actual = jest.requireActual<typeof import('fs')>('fs');
+ return { ...actual, chmodSync: jest.fn(actual.chmodSync) };
+});
+
describe('writeSecretFile', () => {
let dir: string;
beforeEach(() => {
@@ -39,17 +47,32 @@ describe('writeSecretFile', () => {
expect(readFileSync(p, 'utf8')).toBe('new');
});
- it('warns to the console when a chmod fails (does not stay silently world-readable)', () => {
- const p = join(dir, 'ghost');
+ it('does not warn when the file simply does not exist yet (create-mode covers it)', () => {
+ const p = join(dir, 'fresh');
+ const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
+
+ writeSecretFile(p, 'secret');
+
+ expect(warnSpy).not.toHaveBeenCalled();
+ expect(readFileSync(p, 'utf8')).toBe('secret');
+ warnSpy.mockRestore();
+ });
+
+ it('warns to the console when a chmod fails for any other reason (does not stay silently world-readable)', () => {
+ const p = join(dir, 'nochmod');
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
+ jest.mocked(fs.chmodSync).mockImplementation(() => {
+ throw Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' });
+ });
- // chmod a path that does not exist → ENOENT on the pre-write call. The write still succeeds
- // (create-mode), and the failure is surfaced via console.warn instead of being swallowed.
+ // The write still succeeds (create-mode), and both failures are surfaced instead of swallowed.
writeSecretFile(p, 'secret');
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('pre-write chmod 0o600 failed'));
+ expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('post-write chmod 0o600 failed'));
expect(readFileSync(p, 'utf8')).toBe('secret');
+ jest.mocked(fs.chmodSync).mockImplementation(jest.requireActual<typeof import('fs')>('fs').chmodSync);
warnSpy.mockRestore();
});
});
diff --git a/src/common/utils/secret-file.ts b/src/common/utils/secret-file.ts
index baaba53ef..79fdc8422 100644
--- a/src/common/utils/secret-file.ts
+++ b/src/common/utils/secret-file.ts
@@ -13,10 +13,12 @@ export function writeSecretFile(filePath: string, content: string): void {
try {
chmodSync(filePath, 0o600);
} catch (error) {
- // file not present yet, or chmod unsupported — create-mode below covers a new file. Log the
- // failure so a world-readable secret on a chmod-unsupported FS (or an unexpected error) is
- // not silently left world-readable after a rewrite.
- console.warn(`[OpenWA] pre-write chmod 0o600 failed for ${filePath}: ${(error as Error).message}`);
+ // A file not present yet is expected: create-mode below covers it. Log any other failure so a
+ // world-readable secret on a chmod-unsupported FS (or an unexpected error) is not silently left
+ // world-readable after a rewrite.
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
+ console.warn(`[OpenWA] pre-write chmod 0o600 failed for ${filePath}: ${(error as Error).message}`);
+ }
}
writeFileSync(filePath, content, { mode: 0o600 });
try {