From 2ee51222443695f8a90e51d91fa0bbe358c7fe88 Mon Sep 17 00:00:00 2001 From: onewe Date: Fri, 22 Apr 2022 15:51:40 +0800 Subject: [PATCH] [ISSUE #8208] fix ip validate (#8210) Close #8208 --- .../com/alibaba/nacos/naming/core/Instance.java | 17 +++-------------- .../alibaba/nacos/naming/core/InstanceTest.java | 7 +++++++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index b047b5f17..3ac317af7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -30,8 +30,6 @@ import com.alibaba.nacos.common.utils.NumberUtils; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * IP under service. @@ -54,8 +52,6 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme private String app; - private static final Pattern ONLY_DIGIT_AND_DOT = Pattern.compile("(\\d|\\.)+"); - private static final String SPLITER = "_"; public Instance() { @@ -352,11 +348,9 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme * @throws NacosException if instance is not validate */ public void validate() throws NacosException { - if (onlyContainsDigitAndDot()) { - if (!InternetAddressUtil.containsPort(getIp() + InternetAddressUtil.IP_PORT_SPLITER + getPort())) { - throw new NacosException(NacosException.INVALID_PARAM, - "instance format invalid: Your IP address is spelled incorrectly"); - } + if (!InternetAddressUtil.isIP(getIp())) { + throw new NacosException(NacosException.INVALID_PARAM, + "instance format invalid: Your IP address is spelled incorrectly"); } if (getWeight() > com.alibaba.nacos.naming.constants.Constants.MAX_WEIGHT_VALUE @@ -368,11 +362,6 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme } - private boolean onlyContainsDigitAndDot() { - Matcher matcher = ONLY_DIGIT_AND_DOT.matcher(getIp()); - return matcher.matches(); - } - @Override public int compareTo(Object o) { if (!(o instanceof Instance)) { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceTest.java index 31cdad750..a2e04d67d 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.core; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.naming.healthcheck.RsInfo; import org.junit.Before; @@ -131,4 +132,10 @@ public class InstanceTest { RsInfo info1 = JacksonUtils.toObj(json, RsInfo.class); System.out.println(info1); } + + @Test(expected = NacosException.class) + public void testIpValidate() throws NacosException { + Instance instance1 = new Instance("192.168.1.3d", 8080); + instance1.validate(); + } }