From 5022ec4dcc4df5ddd0fdd431392d1543c5896d8f Mon Sep 17 00:00:00 2001 From: Duansg Date: Tue, 8 Sep 2026 19:18:54 +0800 Subject: [PATCH] [fix] correct inverted RegistryProtocol param validation (#4366) --- .../entity/job/protocol/RegistryProtocol.java | 4 +- .../job/protocol/RegistryProtocolTest.java | 187 +++++++++++++++--- 2 files changed, 157 insertions(+), 34 deletions(-) diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java index aaa63161e3..6041738392 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java @@ -49,7 +49,7 @@ public class RegistryProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { - return validateIpDomain(host) && validPort(port) - && StringUtils.isAnyBlank(host, String.valueOf(port), discoveryClientTypeName); + return !validateIpDomain(host) || !validPort(port) + || StringUtils.isBlank(discoveryClientTypeName); } } diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java index f11e2ba911..9506a5cf0f 100644 --- a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java @@ -1,23 +1,20 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ - package org.apache.hertzbeat.common.entity.job.protocol; import org.junit.jupiter.api.Test; @@ -28,26 +25,152 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class RegistryProtocolTest { @Test - void isInvalid() { + void isInvalidValidProtocol() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("8848") + .discoveryClientTypeName("Nacos") + .build(); + assertFalse(protocol.isInvalid()); + } - RegistryProtocol protocol1 = new RegistryProtocol(); - protocol1.setPort("8080"); - protocol1.setHost("127.0.0.1"); - assertTrue(protocol1.isInvalid()); + @Test + void isInvalidValidProtocolWithDomain() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("nacos.example.com") + .port("8848") + .discoveryClientTypeName("Nacos") + .build(); + assertFalse(protocol.isInvalid()); + } - RegistryProtocol protocol2 = new RegistryProtocol(); - protocol2.setPort("8080"); - protocol2.setHost("www.baidu.com"); - assertTrue(protocol2.isInvalid()); + @Test + void isInvalidValidProtocolWithLocalhost() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("localhost") + .port("8848") + .discoveryClientTypeName("Consul") + .build(); + assertFalse(protocol.isInvalid()); + } - RegistryProtocol protocol3 = new RegistryProtocol(); - protocol3.setPort("8080"); - protocol3.setHost("www.baidu.com."); - assertFalse(protocol3.isInvalid()); + @Test + void isInvalidValidProtocolWithIpv6() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("::1") + .port("8848") + .discoveryClientTypeName("Consul") + .build(); + assertFalse(protocol.isInvalid()); + } - RegistryProtocol protocol4 = new RegistryProtocol(); - protocol3.setPort("80800"); - protocol3.setHost("10.45.56.344"); - assertFalse(protocol4.isInvalid()); + @Test + void isInvalidNullHost() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host(null) + .port("8848") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankHost() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host(" ") + .port("8848") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidMalformedHost() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("???") + .port("8848") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidHostWithTrailingDot() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("www.baidu.com.") + .port("8080") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullPort() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port(null) + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankPort() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidOutOfRangePort() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("99999") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNonNumericPort() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("abc") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidOutOfRangePortWithDomainLikeHost() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("10.45.56.344") + .port("80800") + .discoveryClientTypeName("Nacos") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullDiscoveryClientTypeName() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("8848") + .discoveryClientTypeName(null) + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankDiscoveryClientTypeName() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host("192.168.1.1") + .port("8848") + .discoveryClientTypeName(" ") + .build(); + assertTrue(protocol.isInvalid()); } }