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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new a99297ce3 MINOR: [Flight] Fix cookie header parsing in 
ServerSessionMiddleware (#1517)
a99297ce3 is described below

commit a99297ce3ca5c6cd8dc2c5ea3f5541b0788fdd62
Author: Abdul Rawoof Khan <[email protected]>
AuthorDate: Mon Nov 23 21:06:07 2026 +0530

    MINOR: [Flight] Fix cookie header parsing in ServerSessionMiddleware (#1517)
    
    ## What's Changed
    
    `ServerSessionMiddleware.Factory.onCallStarted` splits the `cookie`
    header on `" ;"` (space, then semicolon) and then on every `=`. Clients
    send cookie lists as `name=value; name=value` (RFC 6265, and what
    `ClientCookieMiddleware` and the C++ Flight client produce), so as soon
    as any other cookie is present, for example a load balancer affinity
    cookie, the whole header becomes one token with two or more `=`, the
    loop gives up, and `arrow_flight_session_id` is never found. The call is
    then served without a session and any options set through
    `SetSessionOptions` are silently gone. A session id that itself contains
    `=` (base64 padding) is truncated and rejected with NOT_FOUND.
    
    The parser now splits on `;`, splits each pair at the first `=`, trims,
    strips surrounding double quotes, and skips a malformed entry instead of
    abandoning the rest of the header, which is what the C++
    `ServerSessionMiddlewareFactory::ParseCookieString` does. A header
    holding only `arrow_flight_session_id=<id>` behaves exactly as before.
    
    `TestServerSessionMiddleware` covers the single-cookie, multi-cookie,
    quoted, `=`-containing, malformed, second-header, missing and unknown-id
    cases; four of them fail on `main`.
---
 .../arrow/flight/ServerSessionMiddleware.java      |  18 ++-
 .../arrow/flight/TestServerSessionMiddleware.java  | 139 +++++++++++++++++++++
 2 files changed, 151 insertions(+), 6 deletions(-)

diff --git 
a/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerSessionMiddleware.java
 
b/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerSessionMiddleware.java
index 5ec01b9c8..7584c445a 100644
--- 
a/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerSessionMiddleware.java
+++ 
b/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerSessionMiddleware.java
@@ -82,15 +82,21 @@ public class ServerSessionMiddleware implements 
FlightServerMiddleware {
       final Iterable<String> it = incomingHeaders.getAll("cookie");
       findIdCookie:
       for (final String headerValue : it) {
-        for (final String cookie : headerValue.split(" ;")) {
-          final String[] cookiePair = cookie.split("=");
-          if (cookiePair.length != 2) {
+        // RFC 6265: cookie-pairs are separated by "; " and a value may itself 
contain '='
+        for (final String cookie : headerValue.split(";")) {
+          final int separator = cookie.indexOf('=');
+          if (separator == -1) {
             // Soft failure:  Ignore invalid cookie list field
-            break;
+            continue;
           }
 
-          if (sessionCookieName.equals(cookiePair[0]) && 
cookiePair[1].length() > 0) {
-            sessionId = cookiePair[1];
+          final String name = cookie.substring(0, separator).trim();
+          String value = cookie.substring(separator + 1).trim();
+          if (value.length() >= 2 && value.startsWith("\"") && 
value.endsWith("\"")) {
+            value = value.substring(1, value.length() - 1);
+          }
+          if (sessionCookieName.equals(name) && !value.isEmpty()) {
+            sessionId = value;
             break findIdCookie;
           }
         }
diff --git 
a/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerSessionMiddleware.java
 
b/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerSessionMiddleware.java
new file mode 100644
index 000000000..abe4b29d7
--- /dev/null
+++ 
b/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerSessionMiddleware.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.flight;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.arrow.flight.grpc.RequestContextAdapter;
+import org.junit.jupiter.api.Test;
+
+public class TestServerSessionMiddleware {
+  private static final String SESSION_ID = 
"6f1a3c8e-2b4d-4f7a-9c0e-1d2e3f4a5b6c";
+  private static final CallInfo CALL_INFO = new 
CallInfo(FlightMethod.DO_ACTION);
+
+  /** Creates a factory holding one session with {@link #SESSION_ID}. */
+  private static ServerSessionMiddleware.Factory factoryWithSession() {
+    final ServerSessionMiddleware.Factory factory =
+        new ServerSessionMiddleware.Factory(() -> SESSION_ID);
+    final ServerSessionMiddleware middleware =
+        factory.onCallStarted(CALL_INFO, new FlightCallHeaders(), new 
RequestContextAdapter());
+    assertFalse(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+    return factory;
+  }
+
+  private static ServerSessionMiddleware callWithCookie(
+      ServerSessionMiddleware.Factory factory, String... cookieHeaders) {
+    final FlightCallHeaders headers = new FlightCallHeaders();
+    for (String cookieHeader : cookieHeaders) {
+      headers.insert("cookie", cookieHeader);
+    }
+    return factory.onCallStarted(CALL_INFO, headers, new 
RequestContextAdapter());
+  }
+
+  @Test
+  public void sessionCookieAlone() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(factory, ServerSessionMiddleware.sessionCookieName + 
"=" + SESSION_ID);
+    assertTrue(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+  }
+
+  /** The session cookie is found when other cookies share the header (RFC 
6265 "; " list). */
+  @Test
+  public void sessionCookieAfterOtherCookies() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(
+            factory,
+            "AWSALB=abc; "
+                + ServerSessionMiddleware.sessionCookieName
+                + "="
+                + SESSION_ID
+                + "; x=y");
+    assertTrue(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+  }
+
+  /** A cookie value may contain '=' (for example base64 padding). */
+  @Test
+  public void sessionCookieValueWithEquals() {
+    final ServerSessionMiddleware.Factory factory =
+        new ServerSessionMiddleware.Factory(() -> "c2Vzc2lvbg==");
+    final ServerSessionMiddleware created =
+        factory.onCallStarted(CALL_INFO, new FlightCallHeaders(), new 
RequestContextAdapter());
+    final ServerSessionMiddleware.Session session = created.getSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(factory, ServerSessionMiddleware.sessionCookieName + 
"=c2Vzc2lvbg==");
+    assertTrue(middleware.hasSession());
+    assertSame(session, middleware.getSession());
+  }
+
+  /** A malformed entry earlier in the list does not hide the session cookie. 
*/
+  @Test
+  public void malformedCookieIsSkipped() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(
+            factory, "garbage; " + ServerSessionMiddleware.sessionCookieName + 
"=" + SESSION_ID);
+    assertTrue(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+  }
+
+  @Test
+  public void quotedSessionCookieValue() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(
+            factory, ServerSessionMiddleware.sessionCookieName + "=\"" + 
SESSION_ID + "\"");
+    assertTrue(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+  }
+
+  @Test
+  public void sessionCookieInSecondHeader() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final ServerSessionMiddleware middleware =
+        callWithCookie(
+            factory, "other=1", ServerSessionMiddleware.sessionCookieName + 
"=" + SESSION_ID);
+    assertTrue(middleware.hasSession());
+    assertEquals(SESSION_ID, middleware.getSession().id);
+  }
+
+  @Test
+  public void noSessionCookie() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    assertFalse(callWithCookie(factory, "other=1; another=2").hasSession());
+    assertFalse(
+        callWithCookie(factory, ServerSessionMiddleware.sessionCookieName + 
"=").hasSession());
+  }
+
+  @Test
+  public void unknownSessionCookie() {
+    final ServerSessionMiddleware.Factory factory = factoryWithSession();
+    final FlightRuntimeException e =
+        assertThrows(
+            FlightRuntimeException.class,
+            () -> callWithCookie(factory, 
ServerSessionMiddleware.sessionCookieName + "=unknown"));
+    assertEquals(FlightStatusCode.NOT_FOUND, e.status().code());
+  }
+}

Reply via email to