Adding proper parsing of port ranges for firewall.

This commit is contained in:
stephb9959
2022-05-31 11:47:27 -07:00
parent 4582cbd133
commit 1e16c59743
2 changed files with 25 additions and 5 deletions

View File

@@ -2623,11 +2623,12 @@ static json DefaultUCentralSchema = R"(
void ConfigurationValidator::Init() {
if(Initialized_)
return;
std::string GitSchema;
std::string GitSchema;
if(MicroService::instance().ConfigGetBool("ucentral.datamodel.internal",true)) {
RootSchema_ = DefaultUCentralSchema;
Logger().information("Using uCentral validation from built-in default.");
Initialized_ = Working_ = true;
return;
}
@@ -2699,6 +2700,17 @@ static json DefaultUCentralSchema = R"(
return IsCIDRv4(value) || IsCIDRv6(value);
}
static inline bool IsPortRangeIsValid(const std::string &r) {
const auto ports = Poco::StringTokenizer("-",r,Poco::StringTokenizer::TOK_TRIM);
for(const auto &port:ports) {
uint32_t port_num = std::stoul(port);
if(port_num==0 || port_num>65535)
return false;
}
return true;
}
void ConfigurationValidator::my_format_checker(const std::string &format, const std::string &value)
{
static const std::regex host_regex{"^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,63}$"};
@@ -2749,6 +2761,14 @@ static json DefaultUCentralSchema = R"(
} catch (...) {
}
throw std::invalid_argument(value + " is not a valid URI: should be something like https://hello.world.com.");
} else if(format == "uc-portrange") {
try {
if(IsPortRangeIsValid(value))
return;
throw std::invalid_argument(value + " is not a valid port range: should an integer between 1-65535 or a port range like post-port.");
} catch (...) {
}
throw std::invalid_argument(value + " is not a valid URI: should be something like https://hello.world.com.");
} else if(format == "ip") {
if (IsIP(value))
return;