§
    ‰eîi„  ã                  ó¾   — U d Z ddlmZ ddlZddlmZ ddlmZ  ej        e	¦  «        Z
 ed¦  «        Zded<   dd„Zdaded<   dd„Zdd„Zdd„Zd d„Zdd„Zd!d„ZdS )"ua  Environment variable passthrough registry.

Skills that declare ``required_environment_variables`` in their frontmatter
need those vars available in sandboxed execution environments (execute_code,
terminal).  By default both sandboxes strip secrets from the child process
environment for security.  This module provides a session-scoped allowlist
so skill-declared vars (and user-configured overrides) pass through.

Two sources feed the allowlist:

1. **Skill declarations** â€” when a skill is loaded via ``skill_view``, its
   ``required_environment_variables`` are registered here automatically.
2. **User config** â€” ``terminal.env_passthrough`` in config.yaml lets users
   explicitly allowlist vars for non-skill use cases.

Both ``code_execution_tool.py`` and ``tools/environments/local.py`` consult
:func:`is_env_passthrough` before stripping a variable.
é    )ÚannotationsN)Ú
ContextVar)ÚIterableÚ_allowed_env_varszContextVar[set[str]]Ú_allowed_env_vars_varÚreturnúset[str]c                 óª   — 	 t                                ¦   «         S # t          $ r- t          ¦   «         } t                                | ¦  «         | cY S w xY w)zGGet or create the allowed env vars set for the current context/session.)r   ÚgetÚLookupErrorÚset)Úvals    ú7/home/div/.hermes/hermes-agent/tools/env_passthrough.pyÚ_get_allowedr   !   sZ   € ðÝ$×(Ò(Ñ*Ô*Ð*øÝð ð ð Ý™œˆÝ×!Ò! #Ñ&Ô&Ð&Øˆ
ˆ
ˆ
ðøøøs   ‚ ›4AÁAzfrozenset[str] | NoneÚ_config_passthroughÚnameÚstrÚboolc                ó<   — 	 ddl m} n# t          $ r Y dS w xY w| |v S )u³  True if ``name`` is a Hermes-managed provider credential (API key,
    token, or similar) per ``_HERMES_PROVIDER_ENV_BLOCKLIST``.

    Skill-declared ``required_environment_variables`` frontmatter must
    not be able to override this list â€” that was the bypass in
    GHSA-rhgp-j443-p4rf where a malicious skill registered
    ``ANTHROPIC_TOKEN`` / ``OPENAI_API_KEY`` as passthrough and received
    the credential in the ``execute_code`` child process, defeating the
    sandbox's scrubbing guarantee.

    Non-Hermes API keys (TENOR_API_KEY, NOTION_TOKEN, etc.) are NOT
    in the blocklist and remain legitimately registerable â€” skills that
    wrap third-party APIs still work.
    r   )Ú_HERMES_PROVIDER_ENV_BLOCKLISTF)Útools.environments.localr   Ú	Exception)r   r   s     r   Ú_is_hermes_provider_credentialr   /   sJ   € ðØKÐKÐKÐKÐKÐKÐKøÝð ð ð ØˆuˆuðøøøàÐ1Ð1Ð1s   ‚	 ‰
–Ú	var_namesúIterable[str]ÚNonec                ó  — | D ]€}|                      ¦   «         }|sŒt          |¦  «        rt                               d|¦  «         ŒDt	          ¦   «                              |¦  «         t                               d|¦  «         Œ�dS )uä  Register environment variable names as allowed in sandboxed environments.

    Typically called when a skill declares ``required_environment_variables``.

    Variables that are Hermes-managed provider credentials (from
    ``_HERMES_PROVIDER_ENV_BLOCKLIST``) are rejected here to preserve
    the ``execute_code`` sandbox's credential-scrubbing guarantee per
    GHSA-rhgp-j443-p4rf. A skill that needs to talk to a Hermes-managed
    provider should do so via the agent's main-process tools (web_search,
    web_extract, etc.) where the credential remains safely in the main
    process.

    Non-Hermes third-party API keys (TENOR_API_KEY, NOTION_TOKEN, etc.)
    pass through normally â€” they were never in the sandbox scrub list.
    zÓenv passthrough: refusing to register Hermes provider credential %r (blocked by _HERMES_PROVIDER_ENV_BLOCKLIST). Skills must not override the execute_code sandbox's credential scrubbing; see GHSA-rhgp-j443-p4rf.zenv passthrough: registered %sN)Ústripr   ÚloggerÚwarningr   ÚaddÚdebug)r   r   s     r   Úregister_env_passthroughr#   E   s›   € ð  ð =ð =ˆØ�zŠz‰|Œ|ˆØð 	ØÝ)¨$Ñ/Ô/ð 	Ý�NŠNðAð ñô ð ð Ý‰Œ×Ò˜4Ñ Ô Ð Ý�ŠÐ5°tÑ<Ô<Ð<Ð<ð=ð =ó    úfrozenset[str]c                 ó  — t           �t           S t          ¦   «         } 	 ddlm}  |¦   «         }|                     di ¦  «                             d¦  «        }t          |t          ¦  «        rU|D ]R}t          |t          ¦  «        r;|                     ¦   «         r'|  	                    |                     ¦   «         ¦  «         ŒSn2# t          $ r%}t                               d|¦  «         Y d}~nd}~ww xY wt          | ¦  «        a t           S )z9Load ``tools.env_passthrough`` from config.yaml (cached).Nr   )Úread_raw_configÚterminalÚenv_passthroughz4Could not read tools.env_passthrough from config: %s)r   r   Úhermes_cli.configr'   r   Ú
isinstanceÚlistr   r   r!   r   r   r"   Ú	frozenset)Úresultr'   ÚcfgÚpassthroughÚitemÚes         r   Ú_load_config_passthroughr3   f   s  € õ Ð&Ý"Ð"å‘u”u€Fð	PØ5Ð5Ð5Ð5Ð5Ð5ØˆoÑÔˆØ—g’g˜j¨"Ñ-Ô-×1Ò1Ð2CÑDÔDˆÝ�k¥4Ñ(Ô(ð 	-Ø#ð -ð -�Ý˜d¥CÑ(Ô(ð -¨T¯ZªZ©\¬\ð -Ø—J’J˜tŸzšz™|œ|Ñ,Ô,Ð,øøøÝð Pð Pð PÝ�ŠÐKÈQÑOÔOÐOÐOÐOÐOÐOÐOøøøøðPøøøõ $ FÑ+Ô+ÐÝÐs   žB#C Ã
C1ÃC,Ã,C1Úvar_namec                óF   — | t          ¦   «         v rdS | t          ¦   «         v S )zÄCheck whether *var_name* is allowed to pass through to sandboxes.

    Returns ``True`` if the variable was registered by a skill or listed in
    the user's ``tools.env_passthrough`` config.
    T)r   r3   )r4   s    r   Úis_env_passthroughr6   |   s*   € ð •<‘>”>Ð!Ð!ØˆtØÕ/Ñ1Ô1Ð1Ð1r$   c                 óV   — t          t          ¦   «         ¦  «        t          ¦   «         z  S )zGReturn the union of skill-registered and config-based passthrough vars.)r-   r   r3   © r$   r   Úget_all_passthroughr9   ‡   s    € å•\‘^”^Ñ$Ô$Õ'?Ñ'AÔ'AÑAÐAr$   c                 óF   — t          ¦   «                              ¦   «          dS )z9Reset the skill-scoped allowlist (e.g. on session reset).N)r   Úclearr8   r$   r   Úclear_env_passthroughr<   Œ   s   € å�N„N×ÒÑÔÐÐÐr$   )r   r	   )r   r   r   r   )r   r   r   r   )r   r%   )r4   r   r   r   )r   r   )Ú__doc__Ú
__future__r   ÚloggingÚcontextvarsr   Útypingr   Ú	getLoggerÚ__name__r   r   Ú__annotations__r   r   r   r#   r3   r6   r9   r<   r8   r$   r   ú<module>rE      s5  ððð ð ð& #Ð "Ð "Ð "Ð "Ð "à €€€Ø "Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð à	ˆÔ	˜8Ñ	$Ô	$€ð /9¨jÐ9LÑ.MÔ.MÐ Ð MÐ MÐ MÑ Mðð ð ð ð .2Ð Ð 1Ð 1Ð 1Ñ 1ð2ð 2ð 2ð 2ð,=ð =ð =ð =ðBð ð ð ð,2ð 2ð 2ð 2ðBð Bð Bð Bð
ð ð ð ð ð r$   