fix: guard yaml.safe_load, flock unlock, TOCTOU races, and atomic writes - #28018
fix: guard yaml.safe_load, flock unlock, TOCTOU races, and atomic writes#28018vanthinh6886 wants to merge 1 commit into
Conversation
1. trajectory_compressor.py: yaml.safe_load() returns None on empty
files, crashing with TypeError on `if 'tokenizer' in data`. Fix by
adding `or {}` fallback. (HIGH — blocks startup with empty config)
2. 6 files with fcntl.flock(LOCK_UN) in finally blocks without
try/except: cron/scheduler.py, hermes_cli/auth.py,
agent/shell_hooks.py, tools/skill_usage.py,
tools/environments/file_sync.py, tools/memory_tool.py. If unlock
raises OSError, fd.close() is skipped and the lock is held forever.
The msvcrt branches already had try/except; the fcntl branches did
not. Fix by wrapping in try/except (OSError, IOError): pass.
3. agent/copilot_acp_client.py line 639: TOCTOU race — path.exists()
followed by path.read_text() with no try/except. If file is deleted
between the check and the read, FileNotFoundError propagates. Fix
by using try/except FileNotFoundError.
4. gateway/sticker_cache.py: non-atomic write via Path.write_text()
can leave truncated JSON on crash, causing JSONDecodeError on next
load. Fix by writing to tempfile + fsync + os.replace (atomic).
|
BoardJames triage: current branch-local blocker is "vanthinh6886@gmail.com": "vanthinh6886",to |
outsourc-e
left a comment
There was a problem hiding this comment.
Reviewed locally in a clean worktree. I spot-checked the touched paths and compiled all 9 modified files. The changes are narrowly defensive: safe_load fallback on empty config, fail-safe unlock cleanup, TOCTOU hardening, and atomic sticker-cache writes. I did not find a new security concern in the implementation.
Summary
Fixes 4 classes of bugs across 9 files:
1.
yaml.safe_load()crash on empty config (HIGH)File:
trajectory_compressor.pyyaml.safe_load()returnsNonefor empty files. The subsequentif 'tokenizer' in dataraisesTypeError. Fix: addor {}fallback.2. Unguarded
fcntl.flock(LOCK_UN)in finally blocks (MEDIUM x6)Files:
cron/scheduler.py,hermes_cli/auth.py,agent/shell_hooks.py,tools/skill_usage.py,tools/environments/file_sync.py,tools/memory_tool.pyIf
flock(LOCK_UN)raisesOSError,fd.close()is skipped, leaking the fd and holding the lock forever. Themsvcrtbranches already hadtry/except; thefcntlbranches did not. Fix: wrap intry/except (OSError, IOError): pass.3. TOCTOU race in
copilot_acp_client.py(MEDIUM)path.exists()followed bypath.read_text()with no exception handling. Fix: usetry/except FileNotFoundError.4. Non-atomic cache write in
sticker_cache.py(MEDIUM)Path.write_text()leaves truncated JSON on crash. Fix: tempfile + fsync + os.replace() (atomic).Test Plan
python3 -m py_compile