#11873: fix: eliminate TOCTOU race in readExecApprovalsSnapshot
stale
## Summary
- `readExecApprovalsSnapshot()` used `existsSync()` followed by `readFileSync()` without wrapping the read in a try-catch
- If the approvals file is deleted between the two calls (e.g. by a concurrent reset), `readFileSync` throws an unhandled ENOENT exception
- Replaces the `existsSync` guard with a try-catch around `readFileSync`, matching the pattern already used by `loadExecApprovals()` in the same file (line 255)
## Test plan
- [x] All 44 existing `exec-approvals.test.ts` tests pass
- [x] No behavioral change for normal operation — only the error path is now handled gracefully
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `readExecApprovalsSnapshot()` to avoid a TOCTOU race by removing the `existsSync()` pre-check and instead wrapping `readFileSync()` in a `try/catch`, returning a normalized empty approvals file when the file is missing/unreadable. This aligns the snapshot reader’s error handling with the existing `loadExecApprovals()` pattern in `src/infra/exec-approvals.ts`.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Change is narrowly scoped to error handling around reading the approvals file, removes an actual TOCTOU failure mode, and preserves existing behavior by returning the same normalized empty snapshot on read failure; no new side effects are introduced in the normal path.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#10729: fix: replace existsSync+readFileSync with try-catch in env-file
by Yida-Dev · 2026-02-06
77.8%
#15613: fix(config): align default pipelines across loadConfig and readConf...
by AI-Reviewer-QS · 2026-02-13
76.7%
#16991: fix(config): add missing defaults to config snapshot path
by AI-Reviewer-QS · 2026-02-15
76.5%
#21663: fix(gateway): prevent self-approval of timed-out exec requests
by AI-Reviewer-QS · 2026-02-20
73.8%
#17463: fix: write config files with explicit 0o600 mode instead of post-wr...
by miclaldogan · 2026-02-15
73.7%
#6577: fix: add null checks for stdout/stderr when using inherit-stdio fal...
by ncmalan · 2026-02-01
73.2%
#15628: fix: resolve session write lock race condition
by 1kuna · 2026-02-13
72.7%
#10283: fix(agents): close TOCTOU race in session write lock acquisition
by programming-pupil · 2026-02-06
72.6%
#10367: CLI/Ops: resilient browser fill + failover hardening + operations t...
by cluster2600 · 2026-02-06
72.3%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
72.3%