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

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.

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.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread tests/test_workflows.py
Comment on lines +7106 to +7108
with patch("builtins.open", side_effect=FileNotFoundError):
with pytest.raises(FileNotFoundError, match="Run state not found:"):
RunState.load("nonexistent", project_dir)
@mnriem mnriem added triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate author-needs-disclosure AI use, or the agent/model/settings behind it, not disclosed per CONTRIBUTING author-needs-rebase Branch conflicts with main — rebase/resolve before merge labels Sep 10, 2026
@mnriem

mnriem commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

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 main — please rebase. On severity: the TOCTOU here only changes which error message surfaces (raw FileNotFoundError vs the custom one) in an extreme delete-mid-open race, so it's low-priority and sits behind proven work — but with disclosure + rebase it can be reviewed. Marking author-awaiting.

@mnriem mnriem added the author-awaiting Waiting on author response label Sep 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author-awaiting Waiting on author response author-needs-disclosure AI use, or the agent/model/settings behind it, not disclosed per CONTRIBUTING author-needs-rebase Branch conflicts with main — rebase/resolve before merge triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants