]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: modify string match_wildcards with fnmatch 57907/head
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 20:40:13 +0000 (16:40 -0400)
Fixes: https://tracker.ceph.com/issues/62292
Signed-off-by: zhipeng li <qiuxinyidian@gmail.com>
(cherry picked from commit 94fa9bd2d90857ca2c6a956a79406318ab2fa485)

Fixes: https://tracker.ceph.com/issues/63972
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 13c5b7e16d76f0961009334ac4a8339ca56ef424..6115a7fbed6cf1d314948376d4c49d5f2dfb60a6 100644 (file)
@@ -2128,7 +2128,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 c6f79c35fa852cb318ef36722a7f7bc3e2d022a9..72e65c083b90bc1d6b866e1d4851bb8d1cf0ece2 100644 (file)
@@ -1614,7 +1614,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 1ecb74281a73bc884f14f88ac36ff37c784b45a9..d73a04486713e8f03abc5cb7a95eb82b19cd7656 100644 (file)
@@ -529,7 +529,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 90e64f98a25874f27c7bd40169cfac3a1f5ebcf9..dc9e732e2b1957265d58b0f0ad53f687ce7f650d 100644 (file)
@@ -231,8 +231,8 @@ 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);
 
 #endif
index dac4d2cef3889c57c7efe318da5ec5659a83ba30..7bbb831078d054e5c80eaa9c4c870bb0d440bca4 100644 (file)
@@ -1250,9 +1250,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)