From cd706e469699c4b90ffd3802e64ce2c0543944d4 Mon Sep 17 00:00:00 2001 From: Xiubo Li Date: Wed, 8 Nov 2023 09:20:53 +0800 Subject: [PATCH] mds: fix and improve MDSCapMatch::match_path() 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 --- src/mds/MDSAuthCaps.cc | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/mds/MDSAuthCaps.cc b/src/mds/MDSAuthCaps.cc index ca4add2f323..e5d8c485401 100644 --- a/src/mds/MDSAuthCaps.cc +++ b/src/mds/MDSAuthCaps.cc @@ -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; } -- 2.39.5