]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mds: fix and improve MDSCapMatch::match_path()
authorXiubo Li <xiubli@redhat.com>
Wed, 8 Nov 2023 01:20:53 +0000 (09:20 +0800)
committerKotresh HR <khiremat@redhat.com>
Wed, 9 Oct 2024 08:01:40 +0000 (13:31 +0530)
Just in case the 'target_path' is tailed with '/' it may fail to
match the 'path', such as in case 'path=/foo/' and 'target_path=/foo'
it will fail.

Just try to remove the tailing '/' from 'target_path' and then
also we can simplify the code to make it more readable.

Signed-off-by: Xiubo Li <xiubli@redhat.com>
(cherry picked from commit cd706e469699c4b90ffd3802e64ce2c0543944d4)

src/mds/MDSAuthCaps.cc

index 23374c4039fad69f13922f7f02c62fc316af066c..8c8a196d2ffecbb9b5379b4b725146c29f7a041a 100644 (file)
@@ -167,14 +167,29 @@ bool MDSCapMatch::match(string_view target_path,
 
 bool MDSCapMatch::match_path(string_view target_path) const
 {
-  if (path.length()) {
-    if (target_path.find(path) != 0)
+  string _path = path;
+  // drop any tailing /
+  while (_path.length() && _path[_path.length() - 1] == '/') {
+    _path = path.substr(0, _path.length() - 1);
+  }
+
+  if (_path.length()) {
+    if (target_path.find(_path) != 0)
       return false;
-    // if path doesn't already have a trailing /, make sure the target
-    // does so that path=/foo doesn't match target_path=/food
-    if (target_path.length() > path.length() &&
-       path[path.length()-1] != '/' &&
-       target_path[path.length()] != '/')
+    /* In case target_path.find(_path) == 0 && target_path.length() == _path.length():
+     *  path=/foo  _path=/foo target_path=/foo     --> match
+     *  path=/foo/ _path=/foo target_path=/foo     --> match
+     *
+     * In case target_path.find(_path) == 0 && target_path.length() > _path.length():
+     *  path=/foo/ _path=/foo target_path=/foo/    --> match
+     *  path=/foo  _path=/foo target_path=/foo/    --> match
+     *  path=/foo/ _path=/foo target_path=/foo/d   --> match
+     *  path=/foo  _path=/foo target_path=/food    --> mismatch
+     *
+     * All the other cases                         --> mismatch
+     */
+    if (target_path.length() > _path.length() &&
+       target_path[_path.length()] != '/')
       return false;
   }