Skip to content

Commit bc7c09e

Browse files
committed
Reject a path, query or fragment host combined with _port; document kernel port rules
Check the host for a path, query or fragment before the port is parsed, so 'h.io/path:8443' with _port cannot read as port 8443. Only explicit _port is affected; a host without _port passes through unchanged, as on main. Add Thrift tests pinning that host and _port reach ThriftDatabricksClient unchanged. Update CONNECTION_PARAMETERS.md: _port and _connection_uri are now kernel options, with the embedded-port and falsy rules and the OAuth origin rule. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent c90459f commit bc7c09e

3 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎CONNECTION_PARAMETERS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ to change without notice.
7373
| ----------------- | ----- | :----: | :----: | ------------- | ----------------------------------------------------------------------------------------------------------------------- |
7474
| `server_hostname` | `str` | ✅ | ✅ | — (required) | Workspace hostname, e.g. `dbc-12345.cloud.databricks.com`. |
7575
| `http_path` | `str` | ✅ | ✅ | — (required) | Thrift accepts a SQL-warehouse path **or** an all-purpose-cluster path; Kernel requires a warehouse/endpoint path. |
76-
| `_port` | `int` | ✅ | — | `443` | TCP port (advanced). Not threaded to the kernel; it derives host/port from `server_hostname` + `http_path`. |
77-
| `_connection_uri` | `str` | ✅ | — | `None` | Thrift-only internal override of `server_hostname`/`http_path`. No kernel equivalent. |
76+
| `_port` | `int` | ✅ | ✅ | `443` | TCP port (advanced). On Kernel a falsy value means the default, and a port embedded in `server_hostname` wins over a differing `_port` (with a warning). |
77+
| `_connection_uri` | `str` | ✅ | ✅ | `None` | Overrides `server_hostname`, `_port` and `http_path` on both, and wins over `_port`. On Kernel with OAuth, the URI origin must match `server_hostname`, and OAuth uses the same host:port as the connection (Thrift keeps OAuth on the bare host). |
7878
| `http_headers` | `List[Tuple[str, str]]` | ✅ | ⚠️ | `None` | Extra headers sent on requests. Kernel filters `Authorization` and `x-databricks-org-id` because it manages them itself; other headers are forwarded. |
7979
| `user_agent_entry`| `str` | ✅ | ✅ | `None` | Custom tag folded into the composed `User-Agent` on both. (`_user_agent_entry` is a deprecated alias that warns.) |
8080

‎src/databricks/sql/session.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ def _kernel_host_and_path(
155155
"server_hostname is required on use_kernel=True when _port is set "
156156
"(or pass _connection_uri, which carries its own host)"
157157
)
158+
# Checked before the port is parsed: 'h.io/path:8443' would otherwise read as host
159+
# 'h.io/path' with port 8443. Only the text after any scheme is checked.
160+
authority = server_hostname.rstrip("/").rpartition("://")[2]
161+
if any(c in authority for c in "/?#"):
162+
raise ValueError(
163+
f"Invalid server_hostname {_redact(server_hostname)!r}: a path, query or "
164+
"fragment cannot be combined with _port; put the port in server_hostname"
165+
)
158166
prefix, name, embedded = _split_host(server_hostname)
159167
if embedded is not None:
160168
# A port embedded in server_hostname always wins, as on main (the kernel
@@ -171,13 +179,6 @@ def _kernel_host_and_path(
171179
return server_hostname, http_path
172180
if port == 443:
173181
return server_hostname, http_path
174-
if any(c in name for c in "/?#"):
175-
# Folding _port in would put it after the path, query or fragment, so the
176-
# port could not be applied; reject rather than silently drop it.
177-
raise ValueError(
178-
f"Invalid server_hostname {_redact(server_hostname)!r}: a path, query or "
179-
"fragment cannot be combined with _port; put the port in server_hostname"
180-
)
181182
if ":" in name and not name.startswith("["):
182183
name = f"[{name}]"
183184
return f"{prefix}{name}:{port}", http_path

‎tests/unit/test_session.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,10 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12191219
("h.io/", _P, {"_port": 8443}, ("h.io:8443", _P)),
12201220
("h.io:8443/", _P, {"_port": 8443}, ("h.io:8443/", _P)),
12211221
("h.io/", _P, {}, ("h.io/", _P)),
1222+
# Without _port, a host with a path passes through unchanged, as on main.
1223+
("h.io/x", _P, {}, ("h.io/x", _P)),
1224+
("https://h.io:8443/", _P, {"_port": 8443}, ("https://h.io:8443/", _P)),
1225+
("https://h.io", _P, {"_port": 8443}, ("https://h.io:8443", _P)),
12221226
("::1", _P, {}, ("::1", _P)),
12231227
("::1", _P, {"_port": 8443}, ("[::1]:8443", _P)),
12241228
("[::1]", _P, {"_port": 8443}, ("[::1]:8443", _P)),
@@ -1277,10 +1281,15 @@ def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
12771281
("h.io", {"_port": 8443.5}, "Invalid _port"),
12781282
("h.io:notaport", {"_port": 8443}, "malformed"),
12791283
("h.io:0", {"_port": 8443}, "out of range"),
1280-
# An explicit _port cannot be folded in after a path, query or fragment.
1284+
# An explicit _port with a path, query or fragment is rejected, even when the
1285+
# path looks like a port ('h.io/path:8443') or the port is the default (443).
12811286
("h.io/x", {"_port": 8443}, "path, query or fragment"),
1287+
("h.io/path:8443", {"_port": 8443}, "path, query or fragment"),
1288+
("h.io/x", {"_port": 443}, "path, query or fragment"),
12821289
("h.io?x=1", {"_port": 8443}, "path, query or fragment"),
12831290
("h.io#x", {"_port": 8443}, "path, query or fragment"),
1291+
("https://h.io/x", {"_port": 8443}, "path, query or fragment"),
1292+
("h.io/x/", {"_port": 8443}, "path, query or fragment"),
12841293
# R11: _port needs a server_hostname unless _connection_uri carries the host.
12851294
("", {"_port": 8443}, "server_hostname is required"),
12861295
("", {"_port": 443}, "server_hostname is required"),
@@ -1331,6 +1340,33 @@ def test_embedded_port_equal_to_explicit_port_does_not_warn(caplog):
13311340
assert "differs from _port" not in caplog.text
13321341

13331342

1343+
@pytest.mark.parametrize(
1344+
"server, port",
1345+
[
1346+
("h.io:8443", 443),
1347+
("h.io", 8443),
1348+
("h.io/x", 8443),
1349+
("h.io", ""),
1350+
],
1351+
)
1352+
@patch("databricks.sql.session.ThriftDatabricksClient")
1353+
def test_thrift_receives_host_and_port_unchanged(mock_client_class, server, port):
1354+
# Thrift is unchanged: the host and _port reach ThriftDatabricksClient verbatim, and
1355+
# ThriftDatabricksClient applies its own `port or 443` to the URL. The kernel-only
1356+
# folding above does not apply here.
1357+
connection = databricks.sql.connect(
1358+
server_hostname=server,
1359+
http_path=_P,
1360+
access_token="tok",
1361+
enable_telemetry=False,
1362+
_port=port,
1363+
)
1364+
call_kwargs = mock_client_class.call_args.kwargs
1365+
assert call_kwargs["server_hostname"] == server
1366+
assert call_kwargs["port"] == port
1367+
connection.close()
1368+
1369+
13341370
@pytest.mark.parametrize(
13351371
"server, kwargs, uri, should_raise",
13361372
[

0 commit comments

Comments
 (0)