fix: eliminate TOCTOU race in RunState.load() - #3908
Quratulain-bilal wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Eliminates the RunState.load() TOCTOU race by handling missing files during open().
Changes:
- Replaces the
exists()pre-check with exception handling. - Preserves the custom missing-state error.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/workflows/engine.py |
Makes run-state loading race-safe. |
Review details
Tip
Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 1/1 changed files
- Comments generated: 2
- Review effort level: Balanced
Remove exists() pre-check and wrap open() in try/except FileNotFoundError to provide a clear custom error message even under race conditions.
- Update docstring to describe the direct open() read instead of the removed exists() probe, keeping the security rationale accurate. - Add test_load_not_found_custom_message: verifies the custom 'Run state not found:' error message is raised. - Add test_load_not_found_no_exists_probe: patches builtins.open to raise FileNotFoundError and asserts the custom message, proving the TOCTOU-eliminated code path works correctly.
0ec8d31 to
c010d11
Compare
| with patch("builtins.open", side_effect=FileNotFoundError): | ||
| with pytest.raises(FileNotFoundError, match="Run state not found:"): | ||
| RunState.load("nonexistent", project_dir) |
|
Thanks — and unlike much of the batch, this one ships a regression test, which is appreciated. Two things before it can move: (1) please disclose any AI assistance per CONTRIBUTING; (2) it currently conflicts with |
Problem
RunState.load()checksexists()then callsopen(). Between the two calls the file can be deleted, causing a rawFileNotFoundErrorinstead of the intended custom message.Fix
Remove the
exists()pre-check and wrapopen()intry/except FileNotFoundError.Testing