
    ia                    f    d Z ddlmZ ddlZddlmZ ddlmZ ddZddZ	ddZ
ddZddZddZdS )z:Shared file safety rules used by both tools and ACP shims.    )annotationsN)Path)Optionalreturnr   c                     	 ddl m}   |             S # t          $ r/ t          t          j                            d                    cY S w xY w)zHResolve the active HERMES_HOME (profile-aware) without circular imports.r   get_hermes_homez	~/.hermes)hermes_constantsr	   	Exceptionr   ospath
expanduserr   s    6/home/ubuntu/.hermes/hermes-agent/agent/file_safety.py_hermes_home_pathr   
   sf    5444444    5 5 5BG&&{33444445s    6A
Ahomestrset[str]c                   t                      }d t          j                            | dd          t          j                            | dd          t          j                            | dd          t          j                            | dd          t	          |dz            t          j                            | d          t          j                            | d	          t          j                            | d
          t          j                            | d          t          j                            | d          t          j                            | d          t          j                            | d          t          j                            | d          t          j                            | d          dddfD             S )z8Return exact sensitive paths that must never be written.c                L    h | ]!}t           j                            |          "S  )r   r   realpath.0ps     r   	<setcomp>z+build_write_denied_paths.<locals>.<setcomp>   s8        	      .sshauthorized_keysid_rsa
id_ed25519configz.envz.bashrcz.zshrcz.profilez.bash_profilez	.zprofilez.netrcz.pgpassz.npmrcz.pypircz/etc/sudoersz/etc/passwdz/etc/shadow)r   r   r   joinr   )r   hermes_homes     r   build_write_denied_pathsr$      sK   #%%K  GLLv'899GLLvx00GLLv|44GLLvx00f$%%GLLy))GLLx((GLLz**GLL//GLL{++GLLx((GLLy))GLLx((GLLy))#
   r   	list[str]c                   d t           j                            | d          t           j                            | d          t           j                            | d          t           j                            | d          ddt           j                            | d          t           j                            | d	          t           j                            | d
d          f	D             S )z?Return sensitive directory prefixes that must never be written.c                f    g | ].}t           j                            |          t           j        z   /S r   )r   r   r   sepr   s     r   
<listcomp>z/build_write_denied_prefixes.<locals>.<listcomp>0   s?        	bf$  r   r   z.awsz.gnupgz.kubez/etc/sudoers.dz/etc/systemdz.dockerz.azurez.configgh)r   r   r"   )r   s    r   build_write_denied_prefixesr+   .   s      GLLv&&GLLv&&GLLx((GLLw''GLLy))GLLx((GLLy$//

   r   Optional[str]c                     t          j        dd          } | sdS 	 t           j                            t           j                            |                     S # t
          $ r Y dS w xY w)zBReturn the resolved HERMES_WRITE_SAFE_ROOT path, or None if unset.HERMES_WRITE_SAFE_ROOT N)r   getenvr   r   r   r   )roots    r   get_safe_write_rootr2   @   sn    9-r22D tw 2 24 8 8999   tts   ;A 
A%$A%r   boolc                   t           j                            t           j                            d                    }t           j                            t           j                            t	          |                               }|t          |          v rdS t          |          D ]}|                    |          r dS t                      }|r*||k    s$|                    |t           j	        z             sdS dS )zBReturn True if path is blocked by the write denylist or safe root.~TF)
r   r   r   r   r   r$   r+   
startswithr2   r(   )r   r   resolvedprefix	safe_roots        r   is_write_deniedr:   K   s    7BG..s3344Dw 2 23t99 = =>>H+D1111t-d33  v&& 	44	 $%%I (i//83F3FySUSYGY3Z3Z/t5r   c                8   t          |                                                                           }t                                                      }|dz  dz  dz  |dz  dz  g}|D ]0}	 |                    |           n# t
          $ r Y %w xY wd|  dc S dS )zHReturn an error message when a read targets internal Hermes cache files.skillsz.hubzindex-cachezAccess denied: z is an internal Hermes cache file and cannot be read directly to prevent prompt injection. Use the skills_list or skill_view tools instead.N)r   r   resolver   relative_to
ValueError)r   r7   r#   blocked_dirsblockeds        r   get_read_block_errorrB   ]   s    Dzz$$&&..00H#%%--//Kh'-7h'L   	
 	
	  )))) 	 	 	H	?d ? ? ?	
 	
 	

 4s   ,B
BB)r   r   )r   r   r   r   )r   r   r   r%   )r   r,   )r   r   r   r3   )r   r   r   r,   )__doc__
__future__r   r   pathlibr   typingr   r   r$   r+   r2   r:   rB   r   r   r   <module>rG      s    @ @ " " " " " " 				            5 5 5 5   6   $      $     r   