mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Replace json.dumps({"error": ...}) boilerplate with the documented
tools/registry.py tool_error() helper across 13 files.
Migrated: 59 sites (58 code sites + 1 docstring example in
path_security.py), incl. multi-key envelopes passed via kwargs
(available_actions, path/already_read, pattern/already_searched,
parameters/hint, needs_reauth/server, error_type/tool/result_type).
Also removed 2 now-redundant local tool_error imports in mcp_tool.py
in favor of a module-level import.
Skipped (not byte/shape-compatible with tool_error):
- {"success": false, "error": ...} envelopes (browser_tool,
browser_camofox, browser_dialog_tool, web_tools, tts_tool,
skills_tool, image_generation_tool, project_tools, memory_tool,
cronjob_tools, x_search_tool, xai_video_tools) — leading keys
differ; key order would change.
- terminal_tool/code_execution_tool envelopes carrying output/
exit_code/status leading keys.
- tool_search.py:912-area multi-key success paths (non-error).
- mcp_tool.py MCPSampling._error — returns MCP-spec ErrorData
object, not a JSON string; incompatible.
- send_message_tool._error — returns a dict (not str) and applies
secret redaction; return type must be preserved.
Behavior note: sites that previously omitted ensure_ascii=False now
emit raw UTF-8 (tool_error's canonical behavior) — JSON-equivalent.
Tests: 23 targeted files (tool_search, discord, file_tools/read
guards/operations, registry, clarify, homeassistant, code_execution,
send_message, delegate, terminal, mcp, model_tools, sanitize_tool_error,
retaindb plugin) — all pass. ruff clean.
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""Shared path validation helpers for tool implementations.
|
|
|
|
Extracts the ``resolve() + relative_to()`` and ``..`` traversal check
|
|
patterns previously duplicated across skill_manager_tool, skills_tool,
|
|
skills_hub, cronjob_tools, and credential_files.
|
|
"""
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def validate_within_dir(path: Path, root: Path) -> Optional[str]:
|
|
"""Ensure *path* resolves to a location within *root*.
|
|
|
|
Returns an error message string if validation fails, or ``None`` if the
|
|
path is safe. Uses ``Path.resolve()`` to follow symlinks and normalize
|
|
``..`` components.
|
|
|
|
Usage::
|
|
|
|
error = validate_within_dir(user_path, allowed_root)
|
|
if error:
|
|
return tool_error(error)
|
|
"""
|
|
try:
|
|
resolved = path.resolve()
|
|
root_resolved = root.resolve()
|
|
resolved.relative_to(root_resolved)
|
|
except (ValueError, OSError) as exc:
|
|
return f"Path escapes allowed directory: {exc}"
|
|
return None
|
|
|
|
|
|
def has_traversal_component(path_str: str) -> bool:
|
|
"""Return True if *path_str* contains ``..`` traversal components.
|
|
|
|
Quick check for obvious traversal attempts before doing full resolution.
|
|
"""
|
|
parts = Path(path_str).parts
|
|
return ".." in parts
|