From aa3b289b47ea7412a25d90e8138baec849fea8f9 Mon Sep 17 00:00:00 2001 From: badaix Date: Sun, 9 Feb 2025 21:01:18 +0100 Subject: [PATCH] Fix config password parsing --- common/utils/string_utils.cpp | 26 +++++++++++++++++++++++++- common/utils/string_utils.hpp | 11 +++++++++-- server/server_settings.hpp | 2 +- test/test_main.cpp | 17 +++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/common/utils/string_utils.cpp b/common/utils/string_utils.cpp index 6edd0f16..63053d93 100644 --- a/common/utils/string_utils.cpp +++ b/common/utils/string_utils.cpp @@ -140,7 +140,7 @@ std::string uriDecode(const std::string& src) if (int(src[i]) == 37) { unsigned int ii; - sscanf(src.substr(i + 1, 2).c_str(), "%x", &ii); + sscanf(src.substr(i + 1, 2).c_str(), "%x", &ii); // NOLINT ch = static_cast(ii); ret += ch; i += 2; @@ -170,6 +170,22 @@ void split_left(const std::string& s, char delim, std::string& left, std::string } +void split_right(const std::string& s, char delim, std::string& left, std::string& right) +{ + auto pos = s.rfind(delim); + if (pos != std::string::npos) + { + left = s.substr(0, pos); + right = s.substr(pos + 1); + } + else + { + left = s; + right = ""; + } +} + + std::string split_left(const std::string& s, char delim, std::string& right) { std::string left; @@ -178,6 +194,14 @@ std::string split_left(const std::string& s, char delim, std::string& right) } +std::string split_right(const std::string& s, char delim, std::string& right) +{ + std::string left; + split_right(s, delim, left, right); + return left; +} + + std::vector& split(const std::string& s, char delim, std::vector& elems) { diff --git a/common/utils/string_utils.hpp b/common/utils/string_utils.hpp index 534c903e..7811bda8 100644 --- a/common/utils/string_utils.hpp +++ b/common/utils/string_utils.hpp @@ -57,13 +57,20 @@ std::string uriDecode(const std::string& src); /// @return uri encoded version of @p str std::string urlEncode(const std::string& str); -/// Split string @p s at @p delim into @p left and @p right +/// Split string @p s at left-most @p delim into @p left and @p right void split_left(const std::string& s, char delim, std::string& left, std::string& right); -/// Split string @p s at @p delim and left and @p right +/// Split string @p s at right-most @p delim into @p left and @p right +void split_right(const std::string& s, char delim, std::string& left, std::string& right); + +/// Split string @p s at left-most @p delim into left and @p right /// @return the left part std::string split_left(const std::string& s, char delim, std::string& right); +/// Split string @p s at right-most @p delim into left and @p right +/// @return the left part +std::string split_right(const std::string& s, char delim, std::string& right); + /// Split string @p s at @p delim and return the splitted list in @p elems /// @return list of splitted strings std::vector& split(const std::string& s, char delim, std::vector& elems); diff --git a/server/server_settings.hpp b/server/server_settings.hpp index 7c5ce79b..e229445c 100644 --- a/server/server_settings.hpp +++ b/server/server_settings.hpp @@ -99,7 +99,7 @@ struct ServerSettings { std::string perm; name = utils::string::split_left(user_password_role, ':', perm); - password = utils::string::split_left(perm, ':', role_name); + password = utils::string::split_right(perm, ':', role_name); } /// user name diff --git a/test/test_main.cpp b/test/test_main.cpp index 56c26eb0..7d608e06 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -84,6 +84,23 @@ TEST_CASE("String utils") std::vector vec{"1", "2", "3"}; REQUIRE(container_to_string(vec) == "1, 2, 3"); + + std::string right; + std::string left = split_left("left:mid:right", ':', right); + REQUIRE(left == "left"); + REQUIRE(right == "mid:right"); + + left = split_right("left:mid:right", ':', right); + REQUIRE(left == "left:mid"); + REQUIRE(right == "right"); + + std::string user = split_left("username:password:with:colons:role", ':', right); + REQUIRE(user == "username"); + REQUIRE(right == "password:with:colons:role"); + std::string role; + std::string password = split_right(right, ':', role); + REQUIRE(password == "password:with:colons"); + REQUIRE(role == "role"); }