fix: Fix url check in config (#1953)

Fixes #1850
This commit is contained in:
Sergey Kuznetsov
2025-03-11 12:54:22 +00:00
committed by GitHub
parent a46d700390
commit 26842374de
2 changed files with 26 additions and 21 deletions

View File

@@ -22,6 +22,8 @@
#include "util/newconfig/Error.hpp"
#include "util/newconfig/Types.hpp"
#include <boost/asio/ip/address.hpp>
#include <cstdint>
#include <optional>
#include <regex>
@@ -68,20 +70,19 @@ ValidIPConstraint::checkTypeImpl(Value const& ip) const
std::optional<Error>
ValidIPConstraint::checkValueImpl(Value const& ip) const
{
if (std::get<std::string>(ip) == "localhost")
boost::system::error_code errorCode;
boost::asio::ip::make_address(std::get<std::string>(ip), errorCode);
if (not errorCode.failed())
return std::nullopt;
static std::regex const kIPV4(
R"(^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$)"
);
static std::regex const kHOST{
R"regex(^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)regex"
};
static std::regex const kIP_URL(
R"(^((http|https):\/\/)?((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6})|(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])))(:\d{1,5})?(\/[^\s]*)?$)"
);
if (std::regex_match(std::get<std::string>(ip), kIPV4) || std::regex_match(std::get<std::string>(ip), kIP_URL))
if (std::regex_match(std::get<std::string>(ip), kHOST))
return std::nullopt;
return Error{"Ip is not a valid ip address"};
return Error{"Ip is not a valid ip address or hostname"};
}
std::optional<Error>

View File

@@ -198,20 +198,24 @@ TEST_F(ConstraintTest, OneOfConstraint)
TEST_F(ConstraintTest, IpConstraint)
{
auto ip = ConfigValue{ConfigType::String}.defaultValue("127.0.0.1").withConstraint(gValidateIp);
EXPECT_FALSE(ip.setValue("http://127.0.0.1").has_value());
EXPECT_FALSE(ip.setValue("http://127.0.0.1.com").has_value());
auto const err = ip.setValue("123.44");
EXPECT_TRUE(err.has_value());
EXPECT_EQ(err->error, "Unknown_key Ip is not a valid ip address");
EXPECT_FALSE(ip.setValue("126.0.0.2"));
auto ip = ConfigValue{ConfigType::String}.withConstraint(gValidateIp);
EXPECT_TRUE(ip.setValue("644.3.3.0"));
EXPECT_TRUE(ip.setValue("127.0.0.1.0"));
EXPECT_TRUE(ip.setValue(""));
EXPECT_TRUE(ip.setValue("http://example..com"));
EXPECT_FALSE(ip.setValue("127.0.0.1"));
EXPECT_FALSE(ip.setValue("localhost"));
EXPECT_FALSE(ip.setValue("http://example.com:8080/path"));
EXPECT_FALSE(ip.setValue("some-server.com"));
EXPECT_FALSE(ip.setValue("126.0.0.2"));
EXPECT_FALSE(ip.setValue("valid.host-name.com"));
EXPECT_TRUE(ip.setValue("http://127.0.0.1"));
EXPECT_TRUE(ip.setValue(""));
EXPECT_TRUE(ip.setValue("example..com"));
EXPECT_TRUE(ip.setValue("example.com:8080/path"));
EXPECT_TRUE(ip.setValue("-invalid.com"));
EXPECT_TRUE(ip.setValue("invalid-.com"));
auto const err = ip.setValue("extra$@symbols");
ASSERT_TRUE(err.has_value());
EXPECT_EQ(err->error, "Unknown_key Ip is not a valid ip address or hostname");
}
TEST_F(ConstraintTest, positiveNumConstraint)