From 35ad362e10bea03502f46473ff10853fc8dd6b9b Mon Sep 17 00:00:00 2001 From: Vasiliy Mikhailov Date: Sat, 27 Jun 2026 03:31:01 +0000 Subject: [PATCH] Detect ip:port with contains in NetUtils.matchIpExpression The deprecated two-arg matchIpExpression used address.endsWith(":") to detect the ip:port format, but a real address like 192.168.1.63:90 ends with the port, not a colon, so the host/port split was skipped and matching failed. Use contains(":") to detect the separator. --- .../org/apache/dubbo/common/utils/NetUtils.java | 2 +- .../apache/dubbo/common/utils/NetUtilsTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index c9ddff2a5612..29095e884824 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -738,7 +738,7 @@ public static boolean matchIpExpression(String pattern, String address) throws U String host = address; int port = 0; // only works for ipv4 address with 'ip:port' format - if (address.endsWith(":")) { + if (address.contains(":")) { String[] hostPort = address.split(":"); host = hostPort[0]; port = StringUtils.parseInteger(hostPort[1]); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 9f093229ab0e..49a2305f7f6f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -346,6 +346,22 @@ void testMatchIpMatch() throws UnknownHostException { assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", 90)); } + @Test + void testMatchIpExpressionDeprecatedTwoArgsWithIpPort() throws UnknownHostException { + // The deprecated 2-arg matchIpExpression should correctly parse host and port from + // an "ip:port" address string. Normal addresses like "192.168.1.63:90" should + // split into host="192.168.1.63" and port=90. + // With contains(":") this works; with endsWith(":") it does not. + + // Pattern with port: host must match AND port must match + assertTrue(NetUtils.matchIpExpression("192.168.1.63:90", "192.168.1.63:90")); + assertFalse(NetUtils.matchIpExpression("192.168.1.63:90", "192.168.1.63:80")); + + // Pattern without port: only host must match + assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63:90")); + assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199:8080")); + } + @Test void testMatchIpv6WithIpPort() throws UnknownHostException { assertTrue(NetUtils.matchIpRange("[234e:0:4567::3d:ee]", "234e:0:4567::3d:ee", 8090));