Skip to content

Commit 5082c7e

Browse files
cathleenyIsaac
andcommitted
kernel: drop bare IPv6 support on the host:port path
Remove _is_bare_ipv6 and the ipaddress import. Bare IPv6 hosts such as ::1 never worked on the kernel path: the kernel prepends https:// and its URL parser rejects an unbracketed IPv6 literal. IPv6 literals must be bracketed ([::1]). With _port set, urlsplit reads everything after the first colon of a bare IPv6 literal as the port. That text always contains another colon, so the integer parse fails and the existing "malformed IPv6 literal or port" ValueError is raised before any request. This includes _port=443. Without _port the host still passes through unchanged, as on main. Tests: the ("::1", 8443) -> "[::1]:8443" row is removed. ::1 with _port 8443 or 443, and 2001:db8::1 with _port 8443, are now in the reject table. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent f242c0f commit 5082c7e

2 files changed

Lines changed: 9 additions & 19 deletions

File tree

‎src/databricks/sql/session.py‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ipaddress
21
import logging
32
import re
43
from functools import cached_property
@@ -94,26 +93,13 @@ def _redact(value: str) -> str:
9493
return f"{scheme}{sep}{host}"
9594

9695

97-
def _is_bare_ipv6(text: str) -> bool:
98-
"""True for an unbracketed IPv6 literal such as ``::1``, which urlsplit reads as a port."""
99-
# A bare IPv6 literal has at least two colons and no brackets. Checking that first skips
100-
# the costly ValueError that ip_address raises for names and host:port strings.
101-
if text.count(":") < 2 or text.startswith("["):
102-
return False
103-
try:
104-
return ipaddress.ip_address(text).version == 6
105-
except ValueError:
106-
return False
107-
108-
10996
def _split_host(host: str) -> Tuple[str, str, Optional[int]]:
11097
"""Split ``[scheme://]name[:port]`` into ``(prefix, name, port)``. ``name`` keeps the
111-
input's case and any userinfo, and a bare IPv6 literal gets its brackets.
98+
input's case and any userinfo. IPv6 literals must be bracketed: urlsplit reads a bare
99+
``::1`` as a malformed port, so it is rejected.
112100
"""
113101
prefix, sep, rest = host.rstrip("/").rpartition("://")
114102
prefix += sep
115-
if _is_bare_ipv6(rest):
116-
return prefix, f"[{rest}]", None
117103
# urlsplit reads a trailing ':' as an empty port; with an explicit _port that is an error.
118104
if rest.endswith(":"):
119105
raise ValueError(

‎tests/unit/test_session.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12021202
@pytest.mark.parametrize(
12031203
"server, hpath, kwargs, expected",
12041204
[
1205-
# AC-P1: _port folds into host:port; 443 adds nothing; IPv6 is bracketed.
1205+
# AC-P1: _port folds into host:port; 443 adds nothing; a bracketed IPv6 keeps its brackets.
12061206
("h.io", _P, {}, ("h.io", _P)),
12071207
("h.io", _P, {"_port": 8443}, ("h.io:8443", _P)),
12081208
("h.io", _P, {"_port": "8443"}, ("h.io:8443", _P)),
@@ -1224,8 +1224,9 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12241224
("h.io/x", _P, {}, ("h.io/x", _P)),
12251225
("https://h.io:8443/", _P, {"_port": 8443}, ("https://h.io:8443/", _P)),
12261226
("https://h.io", _P, {"_port": 8443}, ("https://h.io:8443", _P)),
1227+
# Without _port a bare IPv6 host passes through, as on main; with _port it is
1228+
# rejected (see test_kernel_host_and_path_stdlib_edges_reject). Bracket it.
12271229
("::1", _P, {}, ("::1", _P)),
1228-
("::1", _P, {"_port": 8443}, ("[::1]:8443", _P)),
12291230
("[::1]", _P, {"_port": 8443}, ("[::1]:8443", _P)),
12301231
("h.io:8443", _P, {}, ("h.io:8443", _P)),
12311232
("h.io:8443", _P, {"_port": 8443}, ("h.io:8443", _P)),
@@ -1358,7 +1359,6 @@ def test_embedded_port_equal_to_explicit_port_does_not_warn(caplog):
13581359
("user@h", {}, ("user@h", _P)),
13591360
("user@h", {"_port": 8443}, ("user@h:8443", _P)),
13601361
("[::1", {}, ("[::1", _P)),
1361-
("::1", {"_port": 443}, ("::1", _P)),
13621362
("H.Example.COM", {}, ("H.Example.COM", _P)),
13631363
("H.Example.COM", {"_port": 8443}, ("H.Example.COM:8443", _P)),
13641364
("h:8443/", {"_port": 8443}, ("h:8443/", _P)),
@@ -1376,6 +1376,10 @@ def test_kernel_host_and_path_stdlib_edges_accept(server, kwargs, expected):
13761376
# Trailing ':' is an empty port, which is malformed with an explicit _port.
13771377
("h:", {"_port": 8443}, "malformed"),
13781378
("[::1", {"_port": 8443}, "malformed"),
1379+
# Bare IPv6 is not supported with _port: urlsplit reads it as a malformed port.
1380+
("::1", {"_port": 8443}, "malformed"),
1381+
("::1", {"_port": 443}, "malformed"),
1382+
("2001:db8::1", {"_port": 8443}, "malformed"),
13791383
("h:abc", {"_port": 8443}, "malformed"),
13801384
# A colon in userinfo is not a port, but a credential-bearing host is never folded.
13811385
("user:pass@h", {"_port": 8443}, "malformed"),

0 commit comments

Comments
 (0)