From: zhipeng li Date: Fri, 25 Aug 2023 16:54:38 +0000 (+0800) Subject: rgw: modify string match_wildcards with fnmatch X-Git-Tag: testing/wip-xiubli-testing-20240812.080715-reef~6^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9612ff3be41fd324c996fbca4a2f1761db8b8d93;p=ceph-ci.git rgw: modify string match_wildcards with fnmatch Fixes: https://tracker.ceph.com/issues/62292 Signed-off-by: zhipeng li (cherry picked from commit 94fa9bd2d90857ca2c6a956a79406318ab2fa485) https://tracker.ceph.com/issues/63971 Signed-off-by: Adam Emerson --- diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index f5d7912ea5b..845dfd67a42 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -2162,7 +2162,7 @@ int rgw_parse_op_type_list(const string& str, uint32_t *perm) return rgw_parse_list_of_flags(op_type_mapping, str, perm); } -bool match_policy(std::string_view pattern, std::string_view input, +bool match_policy(const std::string& pattern, const std::string& input, uint32_t flag) { const uint32_t flag2 = flag & (MATCH_POLICY_ACTION|MATCH_POLICY_ARN) ? diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 648b2e087b4..35cd53f598d 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -1753,7 +1753,7 @@ static constexpr uint32_t MATCH_POLICY_RESOURCE = 0x02; static constexpr uint32_t MATCH_POLICY_ARN = 0x04; static constexpr uint32_t MATCH_POLICY_STRING = 0x08; -extern bool match_policy(std::string_view pattern, std::string_view input, +extern bool match_policy(const std::string& pattern, const std::string& input, uint32_t flag); extern std::string camelcase_dash_http_attr(const std::string& orig); diff --git a/src/rgw/rgw_iam_policy.cc b/src/rgw/rgw_iam_policy.cc index 35aeb15fcdc..cf56f0f5572 100644 --- a/src/rgw/rgw_iam_policy.cc +++ b/src/rgw/rgw_iam_policy.cc @@ -578,7 +578,7 @@ bool ParseState::do_string(CephContext* cct, const char* s, size_t l) { t->action = allValue : t->notaction = allValue); } else { for (auto& p : actpairs) { - if (match_policy({s, l}, p.name, MATCH_POLICY_ACTION)) { + if (match_policy(string(s, l), p.name, MATCH_POLICY_ACTION)) { is_validaction = true; (w->id == TokenID::Action ? t->action[p.bit] = 1 : t->notaction[p.bit] = 1); } diff --git a/src/rgw/rgw_string.cc b/src/rgw/rgw_string.cc index 7be82f854a8..420db96c4f2 100644 --- a/src/rgw/rgw_string.cc +++ b/src/rgw/rgw_string.cc @@ -2,44 +2,21 @@ // vim: ts=8 sw=2 smarttab ft=cpp #include "rgw_string.h" +#include -static bool char_eq(char c1, char c2) -{ - return c1 == c2; -} - -static bool ci_char_eq(char c1, char c2) -{ - return tolower(c1) == tolower(c2); -} - -bool match_wildcards(std::string_view pattern, std::string_view input, +bool match_wildcards(const std::string& pattern, const std::string& input, uint32_t flags) { - const auto eq = (flags & MATCH_CASE_INSENSITIVE) ? &ci_char_eq : &char_eq; + bool case_insensive = flags & MATCH_CASE_INSENSITIVE; + uint32_t flag = 0; + + if (case_insensive) { + flag = FNM_CASEFOLD; + } - auto it1 = pattern.begin(); - auto it2 = input.begin(); - while (true) { - if (it1 == pattern.end()) - return it2 == input.end(); - if (*it1 == '*') { - if (it1 + 1 == pattern.end()) - return true; - if (it2 == input.end() || eq(*(it1 + 1), *it2)) - ++it1; - else - ++it2; - continue; - } - if (it2 == input.end()) - return false; - if (*it1 == '?' || eq(*it1, *it2)) { - ++it1; - ++it2; - continue; - } + if (fnmatch(pattern.data(), input.data(), flag) == 0) { + return true; + } else { return false; } - return false; } diff --git a/src/rgw/rgw_string.h b/src/rgw/rgw_string.h index e58a356f471..c6de42e3841 100644 --- a/src/rgw/rgw_string.h +++ b/src/rgw/rgw_string.h @@ -230,6 +230,6 @@ static constexpr uint32_t MATCH_CASE_INSENSITIVE = 0x01; /// attempt to match the given input string with the pattern, which may contain /// the wildcard characters * and ? -extern bool match_wildcards(std::string_view pattern, - std::string_view input, +extern bool match_wildcards(const std::string& pattern, + const std::string& input, uint32_t flags = 0); diff --git a/src/test/rgw/test_rgw_iam_policy.cc b/src/test/rgw/test_rgw_iam_policy.cc index f4c3c6aff6f..f1ef29a00f2 100644 --- a/src/test/rgw/test_rgw_iam_policy.cc +++ b/src/test/rgw/test_rgw_iam_policy.cc @@ -1258,9 +1258,8 @@ TEST(MatchWildcards, Asterisk) "http://example.com/index.html")); EXPECT_TRUE(match_wildcards("http://example.com/*/*.jpg", "http://example.com/fun/smiley.jpg")); - // note: parsing of * is not greedy, so * does not match 'bc' here - EXPECT_FALSE(match_wildcards("a*c", "abcc")); - EXPECT_FALSE(match_wildcards("a*c", "abcc", MATCH_CASE_INSENSITIVE)); + EXPECT_TRUE(match_wildcards("a*c", "abcc")); + EXPECT_TRUE(match_wildcards("a*c", "abcc", MATCH_CASE_INSENSITIVE)); } TEST(MatchPolicy, Action)