Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: different behavior of glob_match from other casbin #254

Merged
merged 3 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 111 additions & 3 deletions casbin/util/builtin_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import ipaddress
import re

from wcmatch import pathlib

KEY_MATCH2_PATTERN = re.compile(r"(.*?):[^\/]+(.*?)")
KEY_MATCH3_PATTERN = re.compile(r"(.*?){[^\/]+}(.*?)")
KEY_MATCH4_PATTERN = re.compile(r"{([^/]+)}")
Expand Down Expand Up @@ -151,9 +149,119 @@ def regex_match_func(*args):
return regex_match(name1, name2)


def range_match(pattern, pattern_index, test):
"""check the if char `test` in string is match with the scope of [...] in pattern"""

pattern_len = len(pattern)
if pattern_index == pattern_len:
return -1
negate = pattern[pattern_index] == "!" or pattern[pattern_index] == "^"
if negate:
pattern_index += 1
ok = 0
while True:
if pattern_index == pattern_len:
break
c = pattern[pattern_index]
pattern_index += 1
if c == "]":
break
if c == "\\":
if pattern_index == pattern_len:
return -1
c = pattern[pattern_index]
pattern_index += 1
if (
pattern_index != pattern_len
and pattern[pattern_index] == "-"
and pattern_index + 1 != pattern_len
and pattern[pattern_index + 1] != "]"
):
c2 = pattern[pattern_index + 1]
pattern_index += 2
if c2 == "\\":
if pattern_index == pattern_len:
return -1
c2 = pattern[pattern_index]
pattern_index += 1
if c <= test <= c2:
ok = 1
elif c == test:
ok = 1

if ok == negate:
return -1
else:
return pattern_index


def glob_match(string, pattern):
"""determines whether string matches the pattern in glob expression."""
return pathlib.Path(string).globmatch(pattern)

pattern_len = len(pattern)
string_len = len(string)
if pattern_len == 0:
return string_len == 0
pattern_index = 0
string_index = 0
while True:
if pattern_index == pattern_len:
return string_len == string_index
c = pattern[pattern_index]
pattern_index += 1
if c == "?":
if string_index == string_len:
return False
if string[string_index] == "/":
return False
string_index += 1
continue
if c == "*":
while (pattern_index != pattern_len) and (c == "*"):
c = pattern[pattern_index]
pattern_index += 1
if pattern_index == pattern_len:
return string.find("/", string_index) == -1
else:
if c == "/":
string_index = string.find("/", string_index)
if string_index == -1:
return False
else:
string_index += 1
# General case, use recursion.
while string_index != string_len:
if glob_match(string[string_index:], pattern[pattern_index:]):
return True
if string[string_index] == "/":
break
string_index += 1
continue
if c == "[":
if string_index == string_len:
return False
if string[string_index] == "/":
return False
pattern_index = range_match(pattern, pattern_index, string[string_index])
if pattern_index == -1:
return False
string_index += 1
continue
if c == "\\":
if pattern_index == pattern_len:
c = "\\"
else:
c = pattern[pattern_index]
pattern_index += 1
# fall through
# other cases and c == "\\"
if string_index == string_len:
return False
else:
if c == string[string_index]:
string_index += 1
else:
return False


def glob_match_func(*args):
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
simpleeval >= 0.9.11
wcmatch >= 8.1.2
simpleeval >= 0.9.11
56 changes: 43 additions & 13 deletions tests/util/test_builtin_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ def test_glob_match(self):
self.assertFalse(util.glob_match_func("/foobar", "/foo"))
self.assertTrue(util.glob_match_func("/foobar", "/foo*"))
self.assertFalse(util.glob_match_func("/foobar", "/foo/*"))

self.assertTrue(util.glob_match_func("/foo", "*/foo"))
self.assertTrue(util.glob_match_func("/foo", "*/foo*"))
self.assertFalse(util.glob_match_func("/foo", "*/foo/*"))
self.assertFalse(util.glob_match_func("/foo/bar", "*/foo"))
self.assertFalse(util.glob_match_func("/foo/bar", "*/foo*"))
self.assertTrue(util.glob_match_func("/foo/bar", "*/foo/*"))
self.assertFalse(util.glob_match_func("/foobar", "*/foo"))
self.assertTrue(util.glob_match_func("/foobar", "*/foo*"))
self.assertFalse(util.glob_match_func("/foobar", "*/foo/*"))
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo"))
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo/*"))
Expand All @@ -133,7 +141,6 @@ def test_glob_match(self):
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo"))
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo/*"))

self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo/*"))
Expand All @@ -144,17 +151,40 @@ def test_glob_match(self):
self.assertFalse(util.glob_match_func("/prefix/subprefix/foobar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foobar", "*/foo/*"))

def test_glob_match2(self):
# add missing tests from Go to Python
self.assertFalse(util.glob_match_func("/foo", "*/foo")) # different from Go
self.assertFalse(util.glob_match_func("/foo", "*/foo*")) # different from Go
self.assertFalse(util.glob_match_func("/foo", "*/foo/*"))
self.assertFalse(util.glob_match_func("/foo/bar", "*/foo"))
self.assertFalse(util.glob_match_func("/foo/bar", "*/foo*"))
self.assertFalse(util.glob_match_func("/foo/bar", "*/foo/*")) # different from Go
self.assertFalse(util.glob_match_func("/foobar", "*/foo"))
self.assertFalse(util.glob_match_func("/foobar", "*/foo*")) # different from Go
self.assertFalse(util.glob_match_func("/foobar", "*/foo/*"))
self.assertTrue(util.glob_match_func("/f", "/?"))
self.assertTrue(util.glob_match_func("/foobar", "/foo?ar"))
self.assertFalse(util.glob_match_func("/fooar", "/foo?ar"))
self.assertTrue(util.glob_match_func("/foobbar", "/foo??ar"))
self.assertTrue(util.glob_match_func("/foobbbbar", "/foo????ar"))
self.assertTrue(util.glob_match_func("/foobar", "/foo[bc]ar"))
self.assertFalse(util.glob_match_func("/fooaar", "/foo[bc]ar"))
self.assertFalse(util.glob_match_func("/foodar", "/foo[bc]ar"))
self.assertTrue(util.glob_match_func("/foobar", "/foo[b-b]ar"))
self.assertFalse(util.glob_match_func("/fooaar", "/foo[b-c]ar"))
self.assertTrue(util.glob_match_func("/foobar", "/foo[b-c]ar"))
self.assertTrue(util.glob_match_func("/foocar", "/foo[b-c]ar"))
self.assertFalse(util.glob_match_func("/foodar", "/foo[b-c]ar"))
self.assertTrue(util.glob_match_func("/foo1ar", "/foo[!234]ar"))
self.assertFalse(util.glob_match_func("/foo3ar", "/foo[!234]ar"))
self.assertTrue(util.glob_match_func("/foo5ar", "/foo[!234]ar"))
self.assertTrue(util.glob_match_func("/foo1ar", "/foo[!2-5]ar"))
self.assertFalse(util.glob_match_func("/foo2ar", "/foo[!2-5]ar"))
self.assertTrue(util.glob_match_func("/foo1ar", "/foo[^234]ar"))
self.assertFalse(util.glob_match_func("/foo3ar", "/foo[^234]ar"))
self.assertTrue(util.glob_match_func("/foo5ar", "/foo[^234]ar"))
self.assertTrue(util.glob_match_func("/foo1ar", "/foo[^2-5]ar"))
self.assertFalse(util.glob_match_func("/foo2ar", "/foo[^2-5]ar"))

self.assertTrue(util.glob_match_func("\\", "\\\\"))
self.assertTrue(util.glob_match_func("/a", "/\\a"))
self.assertTrue(util.glob_match_func("/*", "/\\*"))
self.assertFalse(util.glob_match_func("a", "\\?"))
self.assertTrue(util.glob_match_func("?", "\\?"))
self.assertTrue(util.glob_match_func("\n", "\n"))
self.assertFalse(util.glob_match_func("\n", "\\n"))
self.assertTrue(util.glob_match_func("[", "\\["))
self.assertTrue(util.glob_match_func("*", "\\*"))
self.assertTrue(util.glob_match_func("\\*", "\\\\\\*"))
Nekotoxin marked this conversation as resolved.
Show resolved Hide resolved

def test_ip_match(self):
self.assertTrue(util.ip_match_func("192.168.2.123", "192.168.2.0/24"))
Expand Down