Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: correctly handle ipv6 (#8650)

Dylan Madisetti7640c705a1e7
diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.py
index 711e50fdc0..af15fa5593 100644
--- a/marimo/_server/api/lifespans.py
+++ b/marimo/_server/api/lifespans.py
@@ -215,7 +215,9 @@ async def etc(app: Starlette) -> AsyncIterator[None]:
def _startup_url(state: AppStateBase) -> str:
- host = state.host
+ host = state.host.strip(
+ "[]"
+ ) # normalize: remove brackets if user passed [addr]
port = state.port
try:
# pretty printing:
@@ -231,11 +233,16 @@ def _startup_url(state: AppStateBase) -> str:
# printing the host name
...
- url = f"http://{host}:{port}{state.base_url}"
+ # Strip IPv6 zone ID (e.g. fe80::1%eth0 -> fe80::1); zone IDs are
+ # interface-specific and not valid in URLs
+ url_host_bare = host.split("%")[0]
+ # IPv6 addresses must be wrapped in brackets in URLs (RFC 3986)
+ url_host = f"[{url_host_bare}]" if ":" in url_host_bare else url_host_bare
+ url = f"http://{url_host}:{port}{state.base_url}"
if port == 80:
- url = f"http://{host}{state.base_url}"
+ url = f"http://{url_host}{state.base_url}"
elif port == 443:
- url = f"https://{host}{state.base_url}"
+ url = f"https://{url_host}{state.base_url}"
if AuthToken.is_empty(state.session_manager.auth_token):
return url
@@ -243,7 +250,9 @@ def _startup_url(state: AppStateBase) -> str:
def _mcp_startup_url(state: AppStateBase) -> str:
- host = state.host
+ host = state.host.strip(
+ "[]"
+ ) # normalize: remove brackets if user passed [addr]
port = state.port
base_url = state.base_url
@@ -257,15 +266,18 @@ def _mcp_startup_url(state: AppStateBase) -> str:
except Exception:
...
+ # Strip IPv6 zone ID; IPv6 addresses must be wrapped in brackets (RFC 3986)
+ url_host_bare = host.split("%")[0]
+ url_host = f"[{url_host_bare}]" if ":" in url_host_bare else url_host_bare
# Construct MCP endpoint URL
mcp_prefix = "/mcp"
mcp_name = "server"
full_mcp_path = f"{mcp_prefix}/{mcp_name}"
- url = f"http://{host}:{port}{base_url}{full_mcp_path}"
+ url = f"http://{url_host}:{port}{base_url}{full_mcp_path}"
if port == 80:
- url = f"http://{host}{base_url}{full_mcp_path}"
+ url = f"http://{url_host}{base_url}{full_mcp_path}"
elif port == 443:
- url = f"https://{host}{base_url}{full_mcp_path}"
+ url = f"https://{url_host}{base_url}{full_mcp_path}"
# Add access token if not empty
if AuthToken.is_empty(state.session_manager.auth_token):
diff --git a/marimo/_server/print.py b/marimo/_server/print.py
index 5ad9aa4872..99d87284b1 100644
--- a/marimo/_server/print.py
+++ b/marimo/_server/print.py
@@ -108,7 +108,11 @@ def _colorized_url(url_string: str) -> str:
else:
query = ""
- url_string = f"{url.scheme}://{url.hostname}"
+ hostname = url.hostname or ""
+ # IPv6 addresses need brackets when embedded in a URL (RFC 3986)
+ if ":" in hostname:
+ hostname = f"[{hostname}]"
+ url_string = f"{url.scheme}://{hostname}"
# raw https and http urls do not have a port to parse
try:
if url.port:
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index 935cbec0e5..67d3c0f8fb 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -355,7 +355,9 @@ def start(
uvicorn.Config(
app,
port=port,
- host=host,
+ host=host.strip(
+ "[]"
+ ), # uvicorn expects bare IPv6 without brackets
log_level=log_level,
# uvicorn times out HTTP connections (i.e. TCP sockets) every 5
# seconds by default; for some reason breaks the server in
diff --git a/tests/_server/test_print.py b/tests/_server/test_print.py
index 66c45eb954..ba0f2299df 100644
--- a/tests/_server/test_print.py
+++ b/tests/_server/test_print.py
@@ -42,6 +42,19 @@ def test_colorized_url() -> None:
assert "localhost:8000/path" in result
assert "query=value" in result
+ # Test with an IPv6 address (RFC 3986 requires brackets)
+ result = _colorized_url("http://[2001:db8::1]:8000/path")
+ assert "[2001:db8::1]:8000/path" in result
+
+ # Test with IPv6 loopback
+ result = _colorized_url("http://[::1]:2718")
+ assert "[::1]:2718" in result
+
+ # Zone IDs must not appear in URLs (stripped before URL construction)
+ result = _colorized_url("http://[fe80::1]:2718")
+ assert "[fe80::1]:2718" in result
+ assert "%" not in result
+
def test_get_network_url() -> None:
"""Test the _get_network_url function."""
diff --git a/tests/_server/test_startup_url.py b/tests/_server/test_startup_url.py
new file mode 100644
index 0000000000..4d901ba048
--- /dev/null
+++ b/tests/_server/test_startup_url.py
@@ -0,0 +1,71 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from unittest.mock import MagicMock
+
+import pytest
+
+from marimo._server.api.lifespans import _startup_url
+from marimo._server.tokens import AuthToken
+
+
+def _make_state(host: str, port: int = 2718, base_url: str = "/") -> MagicMock:
+ state = MagicMock()
+ state.host = host
+ state.port = port
+ state.base_url = base_url
+ state.session_manager.auth_token = AuthToken("") # empty = no token
+ return state
+
+
+ ("host", "expected"),
+ [
+ # IPv4
+ ("127.0.0.1", "http://localhost:2718/"), # loopback pretty-printed
+ ("0.0.0.0", "http://0.0.0.0:2718/"),
+ # Bare IPv6
+ ("fd00::cafe", "http://[fd00::cafe]:2718/"),
+ ("2001:db8::1", "http://[2001:db8::1]:2718/"),
+ ("::1", "http://localhost:2718/"), # loopback pretty-printed
+ ("::", "http://[::]:2718/"),
+ # Full non-abbreviated IPv6
+ (
+ "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
+ "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:2718/",
+ ),
+ # Bracketed IPv6 (user passed [addr])
+ ("[fd00::cafe]", "http://[fd00::cafe]:2718/"),
+ # Zone ID must be stripped from URL
+ ("fe80::1%eth0", "http://[fe80::1]:2718/"),
+ ("[fe80::1%lo0]", "http://[fe80::1]:2718/"),
+ ],
+)
+def test_startup_url_ipv6(host: str, expected: str) -> None:
+ state = _make_state(host)
+ assert _startup_url(state) == expected
+
+
+def test_startup_url_getnameinfo_failure() -> None:
+ """If getnameinfo raises (e.g. host not resolvable), URL is still valid."""
+ from unittest.mock import patch
+
+ state = _make_state("fd00::dead")
+ with patch("socket.getnameinfo", side_effect=OSError("unreachable")):
+ url = _startup_url(state)
+ assert url == "http://[fd00::dead]:2718/"
+
+
+def test_startup_url_ipv6_with_token() -> None:
+ """Full IPv6 URL including access token is well-formed."""
+ state = _make_state("fd00::cafe", port=2718, base_url="/")
+ state.session_manager.auth_token = AuthToken("tok3n")
+ url = _startup_url(state)
+ assert url == "http://[fd00::cafe]:2718/?access_token=tok3n"
+ # Verify it's parseable and components are correct
+ from urllib.parse import urlparse
+
+ parsed = urlparse(url)
+ assert parsed.hostname == "fd00::cafe"
+ assert parsed.port == 2718
+ assert parsed.query == "access_token=tok3n"