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 5a1896fa24 [ISSUE #11530] fix(common): validate address family and 
prefix length in IPAddressUtils#isIPInRange (#11531)
5a1896fa24 is described below

commit 5a1896fa244deaba9cde63c4b3ef28964e173d47
Author: leaveWhite9088 <[email protected]>
AuthorDate: Wed Nov 18 10:08:52 2026 +0800

    [ISSUE #11530] fix(common): validate address family and prefix length in 
IPAddressUtils#isIPInRange (#11531)
---
 .../rocketmq/common/utils/IPAddressUtils.java      | 24 +++++++++++++++++-----
 .../rocketmq/common/utils/IPAddressUtilsTest.java  | 19 +++++++++++++++++
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git 
a/common/src/main/java/org/apache/rocketmq/common/utils/IPAddressUtils.java 
b/common/src/main/java/org/apache/rocketmq/common/utils/IPAddressUtils.java
index 5133219d9c..2c29fb5e39 100644
--- a/common/src/main/java/org/apache/rocketmq/common/utils/IPAddressUtils.java
+++ b/common/src/main/java/org/apache/rocketmq/common/utils/IPAddressUtils.java
@@ -82,21 +82,35 @@ public class IPAddressUtils {
     }
 
     public static boolean isIPInRange(String ip, String cidr) {
+        if (StringUtils.isBlank(ip) || StringUtils.isBlank(cidr)) {
+            return false;
+        }
         try {
-            String[] parts = cidr.split(SLASH);
+            String[] parts = cidr.split(SLASH, -1);
             if (parts.length == 1) {
-                return StringUtils.equals(ip, cidr);
+                return ip.equals(cidr);
             }
             if (parts.length != 2) {
                 return false;
             }
             InetAddress cidrIp = InetAddress.getByName(parts[0]);
+            InetAddress targetIp = InetAddress.getByName(ip);
+            byte[] cidrBytes = cidrIp.getAddress();
+            byte[] ipBytes = targetIp.getAddress();
+            if (cidrBytes.length != ipBytes.length) {
+                return false;
+            }
+
             int prefixLength = Integer.parseInt(parts[1]);
+            int maxPrefixLength = cidrBytes.length * 8;
+            if (prefixLength < 0 || prefixLength > maxPrefixLength) {
+                return false;
+            }
 
-            BigInteger cidrIpBigInt = new BigInteger(1, cidrIp.getAddress());
-            BigInteger ipBigInt = new BigInteger(1, 
InetAddress.getByName(ip).getAddress());
+            BigInteger cidrIpBigInt = new BigInteger(1, cidrBytes);
+            BigInteger ipBigInt = new BigInteger(1, ipBytes);
 
-            BigInteger mask = 
BigInteger.valueOf(-1).shiftLeft(cidrIp.getAddress().length * 8 - prefixLength);
+            BigInteger mask = BigInteger.valueOf(-1).shiftLeft(maxPrefixLength 
- prefixLength);
             BigInteger cidrIpLower = cidrIpBigInt.and(mask);
             BigInteger cidrIpUpper = cidrIpLower.add(mask.not());
 
diff --git 
a/common/src/test/java/org/apache/rocketmq/common/utils/IPAddressUtilsTest.java 
b/common/src/test/java/org/apache/rocketmq/common/utils/IPAddressUtilsTest.java
index 7830bc3aed..b2df9df026 100644
--- 
a/common/src/test/java/org/apache/rocketmq/common/utils/IPAddressUtilsTest.java
+++ 
b/common/src/test/java/org/apache/rocketmq/common/utils/IPAddressUtilsTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.rocketmq.common.utils;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 import static 
org.apache.rocketmq.common.utils.NetworkUtil.validCommonInet6Address;
@@ -37,6 +38,24 @@ public class IPAddressUtilsTest {
         String ipv6Address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
         String ipv6Cidr = "2001:0db8:85a3::/48";
         assert IPAddressUtils.isIPInRange(ipv6Address, ipv6Cidr);
+
+        // Cross-family CIDR must not match
+        Assert.assertFalse(IPAddressUtils.isIPInRange("192.168.1.10", 
"::/64"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("0.0.0.1", "::1/128"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("::1", "0.0.0.0/0"));
+
+        // Prefix length boundaries and invalid prefix lengths
+        Assert.assertTrue(IPAddressUtils.isIPInRange("192.168.1.10", 
"0.0.0.0/0"));
+        Assert.assertTrue(IPAddressUtils.isIPInRange("192.168.1.10", 
"192.168.1.10/32"));
+        Assert.assertTrue(IPAddressUtils.isIPInRange("::1", "::1/128"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("192.168.1.10", 
"192.168.1.0/33"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("192.168.1.10", 
"192.168.1.0/-1"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("::1", "::/129"));
+
+        // Blank or null inputs
+        Assert.assertFalse(IPAddressUtils.isIPInRange(null, null));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("", "192.168.1.0/24"));
+        Assert.assertFalse(IPAddressUtils.isIPInRange("192.168.1.10", ""));
     }
 
     @Test

Reply via email to