Files
hermes-agent/hermes_cli/url_utils.py
Austin Pickett a39c2610e5 fix(dashboard): bracket IPv6 URL authorities everywhere hosts compose URLs
An IPv6 literal followed by :port is only a valid URL authority inside
brackets; http://::1:9119 is unparseable by browsers and urllib. Add
hermes_cli/url_utils.format_url_host and use it at every site that
builds an http URL from a (possibly IPv6) bind host: the named-profile
dashboard attach URL, the browser auto-open URL, the startup banner,
the plugin-activation loopback request, the proxy listening banner, and
the gateway-migration listener URLs. Zone identifiers (%eth0) are
percent-encoded per RFC 6874.

Fixes #67367

Co-authored-by: sjungwon03 <sjungwon03@gmail.com>
2026-10-06 14:23:00 -04:00

19 lines
620 B
Python

"""Small helpers for composing URLs from configured network hosts."""
def format_url_host(host: str) -> str:
"""Return *host* in HTTP URL-authority form.
IPv6 literals require brackets when followed by a port. Already-bracketed
values are preserved so callers can safely pass either representation.
"""
host = host.strip()
if host.startswith("[") and host.endswith("]"):
inner_host = host[1:-1]
if ":" in inner_host:
return f"[{inner_host.replace('%', '%25')}]"
return host
if ":" in host:
return f"[{host.replace('%', '%25')}]"
return host