You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2026/10/16 19:27:29 UTC

(spark-connect-swift) branch main updated: [SPARK-60299] Fix `SparkSession` to reject an invalid `session_id` instead of silently truncating it

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

dongjoon-hyun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-connect-swift.git


The following commit(s) were added to refs/heads/main by this push:
     new 63cf898  [SPARK-60299] Fix `SparkSession` to reject an invalid `session_id` instead of silently truncating it
63cf898 is described below

commit 63cf898f50ca561c27e38b3ba2dc5f109907d616
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Fri Oct 16 12:27:23 2026 -0700

    [SPARK-60299] Fix `SparkSession` to reject an invalid `session_id` instead of silently truncating it
    
    ### What changes were proposed in this pull request?
    
    This PR aims to fix `SparkSession` to reject an invalid `session_id` in the connection string instead of silently truncating it.
    
    - Read the whole `session_id` value (`[^;]*`) instead of stopping at the first character outside `[a-zA-Z0-9-]`.
    - Throw `SparkConnectError.InvalidSessionID` if the value is not a valid UUID.
    
    ### Why are the changes needed?
    
    `;session_id=abc_def` was read as `abc`, and `;session_id=<uuid>_suffix` was accepted as `<uuid>`. Like the Python client, an invalid `session_id` should be an error.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. An invalid `session_id` such as `abc_def` now fails with `InvalidSessionID`. Valid UUIDs behave as before.
    
    ### How was this patch tested?
    
    Added `SparkSessionTests.invalidSessionIDInConnectionString`. It needs no Spark Connect server. I could not run it locally because the `Testing` module is missing, so CI should confirm it.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Sonnet 5.5
    
    Closes #598 from dongjoon-hyun/SPARK-60299.
    
    Authored-by: Dongjoon Hyun <do...@apache.org>
    Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
 Sources/SparkConnect/SparkSession.swift         | 10 +++++++---
 Tests/SparkConnectTests/SparkSessionTests.swift | 12 ++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/Sources/SparkConnect/SparkSession.swift b/Sources/SparkConnect/SparkSession.swift
index ad8ca93..9b33d69 100644
--- a/Sources/SparkConnect/SparkSession.swift
+++ b/Sources/SparkConnect/SparkSession.swift
@@ -42,7 +42,7 @@ public actor SparkSession {
   /// Runtime configuration interface for Spark.
   public let conf: RuntimeConf
 
-  let regexSessionID = /;session_id=([a-zA-Z0-9-]+)/
+  let regexSessionID = /;session_id=([^;]*)/
 
   /// Create a session that uses the specified connection string and userID.
   /// - Parameters:
@@ -55,8 +55,12 @@ public actor SparkSession {
     // Since `Session ID` belongs to `SparkSession`, we handle this here.
     if let sessionID {
       self.sessionID = sessionID
-    } else if connection.contains(regexSessionID) {
-      self.sessionID = connection.firstMatch(of: regexSessionID)!.1.lowercased()
+    } else if let match = connection.firstMatch(of: regexSessionID) {
+      // Reject the whole value instead of silently truncating it, e.g., `abc_def` to `abc`.
+      guard let uuid = UUID(uuidString: String(match.1)) else {
+        throw SparkConnectError.InvalidSessionID
+      }
+      self.sessionID = uuid.uuidString.lowercased()
     } else {
       self.sessionID = UUID().uuidString.lowercased()
     }
diff --git a/Tests/SparkConnectTests/SparkSessionTests.swift b/Tests/SparkConnectTests/SparkSessionTests.swift
index 2d17096..0353f70 100644
--- a/Tests/SparkConnectTests/SparkSessionTests.swift
+++ b/Tests/SparkConnectTests/SparkSessionTests.swift
@@ -109,6 +109,18 @@ struct SparkSessionTests {
     #expect(spark1 == spark2)
   }
 
+  @Test
+  func invalidSessionIDInConnectionString() throws {
+    for invalid in ["abc_def", "not-a-uuid", "\(UUID().uuidString)_suffix"] {
+      #expect(throws: SparkConnectError.InvalidSessionID) {
+        try SparkSession("sc://localhost/;session_id=\(invalid)")
+      }
+    }
+    let uuid = UUID().uuidString
+    let session = try SparkSession("sc://localhost/;session_id=\(uuid);use_ssl=false")
+    #expect(session.sessionID == uuid.lowercased())
+  }
+
   @Test
   func closedSessionID() async throws {
     await SparkSession.builder.clear()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org