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

tballison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 941424c549 fix grpc tests port toctou (#3121)
941424c549 is described below

commit 941424c5499e15808974da840db9e69895d001e1
Author: Tim Allison <[email protected]>
AuthorDate: Wed Sep 2 20:59:18 2026 -0400

    fix grpc tests port toctou (#3121)
---
 .../org/apache/tika/pipes/grpc/TikaGrpcServer.java  |  7 +++++++
 .../PipesBiDirectionalStreamingIntegrationTest.java | 21 +++++++--------------
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git 
a/tika-grpc/src/main/java/org/apache/tika/pipes/grpc/TikaGrpcServer.java 
b/tika-grpc/src/main/java/org/apache/tika/pipes/grpc/TikaGrpcServer.java
index f55b5bac1b..3a98ba6c42 100644
--- a/tika-grpc/src/main/java/org/apache/tika/pipes/grpc/TikaGrpcServer.java
+++ b/tika-grpc/src/main/java/org/apache/tika/pipes/grpc/TikaGrpcServer.java
@@ -117,6 +117,8 @@ public class TikaGrpcServer {
                 .addService(ProtoReflectionServiceV1.newInstance())
                 .build()
                 .start();
+        // port 0 asks the OS to pick one; adopt what it actually bound
+        port = server.getPort();
         LOGGER.info("Server started, listening on " + port);
         Runtime
                 .getRuntime()
@@ -208,6 +210,11 @@ public class TikaGrpcServer {
         return this;
     }
 
+    /** The bound port once {@link #start()} has run; the requested port 
before that. */
+    public Integer getPort() {
+        return port;
+    }
+
     public TikaGrpcServer setSecure(boolean secure) {
         this.secure = secure;
         return this;
diff --git 
a/tika-grpc/src/test/java/org/apache/tika/pipes/grpc/PipesBiDirectionalStreamingIntegrationTest.java
 
b/tika-grpc/src/test/java/org/apache/tika/pipes/grpc/PipesBiDirectionalStreamingIntegrationTest.java
index 9c4493b3e5..6cc673de4a 100644
--- 
a/tika-grpc/src/test/java/org/apache/tika/pipes/grpc/PipesBiDirectionalStreamingIntegrationTest.java
+++ 
b/tika-grpc/src/test/java/org/apache/tika/pipes/grpc/PipesBiDirectionalStreamingIntegrationTest.java
@@ -17,9 +17,7 @@
 package org.apache.tika.pipes.grpc;
 
 import java.io.File;
-import java.io.IOException;
 import java.net.InetAddress;
-import java.net.ServerSocket;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 import java.time.Duration;
@@ -43,6 +41,7 @@ import io.grpc.stub.StreamObserver;
 import org.apache.commons.io.FileUtils;
 import org.awaitility.Awaitility;
 import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.server.handler.ResourceHandler;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Assertions;
@@ -78,23 +77,18 @@ class PipesBiDirectionalStreamingIntegrationTest {
     String httpFetcherId = "httpFetcherIdHere";
     List<String> files = Arrays.asList("014760.docx", "017091.docx", 
"017097.docx", "018367.docx");
 
-    static int findAvailablePort() throws IOException {
-        try (ServerSocket serverSocket = new ServerSocket(0)) {
-            return serverSocket.getLocalPort();
-        }
-    }
-
     @BeforeAll
     static void setUpHttpServer() throws Exception {
-        // Specify the folder from which files will be served
-        httpServerPort = findAvailablePort();
-        httpServer = new Server(httpServerPort);
+        // bind port 0 and read back what the OS gave us: a port picked and 
released
+        // before binding is a port another process can take in between
+        httpServer = new Server(0);
 
         ResourceHandler resourceHandler = new ResourceHandler();
         resourceHandler.setDirAllowed(true);
         
resourceHandler.setBaseResourceAsString("src/test/resources/test-files");
         httpServer.setHandler(resourceHandler);
         httpServer.start();
+        httpServerPort = ((ServerConnector) 
httpServer.getConnectors()[0]).getLocalPort();
 
         httpServerUrl = "http://"; + InetAddress
                 .getByName("localhost")
@@ -103,8 +97,6 @@ class PipesBiDirectionalStreamingIntegrationTest {
 
     @BeforeAll
     static void setUpGrpcServer() throws Exception {
-        grpcPort = findAvailablePort();
-
         // Read the template config
         String configContent = FileUtils.readFileToString(tikaConfigTemplate, 
StandardCharsets.UTF_8);
 
@@ -131,13 +123,14 @@ class PipesBiDirectionalStreamingIntegrationTest {
 
         grpcServer = new TikaGrpcServer();
         grpcServer.setTikaConfig(tikaConfig);
-        grpcServer.setPort(grpcPort);
+        grpcServer.setPort(0);
         grpcServer.setSecure(true);
         grpcServer.setCertChain(Paths.get("src", "test", "resources", "certs", 
"server1.pem").toFile());
         grpcServer.setPrivateKey(Paths.get("src", "test", "resources", 
"certs", "server1.key").toFile());
         grpcServer.setTrustCertCollection(Paths.get("src", "test", 
"resources", "certs", "ca.pem").toFile());
         grpcServer.setClientAuthRequired(true);
         grpcServer.start();
+        grpcPort = grpcServer.getPort();
 
         String target = InetAddress
                 .getByName("localhost")

Reply via email to