Files
hermes-agent/tools/path_security.py
Maciej Lisiewski 05d3083e7a fix(gateway): refuse sandbox .env fetches and pin the canonical-path guard
fetch_remote_media copies a sandbox file to remote_<hex>_<name> before
re-validating the copy, so the project-env basename rule never saw a
sandbox .env (or a symlink to one). Apply is_project_env_basename to the
realpath'd sandbox path before any bytes move.

Tests pin that the host gate judges the resolved path in both modes
(innocent-named symlink, case variant, file under the cache allowlist).
_log_safe_path moves to base_log_safety to keep base.py under its cap.

Fixes #59044
2026-11-13 17:26:44 -08:00

31 lines
1.2 KiB
Python

"""Shared path validation helpers for tool implementations (skills, cron, credential files)."""
import re
from pathlib import Path
from typing import Optional
def validate_within_dir(path: Path, root: Path) -> Optional[str]:
"""Error message if *path* does not resolve inside *root* (symlinks and ``..`` followed)."""
try:
path.resolve().relative_to(root.resolve())
except (ValueError, OSError) as exc:
return f"Path escapes allowed directory: {exc}"
return None
def has_traversal_component(path_str: str) -> bool:
"""Cheap pre-check for a literal ``..`` component before full resolution."""
return ".." in Path(path_str).parts
# Control chars + Unicode line separators (NEL, LS, PS): newline-bearing paths are legal POSIX
# names, but they corrupt line-delimited protocols (MEDIA: tags) and forge log lines. Same
# class as _LOG_UNSAFE_CHARS in gateway.platforms.base_log_safety.
_UNSAFE_PATH_CHARS = re.compile(r"[\x00-\x1f\x7f\x85\u2028\u2029]")
def has_unsafe_path_chars(path_str: str) -> bool:
"""True when *path_str* contains control characters or line separators."""
return bool(_UNSAFE_PATH_CHARS.search(path_str))