Skip to content

Commit 6250c28

Browse files
cathleenyIsaac
andcommitted
fix(kernel): sanitize connection-URI parse errors and wrap long messages
urlsplit ran outside the error handling, so a malformed URI whose NFKC-normalized netloc failed raised a ValueError that echoed the raw netloc, password included. The parse is now wrapped and re-raised as a redacted ValueError with from None; a regression row covers it. Long error messages and docstrings are wrapped to the 100-column limit in CONTRIBUTING.md. No validation semantics change. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 8f96c0b commit 6250c28

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎src/databricks/sql/session.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _redact(value: str) -> str:
8282

8383

8484
def _split_host(host: str) -> Tuple[str, str, Optional[int]]:
85-
"""Split ``[scheme://]name[:port]`` into ``(prefix, name, port)``; ``name`` keeps IPv6 brackets."""
85+
"""Split ``[scheme://]name[:port]`` into ``(prefix, name, port)``; keeps IPv6 brackets."""
8686
prefix, sep, text = host.rstrip("/").rpartition("://")
8787
prefix += sep
8888
match = _HOSTPORT_RE.fullmatch(text)
@@ -103,8 +103,14 @@ def _split_host(host: str) -> Tuple[str, str, Optional[int]]:
103103

104104

105105
def _uri_host_and_path(uri: str) -> Tuple[str, str]:
106-
"""Split a ``_connection_uri`` into the kernel host (scheme and authority) and path (with query)."""
107-
parts = urlsplit(uri)
106+
"""Split a ``_connection_uri`` into the kernel host (scheme, authority) and path."""
107+
try:
108+
parts = urlsplit(uri)
109+
except ValueError:
110+
# urlsplit echoes the raw netloc, which can carry credentials; re-raise without it.
111+
raise ValueError(
112+
f"Invalid _connection_uri {_redact(uri)!r}: could not be parsed"
113+
) from None
108114
try:
109115
port = parts.port
110116
except ValueError:
@@ -145,7 +151,8 @@ def _kernel_host_and_path(
145151
if embedded is not None:
146152
if embedded != port:
147153
raise ValueError(
148-
f"Conflicting ports: server_hostname {_redact(server_hostname)!r} carries {embedded} but _port={port}"
154+
f"Conflicting ports: server_hostname {_redact(server_hostname)!r} "
155+
f"carries {embedded} but _port={port}"
149156
)
150157
return server_hostname, http_path
151158
if port == 443:
@@ -187,8 +194,10 @@ def _check_oauth_origin(server_hostname: str, kernel_host: str, kwargs: dict) ->
187194
port or 443,
188195
):
189196
raise ValueError(
190-
f"OAuth requires the _connection_uri origin to match https://{_redact(server_hostname)}; "
191-
f"the kernel would send the credentials to {_redact(kernel_host)}. Use a PAT or match the origin."
197+
f"OAuth requires the _connection_uri origin to match "
198+
f"https://{_redact(server_hostname)}; "
199+
f"the kernel would send the credentials to {_redact(kernel_host)}. "
200+
"Use a PAT or match the origin."
192201
)
193202

194203

‎tests/unit/test_session.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,8 @@ def test_oauth_origin_guard(server, kwargs, uri, should_raise):
13901390
[
13911391
("https://user:secret@h.io/sql/x", "secret"),
13921392
("https://h.io:8443?token=secret", "secret"),
1393+
# A fullwidth solidus makes urlsplit's NFKC check raise with the raw netloc in its text.
1394+
("https://user:secret@h.io/sql/x", "secret"),
13931395
],
13941396
)
13951397
def test_invalid_connection_uri_message_omits_credentials(uri, secret):

0 commit comments

Comments
 (0)