Skip to content

Commit bf4885f

Browse files
authored
Merge pull request #37 from favipcj/feat-add-unit-test-whitespace
feat: Add a condition to evaluate valid scenarios when using whitespace as delimiter
2 parents ae9a2e4 + 155bf94 commit bf4885f

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sphinx
55
sphinx_rtd_theme
66
pytest
77
pytest-cov
8-
casbin_sqlalchemy_adapter
8+
casbin_sqlalchemy_adapter==0.3.2
99
coverage
1010
pypi-publisher
1111
bumpversion

flask_authz/casbin_enforcer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from functools import wraps
66
from abc import ABC
77
from abc import abstractmethod
8+
import shlex
89

910
from flask_authz.utils import authorization_decoder, UnSupportedAuthType
1011

@@ -161,7 +162,9 @@ def sanitize_group_headers(headers_str, delimiter=',') -> list:
161162
Returns:
162163
list
163164
"""
164-
165+
if delimiter == ' ' and ((headers_str.startswith("'") and headers_str.endswith("'")) or (
166+
headers_str.startswith('"') and headers_str.endswith('"'))):
167+
return [string.strip() for string in shlex.split(headers_str) if string != ""]
165168
return [string.strip() for string in headers_str.split(delimiter) if string != ""]
166169

167170
def manager(self, func):

tests/test_casbin_enforcer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def enforcer(app_fixture):
2525
s.add(CasbinRule(ptype="p", v0="data2_admin", v1="/item", v2="GET"))
2626
s.add(CasbinRule(ptype="g", v0="alice", v1="data2_admin"))
2727
s.add(CasbinRule(ptype="g", v0="users", v1="data2_admin"))
28+
s.add(CasbinRule(ptype="g", v0="group with space", v1="data2_admin"))
2829
s.commit()
2930
s.close()
3031

@@ -57,10 +58,12 @@ def update_callback(self):
5758
("X-User", "bob", "POST", 401, None),
5859
("X-User", "bob", "DELETE", 401, None),
5960
("X-Idp-Groups", "admin", "GET", 401, "X-User"),
60-
("X-Idp-Groups", "users", "GET", 200, None),
61+
("X-Idp-Groups", "group with space, users", "GET", 200, None),
6162
("X-Idp-Groups", "noexist,testnoexist,users", "GET", 200, None),
6263
# ("X-Idp-Groups", "noexist testnoexist users", "GET", 200, None),
6364
("X-Idp-Groups", "noexist, testnoexist, users", "GET", 200, None),
65+
("X-Idp-Groups", "group with space", "GET", 200, None),
66+
("X-Idp-Groups", "somegroup, group with space", "GET", 200, None),
6467
("Authorization", "Basic Ym9iOnBhc3N3b3Jk", "GET", 200, "Authorization"),
6568
("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZGVudGl0eSI6ImJvYiJ9."
6669
"LM-CqxAM2MtT2uT3AO69rZ3WJ81nnyMQicizh4oqBwk", "GET", 200, None),
@@ -214,7 +217,8 @@ def owner_loader():
214217
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
215218
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
216219
("somegroup, group with space", ["somegroup", "group with space"]),
217-
("group with space", ["group with space"])
220+
("group with space", ["group with space"]),
221+
("group 'with, space", ["group 'with", "space"])
218222
]
219223
)
220224
def test_sanitize_group_headers(header_string, expected_list):
@@ -229,6 +233,13 @@ def test_sanitize_group_headers(header_string, expected_list):
229233
("noexist testnoexist users", ["noexist", "testnoexist", "users"]),
230234
("noexist, testnoexist, users", ["noexist,", "testnoexist,", "users"]),
231235
("somegroup, group with space", ["somegroup,", "group", "with", "space"]),
236+
('"agroup" "delimited by" "spaces"', ["agroup", "delimited by", "spaces"]),
237+
("'agroup' 'delimited by' 'spaces'", ["agroup", "delimited by", "spaces"]),
238+
("group'with space", ["group'with", "space"]),
239+
("group' with space", ["group'", "with", "space"]),
240+
("'group with' space", ["'group", "with'", "space"]), # quotes must be used on all groups, not only in 1
241+
('"group with space"', ["group with space"]),
242+
("'group with space'", ["group with space"]),
232243
("group with space", ["group", "with", "space"])
233244
]
234245
)

0 commit comments

Comments
 (0)