Skip to content

Commit

Permalink
[ISSUE alibaba#8208] fix ip validate (alibaba#8210)
Browse files Browse the repository at this point in the history
  • Loading branch information
onewe authored Apr 22, 2022
1 parent eda178f commit 2ee5122
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
17 changes: 3 additions & 14 deletions naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* IP under service.
Expand All @@ -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() {
Expand Down Expand Up @@ -352,11 +348,9 @@ private String generateSnowflakeInstanceId(Set<String> currentInstanceIds) {
* @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
Expand All @@ -368,11 +362,6 @@ public void validate() throws NacosException {

}

private boolean onlyContainsDigitAndDot() {
Matcher matcher = ONLY_DIGIT_AND_DOT.matcher(getIp());
return matcher.matches();
}

@Override
public int compareTo(Object o) {
if (!(o instanceof Instance)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,4 +132,10 @@ public void rsInfo() throws Exception {
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();
}
}

0 comments on commit 2ee5122

Please sign in to comment.