
    PL
j                     0   U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZmZ  ej        e          Z ej        dd          Zej        e         ed	<   d
eddfdZdedej        e         fdZdej        e         ddfdZddedefdZdefdZdefdZdZdZ dZ!dZ"dZ#dZ$dZ%de% dZ&de& de d e  d e# d e$ dZ'de! d e" dZ(d!Z)d"Z*d#d$d%d&d'd(d)d*e*d+z   d,fe*d-z   d.fe*d/z   d0fe*d1z   d2fgZ+ej,        ej-        z  Z.d3 e+D             Z/ ej0        d4ej,                  Z1d5ede2fd6Z3d5ede2fd7Z4d8ede5fd9Z6d8ede5fd:Z7g d;d<d=d>d?d@dAdBdCdDdEdFdGdHe& dIfdJdKdLdMdNdOd)dPdQdRdSdTe' dUfdVe' dWfdTe( dXe) dYfdVe( dXe) dZfd[d\d]d^d_d`dadbdcdddee& dffdge( dXe) dhfdie& djfdke& dlfdmdndodpdqdrdsdtduZ8dv e8D             Z9dwedefdxZ:i Z;e5ee<e         f         edy<   e8D ]x\  Z=Z> e:e=          Z?e>Z@e;A                    e@ e<                      B                    e@e?h           e;A                    e? e<                      B                    e?e@h           ydzede<e         fd{ZCd5edefd|ZDd5ede2fd}ZE ejF                    ZGi ZHe5ee5f         ed~<   i ZIe5ee<f         ed<    e<            ZJe<e         ed<    e<            ZKe<ed<    G d d          ZLi ZMe5eeNf         ed<   i ZOe5eePf         ed<   deddfdZQdeddfdZR	 ddedededeSfdZTdedefdZUdede5fdZVdedzefdZWdeddfdZXdeddfdZYdeddfdZZdedefdZ[defdZ\dedzedefdZ]dzefdZ^de<fdZ_de<fdZ`de<fdZa	 	 	 dd5ed8edeSdz  dedef
dZbdefdZcde5fdZddefdZedeSfdZfdefdZgd5ed8edefdZh	 dd5edede5fdZide5defdZj	 dd5edede5fdZk e`             dS )a  Dangerous command approval -- detection, prompting, and per-session state.

This module is the single source of truth for the dangerous command system:
- Pattern detection (DANGEROUS_PATTERNS, detect_dangerous_command)
- Per-session approval state (thread-safe, keyed by session_key)
- Approval prompting (CLI interactive + gateway async)
- Smart approval via auxiliary LLM (auto-approve low-risk commands)
- Permanent allowlist persistence (config.yaml)
    N)Optional)cfg_get)env_var_enabledis_truthy_valueapproval_session_key default_approval_session_key	hook_namereturnc                     	 ddl m} n# t          $ r Y dS w xY w	  || fi | dS # t          $ r'}t                              d| |           Y d}~dS d}~ww xY w)a{  Invoke a plugin lifecycle hook for the approval system.

    Lazy-imports the plugin manager to avoid circular imports (approval.py is
    imported very early, long before plugins are discovered). Never raises --
    plugin errors are logged and swallowed.

    Only fires for the two approval-specific hooks in VALID_HOOKS:
    pre_approval_request, post_approval_response.
    r   )invoke_hookNz$Approval hook %s dispatch failed: %s)hermes_cli.pluginsr   	Exceptionloggerdebug)r   kwargsr   excs       2/home/kuhnn/.hermes/hermes-agent/tools/approval.py_fire_approval_hookr   $   s    2222222    	MI((((((( M M M 	;YLLLLLLLLL	Ms    	 
	& 
AAAsession_keyc                 :    t                               | pd          S )z<Bind the active approval session key to the current context.r   )r   setr   s    r   set_current_session_keyr   >   s     $$[%6B777    tokenc                 :    t                               |            dS )z/Restore the prior approval session key context.N)r   reset)r   s    r   reset_current_session_keyr!   C   s    &&&&&r   r
   c                 `    t                                           }|r|S ddlm}  |d|           S )a  Return the active session key, preferring context-local state.

    Resolution order:
    1. approval-specific contextvars (set by gateway before agent.run)
    2. session_context contextvars (set by _set_session_env)
    3. os.environ fallback (CLI, cron, tests)
    r   get_session_envHERMES_SESSION_KEY)r   getgateway.session_contextr$   )r
   r   r$   s      r   get_current_session_keyr(   H   sH     (++--K 777777?/999r   c                  z    	 ddl m}   | dd          pdS # t          $ r t          j        dd          pdcY S w xY w)zBReturn the current gateway platform from contextvars/env fallback.r   r#   HERMES_SESSION_PLATFORMr   )r'   r$   r   osgetenvr#   s    r   _get_session_platformr-   W   sj    >;;;;;;8"==CC > > >y2B77=2===>s    !::c                  |    t          d          rdS t          d          rdS t          t                                S )u  True when this call is inside a gateway/API session.

    Legacy gateway integrations set HERMES_GATEWAY_SESSION in process env.
    Newer concurrent gateway paths bind HERMES_SESSION_PLATFORM via
    contextvars so approval mode does not depend on process-global flags.

    Cron jobs are NEVER gateway-approval contexts even when they originate
    from a gateway platform (cron binds HERMES_SESSION_PLATFORM via
    contextvars for delivery routing). Cron approvals are governed by
    ``approvals.cron_mode`` config, not interactive resolve — letting cron
    fall through to the gateway branch would submit a pending approval
    with no listener and block the job indefinitely.
    HERMES_CRON_SESSIONFHERMES_GATEWAY_SESSIONT)r   boolr-    r   r   _is_gateway_approval_contextr3   a   sE     ,-- u/00 t%''(((r   z$(?:~|\$home|\$\{home\})/\.ssh(?:/|$)z\(?:~\/\.hermes/|(?:\$home|\$\{home\})/\.hermes/|(?:\$hermes_home|\$\{hermes_home\})/)\.env\bz;(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*\.env(?:\.[^/\s"\'`]+)*)z0(?:(?:/|\.{1,2}/)?(?:[^\s/"\'`]+/)*config\.yaml)zJ(?:~|\$home|\$\{home\})/\.(?:bashrc|zshrc|profile|bash_profile|zprofile)\bz9(?:~|\$home|\$\{home\})/\.(?:netrc|pgpass|npmrc|pypirc)\bz/private/(?:etc|var|tmp|home)/z	(?:/etc/|)z(?:z	|/dev/sd||z(?:\s*(?:&&|\|\||;).*)?$zp(?:^|[;&|\n`]|\$\()\s*(?:sudo\s+(?:-[^\s]+\s+)*)?(?:env\s+(?:\w+=\S*\s+)*)?(?:(?:exec|nohup|setsid|time)\s+)*\s*)z&\brm\s+(-[^\s]*\s+)*(/|/\*|/ \*)(\s|$)z#recursive delete of root filesystem)z\brm\s+(-[^\s]*\s+)*(/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/\*|/var|/var/\*|/bin|/bin/\*|/sbin|/sbin/\*|/boot|/boot/\*|/lib|/lib/\*)(\s|$)z$recursive delete of system directory)z-\brm\s+(-[^\s]*\s+)*(~|\$HOME)(/?|/\*)?(\s|$)z"recursive delete of home directory)z\bmkfs(\.[a-z0-9]+)?\bzformat filesystem (mkfs))z9\bdd\b[^\n]*\bof=/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*zdd to raw block device)z.>\s*/dev/(sd|nvme|hd|mmcblk|vd|xvd)[a-z0-9]*\bzredirect to raw block device)z(:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:z	fork bomb)z\bkill\s+(-[^\s]+\s+)*-1\bkill all processesz!(shutdown|reboot|halt|poweroff)\bzsystem shutdown/rebootzinit\s+[06]\bzinit 0/6 (shutdown/reboot)z*systemctl\s+(poweroff|reboot|halt|kexec)\bzsystemctl poweroff/rebootztelinit\s+[06]\bztelinit 0/6 (shutdown/reboot)c                 L    g | ]!\  }}t          j        |t                    |f"S r2   recompile	_RE_FLAGS.0patterndescriptions      r   
<listcomp>r@      s=        Z##[1  r   z)(?:^|[;&|`\n]|&&|\|\||\$\()\s*sudo\s+-S\bcommandc                     dt           j        v rdS t          |                                           }t                              |          rdS dS )u  Detect ``sudo -S`` (stdin password) without configured SUDO_PASSWORD.

    When SUDO_PASSWORD is set, ``_transform_sudo_command`` injects ``-S``
    internally — that path is legitimate and handled elsewhere.  This guard
    only fires when SUDO_PASSWORD is *not* set, meaning the LLM explicitly
    wrote ``sudo -S`` to pipe a guessed password.

    Returns:
        (is_blocked: bool, description: str | None)
    SUDO_PASSWORDFN)Tz*sudo password guessing via stdin (sudo -S))r+   environ _normalize_command_for_detectionlower_SUDO_STDIN_REsearch)rA   
normalizeds     r   _check_sudo_stdin_guardrK      sQ     "*$$}1'::@@BBJZ(( DCC=r   c                     t          |                                           }t          D ] \  }}|                    |          rd|fc S !dS )zCheck if a command matches the unconditional hardline blocklist.

    Returns:
        (is_hardline, description) or (False, None)
    TrD   )rF   rG   HARDLINE_PATTERNS_COMPILEDrI   )rA   rJ   
pattern_rer?   s       r   detect_hardline_commandrO     sb     2'::@@BBJ#= ' '
KZ(( 	'+&&&&	'=r   r?   c                     ddd|  ddS )z5Build the standard block result for a hardline match.FTzBLOCKED (hardline): u   . This command is on the unconditional blocklist and cannot be executed via the agent — not even with --yolo, /yolo, approvals.mode=off, or cron approve mode. If you genuinely need to run it, run it yourself in a terminal outside the agent.)approvedhardlinemessager2   r?   s    r   _hardline_block_resultrU     s/     ;   	  r   c                     dd|  ddS )z5Build the standard block result for sudo stdin guard.Fz	BLOCKED: u   . Do not pipe passwords to 'sudo -S' — this is a brute-force attack vector. Set SUDO_PASSWORD in your .env file if the agent needs passwordless sudo, or run the sudo command manually in your own terminal.rQ   rS   r2   rT   s    r   _sudo_stdin_block_resultrX   *  s,     - - - -	 	 	r   )z\brm\s+(-[^\s]*\s+)*/zdelete in root path)z\brm\s+-[^\s]*rzrecursive delete)z\brm\s+--recursive\bzrecursive delete (long flag))z8\bchmod\s+(-[^\s]*\s+)*(777|666|o\+[rwx]*w|a\+[rwx]*w)\bz world/other-writable permissions)z8\bchmod\s+--recursive\b.*(777|666|o\+[rwx]*w|a\+[rwx]*w)z*recursive world/other-writable (long flag))z\bchown\s+(-[^\s]*)?R\s+rootzrecursive chown to root)z\bchown\s+--recursive\b.*rootz#recursive chown to root (long flag))z\bmkfs\bzformat filesystem)z\bdd\s+.*if=z	disk copy)z>\s*/dev/sdzwrite to block device)z\bDROP\s+(TABLE|DATABASE)\bzSQL DROP)z$\bDELETE\s+FROM\b(?![^\n]*\bWHERE\b)zSQL DELETE without WHERE)z\bTRUNCATE\s+(TABLE)?\s*\wzSQL TRUNCATEz>\s*zoverwrite system config)z8\bsystemctl\s+(-[^\s]+\s+)*(stop|restart|disable|mask)\bzstop/restart system service)z\bkill\s+-9\s+-1\br6   )z\bpkill\s+-9\bzforce kill processes)z,\bkillall\s+(-[^\s]*\s+)*-(9|KILL|SIGKILL)\bz$force kill processes (killall -KILL))z0\bkillall\s+(-[^\s]*\s+)*-s\s+(KILL|SIGKILL|9)\bz&force kill processes (killall -s KILL))z\bkillall\s+(-[^\s]*\s+)*-r\bz$kill processes by regex (killall -r))z%\b(bash|sh|zsh|ksh)\s+-[^\s]*c(\s+|$)zshell command via -c/-lc flag)z)\b(python[23]?|perl|ruby|node)\s+-[ec]\s+zscript execution via -e/-c flag)z\b(curl|wget)\b.*\|\s*(ba)?sh\bzpipe remote content to shell)z1\b(bash|sh|zsh|ksh)\s+<\s*<?\s*\(\s*(curl|wget)\bz.execute remote script via process substitutionz\btee\b.*["\']?zoverwrite system file via teez>>?\s*["\']?z%overwrite system file via redirectionz["\']?z$overwrite project env/config via teez,overwrite project env/config via redirection)z\bxargs\s+.*\brm\bzxargs with rm)z&\bfind\b.*-exec(?:dir)?\s+(/\S*/)?rm\bzfind -exec/-execdir rm)z\bfind\b.*-delete\bzfind -delete)z%\bhermes\s+gateway\s+(stop|restart)\bz2stop/restart hermes gateway (kills running agents))z\bhermes\s+update\bz6hermes update (restarts gateway, kills running agents))z4gateway\s+run\b.*(&\s*$|&\s*;|\bdisown\b|\bsetsid\b)Mstart gateway outside systemd (use 'systemctl --user restart hermes-gateway'))z\bnohup\b.*gateway\s+run\brY   )z1\b(pkill|killall)\b.*\b(hermes|gateway|cli\.py)\bz.kill hermes/gateway process (self-termination))z\bkill\b.*\$\(\s*pgrep\bz3kill process via pgrep expansion (self-termination))z\bkill\b.*`\s*pgrep\bz<kill process via backtick pgrep expansion (self-termination)z\b(cp|mv|install)\b.*\sz&copy/move file into system config pathz\b(cp|mv|install)\b.*\s["\']?z!overwrite project env/config filez\bsed\s+-[^\s]*i.*\szin-place edit of system configz\bsed\s+--in-place\b.*\sz*in-place edit of system config (long flag))z#\b(python[23]?|perl|ruby|node)\s+<<zscript execution via heredoc)z\bgit\s+reset\s+--hard\bz/git reset --hard (destroys uncommitted changes))z\bgit\s+push\b.*--force\bz(git force push (rewrites remote history))z\bgit\s+push\b.*-f\bz3git force push short flag (rewrites remote history))z\bgit\s+clean\s+-[^\s]*fz.git clean with force (deletes untracked files))z\bgit\s+branch\s+-D\bzgit branch force delete)z\bchmod\s+\+x\b.*[;&|]+\s*\./z(chmod +x followed by immediate execution)z8\bsudo\b[^;|&\n]*?\s+(?:-s\b|--stdin\b|-a\b|--askpass\b)z3sudo with privilege flag (stdin/askpass/shell/list))z(\bsudo\b[^;|&\n]*?\s+-[a-z]*[sa][a-z]*\bz,sudo with combined-flag privilege escalationc                 L    g | ]!\  }}t          j        |t                    |f"S r2   r8   r<   s      r   r@   r@     s=        Z##[1  r   r>   c                 T    d| v r|                      d          d         n	| dd         S )zIReproduce the old regex-derived approval key for backwards compatibility.z\b   N   )split)r>   s    r   _legacy_pattern_keyr_     s0    &+w&6&67==""GCRCLHr   _PATTERN_KEY_ALIASESpattern_keyc                 :    t                               | | h          S )zReturn all approval keys that should match this pattern.

    New approvals use the human-readable description string, but older
    command_allowlist entries and session approvals may still contain the
    historical regex-derived key.
    )r`   r&   ra   s    r   _approval_key_aliasesrd     s      ##K+???r   c                 ~    ddl m}  ||           } |                     dd          } t          j        d|           } | S )a  Normalize a command string before dangerous-pattern matching.

    Strips ANSI escape sequences (full ECMA-48 via tools.ansi_strip),
    null bytes, and normalizes Unicode fullwidth characters so that
    obfuscation techniques cannot bypass the pattern-based detection.
    r   )
strip_ansi r   NFKC)tools.ansi_striprf   replaceunicodedata	normalize)rA   rf   s     r   rF   rF     sQ     ,+++++ j!!Goofb))G#FG44GNr   c                     t          |                                           }t          D ]#\  }}|                    |          r	|}d||fc S $dS )zCheck if a command matches any dangerous patterns.

    Returns:
        (is_dangerous, pattern_key, description) or (False, None, None)
    T)FNN)rF   rG   DANGEROUS_PATTERNS_COMPILEDrI   )rA   command_lowerrN   r?   ra   s        r   detect_dangerous_commandrp     sl     5W==CCEEM#> 4 4
K]++ 	4%K+{3333	4 r   _pending_session_approved_session_yolo_permanent_approvedc                   "    e Zd ZdZdZdefdZdS )_ApprovalEntryz@One pending dangerous-command approval inside a gateway session.)eventdataresultrx   c                 R    t          j                    | _        || _        d | _        d S N)	threadingEventrw   rx   ry   )selfrx   s     r   __init__z_ApprovalEntry.__init__  s#    _&&
	%)r   N)__name__
__module____qualname____doc__	__slots__dictr   r2   r   r   rv   rv     s:        JJ+I*T * * * * * *r   rv   _gateway_queues_gateway_notify_cbsc                 Z    t           5  |t          | <   ddd           dS # 1 swxY w Y   dS )ua  Register a per-session callback for sending approval requests to the user.

    The callback signature is ``cb(approval_data: dict) -> None`` where
    *approval_data* contains ``command``, ``description``, and
    ``pattern_keys``.  The callback bridges sync→async (runs in the agent
    thread, must schedule the actual send on the event loop).
    N)_lockr   )r   cbs     r   register_gateway_notifyr     sy     
 . .+-K(. . . . . . . . . . . . . . . . . .    $$c                     t           5  t                              | d           t                              | g           }ddd           n# 1 swxY w Y   |D ]}|j                                         dS )zUnregister the per-session gateway approval callback.

    Signals ALL blocked threads for this session so they don't hang forever
    (e.g. when the agent run finishes or is interrupted).
    N)r   r   popr   rw   r   r   entriesentrys      r   unregister_gateway_notifyr     s     
 7 7T222!%%k2667 7 7 7 7 7 7 7 7 7 7 7 7 7 7    s   7AAAFchoiceresolve_allc                    t           5  t                              |           }|s	 ddd           dS |r$t          |          }|                                 n|                    d          g}|st                              | d           ddd           n# 1 swxY w Y   |D ]"}||_        |j                                         #t          |          S )aT  Called by the gateway's /approve or /deny handler to unblock
    waiting agent thread(s).

    When *resolve_all* is True every pending approval in the session is
    resolved at once (``/approve all``).  Otherwise only the oldest one
    is resolved (FIFO).

    Returns the number of approvals resolved (0 means nothing was pending).
    Nr   )
r   r   r&   listclearr   ry   rw   r   len)r   r   r   queuetargetsr   s         r   resolve_gateway_approvalr     s1    
 
3 
3##K00 	
3 
3 
3 
3 
3 
3 
3 
3  	%5kkGKKMMMMyy||nG 	3T222
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3 
3   w<<s   BABBBc                     t           5  t          t                              |                     cddd           S # 1 swxY w Y   dS )zFCheck if a session has one or more blocking gateway approvals waiting.N)r   r1   r   r&   r   s    r   has_blocking_approvalr   ;  s    	 6 6O''44556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   '<A A approvalc                 Z    t           5  |t          | <   ddd           dS # 1 swxY w Y   dS )z/Store a pending approval request for a session.N)r   rq   )r   r   s     r   submit_pendingr   A  sv    	 ) ) () ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )r   c                     t           5  t                              | t                                                    |           ddd           dS # 1 swxY w Y   dS )z(Approve a pattern for this session only.N)r   rr   
setdefaultr   add)r   ra   s     r   approve_sessionr   G  s    	 J J$$[#%%88<<[IIIJ J J J J J J J J J J J J J J J J Js   ;AAAc                     | sdS t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z,Enable YOLO bypass for a single session key.N)r   rs   r   r   s    r   enable_session_yolor   M  s     	 ' '+&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '   488c                     | sdS t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z-Disable YOLO bypass for a single session key.N)r   rs   discardr   s    r   disable_session_yolor   U  s     	 + +k***+ + + + + + + + + + + + + + + + + +r   c                 l   | sdS t           5  t                              | d           t                              |            t
                              | d           t                              | g           }ddd           n# 1 swxY w Y   |D ]"}d|_        |j        	                                 #dS )z7Remove all approval and yolo state for a given session.Ndeny)
r   rr   r   rs   r   rq   r   ry   rw   r   r   s      r   clear_sessionr   ]  s     	 7 7k4000k***[$'''!%%k266	7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
    	 s   A,BBBc                 ^    | sdS t           5  | t          v cddd           S # 1 swxY w Y   dS )z?Return True when YOLO bypass is enabled for a specific session.FN)r   rs   r   s    r   is_session_yolo_enabledr   m  s     u	 , ,m+, , , , , , , , , , , , , , , , , ,s   	"&&c                  <    t          t          d                    S )zEReturn True when the active approval session has YOLO bypass enabled.r   r	   )r   r(   r2   r   r   is_current_session_yolo_enabledr   u  s    "#:2#F#F#FGGGr   c                 6   t          |          }t          5  t          d |D                       r	 ddd           dS t                              | t                                t          fd|D                       cddd           S # 1 swxY w Y   dS )zCheck if a pattern is approved (session-scoped or permanent).

    Accept both the current canonical key and the legacy regex-derived key so
    existing command_allowlist entries continue to work after key migrations.
    c              3   (   K   | ]}|t           v V  d S r{   )rt   )r=   aliass     r   	<genexpr>zis_approved.<locals>.<genexpr>  s(      AAu++AAAAAAr   NTc              3       K   | ]}|v V  	d S r{   r2   )r=   r   session_approvalss     r   r   zis_approved.<locals>.<genexpr>  s)      CC%5--CCCCCCr   )rd   r   anyrr   r&   r   )r   ra   aliasesr   s      @r   is_approvedr   z  s    $K00G	 D DAAAAAAA 	D D D D D D D D .11+suuEECCCC7CCCCC	D D D D D D D D D D D D D D D D D Ds   B ABBBc                 z    t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z)Add a pattern to the permanent allowlist.N)r   rt   r   rc   s    r   approve_permanentr     s    	 - -,,,- - - - - - - - - - - - - - - - - -   044patternsc                 z    t           5  t                              |            ddd           dS # 1 swxY w Y   dS )z2Bulk-load permanent allowlist entries from config.N)r   rt   update)r   s    r   load_permanentr     s    	 - -""8,,,- - - - - - - - - - - - - - - - - -r   c                     	 ddl m}   |             }t          |                    dg           pg           }|rt	          |           |S # t
          $ r3}t                              d|           t                      cY d}~S d}~ww xY w)zLoad permanently allowed command patterns from config.

    Also syncs them into the approval module so is_approved() works for
    patterns added via 'always' in a previous session.
    r   load_configcommand_allowlistz&Failed to load permanent allowlist: %sN)hermes_cli.configr   r   r&   r   r   r   warning)r   configr   es       r   load_permanent_allowlistr     s    	111111vzz"5r::@bAA 	%8$$$   ?CCCuus   AA
 

B(B<BBc                     	 ddl m}m}  |            }t          |           |d<    ||           dS # t          $ r&}t
                              d|           Y d}~dS d}~ww xY w)z4Save permanently allowed command patterns to config.r   )r   save_configr   zCould not save allowlist: %sN)r   r   r   r   r   r   r   )r   r   r   r   r   s        r   save_permanent_allowlistr     s    :>>>>>>>>&*8nn"#F : : :5q999999999:s   /3 
A#AA#Ttimeout_secondsallow_permanentc                 	  
 |t                      }|D	  || |          S # t          $ r(}t                              d|d           Y d}~dS d}~ww xY w	 ddlm}  |            t                              d	| |           dS n# t          $ r Y nw xY wd
t          j        d<   	 ddl	m
 	 t                       t          d d|                      t          d|             t                       rt           d                     nt           d                     t                       t          j                                         ddi

fd}t          j        |d          }|                                 |                    |           |                                ret          d d          z              	 dt          j        v rt          j        d= t                       t          j                                         dS 
d         }	|	dv rbt           d                     	 dt          j        v rt          j        d= t                       t          j                                         dS |	dv rbt           d                     	 dt          j        v rt          j        d= t                       t          j                                         dS |	d v rƉsbt           d                     	 dt          j        v rt          j        d= t                       t          j                                         dS t           d!                     	 dt          j        v rt          j        d= t                       t          j                                         d"S t           d#                     	 dt          j        v rt          j        d= t                       t          j                                         dS # t(          t*          f$ rf t          d d$          z              Y dt          j        v rt          j        d= t                       t          j                                         dS w xY w# dt          j        v rt          j        d= t                       t          j                                         w xY w)%a  Prompt the user to approve a dangerous command (CLI only).

    Args:
        allow_permanent: When False, hide the [a]lways option (used when
            tirith warnings are present, since broad permanent allowlisting
            is inappropriate for content-level security findings).
        approval_callback: Optional callback registered by the CLI for
            prompt_toolkit integration. Signature:
            (command, description, *, allow_permanent=True) -> str.

    Returns: 'once', 'session', 'always', or 'deny'
    N)r   zApproval callback failed: %sT)exc_infor   r   )get_app_or_nonezDangerous-command approval requested on a thread with no approval callback while prompt_toolkit is active; denying to avoid stdin deadlock. command=%r description=%r1HERMES_SPINNER_PAUSE)tz  zapproval.dangerous_headerrT   z      zapproval.choose_longzapproval.choose_shortr   r   c                      	 r d          n
 d          } t          |                                                                           d<   d S # t          t          f$ r	 dd<   Y d S w xY w)Nzapproval.prompt_longzapproval.prompt_shortr   r   )inputstriprG   EOFErrorOSError)promptr   ry   r   s    r   	get_inputz,prompt_dangerous_approval.<locals>.get_input  s    *:IiQQ5666qqQhOiOiF',V}}':':'<'<'B'B'D'DF8$$$ '* * * *')F8$$$$*s   AA A-,A-)targetdaemontimeout
zapproval.timeout>   ooncezapproval.allowed_oncer   >   ssessionzapproval.allowed_sessionr   >   aalwayszapproval.allowed_alwaysr   zapproval.deniedzapproval.cancelled)_get_approval_timeoutr   r   error"prompt_toolkit.application.currentr   r   r+   rE   
agent.i18nr   printsysstdoutflushr|   Threadstartjoinis_aliver   KeyboardInterrupt)rA   r?   r   r   approval_callbackr   r   r   threadr   ry   r   s      `      @@r   prompt_dangerous_approvalr     sh     /11$	$$Wk5DF F F F 	 	 	LL7TLJJJ66666	FFFFFF?(NNE 	   6 )     		 *-BJ%&9 	!     ,	GGGPqq4+NNNPPQQQ$7$$%%%GGG 2aa.//0000aa/00111GGGJ^F* * * * * * * %YtDDDFLLNNNKKK000   dQQ1222333. "RZ//
12
1 H%F&&aa/00111$ "RZ//
12
) +++aa233444  "RZ//
12
# ?**& %!!677888$ "RZ//
12
 aa122333 "RZ//
12
 aa)**+++ "RZ//
12
 '(   dQQ+,,,---!RZ//
12
 "RZ//
12
sj   % 
AAA,B
 

BB*D+P $P P 3P P =P ,RR RR A	S"c                     t          | t                    r| du rdndS t          | t                    r*|                                                                 }|pdS dS )a"  Normalize approval mode values loaded from YAML/config.

    YAML 1.1 treats bare words like `off` as booleans, so a config entry like
    `approvals:
  mode: off` is parsed as False unless quoted. Treat that as the
    intended string mode instead of falling back to manual approvals.
    Foffmanual)
isinstancer1   strr   rG   )moderJ   s     r   _normalize_approval_moder   -  sc     $ 4uu83$ &ZZ\\''))
%X%8r   c                      	 ddl m}   |             }|                    di           pi S # t          $ r'}t                              d|           i cY d}~S d}~ww xY w)zLRead the approvals config block. Returns a dict with 'mode', 'timeout', etc.r   r   	approvalsz"Failed to load approval config: %sN)r   r   r&   r   r   r   )r   r   r   s      r   _get_approval_configr  <  s    111111zz+r**0b0   ;Q???						s   '* 
AAAAc                  d    t                                          dd          } t          |           S )zHRead the approval mode from config. Returns 'manual', 'smart', or 'off'.r   r   )r  r&   r   )r   s    r   _get_approval_moder  G  s+    !!%%fh77D#D)))r   c                      	 t          t                                          dd                    S # t          t          f$ r Y dS w xY w)z>Read the approval timeout from config. Defaults to 60 seconds.r   <   )intr  r&   
ValueError	TypeErrorr2   r   r   r   r   M  sS    '))--i<<===	"   rrs   .1 AAc                      	 ddl m}   |             }t          t          |ddd                                                                                    }|dv rdS dS # t          $ r Y dS w xY w)	zERead the cron approval mode from config. Returns 'deny' or 'approve'.r   r   r   	cron_moder   r	   >   r   yesallowapprover  )r   r   r   r   rG   r   r   )r   r   r   s      r   _get_cron_approval_moder  U  s    11111176;VLLLMMSSUU[[]]5559v   vvs   AA 
A,+A,c                 D   	 ddl m} d|  d| d} |dd|dgdd	
          }|j        d         j        j        pd                                                                }d|v rdS d|v rdS dS # t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w)a  Use the auxiliary LLM to assess risk and decide approval.

    Returns 'approve' if the LLM determines the command is safe,
    'deny' if genuinely dangerous, or 'escalate' if uncertain.

    Inspired by OpenAI Codex's Smart Approvals guardian subagent
    (openai/codex#13860).
    r   )call_llmzYou are a security reviewer for an AI coding agent. A terminal command was flagged by pattern matching as potentially dangerous.

Command: z
Flagged reason: ul  

Assess the ACTUAL risk of this command. Many flagged commands are false positives — for example, `python -c "print('hello')"` is flagged as "script execution via -c flag" but is completely harmless.

Rules:
- APPROVE if the command is clearly safe (benign script execution, safe file operations, development tools, package installs, git operations, etc.)
- DENY if the command could genuinely damage the system (recursive delete of important paths, overwriting system files, fork bombs, wiping disks, dropping databases, etc.)
- ESCALATE if you're uncertain

Respond with exactly one word: APPROVE, DENY, or ESCALATEr   user)rolecontent   )taskmessagestemperature
max_tokensr   APPROVEr  DENYr   escalatez1Smart approvals: LLM call failed (%s), escalatingN)
agent.auxiliary_clientr  choicesrS   r  r   upperr   r   r   )rA   r?   r  r   responseanswerr   s          r   _smart_approver!  b  s   #333333=
= = = = = 8%&99:	
 
 
 "1%-5;BBDDJJLL9v6:   H!LLLzzzzzs   A#A/ 'A/ /
B9BBenv_typec           
      ~   |dv rdddS t          |           \  }}|r3t                              d|| dd                    t          |          S t	          t          j        d                    st                      rdddS t          |           \  }}}|sdddS t                      }t          ||          rdddS t          d          }	t                      }
|	s1|
s/t          d	          rt                      d
k    r	dd| ddS dddS |
st          d          r$t          || ||d           d|d| |d| d|  ddS t          | ||          }|d
k    rdd| d||dS |dk    rt!          ||           n9|dk    r3t!          ||           t#          |           t%          t&                     dddS )a  Check if a command is dangerous and handle approval.

    This is the main entry point called by terminal_tool before executing
    any command. It orchestrates detection, session checks, and prompting.

    Args:
        command: The shell command to check.
        env_type: Terminal backend type ('local', 'ssh', 'docker', etc.).
        approval_callback: Optional CLI callback for interactive prompts.

    Returns:
        {"approved": True/False, "message": str or None, ...}
    >   modaldockerdaytonasingularityvercel_sandboxTNrW    Hardline block: %s (command: %s)   HERMES_YOLO_MODEHERMES_INTERACTIVEr/   r   F'BLOCKED: Command flagged as dangerous () but cron jobs run without a user present to approve it. Find an alternative approach that avoids this command. To allow dangerous commands in cron jobs, set approvals.cron_mode: approve in config.yaml.HERMES_EXEC_ASK)rA   ra   r?   approval_requiredu.   ⚠️ This command is potentially dangerous (z3). Asking the user for approval.

**Command:**
```

```rQ   ra   statusrA   r?   rS   )r   zBBLOCKED: User denied this potentially dangerous command (matched 'zL' pattern). Do NOT retry this command - the user has explicitly rejected it.rQ   rS   ra   r?   r   r   )rO   r   r   rU   r   r+   r,   r   rp   r(   r   r   r3   r  r   r   r   r   r   rt   )rA   r"  r   is_hardlinehardline_descis_dangerousra   r?   r   is_cli
is_gatewayr   s               r   check_dangerous_commandr:    s    RRR T222 "9!A!AK 59='RVSVRV-XXX%m444 ry!34455 39X9Z9Z 3 T222-Eg-N-N*L+{ 3 T222)++K;,, 3 T222122F-//J 3* 3011 	&((F22 %G+ G G G	 	 	 !T222 
_%677 
{&&%
 %
 	 	 	 &)&V V VGNV V V

 

 
	
 'w9JL L LF  v\g  v  v  v&&	
 
 	
 [1111	8		[111+&&& !4555...r   tirith_resultc           	         |                      d          pg }|s|                      d          pd}d| S g }|D ]}|                     dd          }|                     dd          }|                     dd          }|r*|r(|                    |rd	| d
| d| n| d|            p|r|                    |rd	| d
| n|           |s|                      d          pd}d| S dd                    |          z   S )zBuild a human-readable description from tirith findings.

    Includes severity, title, and description for each finding so users
    can make an informed approval decision.
    findingssummaryzsecurity issue detectedzSecurity scan: severityr   titler?   [z] z: u   Security scan — ; )r&   appendr   )r;  r=  r>  partsfr?  r@  descs           r   _format_tirith_descriptionrG    so      ,,2H +##I..K2K****E I I55R((gr""uu]B'' 	IT 	ILLH\8X8888$888UJ\J\VZJ\J\]]]] 	ILLHG0X00000%HHH +##I..K2K****$))E"2"222r   c           
         |dv rdddS t          |           \  }}|r3t                              d|| dd                    t          |          S t	          |           \  }}|r3t                              d|| dd                    t          |          S t                      }t          t          j	        d                    st                      s|d	k    rdddS t          d
          }t                      }	t          d          }
|sH|	sF|
sDt          d          r0t                      dk    rt          |           \  }}}|r	dd| ddS dddS dg dd}	 ddlm}  ||           }n# t"          $ r Y nw xY wt          |           \  }}}g }t%                      }|d         dv rs|                    d          pg }|r|d                             dd          nd}d| }t)          |          }t+          ||          s|                    ||df           |r(t+          ||          s|                    ||df           |sdddS |dk    rd                    d |D                       }t1          | |          }|dk    rD|D ]\  }}}t3          ||           t                              d | dd!         |           ddd|d"S |dk    r)d                    d# |D                       }dd$| d%dd&S d                    d' |D                       }|d         d         }d( |D             }t7          d) |D                       }|	s|
rd}t8          5  t:                              |          }ddd           n# 1 swxY w Y   |P| |||d*} t=          |           }!t8          5  t>                               |g                               |!           ddd           n# 1 swxY w Y   tC          d+| ||tE          |          |d,-           	  ||            n# tF          $ r}"t                              d.|"           t8          5  t>                              |g           }#|!|#v r|#$                    |!           |#st>          %                    |d           ddd           n# 1 swxY w Y   dd/||d0cY d}"~"S d}"~"ww xY wtM                                          d1d2          }$	 tO          |$          }$n# tP          tR          f$ r d2}$Y nw xY w	 dd3l*m+}% n# tF          $ r d}%Y nw xY wtY          j-                    }&|&t]          |$d          z   }'|&|&d4}(d})	 |'tY          j-                    z
  }*|*dk    rn;|!j/        0                    tc          d5|*          6          rd})n|% |%|(d7           Xt8          5  t>                              |g           }#|!|#v r|#$                    |!           |#st>          %                    |d           ddd           n# 1 swxY w Y   |!j2        }+|)sd8n|+r|+nd8},tC          d9| ||tE          |          |d,|,:           |)r|+|+dk    r|)sd;nd<}-dd=|- d>||d0S |D ]^\  }}}.|+d?k    s|+d@k    r|.rt3          ||           %|+d@k    r3t3          ||           tg          |           ti          tj                     _ddd|dAS tm          || |||d*           d|dB| |dC| dD|  dEdFS tC          d+| ||tE          |          |dG-           to          | || |H          }+tC          d9| ||tE          |          |dG|+:           |+dk    rddI||d0S |D ]^\  }}}.|+d?k    s|+d@k    r|.rt3          ||           %|+d@k    r3t3          ||           tg          |           ti          tj                     _ddd|dAS )JaC  Run all pre-exec security checks and return a single approval decision.

    Gathers findings from tirith and dangerous-command detection, then
    presents them as a single combined approval request. This prevents
    a gateway force=True replay from bypassing one check when only the
    other was shown to the user.
    >   r$  r%  r&  r'  r(  TNrW   r)  r*  z(Sudo stdin guard block: %s (command: %s)r+  r   r,  r/  r/   r   Fr-  r.  r  r   )actionr=  r>  r   )check_command_securityrI  >   warnblockr=  rule_idunknownztirith:smartrB  c              3   "   K   | ]
\  }}}|V  d S r{   r2   r=   _rF  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      )J)J:1dA$)J)J)J)J)J)Jr   r  z'Smart approval: auto-approved '%s' (%s)r  )rQ   rS   smart_approvedr?   c              3   "   K   | ]
\  }}}|V  d S r{   r2   rQ  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      -N-Nzq$d-N-N-N-N-N-Nr   zBLOCKED by smart approval: z@. The command was assessed as genuinely dangerous. Do NOT retry.)rQ   rS   smart_deniedc              3   "   K   | ]
\  }}}|V  d S r{   r2   rQ  s      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      >>zq$d>>>>>>r   c                     g | ]\  }}}|	S r2   r2   )r=   keyrR  s      r   r@   z,check_all_command_guards.<locals>.<listcomp>  s    ...	Q...r   c              3   "   K   | ]
\  }}}|V  d S r{   r2   )r=   rR  is_ts      r   r   z+check_all_command_guards.<locals>.<genexpr>  s(      55jaDT555555r   )rA   ra   pattern_keysr?   pre_approval_requestgateway)rA   r?   ra   r[  r   surfacez"Gateway approval notify failed: %sz?BLOCKED: Failed to send approval request to user. Do NOT retry.r4  gateway_timeouti,  )touch_activity_if_due)
last_touchr   g      ?r   zwaiting for user approvalr   post_approval_response)rA   r?   ra   r[  r   r^  r   z	timed outzdenied by userzBLOCKED: Command z. Do NOT retry this command.r   r   )rQ   rS   user_approvedr?   r0  u   ⚠️ z2. Asking the user for approval.

**Command:**
```
r1  r2  cli)r   r   z#BLOCKED: User denied. Do NOT retry.)8rO   r   r   rU   rK   rX   r  r   r+   r,   r   r   r3   r  rp   tools.tirith_securityrJ  ImportErrorr(   r&   rG  r   rC  r   r!  r   r   r   r   r   rv   r   r   r   r   r   remover   r  r  r  r  tools.environments.baser`  time	monotonicmaxrw   waitminry   r   r   rt   r   r   )/rA   r"  r   r5  r6  is_sudo_guesssudo_guess_descapproval_moder8  r9  is_askr7  _pkr?   r;  rJ  ra   warningsr   r=  rM  
tirith_keytirith_desccombined_desc_for_llmverdictrX  rR  combined_descprimary_keyall_keys
has_tirith	notify_cbapproval_datar   r   r   r   r`  _now	_deadline_activity_stateresolved
_remainingr   _outcomereason	is_tiriths/                                                  r   check_all_command_guardsr    s%    RRR T222 "9!A!AK 59='RVSVRV-XXX%m444 &=W%E%E"M? 9A&	7 	7 	7'888 '((Mry!34455 39X9Z9Z 3^kot^t^t T222122F-//J.//F  3* 3V 3011 	&((F221I'1R1R.c; 
$)Kk K K K	 	 	 !T222  'B2FFM@@@@@@..w77    .Fg-N-N*L+{
 H)++K X"333 $$Z006B;CR(1+//)Y777(w((
0??;
33 	=OOZd;<<< ?;44 	?OO[+u=>>>  3 T222  $		)J)J)J)J)J J J *?@@i% 2 2	QS1111LLB "'<> > > $&*#8: : : $(II-N-NX-N-N-N$N$N!!\9N \ \ \ $	   II>>X>>>>>M1+a.K..X...H55H55555J  [
V [
	 	= 	=+//<<I	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=  
 #* (,	 M #=11E J J**;;;BB5IIIJ J J J J J J J J J J J J J J  &)'!(^^'!   	-((((   CSIII ? ?+//R@@E~~U+++  ?'++K>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? !&`#.#0	       . +,,001BCHHGg,,	*   -IIIIIII - - -(,%%%- >##Ds7A.I-1DAAOH&)9)99
?? ;##CZ,@,@#AA #H(4))')D     ; ;'++K<<E>>LL''' ;#''T:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; \F
 "* 7		 &5ffI   ()'!(^^'!	 	 	 	  v~61A1A,4J:J %W6WWW#.#0	   &. B B!Q	Y&&6X+=+=)+=#K5555x''#K555%c***,-@AAA !%%)-I I I
 	{&$(	%
 %
 	 	 	 &)(m-mm_fmmm	
 	
 		
 !(^^    'w;E~9JL L LF  !(^^	 	 	 	 <&(	
 
 	
 & : :Q	Y6X#5#5)#5K----xK---c"""$%8999!-A A As   E/ /
E<;E<NN	N	0/O++O/2O/P& &
S0"SAR0$S0R4	4S7R4	8	SSS2T TTT# #T21T2AX  X$'X$)F)NTNr{   )lr   contextvarsloggingr+   r9   r   r|   ri  rk   typingr   r   r   utilsr   r   	getLoggerr   r   
ContextVarr   r   __annotations__r   Tokenr   r!   r(   r-   r1   r3   _SSH_SENSITIVE_PATH_HERMES_ENV_PATH_PROJECT_ENV_PATH_PROJECT_CONFIG_PATH_SHELL_RC_FILES_CREDENTIAL_FILES_MACOS_PRIVATE_SYSTEM_PATH_SYSTEM_CONFIG_PATH_SENSITIVE_WRITE_TARGET_PROJECT_SENSITIVE_WRITE_TARGET_COMMAND_TAIL_CMDPOSHARDLINE_PATTERNS
IGNORECASEDOTALLr;   rM   r:   rH   tuplerK   rO   r   rU   rX   DANGEROUS_PATTERNSrn   r_   r`   r   _pattern_description_legacy_key_canonical_keyr   r   rd   rF   rp   Lockr   rq   rr   rs   rt   rv   r   r   r   objectr   r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r!  r:  rG  r  r2   r   r   <module>r     s@          				 				 



                % % % % % % 2 2 2 2 2 2 2 2		8	$	$ 6L[5K6 6 6 {-c2   M3 MT M M M M48 81B31G 8 8 8 8
'[%6s%; ' ' ' ' '
: :S : : : : :>s > > > >)d ) ) ) ), >   S J 8 
'  ? 
 /+...      	  		    #U):"T"T=Q"T"T"T +F  W J\;\W>9
 335MN!=><<>YZ""$CD+ < MBI%	  1     0M 
S U    &
S 
U 
 
 
 
      # $    $c5c,c >c f	c
 pc Ac Nc 'c #c .c 1c Jc 4c  # ""$=>!c" a#c$ 2%c& 0'c0 ^1c2 d3c4 O5c6 ?7c: P;c< V=c> I?c@ mAcB 2/113RSCcD /,..0WXEcF O7NN}NNPvwGcH L4KKMKKM{|IcJ -KcT JUcV -Wc^ e_c` Wacd Oecf ugcj mkct Yucv _wc| 6 3557_`}c~ ]&E\\]\\  _B  Cc@ 30224TUAcB 7!4668deCcH MIcN UOcP OQcR UScT TUcV :Wc^ S_cz<{cB5Cc N  2   I I I I I I
 -/ d3C=) . . .0 ^ ^Hl%%h//K!N##NCCEE::AA>S^B_```##K77>>^?\]]]]@s @s3x @ @ @ @c c    $c e    $ 		$sDy/   $& 4S> & & &#%%s3x   355 S      * * * * * * * * $&c4i % % %)+ T#v+& + + +	. 	.T 	. 	. 	. 	.
3 
4 
 
 
 
 27 # s *.;>   :6s 6t 6 6 6 6) )t ) ) ) )J J3 J J J J'S 'T ' ' ' '+c +d + + + +s t     , , , , , ,H H H H H
DS Ds Dt D D D D-3 - - - --S - - - -#    $:s : : : :  =A6:04p ps p p/2Tzp/3p :=p p p pfc    d    *C * * * *s    
 
 
 
 
,C ,c ,c , , , ,` /3`/ `/S `/C `/7;`/ `/ `/ `/N3d 3s 3 3 3 38 04YA YAc YAS YA8<YA YA YA YAz
       r   