
    |+jR                        U d Z ddlmZ ddlZddlZddlZddlmZ  ej	        e
          Z ed          Z e            Zded<   ddZddZddZddZddZddZdS )uz  Helpers for X-Forwarded-Prefix support.

Mission-control style deploys reverse-proxy the dashboard at a path
prefix (e.g. ``mission-control.tilos.com/hermes/*`` -> dashboard on
:9119), injecting ``X-Forwarded-Prefix: /hermes`` so the backend can
reconstruct prefixed URLs (Location: headers, OAuth redirect_uri,
cookie Path attributes, SPA asset URLs).

This module is also the home of the ``HERMES_DASHBOARD_PUBLIC_URL`` /
``dashboard.public_url`` resolution — when the operator declares a
complete public URL (scheme + host + optional path prefix), we use
that directly for the OAuth ``redirect_uri`` and skip the
X-Forwarded-Prefix reconstruction. Relief valve for deploys where the
proxy header chain isn't reliable.

The single source of truth for both helpers lives here so the gate
middleware, the OAuth routes, the cookie helpers, and the SPA mount
all agree on validation rules.
    )annotationsN)Optional)"'<> 
	set_warned_malformed_public_urlssourcestrrawreturnNonec                    |r|                                 nd}|sdS | |f}|t          v rdS t                              |           t                              d| ||                    d          d         pd           dS )u  Warn (once per distinct value) when a non-empty public-url value
    was rejected by :func:`_normalise_public_url`.

    A non-empty value that normalises to ``""`` is almost always a
    missing scheme (``hermes.example.com`` instead of
    ``https://hermes.example.com``) — the single most common cause of
    "I set HERMES_DASHBOARD_PUBLIC_URL but the OAuth callback is still
    http://". Without this warning the value is silently discarded and
    the dashboard falls back to reconstructing the redirect URI from
    request headers, which behind a reverse proxy can yield the wrong
    scheme. Surfacing it turns a silent footgun into a self-diagnosing
    one.
     Nu  %s is set to %r but was ignored because it is not a valid absolute URL — it must include an http:// or https:// scheme (e.g. https://%s). Falling back to reconstructing the OAuth redirect URI from request headers, which may produce the wrong scheme behind a reverse proxy.z://zhermes.example.com)stripr   add_logwarningsplit)r   r   cleanedkeys       ?/usr/local/lib/hermes-agent/hermes_cli/dashboard_auth/prefix.py_warn_if_malformedr   *   s     !(ciikkkbG 7
C
+++!%%c***LL	)
 	eR 8$8	 	 	 	 	    Optional[str]c                   | sdS |                                  sdS                     d          sdz                       d          dv s$dv s t          fdt          D                       rdS t                    dk    rdS S )aT  Normalise an X-Forwarded-Prefix header value.

    Returns a string like ``"/hermes"`` (no trailing slash) or ``""``
    when no prefix is set / the header is malformed. We deliberately
    reject anything containing ``..`` or non-printable bytes so a
    hostile proxy can't inject HTML or path-traversal sequences via the
    prefix.
    r   /z//z..c              3      K   | ]}|v V  	d S N ).0cps     r   	<genexpr>z#normalise_prefix.<locals>.<genexpr>_   s'      --!qAv------r    @   )r   
startswithrstripany_REJECT_CHARSlen)r   r)   s    @r   normalise_prefixr1   K   s      r		A r<< !G	A		199----}-----  r
1vv{{rHr    c                P    t          | j                            d                    S )zConvenience wrapper that reads the header off a Starlette/FastAPI
    Request and normalises it. Returns ``""`` when no prefix.
    zx-forwarded-prefix)r1   headersget)requests    r   prefix_from_requestr6   g   s#     GO//0DEEFFFr    c                6   | sdS |                                  sdS t          fdt          D                       rdS 	 t          j                                      }n# t          $ r Y dS w xY w|j        dvrdS |j        sdS 	                    d          S )u  Normalise a ``dashboard.public_url`` value.

    Returns the cleaned URL (scheme://netloc[/path], trailing slash
    removed) on success, or ``""`` when the value is empty, malformed,
    or contains characters that suggest header injection. The caller
    must treat ``""`` as "fall back to request reconstruction" — never
    as "the user explicitly chose no public URL", because the two are
    indistinguishable from an empty env var.
    r   c              3      K   | ]}|v V  	d S r%   r&   )r'   r(   urls     r   r*   z(_normalise_public_url.<locals>.<genexpr>   s'      
+
+18
+
+
+
+
+
+r    >   httphttpsr#   )
r   r.   r/   urllibparseurlparse
ValueErrorschemenetlocr-   )r   parsedr9   s     @r   _normalise_public_urlrC   s   s      r
))++C r
 
+
+
+
+]
+
+
+++ r&&s++   rr}---r= r ::c??s   A! !
A/.A/dictc                 @   	 ddl m}  n# t          $ r i cY S w xY w	  |             }n4# t          $ r'}t                              d|           i cY d}~S d}~ww xY wt          |t                    r|                    d          nd}t          |t                    r|ni S )aY  Return the ``dashboard`` block from ``config.yaml`` if it exists
    and is a dict; otherwise an empty dict.

    Robust to (a) load_config() raising (malformed YAML, IO error,
    config.yaml absent), and (b) ``dashboard`` being absent or non-dict.
    Both shapes fall through to ``{}`` so the caller can rely on
    ``.get(...)`` access.
    r   )load_configzVdashboard-auth.prefix: load_config() raised %s; falling back to env-only configurationN	dashboard)hermes_cli.configrF   	Exceptionr   debug
isinstancerD   r4   )rF   cfgexcsections       r   _load_dashboard_sectionrO      s    1111111   			kmm   

5	
 	
 	

 						 '1d&;&;Ecggk"""G $//777R7s&   	 
' 
AAAAc                 ,   t           j                            dd          } t          |           }|r|S t	          d|            t          t                                          dd                    }t          |          }|st	          d|           |S )u  Resolve the operator-declared dashboard public URL.

    Precedence (mirrors ``dashboard.oauth.client_id``):

      1. ``HERMES_DASHBOARD_PUBLIC_URL`` env var (when non-empty after
         strip — empty values are treated as unset so a provisioned-but-
         not-populated Fly secret can't shadow a valid config.yaml entry).
      2. ``dashboard.public_url`` in ``config.yaml``.
      3. Empty string — signals "no override, reconstruct from request"
         to the caller.

    Each candidate value is run through :func:`_normalise_public_url`.
    A malformed env var falls through to the config.yaml entry; a
    malformed config entry falls through to ``""``. This means a typo
    in one surface doesn't prevent the other from working.
    HERMES_DASHBOARD_PUBLIC_URLr   z#HERMES_DASHBOARD_PUBLIC_URL env var
public_urlz#dashboard.public_url in config.yaml)osenvironr4   rC   r   r   rO   )env_raw	env_cleancfg_raw	cfg_cleans       r   resolve_public_urlrY      s    " jnn:B??G%g..I <gFFF)++//bAABBG%g..I K@'JJJr    )r   r   r   r   r   r   )r   r!   r   r   )r   r   )r   rD   )__doc__
__future__r   loggingrS   urllib.parser<   typingr   	getLogger__name__r   	frozensetr/   r   r   __annotations__r   r1   r6   rC   rO   rY   r&   r    r   <module>rc      s    & # " " " " "  				          w""
 	EFF &)SUU  * * * *   B   8G G G G   D8 8 8 84     r    