Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion agent/shell_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ def save_allowlist(data: Dict[str, Any]) -> None:
try:
with os.fdopen(fd, "w") as fh:
fh.write(json.dumps(data, indent=2, sort_keys=True))
os.replace(tmp_path, p)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(p) if os.path.islink(p) else p
os.replace(tmp_path, real_path)
except Exception:
try:
os.unlink(tmp_path)
Expand Down
8 changes: 6 additions & 2 deletions cron/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ def save_jobs(jobs: List[Dict[str, Any]]):
json.dump({"jobs": jobs, "updated_at": _hermes_now().isoformat()}, f, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, JOBS_FILE)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(JOBS_FILE) if os.path.islink(JOBS_FILE) else JOBS_FILE
os.replace(tmp_path, real_path)
_secure_file(JOBS_FILE)
except BaseException:
try:
Expand Down Expand Up @@ -863,7 +865,9 @@ def save_job_output(job_id: str, output: str):
f.write(output)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, output_file)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(output_file) if os.path.islink(output_file) else output_file
os.replace(tmp_path, real_path)
_secure_file(output_file)
except BaseException:
try:
Expand Down
12 changes: 9 additions & 3 deletions hermes_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3574,7 +3574,9 @@ def sanitize_env_file() -> int:
f.writelines(sanitized)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, env_path)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(env_path) if os.path.islink(env_path) else env_path
os.replace(tmp_path, real_path)
except BaseException:
try:
os.unlink(tmp_path)
Expand Down Expand Up @@ -3677,7 +3679,9 @@ def save_env_value(key: str, value: str):
f.writelines(lines)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, env_path)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(env_path) if os.path.islink(env_path) else env_path
os.replace(tmp_path, real_path)
# Restore original permissions before _secure_file may tighten them.
if original_mode is not None:
try:
Expand Down Expand Up @@ -3733,7 +3737,9 @@ def remove_env_value(key: str) -> bool:
f.writelines(new_lines)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, env_path)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(env_path) if os.path.islink(env_path) else env_path
os.replace(tmp_path, real_path)
if original_mode is not None:
try:
os.chmod(env_path, original_mode)
Expand Down
5 changes: 4 additions & 1 deletion tools/memory_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ def _write_file(path: Path, entries: List[str]):
f.write(content)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, str(path)) # Atomic on same filesystem
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
path_str = str(path)
real_path = os.path.realpath(path_str) if os.path.islink(path_str) else path_str
os.replace(tmp_path, real_path)
except BaseException:
# Clean up temp file on any failure
try:
Expand Down
4 changes: 3 additions & 1 deletion tools/skill_manager_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ def _atomic_write_text(file_path: Path, content: str, encoding: str = "utf-8") -
try:
with os.fdopen(fd, "w", encoding=encoding) as f:
f.write(content)
os.replace(temp_path, file_path)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(file_path) if os.path.islink(file_path) else file_path
os.replace(temp_path, real_path)
except Exception:
# Clean up temp file on error
try:
Expand Down
4 changes: 3 additions & 1 deletion tools/skills_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def _write_manifest(entries: Dict[str, str]):
f.write(data)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, MANIFEST_FILE)
# Resolve symlinks so os.replace writes to the real file (GitHub #16743).
real_path = os.path.realpath(MANIFEST_FILE) if os.path.islink(MANIFEST_FILE) else MANIFEST_FILE
os.replace(tmp_path, real_path)
except BaseException:
try:
os.unlink(tmp_path)
Expand Down
14 changes: 10 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ def atomic_json_write(
)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, path)
_restore_file_mode(path, original_mode)
# Resolve symlinks so os.replace writes to the real file instead of
# replacing the symlink with a regular file (GitHub #16743).
real_path = os.path.realpath(path) if os.path.islink(path) else path
os.replace(tmp_path, real_path)
_restore_file_mode(real_path, original_mode)
except BaseException:
# Intentionally catch BaseException so temp-file cleanup still runs for
# KeyboardInterrupt/SystemExit before re-raising the original signal.
Expand Down Expand Up @@ -150,8 +153,11 @@ def atomic_yaml_write(
f.write(extra_content)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, path)
_restore_file_mode(path, original_mode)
# Resolve symlinks so os.replace writes to the real file instead of
# replacing the symlink with a regular file (GitHub #16743).
real_path = os.path.realpath(path) if os.path.islink(path) else path
os.replace(tmp_path, real_path)
_restore_file_mode(real_path, original_mode)
except BaseException:
# Match atomic_json_write: cleanup must also happen for process-level
# interruptions before we re-raise them.
Expand Down
Loading