
    i*                     z    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dS )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                     	 |                                  }|                                 }|                    |           n## t          t          f$ r}d| cY d}~S d}~ww xY wdS )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        8/home/ubuntu/.hermes/hermes-agent/tools/path_security.pyvalidate_within_dirr      s~    8<<>>]++++  8 8 87#7777777784s   =A   A AA A path_strc                 2    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    
 NN E5=    )__doc__loggingpathlibr   typingr   	getLogger__name__loggerstrr   boolr    r   r   <module>r!      s                 		8	$	$d $ 8C=    ,c d      r   