Skip to content

Commit bdeef64

Browse files
cathleenyIsaac
andcommitted
kernel: parse host:port with urllib and check the JWT issuer origin
Replace the _HOSTPORT_RE regex, the _port regex and the manual bracketing in the kernel host folding with urllib.parse.urlsplit, int() and an ipaddress pre-check for bare IPv6 literals. The original host string still passes through unchanged unless a port is folded in; folded IPv6 hosts are bracketed. _check_oauth_origin treats an access_token that decodes as a JWT whose issuer is not server_hostname as OAuth. The kernel wraps every provider with the connection host, so such a token would be exchanged at the _connection_uri origin, while Thrift exchanges it at server_hostname. A mismatched origin now raises before any request. PATs and same-host issuers are unaffected. The port-conflict warning is skipped when _port is the default 443, matching Node. Non-default ports still warn on a mismatch. Behavior-table tests in test_session.py pass unchanged. One warning test changed: test_embedded_port_wins_over_differing_explicit_port lost its ('h.io:8443', 443) row, which asserted the 443 warning. That case moved to test_embedded_port_with_default_443_does_not_warn under the new rule. Known differences from bc7c09e: the OAuth origin check now compares the parsed hostname, so a server_hostname such as 'h.io/x' or 'user@h' with a matching _connection_uri origin passes where it was previously refused. The error text for 'h:70000', 'h:abc' and '[::1' is now one generic malformed-or-out-of-range message. 'user:pass@h' with _port is still refused. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent bc7c09e commit bdeef64

2 files changed

Lines changed: 171 additions & 28 deletions

File tree

‎src/databricks/sql/session.py‎

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import logging
23
import re
34
from functools import cached_property
@@ -6,8 +7,10 @@
67

78
from databricks.sql.types import SSLOptions
89
from databricks.sql.auth.auth import get_python_sql_connector_auth_provider
10+
from databricks.sql.auth.auth_utils import decode_token, is_same_host
911
from databricks.sql.auth.authenticators import AccessTokenAuthProvider
1012
from databricks.sql.auth.common import ClientContext
13+
from databricks.sql.common.url_utils import normalize_host_with_protocol
1114
from databricks.sql.exc import SessionAlreadyClosedError, DatabaseError, RequestError
1215
from databricks.sql import __version__
1316
from databricks.sql import USER_AGENT_NAME
@@ -49,7 +52,6 @@ def __getattr__(name: str) -> Any:
4952

5053

5154
_OAUTH_AUTH_TYPES = ("databricks-oauth", "azure-oauth", "azure-sp-m2m")
52-
_HOSTPORT_RE = re.compile(r"(\[[^\]]*\]|(?!\[)[^:]*)(?::([0-9]+))?")
5355

5456

5557
def _port_or_none(value: Any) -> Optional[int]:
@@ -63,7 +65,8 @@ def _port_or_none(value: Any) -> Optional[int]:
6365
if isinstance(value, (int, str)) and not isinstance(value, bool)
6466
else ""
6567
)
66-
if not re.fullmatch(r"[0-9]+", text) or not int(text) <= 65535:
68+
# isdigit with isascii rejects signs, whitespace and underscores that int() would accept.
69+
if not (text.isascii() and text.isdigit()) or int(text) > 65535:
6770
raise ValueError(
6871
f"Invalid _port {value!r}: expected an integer between 1 and 65535"
6972
)
@@ -83,29 +86,57 @@ def _redact(value: str) -> str:
8386
# userinfo marker is present the whole authority is dropped rather than guessed at.
8487
if "@" in value or "%40" in value.lower():
8588
return f"{scheme}{sep}<redacted>"
86-
host = rest.split("/", 1)[0]
87-
return f"{scheme}{sep}{re.split(r'[?#]', host, maxsplit=1)[0]}"
89+
try:
90+
host = urlsplit(f"//{rest}").netloc
91+
except ValueError:
92+
# urlsplit refuses some hosts (bad IPv6, NFKC-unsafe text); show none of them.
93+
return f"{scheme}{sep}<redacted>"
94+
return f"{scheme}{sep}{host}"
95+
96+
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+
try:
100+
return ipaddress.ip_address(text).version == 6
101+
except ValueError:
102+
return False
88103

89104

90105
def _split_host(host: str) -> Tuple[str, str, Optional[int]]:
91-
"""Split ``[scheme://]name[:port]`` into ``(prefix, name, port)``; keeps IPv6 brackets."""
92-
prefix, sep, text = host.rstrip("/").rpartition("://")
106+
"""Split ``[scheme://]name[:port]`` into ``(prefix, name, port)``. ``name`` keeps the
107+
input's case and any userinfo, and a bare IPv6 literal gets its brackets.
108+
"""
109+
prefix, sep, rest = host.rstrip("/").rpartition("://")
93110
prefix += sep
94-
match = _HOSTPORT_RE.fullmatch(text)
95-
if match is None:
96-
if text.startswith("[") or text.count(":") == 1:
97-
raise ValueError(
98-
f"Invalid server_hostname {_redact(host)!r}: malformed IPv6 literal or port"
99-
)
100-
return prefix, text, None # an unbracketed IPv6 literal such as ::1 has no port
101-
name, port_text = match.group(1), match.group(2)
102-
if port_text is None:
103-
return prefix, name, None
104-
if not 1 <= int(port_text) <= 65535:
111+
if _is_bare_ipv6(rest):
112+
return prefix, f"[{rest}]", None
113+
# urlsplit reads a trailing ':' as an empty port; with an explicit _port that is an error.
114+
if rest.endswith(":"):
115+
raise ValueError(
116+
f"Invalid server_hostname {_redact(host)!r}: malformed IPv6 literal or port"
117+
)
118+
try:
119+
parts = urlsplit(f"//{rest}")
120+
port = parts.port
121+
except ValueError:
122+
# ``from None`` drops the urlsplit exception, whose text can quote the raw netloc.
123+
raise ValueError(
124+
f"Invalid server_hostname {_redact(host)!r}: malformed IPv6 literal or port; "
125+
"a port must be 1-65535"
126+
) from None
127+
if parts.password is not None:
128+
# A colon before '@' is userinfo, not a port, but a credential-bearing host is
129+
# still refused when a port is requested (as on main), so it is never folded.
130+
raise ValueError(
131+
f"Invalid server_hostname {_redact(host)!r}: malformed IPv6 literal or port"
132+
)
133+
if port is None:
134+
return prefix, parts.netloc, None
135+
if not 1 <= port <= 65535:
105136
raise ValueError(
106-
f"Invalid server_hostname {_redact(host)!r}: port {port_text} is out of range"
137+
f"Invalid server_hostname {_redact(host)!r}: port {port} is out of range"
107138
)
108-
return prefix, name, int(port_text)
139+
return prefix, parts.netloc.rpartition(":")[0], port
109140

110141

111142
def _uri_host_and_path(uri: str) -> Tuple[str, str]:
@@ -167,7 +198,8 @@ def _kernel_host_and_path(
167198
if embedded is not None:
168199
# A port embedded in server_hostname always wins, as on main (the kernel
169200
# connected to it); a differing _port is ignored with a warning, not an error.
170-
if embedded != port:
201+
# The default 443 is not a choice worth warning about, so it is never flagged.
202+
if port != 443 and embedded != port:
171203
logger.warning(
172204
"server_hostname %s carries port %d, which differs from _port=%d; "
173205
"connecting to port %d. Set the port in one place only.",
@@ -179,40 +211,57 @@ def _kernel_host_and_path(
179211
return server_hostname, http_path
180212
if port == 443:
181213
return server_hostname, http_path
182-
if ":" in name and not name.startswith("["):
183-
name = f"[{name}]"
184214
return f"{prefix}{name}:{port}", http_path
185215

186216

217+
def _is_foreign_jwt(access_token: str, server_hostname: str) -> bool:
218+
"""True when ``access_token`` is a JWT whose issuer is not ``server_hostname``. Thrift
219+
token-exchanges such a token at server_hostname (TokenFederationProvider); the kernel
220+
wraps every provider with the connection host, so it exchanges at the connection host.
221+
"""
222+
claims = decode_token(access_token)
223+
if not claims:
224+
return False
225+
issuer = claims.get("iss", "")
226+
return not is_same_host(issuer, normalize_host_with_protocol(server_hostname))
227+
228+
187229
def _check_oauth_origin(server_hostname: str, kernel_host: str, kwargs: dict) -> None:
188230
"""AC-P4: with OAuth, a ``_connection_uri`` origin must equal ``https://server_hostname``.
189231
Thrift keeps OAuth on server_hostname; the kernel uses one host for API and token calls.
190232
URI-only use (empty server_hostname) has no server origin to compare and is not checked.
191233
"""
234+
if not (kwargs.get("_connection_uri") and server_hostname):
235+
return
236+
access_token = kwargs.get("access_token")
237+
# A JWT from a foreign issuer is exchanged, so it is an OAuth credential even though
238+
# it arrives as access_token.
239+
foreign_jwt = bool(access_token) and _is_foreign_jwt(access_token, server_hostname)
192240
oauth = (
193241
kwargs.get("auth_type") in _OAUTH_AUTH_TYPES
194242
or kwargs.get("oauth_jwt_key_file")
195243
or (kwargs.get("oauth_client_id") and kwargs.get("oauth_client_secret"))
244+
or foreign_jwt
196245
)
197246
# The kernel auth bridge routes an access_token with no JWT or client-secret
198247
# M2M credentials to PAT, even for auth_type='databricks-oauth'; azure-sp-m2m
199248
# is resolved before that branch and stays OAuth. A custom credentials_provider
200249
# is rejected by the bridge, so it is not an OAuth origin here.
201250
routed_to_pat = (
202-
bool(kwargs.get("access_token"))
251+
bool(access_token)
252+
and not foreign_jwt
203253
and kwargs.get("auth_type") != "azure-sp-m2m"
204254
and not kwargs.get("oauth_jwt_key_file")
205255
and not (kwargs.get("oauth_client_id") and kwargs.get("oauth_client_secret"))
206256
)
207-
if routed_to_pat or not (
208-
kwargs.get("_connection_uri") and server_hostname and oauth
209-
):
257+
if routed_to_pat or not oauth:
210258
return
211259
_, name, port = _split_host(server_hostname)
212260
uri = urlsplit(kernel_host)
261+
server_host = urlsplit(f"//{name}").hostname or ""
213262
if (uri.scheme, uri.hostname or "", uri.port or 443) != (
214263
"https",
215-
name.strip("[]").lower(),
264+
server_host,
216265
port or 443,
217266
):
218267
raise ValueError(

‎tests/unit/test_session.py‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import contextmanager
44
import gc
55
import importlib.metadata as ilm
6+
import jwt
67
import logging
78
import socket
89
import sys
@@ -1317,7 +1318,6 @@ def test_kernel_host_and_path_rejects(server, kwargs, message):
13171318
"server, port, embedded",
13181319
[
13191320
("h.io:7000", 8443, 7000),
1320-
("h.io:8443", 443, 8443),
13211321
("[::1]:7000", 8443, 7000),
13221322
("h.io:443", 8443, 443),
13231323
],
@@ -1334,12 +1334,61 @@ def test_embedded_port_wins_over_differing_explicit_port(
13341334
assert f"_port={port}" in caplog.text
13351335

13361336

1337+
@pytest.mark.parametrize("server", ["h.io:8443", "[::1]:8443", "h.io:7000"])
1338+
def test_embedded_port_with_default_443_does_not_warn(server, caplog):
1339+
# An explicit _port of the default 443 is not a conflict worth a warning (aligned
1340+
# with Node), so only a non-default _port that differs from the embedded port logs.
1341+
with caplog.at_level(logging.WARNING, logger="databricks.sql.session"):
1342+
host, path = _kernel_host_and_path(server, _P, {"_port": 443})
1343+
assert (host, path) == (server, _P)
1344+
assert "carries port" not in caplog.text
1345+
1346+
13371347
def test_embedded_port_equal_to_explicit_port_does_not_warn(caplog):
13381348
with caplog.at_level(logging.WARNING, logger="databricks.sql.session"):
13391349
_kernel_host_and_path("h.io:8443", _P, {"_port": 8443})
13401350
assert "differs from _port" not in caplog.text
13411351

13421352

1353+
@pytest.mark.parametrize(
1354+
"server, kwargs, expected",
1355+
[
1356+
# urlsplit edge cases: the original string passes through unless a port is folded.
1357+
("h:", {}, ("h:", _P)),
1358+
("user@h", {}, ("user@h", _P)),
1359+
("user@h", {"_port": 8443}, ("user@h:8443", _P)),
1360+
("[::1", {}, ("[::1", _P)),
1361+
("::1", {"_port": 443}, ("::1", _P)),
1362+
("H.Example.COM", {}, ("H.Example.COM", _P)),
1363+
("H.Example.COM", {"_port": 8443}, ("H.Example.COM:8443", _P)),
1364+
("h:8443/", {"_port": 8443}, ("h:8443/", _P)),
1365+
("h/x", {}, ("h/x", _P)),
1366+
("h?q", {}, ("h?q", _P)),
1367+
],
1368+
)
1369+
def test_kernel_host_and_path_stdlib_edges_accept(server, kwargs, expected):
1370+
assert _kernel_host_and_path(server, _P, kwargs) == expected
1371+
1372+
1373+
@pytest.mark.parametrize(
1374+
"server, kwargs, message",
1375+
[
1376+
# Trailing ':' is an empty port, which is malformed with an explicit _port.
1377+
("h:", {"_port": 8443}, "malformed"),
1378+
("[::1", {"_port": 8443}, "malformed"),
1379+
("h:abc", {"_port": 8443}, "malformed"),
1380+
# A colon in userinfo is not a port, but a credential-bearing host is never folded.
1381+
("user:pass@h", {"_port": 8443}, "malformed"),
1382+
("h:70000", {"_port": 8443}, "1-65535"),
1383+
("h/x", {"_port": 8443}, "path, query or fragment"),
1384+
("h?q", {"_port": 8443}, "path, query or fragment"),
1385+
],
1386+
)
1387+
def test_kernel_host_and_path_stdlib_edges_reject(server, kwargs, message):
1388+
with pytest.raises(ValueError, match=message):
1389+
_kernel_host_and_path(server, _P, kwargs)
1390+
1391+
13431392
@pytest.mark.parametrize(
13441393
"server, port",
13451394
[
@@ -1459,6 +1508,51 @@ def test_oauth_origin_guard(server, kwargs, uri, should_raise):
14591508
_check_oauth_origin(server, kernel_host, kwargs)
14601509

14611510

1511+
def _jwt(issuer):
1512+
# Unverified on purpose: the guard only reads the iss claim, so the key is a placeholder.
1513+
return jwt.encode({"iss": issuer, "sub": "user"}, "k" * 32, algorithm="HS256")
1514+
1515+
1516+
@pytest.mark.parametrize(
1517+
"server, uri, token, should_raise",
1518+
[
1519+
# A foreign-issuer JWT is exchanged at the kernel's connection host, so a
1520+
# mismatched _connection_uri origin must fail before any request.
1521+
(
1522+
"h.io",
1523+
"https://evil.io/sql/x",
1524+
_jwt("https://login.microsoftonline.com/tenant/v2.0"),
1525+
True,
1526+
),
1527+
# The same foreign JWT is fine when the _connection_uri origin matches.
1528+
(
1529+
"h.io",
1530+
"https://h.io/sql/x",
1531+
_jwt("https://login.microsoftonline.com/tenant/v2.0"),
1532+
False,
1533+
),
1534+
# An issuer on the server host is not exchanged (Thrift parity), so it passes.
1535+
("h.io", "https://h.io/sql/x", _jwt("https://h.io/oidc"), False),
1536+
# A PAT is not a JWT and is not guarded.
1537+
("h.io", "https://evil.io/sql/x", "dapi-xyz", False),
1538+
],
1539+
)
1540+
def test_foreign_issuer_access_token_origin_guard(server, uri, token, should_raise):
1541+
kwargs = {"access_token": token, "_connection_uri": uri}
1542+
kernel_host = _kernel_host_and_path(server, _P, kwargs)[0]
1543+
if should_raise:
1544+
with pytest.raises(ValueError, match="requires the _connection_uri origin"):
1545+
_check_oauth_origin(server, kernel_host, kwargs)
1546+
else:
1547+
_check_oauth_origin(server, kernel_host, kwargs)
1548+
1549+
1550+
def test_foreign_issuer_access_token_without_connection_uri_is_unaffected():
1551+
# No _connection_uri: the kernel host is the server_hostname, so there is no origin to check.
1552+
kwargs = {"access_token": _jwt("https://login.microsoftonline.com/tenant/v2.0")}
1553+
_check_oauth_origin("h.io", "h.io", kwargs)
1554+
1555+
14621556
@pytest.mark.parametrize(
14631557
"uri, secret",
14641558
[

0 commit comments

Comments
 (0)