#17463: fix: write config files with explicit 0o600 mode instead of post-write chmod
size: M
Cluster:
Config File Security Enhancements
Closes #17440
## Changes
### Problem
Config files containing API keys/secrets were written with default umask permissions, then `chmod 0o600` was called separately. This created:
1. A race window where secrets could be world-readable between write and chmod
2. Silent failures when chmod failed (errors were swallowed with empty `.catch(() => {})`)
### Fix
- **`src/infra/json-file.ts`**: Pass `{ mode: 0o600 }` directly to `writeFileSync` so files are created with correct permissions atomically. Keep `chmodSync` as best-effort fallback for pre-existing files whose permissions drifted.
- **`src/config/io.ts`**: Replace silent `.catch(() => {})` with a warning log when chmod fails in the Windows rename-fallback path.
- **`src/infra/tls/gateway.ts`**: Replace silent `.catch(() => {})` with warning logs when TLS key/cert chmod fails. Added `warn` to the log type signature.
### Tests
- Added `src/infra/json-file.test.ts` — verifies 0o600 permissions, JSON content, overwrite behavior, and parent dir creation (0o700).
- Added `src/config/io.permissions.test.ts` — verifies writeConfigFile creates files with 0o600 and logs warnings on chmod failure (mocked fs).
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Closes a security race condition where config files containing API keys/secrets were briefly world-readable between write and chmod operations. The fix passes `mode: 0o600` atomically to `writeFileSync` and sets a restrictive umask before TLS certificate generation, eliminating the race window. Silent error handlers have been replaced with warning logs for better observability.
- `src/infra/json-file.ts`: atomic permission setting via `writeFileSync` mode option
- `src/infra/tls/gateway.ts`: umask protection (0o077) around openssl cert generation
- `src/config/io.ts`: warning logs when chmod fails in Windows fallback path
- Comprehensive test coverage for permission verification and error logging
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with no identified issues
- The security fix is well-implemented with atomic permission setting, comprehensive test coverage validates the changes on Unix systems, error handling has been improved with proper logging, and the umask approach for TLS certificate generation is a best practice for closing race windows
- No files require special attention
<sub>Last reviewed commit: 5d5dede</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17896: fix(config): warn instead of silently swallowing chmod failure on c...
by PlayerGhost · 2026-02-16
86.0%
#6257: Fix: Create sensitive directories with mode 0o700
by sloppy-claw · 2026-02-01
80.3%
#18954: fix(security): secure cron, browser, settings dirs in doctor --fix
by BinHPdev · 2026-02-17
79.5%
#18924: fix(security): tighten permissions on cron/, browser/, settings/ dirs…
by rexlunae · 2026-02-17
79.2%
#5649: fix(security): harden file permissions and header merging
by sfo2001 · 2026-01-31
78.1%
#8751: fix(security): use 0o600 permissions for session transcript files
by revenuestack · 2026-02-04
78.1%
#18878: fix(cron): set secure file permissions (0o600) on jobs.json
by MisterGuy420 · 2026-02-17
77.6%
#19191: fix(security): harden cron file permissions to 0o600
by Kropiunig · 2026-02-17
77.4%
#17887: refactor: consolidate atomic JSON writes into shared writeJsonAtomic
by iyoda · 2026-02-16
76.9%
#18939: fix: tighten permissions on cron/, browser/, settings/, logs/ in do...
by sriram369 · 2026-02-17
76.7%