]> git.apps.os.sepia.ceph.com Git - ceph.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)
committerXiubo Li <xiubli@redhat.com>
Thu, 9 Nov 2023 06:40:54 +0000 (14:40 +0800)
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>
src/mds/MDSAuthCaps.cc

index ca4add2f323b4cb9be3318b9bc8011ffce4ad5d8..e5d8c4854017bec379baf109fa6c8060b894cb95 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;
   }