[ISSUE #8208] fix ip validate (#8210)

Close #8208
This commit is contained in:
onewe 2022-04-22 15:51:40 +08:00 committed by GitHub
parent eda178f27b
commit 2ee5122244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View File

@ -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)) {

View File

@ -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();
}
}