This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new 57ba8675 [SPARK-61472][CONNECT][PYTHON] Parse the host and port of a 
Spark Connect URL as urllib does, and raise SparkConnectException for a too 
long user_agent
57ba8675 is described below

commit 57ba8675fd8318a4afbb5158e1aea6c6b4299b07
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 09:42:15 2026 +0900

    [SPARK-61472][CONNECT][PYTHON] Parse the host and port of a Spark Connect 
URL as urllib does, and raise SparkConnectException for a too long user_agent
    
    ### What changes were proposed in this pull request?
    
    The host and the port of a Spark Connect URL (`sc://...`) are now read as 
`urllib.parse` reads them, with the `urlsplit` checks that PySpark's channel 
builder runs on the netloc. A `user_agent` longer than 2048 characters after 
URL escaping now raises `SparkConnectException`, as PySpark does.
    
    In `crates/spark-connect-core/src/channel.rs`:
    - The host is what follows the last ``, so a userinfo such as `user` or 
`user:pw` is dropped (`_hostinfo`).
    - A bracketed host ends at its `]`, may be followed only by nothing or 
`:port`, and must be an IPv6 address (with an optional zone) or an IPvFuture; 
an IPv4 address or a name in brackets is an error (`_check_bracketed_netloc`, 
`_check_bracketed_host`).
    - Otherwise the host ends at the first `:`, so `a:1:2` has the bad port 
`1:2`.
    - The `urlsplit` checks run before the path and the params are read, as in 
`urllib`; the missing-host check and the port come after the params, as before.
    - The quoted text of the bad-port and bad-host messages has its secrets 
redacted with the existing `redact_url`, so `sc://localhost:x;token=SECRET` 
shows `'x;token=<redacted>'`.
    
    ### Why are the changes needed?
    
    The client accepted URLs that PySpark 4.3.0 rejects, and misread some valid 
ones:
    
    | URL | Before | PySpark 4.3.0 (and after) |
    |---|---|---|
    | `sc://localhost:15012:1/` | created; the RPC then retries forever | 
`ValueError: Port could not be cast to integer value as '15012:1'` |
    | `sc://a:1:2` | created | `ValueError: Port could not be cast to integer 
value as '1:2'` |
    | `sc://userlocalhost:15012/;token=SECRET` | `PySparkRuntimeError: Failed 
to configure TLS` (host `userlocalhost`) | connects to `localhost` in plaintext 
|
    | `sc://user:pw:15002` | created | `PySparkValueError [INVALID_CONNECT_URL] 
... Hostname is missing ...` |
    | `sc://[::1]x`, `sc://[::1]x:15002` | created | `ValueError: Invalid IPv6 
URL` |
    | `sc://[1.2.3.4]:15002` | created | `ValueError: An IPv4 address cannot be 
in brackets` |
    | `sc://[localhost]:15002` | created | `ValueError: 'localhost' does not 
appear to be an IPv4 or IPv6 address` |
    
    A `user_agent` of more than 2048 characters raised `PySparkRuntimeError`, 
where PySpark raises `SparkConnectException` with the same message. This is a 
follow-up of SPARK-61389 (#479) and SPARK-61397 (#483).
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. Connection strings that PySpark rejects are now rejected with 
PySpark's error instead of connecting or retrying forever, and a URL with a 
userinfo connects as PySpark does. A too long `user_agent` now raises 
`SparkConnectException`.
    
    ### How was this patch tested?
    
    - Rust unit tests in `channel.rs`, `authority_is_read_as_urllib_reads_it` 
(accepted hosts and ports, and 14 error messages including the redaction) and 
`a_too_long_user_agent_is_a_connect_exception`, both failing without this 
change.
    - Offline Python tests in `python/tests/test_dropin_offline.py`, 
`test_create_raises_for_an_authority_that_urllib_rejects_as_pyspark` and 
`test_create_raises_for_a_too_long_user_agent_as_pyspark`, both failing without 
this change.
    - A probe of 25 URLs through the reference client and this one: the same 
exception type and message for each. The missing-host message still quotes the 
`sc://` URL where PySpark quotes its rewritten `http://` URL, as before.
    - The offline suite (1837 passed), the Python e2e suite against a Spark 
4.3.0 server (261 passed), the Rust e2e integration and session-catalog suites, 
the Rust library tests, the `--no-run` builds (with and without `wasm-udf`), 
`cargo fmt --check`, `scripts/audit_no_stubs.sh` and the ruff gates.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    This pull request and its description were written by Isaac.
    
    Closes #528 from HyukjinKwon/SPARK-61472-url-authority.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 crates/spark-connect-core/src/channel.rs | 228 +++++++++++++++++++++++++++----
 python/tests/test_dropin_offline.py      |  46 +++++++
 2 files changed, 244 insertions(+), 30 deletions(-)

diff --git a/crates/spark-connect-core/src/channel.rs 
b/crates/spark-connect-core/src/channel.rs
index 09dc9251..70d037e4 100644
--- a/crates/spark-connect-core/src/channel.rs
+++ b/crates/spark-connect-core/src/channel.rs
@@ -100,6 +100,8 @@ impl ChannelBuilder {
         let auth_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
         let authority = &rest[..auth_end];
         let tail = &rest[auth_end..];
+        // urlsplit checks the netloc before the path and the params are read.
+        let (host_text, port_text) = split_hostinfo(authority)?;
 
         // Path is the tail up to any query/fragment.
         let path_end = tail.find(['?', '#']).unwrap_or(tail.len());
@@ -144,7 +146,7 @@ impl ChannelBuilder {
             }
         }
 
-        let (host_raw, port) = parse_authority(authority, &redact_url(url))?;
+        let (host_raw, port) = parse_authority(host_text, port_text, 
&redact_url(url))?;
         // urllib lowercases the hostname.
         let hostname = host_raw.to_ascii_lowercase();
         let host = if hostname.contains(':') {
@@ -309,7 +311,7 @@ impl ChannelBuilder {
             .unwrap_or_else(|| "_SPARK_CONNECT_PYTHON".to_string());
         let ua_len = quote_len(&ua);
         if ua_len > 2048 {
-            return Err(SparkError::connect_msg(format!(
+            return Err(SparkError::connect_exception_msg(format!(
                 "'user_agent' parameter should not exceed 2048 characters 
after URL \
                  escaping, found {ua_len} characters."
             )));
@@ -451,11 +453,81 @@ fn redact_url(url: &str) -> String {
         .join(";")
 }
 
-/// Parse `host`, `host:port`, `[ipv6]`, or `[ipv6]:port` into (host, port). 
`shown_url` is the
-/// URL for the messages, with its secrets redacted.
-fn parse_authority(authority: &str, shown_url: &str) -> Result<(String, u16)> {
-    let missing_host = || {
-        SparkError::value(
+/// The host and the port texts of a netloc, as `urllib.parse` splits them 
(`urlsplit` and
+/// `_hostinfo`): the host is what follows the last `@`, a bracketed host ends 
at its `]`, and
+/// otherwise the host ends at the first `:`. The checks are those of 
`urlsplit`, which raises
+/// their ValueErrors before the path is read. The messages quote the text 
with its secrets
+/// redacted.
+fn split_hostinfo(authority: &str) -> Result<(&str, Option<&str>)> {
+    // A `[` and a `]` come in pairs in the netloc, the userinfo included.
+    if authority.contains('[') != authority.contains(']') {
+        return Err(SparkError::builtin_value_msg("Invalid IPv6 URL"));
+    }
+    let hostinfo = authority
+        .rsplit_once('@')
+        .map_or(authority, |(_, hostinfo)| hostinfo);
+    let (host, port) = match hostinfo.split_once('[') {
+        Some((before, bracketed)) => {
+            // `_check_bracketed_netloc`: no data before the bracket, nor 
after it but a port.
+            let (addr, after) = 
bracketed.split_once(']').unwrap_or((bracketed, ""));
+            if !before.is_empty() || !(after.is_empty() || 
after.starts_with(':')) {
+                return Err(SparkError::builtin_value_msg("Invalid IPv6 URL"));
+            }
+            (addr, after.strip_prefix(':'))
+        }
+        None => match hostinfo.split_once(':') {
+            Some((host, port)) => (host, Some(port)),
+            None => (hostinfo, None),
+        },
+    };
+    // A netloc with brackets checks its host as an address, bracketed or not.
+    if authority.contains('[') {
+        check_bracketed_host(host)?;
+    }
+    // An empty port is no port.
+    Ok((host, port.filter(|port| !port.is_empty())))
+}
+
+/// `urllib`'s check of the host of a netloc with brackets, as 
`ipaddress.ip_address` makes it: an
+/// IPv6 address (with a zone), or an IPvFuture (`v` hex digits `.` text), but 
no IPv4 address.
+fn check_bracketed_host(host: &str) -> Result<()> {
+    if let Some(future) = host.strip_prefix('v') {
+        let hex = future.bytes().take_while(u8::is_ascii_hexdigit).count();
+        let text = future[hex..].strip_prefix('.');
+        return match text {
+            Some(text) if hex > 0 && !text.is_empty() && !text.contains('\n') 
=> Ok(()),
+            _ => Err(SparkError::builtin_value_msg(
+                "IPvFuture address is invalid",
+            )),
+        };
+    }
+    if host.parse::<std::net::Ipv4Addr>().is_ok() {
+        return Err(SparkError::builtin_value_msg(
+            "An IPv4 address cannot be in brackets",
+        ));
+    }
+    let ipv6 = match host.split_once('%') {
+        // A zone is the text after the `%`, which may not be empty or hold 
another `%`.
+        Some((addr, zone)) => {
+            !zone.is_empty() && !zone.contains('%') && 
addr.parse::<std::net::Ipv6Addr>().is_ok()
+        }
+        None => host.parse::<std::net::Ipv6Addr>().is_ok(),
+    };
+    if ipv6 {
+        Ok(())
+    } else {
+        Err(SparkError::builtin_value_msg(format!(
+            "{} does not appear to be an IPv4 or IPv6 address",
+            python_repr(&redact_url(host))
+        )))
+    }
+}
+
+/// The host and the port of a connection string, once its params are read: 
the host must not be
+/// empty (urllib's `hostname`), and the port is the one of the netloc, or the 
default.
+fn parse_authority(host: &str, port: Option<&str>, shown_url: &str) -> 
Result<(String, u16)> {
+    if host.is_empty() {
+        return Err(SparkError::value(
             "INVALID_CONNECT_URL",
             &[(
                 "detail",
@@ -464,42 +536,25 @@ fn parse_authority(authority: &str, shown_url: &str) -> 
Result<(String, u16)> {
                      to follow the correct format, e.g., 'sc://hostname:port'."
                 ),
             )],
-        )
-    };
-
-    let (host, port_str) = if let Some(bracket_end) = 
authority.strip_prefix('[') {
-        // IPv6: [addr] or [addr]:port
-        let close = bracket_end.find(']').ok_or_else(missing_host)?;
-        let addr = &bracket_end[..close];
-        let after = &bracket_end[close + 1..];
-        let port = after.strip_prefix(':');
-        (addr.to_string(), port)
-    } else if let Some((h, p)) = authority.rsplit_once(':') {
-        (h.to_string(), Some(p))
-    } else {
-        (authority.to_string(), None)
-    };
-
-    if host.is_empty() {
-        return Err(missing_host());
+        ));
     }
 
-    let port = match port_str {
-        None | Some("") => DEFAULT_PORT,
+    let port = match port {
+        None => DEFAULT_PORT,
         Some(p) => parse_port(p)?,
     };
 
-    Ok((host, port))
+    Ok((host.to_string(), port))
 }
 
 /// The port of a connection string as `urllib.parse`'s `port` reads it: ASCII 
digits, within
 /// 0-65535. Its messages are those of its ValueError, which the reference 
client raises
-/// unwrapped and without the URL.
+/// unwrapped and without the URL; the text is quoted with its secrets 
redacted.
 fn parse_port(text: &str) -> Result<u16> {
     if !text.bytes().all(|b| b.is_ascii_digit()) {
         return Err(SparkError::builtin_value_msg(format!(
             "Port could not be cast to integer value as {}",
-            python_repr(text)
+            python_repr(&redact_url(text))
         )));
     }
     // Saturating, so that a long run of digits is out of range rather than 
wrapping around.
@@ -771,6 +826,119 @@ mod tests {
         }
     }
 
+    #[test]
+    fn authority_is_read_as_urllib_reads_it() {
+        // The host (lowercased, a bracketed IPv6 address wrapped in `[ ]`) 
and the port of the
+        // netloc: the host follows the last `@`, a bracketed host ends at its 
`]`, and otherwise
+        // the host ends at the first `:`.
+        for (url, host, port) in [
+            (
+                "sc://user@localhost:15012/;token=SECRET",
+                "localhost",
+                15012,
+            ),
+            ("sc://user:pw@localhost:15012/", "localhost", 15012),
+            ("sc://u]@[::1]:15002", "[::1]", 15002),
+            ("sc://[::1]:15012/", "[::1]", 15012),
+            ("sc://LOCALHOST:15012/", "localhost", 15012),
+            ("sc://[v1.x]:15002", "v1.x", 15002),
+            ("sc://[fe80::1%25eth0]:15002", "[fe80::1%25eth0]", 15002),
+            ("sc://[::1]:/", "[::1]", DEFAULT_PORT),
+        ] {
+            let c = ChannelBuilder::parse(url).unwrap();
+            assert_eq!(c.host(), host, "{url}");
+            assert_eq!(c.port(), port, "{url}");
+        }
+        // The messages are the ValueErrors of urllib, with the secrets of the 
URL redacted.
+        for (url, message) in [
+            (
+                "sc://localhost:15012:1/",
+                "Port could not be cast to integer value as '15012:1'",
+            ),
+            (
+                "sc://a:1:2",
+                "Port could not be cast to integer value as '1:2'",
+            ),
+            (
+                "sc://localhost:x;token=SECRET",
+                "Port could not be cast to integer value as 
'x;token=<redacted>'",
+            ),
+            (
+                "sc://localhost:15002;token=SECRET",
+                "Port could not be cast to integer value as 
'15002;token=<redacted>'",
+            ),
+            ("sc://[::1]x", "Invalid IPv6 URL"),
+            ("sc://[::1]x:15002", "Invalid IPv6 URL"),
+            (
+                "sc://]@[x",
+                "'x' does not appear to be an IPv4 or IPv6 address",
+            ),
+            (
+                "sc://[::1]@host:15002",
+                "'host' does not appear to be an IPv4 or IPv6 address",
+            ),
+            (
+                "sc://[::1@x]",
+                "'x]' does not appear to be an IPv4 or IPv6 address",
+            ),
+            (
+                "sc://[1.2.3.4]:15002",
+                "An IPv4 address cannot be in brackets",
+            ),
+            (
+                "sc://[localhost]:15002",
+                "'localhost' does not appear to be an IPv4 or IPv6 address",
+            ),
+            (
+                "sc://[]:15002",
+                "'' does not appear to be an IPv4 or IPv6 address",
+            ),
+            ("sc://[vz]:15002", "IPvFuture address is invalid"),
+            (
+                "sc://[a;token=SECRET]:1",
+                "'a;token=<redacted>' does not appear to be an IPv4 or IPv6 
address",
+            ),
+        ] {
+            let e = ChannelBuilder::parse(url).unwrap_err();
+            assert_eq!(
+                e.kind,
+                crate::error::SparkErrorKind::BuiltinValueError,
+                "{url}"
+            );
+            assert_eq!(e.message(), message, "{url}");
+            assert!(!e.message().contains("SECRET"), "{url}");
+        }
+        // A missing host is the INVALID_CONNECT_URL of the reference client, 
read after params.
+        let e = ChannelBuilder::parse("sc://user:pw@:15002").unwrap_err();
+        assert_eq!(e.kind, crate::error::SparkErrorKind::ValueError);
+        assert!(
+            e.message().contains("Hostname is missing"),
+            "{}",
+            e.message()
+        );
+    }
+
+    #[test]
+    fn a_too_long_user_agent_is_a_connect_exception() {
+        // PySpark raises a SparkConnectException of this message, not a 
RuntimeError.
+        let _env = set_env(&[("SPARK_CONNECT_USER_AGENT", None)]);
+        let url = |len: usize| format!("sc://localhost/;user_agent={}", 
"a".repeat(len));
+        assert!(ChannelBuilder::parse(&url(2048))
+            .unwrap()
+            .user_agent()
+            .is_ok());
+        let e = ChannelBuilder::parse(&url(2049))
+            .unwrap()
+            .user_agent()
+            .unwrap_err();
+        assert_eq!(e.kind, crate::error::SparkErrorKind::ConnectMessage);
+        assert_eq!(
+            e.message(),
+            "'user_agent' parameter should not exceed 2048 characters after 
URL escaping, \
+             found 2049 characters."
+        );
+    }
+
     #[test]
     fn user_agent_names_the_os_as_pyspark() {
         // As Python's `platform.uname().system.lower()`.
diff --git a/python/tests/test_dropin_offline.py 
b/python/tests/test_dropin_offline.py
index 0f4891bf..ca74ad35 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -3668,6 +3668,52 @@ def 
test_create_raises_for_a_port_that_urllib_rejects_as_pyspark():
         assert str(error.value) == message, url
 
 
+def test_create_raises_for_an_authority_that_urllib_rejects_as_pyspark():
+    # As PySpark's channel builder, whose netloc urlsplit checks before the 
path and the params
+    # are read, and whose port urllib reads: the ValueErrors of urllib, with 
no token in them.
+    from pyspark.sql import SparkSession
+
+    for url, message in [
+        ("sc://a:1:2", "Port could not be cast to integer value as '1:2'"),
+        ("sc://localhost:15012:1/", "Port could not be cast to integer value 
as '15012:1'"),
+        ("sc://localhost:x;token=SECRET",
+         "Port could not be cast to integer value as 'x;token=<redacted>'"),
+        ("sc://[::1]x", "Invalid IPv6 URL"),
+        ("sc://[::1]x:15002", "Invalid IPv6 URL"),
+        ("sc://[1.2.3.4]:15002", "An IPv4 address cannot be in brackets"),
+        ("sc://[localhost]:15002", "'localhost' does not appear to be an IPv4 
or IPv6 address"),
+        ("sc://[a;token=SECRET]:1",
+         "'a;token=<redacted>' does not appear to be an IPv4 or IPv6 address"),
+    ]:
+        with pytest.raises(ValueError) as error:
+            SparkSession.builder.remote(url).create()
+        assert type(error.value) is ValueError, url
+        assert str(error.value) == message, url
+    # The userinfo ends at the last '@', so the host is localhost and the 
token is plaintext, as
+    # urllib reads it: the session is made, and no TLS is configured for it.
+    session = 
SparkSession.builder.remote("sc://user@localhost:15012/;token=SECRET").create()
+    # No server to release on: a release would wait for the retries of a 
refused port.
+    session.release_session_on_close = False
+    session.stop()
+
+
+def test_create_raises_for_a_too_long_user_agent_as_pyspark(monkeypatch):
+    # As PySpark's channel builder: a user agent of more than 2048 characters 
after the URL
+    # escaping raises a SparkConnectException of the message of the reference 
client.
+    from pyspark.errors.exceptions.connect import SparkConnectException
+    from pyspark.sql import SparkSession
+
+    monkeypatch.delenv("SPARK_CONNECT_USER_AGENT", raising=False)
+    with pytest.raises(SparkConnectException) as error:
+        SparkSession.builder.remote("sc://localhost/;user_agent=" + "a" * 
2049).create()
+    assert type(error.value) is SparkConnectException
+    assert str(error.value) == (
+        "'user_agent' parameter should not exceed 2048 characters after URL 
escaping, "
+        "found 2049 characters."
+    )
+    assert error.value.getCondition() is None
+
+
 def 
test_the_plan_of_a_dataframe_that_a_hook_puts_in_is_compressed_as_pyspark(mock_connect):
     # As PySpark's hook, which would put in the plan of `to_proto`, which 
compresses it: the plan
     # of a DataFrame that an on_execute_plan hook puts in is sent compressed 
over the threshold.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to