Skip to content

fix: eliminate TOCTOU race in RunState.load() - #3908

Open
Quratulain-bilal wants to merge 2 commits into
github:mainfrom
Quratulain-bilal:fix/load-state-toctou-v2
Open

fix: eliminate TOCTOU race in RunState.load()#3908
Quratulain-bilal wants to merge 2 commits into
github:mainfrom
Quratulain-bilal:fix/load-state-toctou-v2

Conversation

@Quratulain-bilal

Copy link
Copy Markdown
Contributor

Problem

RunState.load() checks exists() then calls open(). Between the two calls the file can be deleted, causing a raw FileNotFoundError instead of the intended custom message.

Fix

Remove the exists() pre-check and wrap open() in try/except FileNotFoundError.

Testing

  • Verified custom error message is raised when state.json is missing

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/specify_cli/workflows/engine.py
Comment thread src/specify_cli/workflows/engine.py
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.
@Quratulain-bilal
Quratulain-bilal force-pushed the fix/load-state-toctou-v2 branch from 0ec8d31 to c010d11 Compare August 10, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants