
    `gj*                     v    d Z ddlZddlmZ ddlmZ  ej                  e      Zdededee	   fdZ
d	e	defd
Zy)zShared 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.
    N)Path)Optionalpathrootreturnc                     	 | j                         }|j                         }|j                  |       y# t        t        f$ r}d| cY d}~S d}~ww xY w)au  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 json.dumps({"error": error})
    z Path escapes allowed directory: N)resolverelative_to
ValueErrorOSError)r   r   resolvedroot_resolvedexcs        F/root/.hermes/venv/lib/python3.12/site-packages/tools/path_security.pyvalidate_within_dirr      sT    8<<>]+    81#778s   14 AAAApath_strc                 4    t        |       j                  }d|v S )zReturn True if *path_str* contains ``..`` traversal components.

    Quick check for obvious traversal attempts before doing full resolution.
    z..)r   parts)r   r   s     r   has_traversal_componentr   %   s    
 N  E5=    )__doc__loggingpathlibr   typingr   	getLogger__name__loggerstrr   boolr    r   r   <module>r!      sX      			8	$d $ 8C= ,c d r   