You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/10/18 18:26:26 UTC

(maven-mvnd) branch master updated: Connect only to loopback daemons whose process is still running

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

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git


The following commit(s) were added to refs/heads/master by this push:
     new aa1fd99f Connect only to loopback daemons whose process is still running
aa1fd99f is described below

commit aa1fd99f1543445bfac1deb183ec4c73ac1ce15f
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Sun Oct 18 15:53:01 2026 +0200

    Connect only to loopback daemons whose process is still running
---
 .../org/mvndaemon/mvnd/client/DaemonConnector.java | 24 ++++++++++++++++
 .../mvndaemon/mvnd/client/DaemonConnectorTest.java | 33 ++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java
index 183c3df3..3c08a16e 100644
--- a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java
@@ -19,6 +19,7 @@
 package org.mvndaemon.mvnd.client;
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
@@ -533,6 +534,10 @@ public class DaemonConnector {
             throws DaemonException.ConnectException {
         LOGGER.debug("Connecting to Daemon");
         try {
+            if (!isAlive(daemon.getPid())) {
+                throw new DaemonException.ConnectException(
+                        String.format("The daemon process %d is no longer running.", daemon.getPid()));
+            }
             DaemonConnection connection = connect(daemon.getAddress(), daemon.getToken());
             return new DaemonClientConnection(connection, daemon, staleAddressDetector, newDaemon, parameters);
         } catch (DaemonException.ConnectException e) {
@@ -563,8 +568,27 @@ public class DaemonConnector {
         }
     }
 
+    /**
+     * Daemons only listen on the loopback interface, so the client never sends its environment and arguments to an
+     * address read from the registry that points anywhere else.
+     */
+    static void checkLocal(SocketAddress address) throws DaemonException.ConnectException {
+        if (address instanceof InetSocketAddress) {
+            InetAddress ia = ((InetSocketAddress) address).getAddress();
+            if (ia == null || !ia.isLoopbackAddress()) {
+                throw new DaemonException.ConnectException(
+                        String.format("Refusing to connect to %s: not a loopback address.", address));
+            }
+        }
+    }
+
+    static boolean isAlive(int pid) {
+        return pid <= 0 || ProcessHandle.of(pid).map(ProcessHandle::isAlive).orElse(false);
+    }
+
     public DaemonConnection connect(String str, byte[] token) throws DaemonException.ConnectException {
         SocketAddress address = SocketFamily.fromString(str);
+        checkLocal(address);
         try {
             LOGGER.debug("Trying to connect to address {}.", address);
             SocketFamily family = SocketFamily.familyOf(address);
diff --git a/client/src/test/java/org/mvndaemon/mvnd/client/DaemonConnectorTest.java b/client/src/test/java/org/mvndaemon/mvnd/client/DaemonConnectorTest.java
index d7b74c30..4137b88f 100644
--- a/client/src/test/java/org/mvndaemon/mvnd/client/DaemonConnectorTest.java
+++ b/client/src/test/java/org/mvndaemon/mvnd/client/DaemonConnectorTest.java
@@ -18,10 +18,18 @@
  */
 package org.mvndaemon.mvnd.client;
 
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
 import org.junit.jupiter.api.Test;
+import org.mvndaemon.mvnd.common.DaemonException;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class DaemonConnectorTest {
 
@@ -31,4 +39,29 @@ public class DaemonConnectorTest {
         assertNotNull(id);
         assertEquals(8, id.length());
     }
+
+    @Test
+    public void connectsOnlyToLoopback() throws Exception {
+        assertDoesNotThrow(
+                () -> DaemonConnector.checkLocal(new InetSocketAddress(InetAddress.getLoopbackAddress(), 4000)));
+        assertDoesNotThrow(() -> DaemonConnector.checkLocal(
+                new InetSocketAddress(InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 4000)));
+        assertThrows(
+                DaemonException.ConnectException.class,
+                () -> DaemonConnector.checkLocal(
+                        new InetSocketAddress(InetAddress.getByAddress(new byte[] {10, 1, 2, 3}), 4000)));
+        assertThrows(
+                DaemonException.ConnectException.class,
+                () -> DaemonConnector.checkLocal(InetSocketAddress.createUnresolved("example.com", 4000)));
+    }
+
+    @Test
+    public void detectsDeadProcesses() throws Exception {
+        assertTrue(DaemonConnector.isAlive((int) ProcessHandle.current().pid()));
+        Process p =
+                new ProcessBuilder("java", "-version").redirectErrorStream(true).start();
+        int pid = (int) p.pid();
+        p.waitFor();
+        assertFalse(DaemonConnector.isAlive(pid));
+    }
 }