Skip to content

Commit d22efa3

Browse files
author
Carme, Pamy
committed
feat: add condition when using whitespace delimiter to handle more valid scenarios
Signed-off-by: Carme, Pamy <pamy.carme@sciencelogic.com>
1 parent 0a20a31 commit d22efa3

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

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: 28 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),
@@ -146,6 +149,21 @@ def item():
146149
assert rv.status_code == status
147150

148151

152+
@pytest.mark.parametrize(
153+
"header_string, expected_list",
154+
[
155+
("noexist,testnoexist,users ", ["noexist", "testnoexist", "users"]),
156+
("noexist testnoexist users", ["noexist", "testnoexist", "users"]),
157+
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
158+
("somegroup, group with space", ["somegroup", "group with space"]),
159+
("group with space", ["group with space"])
160+
]
161+
)
162+
def test_sanitize_group_headers(header_string, expected_list):
163+
header_list = CasbinEnforcer.sanitize_group_headers(header_string)
164+
assert header_list == expected_list
165+
166+
149167
def test_manager(app_fixture, enforcer):
150168
@app_fixture.route("/manager", methods=["POST"])
151169
@enforcer.manager
@@ -214,7 +232,8 @@ def owner_loader():
214232
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
215233
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
216234
("somegroup, group with space", ["somegroup", "group with space"]),
217-
("group with space", ["group with space"])
235+
("group with space", ["group with space"]),
236+
("group 'with, space", ["group 'with", "space"])
218237
]
219238
)
220239
def test_sanitize_group_headers(header_string, expected_list):
@@ -229,6 +248,13 @@ def test_sanitize_group_headers(header_string, expected_list):
229248
("noexist testnoexist users", ["noexist", "testnoexist", "users"]),
230249
("noexist, testnoexist, users", ["noexist,", "testnoexist,", "users"]),
231250
("somegroup, group with space", ["somegroup,", "group", "with", "space"]),
251+
('"agroup" "delimited by" "spaces"', ["agroup", "delimited by", "spaces"]),
252+
("'agroup' 'delimited by' 'spaces'", ["agroup", "delimited by", "spaces"]),
253+
("group'with space", ["group'with", "space"]),
254+
("group' with space", ["group'", "with", "space"]),
255+
("'group with' space", ["'group", "with'", "space"]), # quotes must be used on all groups, not only in 1
256+
('"group with space"', ["group with space"]),
257+
("'group with space'", ["group with space"]),
232258
("group with space", ["group", "with", "space"])
233259
]
234260
)

0 commit comments

Comments
 (0)