Skip to content

Commit 233f2b5

Browse files
cathleenyIsaac
andcommitted
fix(kernel): report an uncomparable server_hostname as an OAuth origin mismatch
With OAuth and _connection_uri, the origin guard parsed server_hostname with the _port parser, so a host such as '::1', 'h.io:' or 'user:pass@h.io' raised 'malformed IPv6 literal or port' even though no _port was set. The guard now treats a server_hostname it cannot parse as an origin that cannot match, and raises its own origin-mismatch error. The changelog entry no longer says 'as on main'. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 81c1cff commit 233f2b5

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
# Unreleased
4-
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. `_port` sends the connection to that port (default 443 adds nothing); `_connection_uri` (`http(s)://host[:port]/path?query`) sets host, port, path and query and wins over `_port`; URI-only connections work. A port embedded in `server_hostname` wins over a differing `_port`, with a warning, as on main. Invalid ports, an empty `server_hostname` with `_port`, and URIs without a scheme or path raise `ValueError`. With OAuth, a `_connection_uri` origin that differs from `server_hostname` raises. With OAuth and a non-default `_port`, the token exchange goes to the same host:port, unlike Thrift (documented, not guarded).
4+
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. `_port` sends the connection to that port (default 443 adds nothing); `_connection_uri` (`http(s)://host[:port]/path?query`) sets host, port, path and query and wins over `_port`; URI-only connections work. A port embedded in `server_hostname` wins over a differing `_port`, with a warning, as it did before. Invalid ports, an empty `server_hostname` with `_port`, and URIs without a scheme or path raise `ValueError`. With OAuth, a `_connection_uri` origin that differs from `server_hostname` raises. With OAuth and a non-default `_port`, the token exchange goes to the same host:port, unlike Thrift (documented, not guarded).
55
- Honor `max_download_threads` on kernel connections, with a default download window of 16 and explicit chunk-setting overrides. Invalid values raise `ValueError` when connecting with `use_kernel=True`.
66
- Fix the kernel backend ignoring `use_cloud_fetch=False`, so complete results can be fetched without cloud-storage downloads.
77
- Fix inline parameters (`use_inline_params=True`) so a sequence directly after `IN` renders as a list again: `x IN %(ids)s` renders as `x IN (1,2,3)` instead of `x IN ARRAY(1,2,3)`, which Spark rejects. Inside the list, tuples render as rows for a multi-column `IN`, and a `set` or `frozenset` is accepted again. Sequences anywhere else still render as `ARRAY(...)` ([#629](https://github.com/databricks/databricks-sql-python/issues/629))

‎src/databricks/sql/session.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,15 @@ def _check_oauth_origin(server_hostname: str, kernel_host: str, kwargs: dict) ->
254254
)
255255
if routed_to_pat or not oauth:
256256
return
257-
_, name, port = _split_host(server_hostname)
257+
try:
258+
_, name, port = _split_host(server_hostname)
259+
server_host = urlsplit(f"//{name}").hostname or ""
260+
except ValueError:
261+
# A server_hostname with no comparable origin (a bare IPv6 literal, an empty port,
262+
# credentials) cannot match the URI origin, so it is reported as an origin mismatch
263+
# rather than as a malformed host.
264+
server_host, port = "", None
258265
uri = urlsplit(kernel_host)
259-
server_host = urlsplit(f"//{name}").hostname or ""
260266
if (uri.scheme, uri.hostname or "", uri.port or 443) != (
261267
"https",
262268
server_host,

‎tests/unit/test_session.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,16 @@ def test_thrift_receives_host_and_port_unchanged(mock_client_class, server, port
15041504
None,
15051505
False,
15061506
), # documented, not guarded
1507+
# A server_hostname with no comparable origin is an origin mismatch, not a
1508+
# "malformed host" error.
1509+
("::1", {"auth_type": "databricks-oauth"}, "https://h.io/sql/x", True),
1510+
("h.io:", {"auth_type": "databricks-oauth"}, "https://h.io/sql/x", True),
1511+
(
1512+
"user:pass@h.io",
1513+
{"auth_type": "databricks-oauth"},
1514+
"https://h.io/sql/x",
1515+
True,
1516+
),
15071517
],
15081518
)
15091519
def test_oauth_origin_guard(server, kwargs, uri, should_raise):

0 commit comments

Comments
 (0)