Skip to content

The agent that grows with you

fix(webapp): read a bracketed IPv6 webapp.host as the address

emozillaf39aa2a1a1f5
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index 0fd94df5e3e..db7c5096165 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -97,7 +97,8 @@ def webapp_settings(home) -> WebappSettings:
return off(f"webapp.enabled must be true or false in {path}")
host = block.get("host", DEFAULT_HOST)
port = block.get("port", DEFAULT_PORT)
- if not isinstance(host, str) or not (_is_loopback(host.strip()) or _is_wildcard(host.strip())):
+ host = _bare_host(host) if isinstance(host, str) else None
+ if host is None or not (_is_loopback(host) or _is_wildcard(host)):
return off(f"webapp.host must be a loopback address, 0.0.0.0 or :: in {path}")
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
return off(f"webapp.port must be a port number (1-65535) in {path}")
@@ -105,7 +106,16 @@ def webapp_settings(home) -> WebappSettings:
if public_url is None:
return off(f"webapp.public_url must be an http:// or https:// address with no credentials, "
f"query or fragment in {path}")
- return WebappSettings(enabled=True, host=host.strip(), port=port, public_url=public_url)
+ return WebappSettings(enabled=True, host=host, port=port, public_url=public_url)
+
+
+def _bare_host(host: str) -> str:
+ """*host* without surrounding space or the brackets a URL puts around an IPv6 address
+ (``[::1]`` is ``::1``); brackets around anything else stay and fail the host check."""
+ host = host.strip()
+ if host.startswith("[") and host.endswith("]") and ":" in host[1:-1] and "[" not in host[1:-1]:
+ return host[1:-1]
+ return host
def _public_url(value) -> str | None:
@@ -128,7 +138,7 @@ def _is_loopback(host: str) -> bool:
if host == "localhost":
return True
try:
- return ipaddress.ip_address(host.strip("[]")).is_loopback
+ return ipaddress.ip_address(host).is_loopback
except ValueError:
return False
diff --git a/tests/gateway/test_webapp_settings.py b/tests/gateway/test_webapp_settings.py
index 4fd089274c1..b5ccdd84eb5 100644
--- a/tests/gateway/test_webapp_settings.py
+++ b/tests/gateway/test_webapp_settings.py
@@ -43,6 +43,20 @@ def test_a_webapp_block_that_is_off_or_valid_has_no_reason(tmp_path, block):
assert webapp.webapp_settings(_home(tmp_path, block)).reason is None
[email protected](("host", "bind_host"), [("'[::1]'", "::1"), ("'[::]'", "::"), ("' ::1 '", "::1")])
+def test_a_bracketed_ipv6_host_is_read_as_the_address(tmp_path, host, bind_host):
+ # Brackets belong to URLs; the bind address and the address native clients dial carry none.
+ settings = webapp.webapp_settings(_home(tmp_path, f"webapp:\n enabled: true\n host: {host}\n"))
+ assert settings.bind == (bind_host, webapp.DEFAULT_PORT)
+ assert webapp.dial_origin(settings.host, 9120) == "http://[::1]:9120"
+
+
[email protected]("host", ["'[127.0.0.1]'", "'[localhost]'", "'[::1'", "'::1]'", "'[[::1]]'"])
+def test_brackets_around_anything_but_an_ipv6_address_keep_the_webapp_off(tmp_path, host):
+ settings = webapp.webapp_settings(_home(tmp_path, f"webapp:\n enabled: true\n host: {host}\n"))
+ assert settings.enabled is False and "webapp.host" in settings.reason
+
+
def _gateway(monkeypatch, tmp_path, config):
"""The runtime's listener start for *config*, with the listener itself replaced by a stand-in."""
from gateway import run_api, run_runtime