Skip to content
Open
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
18 changes: 18 additions & 0 deletions tools/file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import os
import tempfile
import threading
from pathlib import Path

Expand Down Expand Up @@ -155,13 +156,30 @@ def _is_blocked_device(filepath: str) -> bool:
_SENSITIVE_EXACT_PATHS = {"/var/run/docker.sock", "/run/docker.sock"}


def _is_allowed_temp_path(resolved: str) -> bool:
"""Return True for files under the platform temp directory.

macOS resolves /var/folders/... to /private/var/folders/..., which is
otherwise covered by the broad /private/var/ sensitive-system guard.
Test and runtime temp files are intentionally writable by file tools.
"""
try:
temp_root = str(Path(tempfile.gettempdir()).resolve())
except (OSError, ValueError):
temp_root = os.path.realpath(tempfile.gettempdir())
temp_root = os.path.normpath(temp_root)
return resolved == temp_root or resolved.startswith(temp_root + os.sep)


def _check_sensitive_path(filepath: str, task_id: str = "default") -> str | None:
"""Return an error message if the path targets a sensitive system location."""
try:
resolved = str(_resolve_path_for_task(filepath, task_id))
except (OSError, ValueError):
resolved = filepath
normalized = os.path.normpath(os.path.expanduser(filepath))
if _is_allowed_temp_path(os.path.normpath(resolved)):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This early return also skips the exact-path and Hermes-config guards below. tests/tools/test_file_tools.py:596-630 requires a config located under a temporary directory to remain blocked for both write and patch; exempt only the broad prefix loop, then continue through the remaining checks.

return None
_err = (
f"Refusing to write to sensitive system path: {filepath}\n"
"Use the terminal tool with sudo if you need to modify system files."
Expand Down