]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
rgw: modify string match_wildcards with fnmatch
authorzhipeng li <qiuxinyidian@gmail.com>
Fri, 25 Aug 2023 16:54:38 +0000 (00:54 +0800)
committerAdam Emerson <aemerson@redhat.com>
Wed, 5 Jun 2024 17:46:07 +0000 (13:46 -0400)
Fixes: https://tracker.ceph.com/issues/62292
Signed-off-by: zhipeng li <qiuxinyidian@gmail.com>
(cherry picked from commit 94fa9bd2d90857ca2c6a956a79406318ab2fa485)

https://tracker.ceph.com/issues/63971

Signed-off-by: Adam Emerson <aemerson@redhat.com>
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_iam_policy.cc
src/rgw/rgw_string.cc
src/rgw/rgw_string.h
src/test/rgw/test_rgw_iam_policy.cc

index f5d7912ea5be3d7bf5059d38aac00b82c0463810..845dfd67a4211bba1e7a0f36bc656d5ec839263b 100644 (file)
@@ -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) ?
index 648b2e087b491ea0e1b1379c2ca7557af2d09311..35cd53f598d8a5f76d1f12d059c5e2c9e575990f 100644 (file)
@@ -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);
index 35aeb15fcdc5aacc7b0e9180c4b774a171788339..cf56f0f5572887a30be3617d1c8386ec0aa70970 100644 (file)
@@ -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);
         }
index 7be82f854a844512dcf2b1d6b268d35a91756e76..420db96c4f2ee483d9d19fe108b8e965195f37ef 100644 (file)
@@ -2,44 +2,21 @@
 // vim: ts=8 sw=2 smarttab ft=cpp
 
 #include "rgw_string.h"
+#include <fnmatch.h>
 
-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;
 }
index e58a356f4715ce3e34c0959e4c06d4a23832b879..c6de42e38414424c4cc0578f6493f038d63a46f9 100644 (file)
@@ -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);
index f4c3c6aff6f1d2ed59f35f42115f080a28f12423..f1ef29a00f22b935a3cdc8449099609697a5d199 100644 (file)
@@ -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)