fix: remove TOCTOU race in RunState.load (#3839)

Remove exists() check before open() and catch FileNotFoundError directly.
This prevents a race where the file is deleted between check and open,
while preserving the descriptive error message.
This commit is contained in:
Quratulain-bilal
2026-08-13 14:52:56 -05:00
committed by GitHub
parent 56aec8a936
commit c807e2450c
+5 -4
View File
@@ -743,12 +743,13 @@ class RunState:
cls._validate_run_id(run_id)
runs_dir = project_root / ".specify" / "workflows" / "runs" / run_id
state_path = runs_dir / "state.json"
if not state_path.exists():
try:
with open(state_path, encoding="utf-8") as f:
state_data = json.load(f)
except FileNotFoundError:
msg = f"Run state not found: {state_path}"
raise FileNotFoundError(msg)
with open(state_path, encoding="utf-8") as f:
state_data = json.load(f)
if not isinstance(state_data, dict):
raise ValueError("Invalid run state: expected a JSON object")
missing_fields = [