Skip to content

Commit 81c1cff

Browse files
cathleenyIsaac
andcommitted
fix(kernel): pass the host through unchanged for _port=443
An explicit _port=443 adds nothing to the kernel host, but it still ran the checks for a folded port, so a host that main sent through as-is was now rejected: a path or query (`h.io/x`, `h.io?x=1`), a bare IPv6 literal (`::1`), userinfo (`user:pw@h`), or an empty host. 443 now returns the host unchanged before those checks, as on main and as the Node driver does. A non-default _port is unchanged. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 973b4e2 commit 81c1cff

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/databricks/sql/session.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def _kernel_host_and_path(
173173
if kwargs.get("_connection_uri"):
174174
return _uri_host_and_path(kwargs["_connection_uri"])
175175
port = _port_or_none(kwargs.get("_port"))
176-
if port is None:
176+
# The default 443 adds nothing, so the host is passed through unchanged, as on main,
177+
# before any of the checks below (a path, userinfo or bare IPv6 host keeps working).
178+
if port is None or port == 443:
177179
return server_hostname, http_path
178180
if not server_hostname:
179181
raise ValueError(
@@ -192,8 +194,7 @@ def _kernel_host_and_path(
192194
if embedded is not None:
193195
# A port embedded in server_hostname always wins, as on main (the kernel
194196
# connected to it); a differing _port is ignored with a warning, not an error.
195-
# The default 443 is not a choice worth warning about, so it is never flagged.
196-
if port != 443 and embedded != port:
197+
if embedded != port:
197198
logger.warning(
198199
"server_hostname %s carries port %d, which differs from _port=%d; "
199200
"connecting to port %d. Set the port in one place only.",
@@ -203,8 +204,6 @@ def _kernel_host_and_path(
203204
embedded,
204205
)
205206
return server_hostname, http_path
206-
if port == 443:
207-
return server_hostname, http_path
208207
return f"{prefix}{name}:{port}", http_path
209208

210209

‎tests/unit/test_session.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,11 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12661266
("h.io:0", _P, {}, ("h.io:0", _P)),
12671267
("h.io:notaport", _P, {}, ("h.io:notaport", _P)),
12681268
("h.io:", _P, {}, ("h.io:", _P)),
1269+
# An explicit _port of the default 443 adds nothing, so the host passes through
1270+
# unchanged as on main, before any path, userinfo or IPv6 check (aligned with Node).
1271+
("h.io/x", _P, {"_port": 443}, ("h.io/x", _P)),
1272+
("h.io?x=1", _P, {"_port": 443}, ("h.io?x=1", _P)),
1273+
("", _P, {"_port": 443}, ("", _P)),
12691274
],
12701275
)
12711276
def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
@@ -1283,18 +1288,16 @@ def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
12831288
("h.io", {"_port": 8443.5}, "Invalid _port"),
12841289
("h.io:notaport", {"_port": 8443}, "malformed"),
12851290
("h.io:0", {"_port": 8443}, "out of range"),
1286-
# An explicit _port with a path, query or fragment is rejected, even when the
1287-
# path looks like a port ('h.io/path:8443') or the port is the default (443).
1291+
# A non-default _port with a path, query or fragment is rejected, even when the
1292+
# path looks like a port ('h.io/path:8443').
12881293
("h.io/x", {"_port": 8443}, "path, query or fragment"),
12891294
("h.io/path:8443", {"_port": 8443}, "path, query or fragment"),
1290-
("h.io/x", {"_port": 443}, "path, query or fragment"),
12911295
("h.io?x=1", {"_port": 8443}, "path, query or fragment"),
12921296
("h.io#x", {"_port": 8443}, "path, query or fragment"),
12931297
("https://h.io/x", {"_port": 8443}, "path, query or fragment"),
12941298
("h.io/x/", {"_port": 8443}, "path, query or fragment"),
12951299
# R11: _port needs a server_hostname unless _connection_uri carries the host.
12961300
("", {"_port": 8443}, "server_hostname is required"),
1297-
("", {"_port": 443}, "server_hostname is required"),
12981301
# AC-P3: _connection_uri must be http(s) with a host and a warehouse path.
12991302
("h", {"_connection_uri": "d.io/sql/x"}, "Invalid _connection_uri"),
13001303
("h", {"_connection_uri": "ftp://h.io/sql/x"}, "Invalid _connection_uri"),
@@ -1364,6 +1367,10 @@ def test_embedded_port_equal_to_explicit_port_does_not_warn(caplog):
13641367
("h:8443/", {"_port": 8443}, ("h:8443/", _P)),
13651368
("h/x", {}, ("h/x", _P)),
13661369
("h?q", {}, ("h?q", _P)),
1370+
# With the default _port=443 these pass through unchanged, as on main.
1371+
("::1", {"_port": 443}, ("::1", _P)),
1372+
("user:pass@h", {"_port": 443}, ("user:pass@h", _P)),
1373+
("https://user:pass@h:8443", {"_port": 443}, ("https://user:pass@h:8443", _P)),
13671374
],
13681375
)
13691376
def test_kernel_host_and_path_stdlib_edges_accept(server, kwargs, expected):
@@ -1378,7 +1385,6 @@ def test_kernel_host_and_path_stdlib_edges_accept(server, kwargs, expected):
13781385
("[::1", {"_port": 8443}, "malformed"),
13791386
# Bare IPv6 is not supported with _port: urlsplit reads it as a malformed port.
13801387
("::1", {"_port": 8443}, "malformed"),
1381-
("::1", {"_port": 443}, "malformed"),
13821388
("2001:db8::1", {"_port": 8443}, "malformed"),
13831389
("h:abc", {"_port": 8443}, "malformed"),
13841390
# A colon in userinfo is not a port, but a credential-bearing host is never folded.

0 commit comments

Comments
 (0)