diff --git a/lib/remote/configpackageutility.cpp b/lib/remote/configpackageutility.cpp index a1f766442..31748708e 100644 --- a/lib/remote/configpackageutility.cpp +++ b/lib/remote/configpackageutility.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include using namespace icinga; @@ -375,9 +376,9 @@ bool ConfigPackageUtility::ValidateName(const String& name) if (ContainsDotDot(name)) return false; - boost::regex expr("^[^a-zA-Z0-9_\\-]*$", boost::regex::icase); - boost::smatch what; - return (!boost::regex_search(name.GetData(), what, expr)); + return std::all_of(name.Begin(), name.End(), [](char c) { + return std::isalnum(c, std::locale::classic()) || c == '_' || c == '-'; + }); } std::mutex& ConfigPackageUtility::GetStaticPackageMutex() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 75c8dc4be..c5e508154 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,7 @@ set(base_test_SOURCES icinga-macros.cpp icinga-notification.cpp icinga-perfdata.cpp + remote-configpackageutility.cpp remote-url.cpp ${base_OBJS} $ @@ -148,6 +149,7 @@ add_boost_test(base icinga_perfdata/multi icinga_perfdata/scientificnotation icinga_perfdata/parse_edgecases + remote_configpackageutility/ValidateName remote_url/id_and_path remote_url/parameters remote_url/get_and_set diff --git a/test/remote-configpackageutility.cpp b/test/remote-configpackageutility.cpp new file mode 100644 index 000000000..7049fcd22 --- /dev/null +++ b/test/remote-configpackageutility.cpp @@ -0,0 +1,25 @@ +/* Icinga 2 | (c) 2021 Icinga GmbH | GPLv2+ */ + +#include "remote/configpackageutility.hpp" +#include +#include +#include + +using namespace icinga; + +BOOST_AUTO_TEST_SUITE(remote_configpackageutility) + +BOOST_AUTO_TEST_CASE(ValidateName) +{ + std::vector validNames {"foo", "foo-bar", "FooBar", "Foo123", "_Foo-", "123bar"}; + for (const std::string& n : validNames) { + BOOST_CHECK_MESSAGE(ConfigPackageUtility::ValidateName(n), "'" << n << "' should be valid"); + } + + std::vector invalidNames {"", ".", "..", "foo.bar", "foo/../bar", "foo/bar", "foo:bar"}; + for (const std::string& n : invalidNames) { + BOOST_CHECK_MESSAGE(!ConfigPackageUtility::ValidateName(n), "'" << n << "' should not be valid"); + } +} + +BOOST_AUTO_TEST_SUITE_END()