Skip to content

Commit 5bb846e

Browse files
cathleenyIsaac
andcommitted
fix(kernel): match main for default host resolution and PAT OAuth guard
A kernel connection without _port or _connection_uri again passes server_hostname through unchanged, so odd hosts no longer raise on the default path. The OAuth origin guard now skips connections the kernel auth bridge routes to PAT: an access_token with no JWT or client-secret M2M credentials, even when auth_type is databricks-oauth. azure-sp-m2m and M2M/JWT stay guarded. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent d98ca45 commit 5bb846e

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎src/databricks/sql/session.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ def _kernel_host_and_path(
121121
if kwargs.get("_connection_uri"):
122122
return _uri_host_and_path(kwargs["_connection_uri"])
123123
port = _port_or_none(kwargs.get("_port"))
124-
prefix, name, embedded = _split_host(server_hostname)
125124
if port is None:
126125
return server_hostname, http_path
126+
prefix, name, embedded = _split_host(server_hostname)
127127
if not server_hostname:
128128
raise ValueError(
129129
"server_hostname is required on use_kernel=True when _port is set "
@@ -153,7 +153,16 @@ def _check_oauth_origin(server_hostname: str, kernel_host: str, kwargs: dict) ->
153153
or (kwargs.get("oauth_client_id") and kwargs.get("oauth_client_secret"))
154154
or kwargs.get("credentials_provider") is not None
155155
)
156-
if not (kwargs.get("_connection_uri") and server_hostname and oauth):
156+
# The kernel auth bridge routes an access_token with no JWT or client-secret
157+
# M2M credentials to PAT, even for auth_type='databricks-oauth'; azure-sp-m2m
158+
# is resolved before that branch and stays OAuth.
159+
routed_to_pat = (
160+
bool(kwargs.get("access_token"))
161+
and kwargs.get("auth_type") != "azure-sp-m2m"
162+
and not kwargs.get("oauth_jwt_key_file")
163+
and not (kwargs.get("oauth_client_id") and kwargs.get("oauth_client_secret"))
164+
)
165+
if routed_to_pat or not (kwargs.get("_connection_uri") and server_hostname and oauth):
157166
return
158167
_, name, port = _split_host(server_hostname)
159168
uri = urlsplit(kernel_host)

‎tests/unit/test_session.py‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,11 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12461246
{"_connection_uri": "https://h.io/sql/x"},
12471247
("https://h.io", "/sql/x"),
12481248
), # URI-only
1249+
# Without _port or _connection_uri the host passes through as on main, even if
1250+
# it is not a valid host:port (no port is parsed on the default path).
1251+
("h.io:0", _P, {}, ("h.io:0", _P)),
1252+
("h.io:notaport", _P, {}, ("h.io:notaport", _P)),
1253+
("h.io:", _P, {}, ("h.io:", _P)),
12491254
],
12501255
)
12511256
def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
@@ -1261,8 +1266,8 @@ def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
12611266
("h.io", {"_port": "abc"}, "Invalid _port"),
12621267
("h.io", {"_port": True}, "Invalid _port"),
12631268
("h.io", {"_port": 8443.5}, "Invalid _port"),
1264-
("h.io:notaport", {}, "malformed"),
1265-
("h.io:0", {}, "out of range"),
1269+
("h.io:notaport", {"_port": 8443}, "malformed"),
1270+
("h.io:0", {"_port": 8443}, "out of range"),
12661271
# AC-P3: a supplied port must agree with a port embedded in server_hostname.
12671272
("h.io:7000", {"_port": 8443}, "Conflicting ports"),
12681273
("h.io:8443", {"_port": 443}, "Conflicting ports"),
@@ -1318,6 +1323,36 @@ def test_kernel_host_and_path_rejects(server, kwargs, message):
13181323
"https://evil.io/sql/x",
13191324
False,
13201325
), # PAT is not guarded
1326+
# The auth bridge routes access_token with no M2M/JWT credentials to PAT,
1327+
# even when auth_type is databricks-oauth; azure-sp-m2m and M2M/JWT stay guarded.
1328+
(
1329+
"h.io",
1330+
{"auth_type": "databricks-oauth", "access_token": "dapi"},
1331+
"https://evil.io/sql/x",
1332+
False,
1333+
),
1334+
(
1335+
"h.io",
1336+
{"auth_type": "azure-sp-m2m", "access_token": "dapi"},
1337+
"https://evil.io/sql/x",
1338+
True,
1339+
),
1340+
(
1341+
"h.io",
1342+
{"oauth_jwt_key_file": "/k.pem", "access_token": "dapi"},
1343+
"https://evil.io/sql/x",
1344+
True,
1345+
),
1346+
(
1347+
"h.io",
1348+
{
1349+
"oauth_client_id": "c",
1350+
"oauth_client_secret": "s",
1351+
"access_token": "dapi",
1352+
},
1353+
"https://evil.io/sql/x",
1354+
True,
1355+
),
13211356
(
13221357
"",
13231358
{"auth_type": "databricks-oauth"},

0 commit comments

Comments
 (0)