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

(maven) branch maven-3.10.x updated: Treat all loopback hosts as local for external mirror rules (fix #8320) (#13526)

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

gnodet pushed a commit to branch maven-3.10.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.10.x by this push:
     new 519a0eb6bd Treat all loopback hosts as local for external mirror rules (fix #8320) (#13526)
519a0eb6bd is described below

commit 519a0eb6bd87ea056384e9ee3196f04bca916aa5
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Oct 15 23:08:42 2026 +0200

    Treat all loopback hosts as local for external mirror rules (fix #8320) (#13526)
    
    Backport of 7f2bc3a809d28c9f97b1ee31be8331cb75c4b74a to maven-3.10.x.
    
    The 3.10.x branch uses maven-compat/ and maven-core/ paths; the fix was
    manually applied. Tests were added to DefaultMirrorSelectorTest and a new
    MavenRepositorySystemTest was created (absent on this branch).
---
 .../maven/repository/DefaultMirrorSelector.java    | 30 +++++++++-
 .../repository/DefaultMirrorSelectorTest.java      | 38 +++++++++++++
 .../apache/maven/bridge/MavenRepositorySystem.java | 30 +++++++++-
 .../maven/bridge/MavenRepositorySystemTest.java    | 66 ++++++++++++++++++++++
 4 files changed, 162 insertions(+), 2 deletions(-)

diff --git a/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java b/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java
index f244551a88..4f16cc8b22 100644
--- a/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java
+++ b/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java
@@ -18,9 +18,12 @@
  */
 package org.apache.maven.repository;
 
+import java.net.InetAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.UnknownHostException;
 import java.util.List;
+import java.util.regex.Pattern;
 
 import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -133,8 +136,33 @@ static boolean isExternalRepo(ArtifactRepository originalRepository) {
         }
     }
 
+    /** An IPv4 address of the loopback range 127.0.0.0/8. */
+    private static final Pattern IPV4_LOOPBACK = Pattern.compile("127(\\.(25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}");
+
+    /**
+     * Checks whether a host names the local machine: {@code localhost}, an IPv4 address of the loopback
+     * range {@code 127.0.0.0/8} or an IPv6 loopback address in brackets. An IPv4-mapped IPv6 address is judged by its
+     * IPv4 address. The names are not resolved.
+     *
+     * @param host the host of a repository URL, may be {@code null}
+     * @return true if the host is local
+     */
     private static boolean isLocal(String host) {
-        return "localhost".equals(host) || "127.0.0.1".equals(host);
+        if (host == null) {
+            return false;
+        }
+        if ("localhost".equals(host) || IPV4_LOOPBACK.matcher(host).matches()) {
+            return true;
+        }
+        if (host.startsWith("[") && host.endsWith("]")) {
+            try {
+                // an IP literal is parsed without any name lookup
+                return InetAddress.getByName(host).isLoopbackAddress();
+            } catch (UnknownHostException e) {
+                return false;
+            }
+        }
+        return false;
     }
 
     /**
diff --git a/maven-compat/src/test/java/org/apache/maven/repository/DefaultMirrorSelectorTest.java b/maven-compat/src/test/java/org/apache/maven/repository/DefaultMirrorSelectorTest.java
index d4bf846303..dfc83a7358 100644
--- a/maven-compat/src/test/java/org/apache/maven/repository/DefaultMirrorSelectorTest.java
+++ b/maven-compat/src/test/java/org/apache/maven/repository/DefaultMirrorSelectorTest.java
@@ -23,6 +23,7 @@
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class DefaultMirrorSelectorTest {
 
@@ -34,4 +35,41 @@ public void testMirrorWithMirrorOfPatternContainingANegationIsNotSelected() {
         System.out.println(matches);
         assertFalse(matches);
     }
+
+    @Test
+    void testLoopbackAddressesAreLocal() {
+        for (String url : new String[] {
+            "http://127.0.0.2/m2/",
+            "http://127.255.255.254:8080/m2/",
+            "http://[::1]/m2/",
+            "http://[::1]:8080/m2/",
+            "http://[0:0:0:0:0:0:0:1]:8080/m2/",
+            // an IPv4 loopback address written as IPv4-mapped IPv6 address
+            "http://[::ffff:127.0.0.1]/m2/",
+            "http://[::ffff:7f00:1]:8080/m2/"
+        }) {
+            assertFalse(DefaultMirrorSelector.isExternalRepo(repo(url)), url);
+            assertFalse(DefaultMirrorSelector.isExternalHttpRepo(repo(url)), url);
+        }
+    }
+
+    @Test
+    void testOtherAddressesAreExternal() {
+        for (String url : new String[] {
+            "http://128.0.0.1/m2/",
+            "http://127.0.0.1.example.com/m2/",
+            "http://127.example.com/m2/",
+            "http://[2001:db8::1]/m2/",
+            "http://[2001:db8::1]:8080/m2/",
+            // an IPv4-mapped IPv6 address which is not a loopback one
+            "http://[::ffff:8.8.8.8]/m2/"
+        }) {
+            assertTrue(DefaultMirrorSelector.isExternalRepo(repo(url)), url);
+            assertTrue(DefaultMirrorSelector.isExternalHttpRepo(repo(url)), url);
+        }
+    }
+
+    private static ArtifactRepository repo(String url) {
+        return new DefaultArtifactRepository("test", url, null);
+    }
 }
diff --git a/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java b/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
index bd0ea99a83..310d44d380 100644
--- a/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
+++ b/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
@@ -23,8 +23,10 @@
 import javax.inject.Singleton;
 
 import java.io.File;
+import java.net.InetAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -34,6 +36,7 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.Artifact;
@@ -803,8 +806,33 @@ static boolean isExternalRepo(ArtifactRepository originalRepository) {
         }
     }
 
+    /** An IPv4 address of the loopback range 127.0.0.0/8. */
+    private static final Pattern IPV4_LOOPBACK = Pattern.compile("127(\\.(25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}");
+
+    /**
+     * Checks whether a host names the local machine: {@code localhost}, an IPv4 address of the loopback
+     * range {@code 127.0.0.0/8} or an IPv6 loopback address in brackets. An IPv4-mapped IPv6 address is judged by its
+     * IPv4 address. The names are not resolved.
+     *
+     * @param host the host of a repository URL, may be {@code null}
+     * @return true if the host is local
+     */
     private static boolean isLocal(String host) {
-        return "localhost".equals(host) || "127.0.0.1".equals(host);
+        if (host == null) {
+            return false;
+        }
+        if ("localhost".equals(host) || IPV4_LOOPBACK.matcher(host).matches()) {
+            return true;
+        }
+        if (host.startsWith("[") && host.endsWith("]")) {
+            try {
+                // an IP literal is parsed without any name lookup
+                return InetAddress.getByName(host).isLoopbackAddress();
+            } catch (UnknownHostException e) {
+                return false;
+            }
+        }
+        return false;
     }
 
     /**
diff --git a/maven-core/src/test/java/org/apache/maven/bridge/MavenRepositorySystemTest.java b/maven-core/src/test/java/org/apache/maven/bridge/MavenRepositorySystemTest.java
new file mode 100644
index 0000000000..144d2e9d96
--- /dev/null
+++ b/maven-core/src/test/java/org/apache/maven/bridge/MavenRepositorySystemTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.maven.bridge;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.MavenArtifactRepository;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MavenRepositorySystemTest {
+
+    @Test
+    void testLoopbackAddressesAreLocal() {
+        for (String url : new String[] {
+            "http://127.0.0.2/m2/",
+            "http://127.255.255.254:8080/m2/",
+            "http://[::1]/m2/",
+            "http://[::1]:8080/m2/",
+            "http://[0:0:0:0:0:0:0:1]:8080/m2/",
+            // an IPv4 loopback address written as IPv4-mapped IPv6 address
+            "http://[::ffff:127.0.0.1]/m2/",
+            "http://[::ffff:7f00:1]:8080/m2/"
+        }) {
+            assertFalse(MavenRepositorySystem.isExternalRepo(repo(url)), url);
+            assertFalse(MavenRepositorySystem.isExternalHttpRepo(repo(url)), url);
+        }
+    }
+
+    @Test
+    void testOtherAddressesAreExternal() {
+        for (String url : new String[] {
+            "http://128.0.0.1/m2/",
+            "http://127.0.0.1.example.com/m2/",
+            "http://127.example.com/m2/",
+            "http://[2001:db8::1]/m2/",
+            "http://[2001:db8::1]:8080/m2/",
+            // an IPv4-mapped IPv6 address which is not a loopback one
+            "http://[::ffff:8.8.8.8]/m2/"
+        }) {
+            assertTrue(MavenRepositorySystem.isExternalRepo(repo(url)), url);
+            assertTrue(MavenRepositorySystem.isExternalHttpRepo(repo(url)), url);
+        }
+    }
+
+    private static ArtifactRepository repo(String url) {
+        return new MavenArtifactRepository("test", url, null, null, null);
+    }
+}