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

ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 28960fd2f7 [ISSUE #11483] fix(remoting): skip CIDR matching for 
non-IPv4 hosts when choosing a SOCKS proxy (#11484)
28960fd2f7 is described below

commit 28960fd2f795a6903665c3d6c037501059df0760
Author: Weidong Qi <[email protected]>
AuthorDate: Wed Nov 18 16:44:07 2026 +0800

    [ISSUE #11483] fix(remoting): skip CIDR matching for non-IPv4 hosts when 
choosing a SOCKS proxy (#11484)
    
    NettyRemotingClient#getProxy passed the host part of every address to
    RemotingHelper#ipInCIDR, which only parses dotted IPv4 literals. With any
    CIDR other than 0.0.0.0/0 configured in socksProxyConfig, connecting to a
    hostname such as namesrv.example.com:9876 (or an IPv6 address) threw a
    NumberFormatException and the connection attempt failed.
    
    Only run the CIDR check for IPv4 hosts; other hosts can still match the
    0.0.0.0/0 catch-all entry.
    
    Co-authored-by: Weidong Qi <[email protected]>
---
 .../remoting/netty/NettyRemotingClient.java        |  5 ++-
 .../remoting/netty/NettyRemotingClientTest.java    | 39 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git 
a/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java
 
b/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java
index 4ba18b53d9..5cd21eaf47 100644
--- 
a/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java
+++ 
b/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java
@@ -51,6 +51,7 @@ import org.apache.rocketmq.common.Pair;
 import org.apache.rocketmq.common.ThreadFactoryImpl;
 import org.apache.rocketmq.common.constant.LoggerName;
 import org.apache.rocketmq.common.utils.FutureUtils;
+import org.apache.rocketmq.common.utils.IPAddressUtils;
 import org.apache.rocketmq.common.utils.NetworkUtil;
 import org.apache.rocketmq.common.utils.ThreadUtils;
 import org.apache.rocketmq.logging.org.slf4j.Logger;
@@ -285,9 +286,11 @@ public class NettyRemotingClient extends 
NettyRemotingAbstract implements Remoti
             return null;
         }
         String[] hostAndPort = this.getHostAndPort(addr);
+        // ipInCIDR only understands IPv4 literals, so a hostname or IPv6 host 
can only match the catch-all CIDR
+        boolean isIPv4Host = IPAddressUtils.isValidIPv4(hostAndPort[0]);
         for (Map.Entry<String, SocksProxyConfig> entry : proxyMap.entrySet()) {
             String cidr = entry.getKey();
-            if (RemotingHelper.DEFAULT_CIDR_ALL.equals(cidr) || 
RemotingHelper.ipInCIDR(hostAndPort[0], cidr)) {
+            if (RemotingHelper.DEFAULT_CIDR_ALL.equals(cidr) || isIPv4Host && 
RemotingHelper.ipInCIDR(hostAndPort[0], cidr)) {
                 return entry;
             }
         }
diff --git 
a/remoting/src/test/java/org/apache/rocketmq/remoting/netty/NettyRemotingClientTest.java
 
b/remoting/src/test/java/org/apache/rocketmq/remoting/netty/NettyRemotingClientTest.java
index 8c9bb8237f..2c83478d7c 100644
--- 
a/remoting/src/test/java/org/apache/rocketmq/remoting/netty/NettyRemotingClientTest.java
+++ 
b/remoting/src/test/java/org/apache/rocketmq/remoting/netty/NettyRemotingClientTest.java
@@ -22,6 +22,8 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.local.LocalChannel;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -31,6 +33,7 @@ import org.apache.rocketmq.common.ServiceThread;
 import org.apache.rocketmq.remoting.ChannelEventListener;
 import org.apache.rocketmq.remoting.InvokeCallback;
 import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.remoting.common.RemotingHelper;
 import org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce;
 import org.apache.rocketmq.remoting.exception.RemotingConnectException;
 import org.apache.rocketmq.remoting.exception.RemotingException;
@@ -39,6 +42,7 @@ import 
org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
 import org.apache.rocketmq.remoting.protocol.RemotingCommand;
 import org.apache.rocketmq.remoting.protocol.RequestCode;
 import org.apache.rocketmq.remoting.protocol.ResponseCode;
+import org.apache.rocketmq.remoting.proxy.SocksProxyConfig;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -343,4 +347,39 @@ public class NettyRemotingClientTest {
         
assertThat(remotingClient.isAddressReachable("[fe80::]:8080")).isFalse();
         verify(bootstrap).connect(eq("[fe80::]"), eq(8080));
     }
+
+    @Test
+    public void testGetProxyMatchesIpv4AddressInCidr() throws Exception {
+        NettyRemotingClient client = 
newClientWithSocksProxy("{\"10.0.0.0/8\":{\"addr\":\"127.0.0.1:1080\"}}");
+        assertThat(getProxy(client, "10.1.2.3:9876")).isNotNull();
+        assertThat(getProxy(client, "192.168.1.1:9876")).isNull();
+    }
+
+    @Test
+    public void testGetProxySkipsCidrForNonIpv4Host() throws Exception {
+        NettyRemotingClient client = 
newClientWithSocksProxy("{\"10.0.0.0/8\":{\"addr\":\"127.0.0.1:1080\"}}");
+        assertThat(getProxy(client, "namesrv.example.com:9876")).isNull();
+        assertThat(getProxy(client, "[fe80::1]:9876")).isNull();
+    }
+
+    @Test
+    public void testGetProxyDefaultCidrMatchesHostname() throws Exception {
+        NettyRemotingClient client = 
newClientWithSocksProxy("{\"0.0.0.0/0\":{\"addr\":\"127.0.0.1:1080\"}}");
+        Map.Entry<String, SocksProxyConfig> proxy = getProxy(client, 
"namesrv.example.com:9876");
+        assertThat(proxy).isNotNull();
+        assertThat(proxy.getKey()).isEqualTo(RemotingHelper.DEFAULT_CIDR_ALL);
+    }
+
+    private static NettyRemotingClient newClientWithSocksProxy(String 
socksProxyConfig) {
+        NettyClientConfig config = new NettyClientConfig();
+        config.setSocksProxyConfig(socksProxyConfig);
+        return new NettyRemotingClient(config);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static Map.Entry<String, SocksProxyConfig> 
getProxy(NettyRemotingClient client, String addr) throws Exception {
+        Method method = 
NettyRemotingClient.class.getDeclaredMethod("getProxy", String.class);
+        method.setAccessible(true);
+        return (Map.Entry<String, SocksProxyConfig>) method.invoke(client, 
addr);
+    }
 }

Reply via email to