Skip to content

Commit e0e90c1

Browse files
ai: apply changes for #915 (1 review thread)
Addresses: - #3802255225 at src/databricks/sql/session.py:93 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent a699cb6 commit e0e90c1

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/databricks/sql/session.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ def _kernel_host_and_path(
9090
# present, so add a temporary one when the host is scheme-less.
9191
host = server_hostname.rstrip("/")
9292
probe = host if "://" in host else "https://" + host
93-
if urlsplit(probe).port is None:
93+
try:
94+
existing_port = urlsplit(probe).port
95+
except ValueError as exc:
96+
# ``SplitResult.port`` raises when the authority carries a
97+
# non-numeric or out-of-range port. Re-raise as a clear,
98+
# connector-side error consistent with the ``_connection_uri``
99+
# validation above, instead of leaking an opaque URL-parsing error.
100+
raise ValueError(
101+
"Invalid server_hostname {!r}: could not parse its port "
102+
"({})".format(server_hostname, exc)
103+
)
104+
if existing_port is None:
94105
host = "{}:{}".format(host, port)
95106
return host, http_path
96107

tests/unit/test_session.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,12 @@ def test_port_folded_into_ipv6_literal_without_port(self):
834834
def test_port_not_double_appended_for_ipv6_literal_with_port(self):
835835
host, _ = _kernel_host_and_path("[::1]:7000", self.PATH, {"_port": 8443})
836836
assert host == "[::1]:7000"
837+
838+
def test_malformed_port_in_hostname_raises_clear_error(self):
839+
# A non-numeric/out-of-range port on server_hostname makes
840+
# ``SplitResult.port`` raise; surface a clear connector-side error
841+
# instead of leaking an opaque URL-parsing ValueError.
842+
with pytest.raises(ValueError, match="could not parse its port"):
843+
_kernel_host_and_path(
844+
self.HOST + ":notaport", self.PATH, {"_port": 8443}
845+
)

0 commit comments

Comments
 (0)