]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21971 Fix isLocal when multiple loopback adresses
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 2 Apr 2024 10:19:09 +0000 (12:19 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 3 Apr 2024 20:02:40 +0000 (20:02 +0000)
On my Ubuntu box, the hosts file contains:

$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 PC-L00XX

And InetAddress.getLocalHost() returns 127.0.1.1, so the test isLocal_returns_true_on_localhost_address_or_host was failing

server/sonar-process/src/main/java/org/sonar/process/NetworkUtilsImpl.java

index cc020e16dfcefe7a2dc7345506c1eddfe79c3e49..0a9765718f6f83e72246df8f487b5cde5bd98530 100644 (file)
@@ -124,7 +124,11 @@ public class NetworkUtilsImpl implements NetworkUtils {
     try {
       Optional<InetAddress> inetAddress = toInetAddress(hostOrAddress);
       if (inetAddress.isPresent()) {
-        return NetworkInterface.getByInetAddress(inetAddress.get()) != null;
+        var addr = inetAddress.get();
+        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
+          return true;
+        }
+        return NetworkInterface.getByInetAddress(addr) != null;
       }
       return false;
     } catch (SocketException e) {