Skip to content

Commit 0683ca3

Browse files
committed
fix: support default delimiter for sanitize_group_headers()
Signed-off-by: ffyuanda <46557895+ffyuanda@users.noreply.github.com>
1 parent c6f28cd commit 0683ca3

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

flask_authz/casbin_enforcer.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def owner_loader(self, callback):
5959
self._owner_loader = callback
6060
return callback
6161

62-
def enforcer(self, func):
62+
def enforcer(self, func, delimiter=','):
6363
@wraps(func)
6464
def wrapper(*args, **kwargs):
6565
if self.e.watcher and self.e.watcher.should_reload():
@@ -117,7 +117,8 @@ def wrapper(*args, **kwargs):
117117
# Split header by ',' in case of groups when groups are
118118
# sent "group1,group2,group3,..." in the header
119119
for owner in self.sanitize_group_headers(
120-
request.headers.get(header)
120+
request.headers.get(header),
121+
delimiter
121122
):
122123
self.app.logger.debug(
123124
"Enforce against owner: %s header: %s"
@@ -149,26 +150,19 @@ def wrapper(*args, **kwargs):
149150
return wrapper
150151

151152
@staticmethod
152-
def sanitize_group_headers(headers_str):
153+
def sanitize_group_headers(headers_str, delimiter=',') -> list:
153154
"""
154155
Sanitizes group header string so that it is easily parsable by enforcer
155156
removes extra spaces, and converts comma delimited or white space
156157
delimited list into a list.
158+
159+
Default delimiter: "," (comma)
160+
157161
Returns:
158-
str
162+
list
159163
"""
160-
# If there are commas and white space in the string,
161-
# remove the whitespace
162-
if " " in headers_str and "," in headers_str:
163-
headers_str = headers_str.replace(" ", "")
164-
# If there are no commas in the string, return a list
165-
# delimited by whitespace
166-
if " " in headers_str and "," not in headers_str:
167-
return headers_str.split(" ")
168-
# There are commas and no whitespace in the string, return a list
169-
# delimited by commas
170-
else:
171-
return headers_str.split(",")
164+
165+
return [string.strip() for string in headers_str.split(delimiter) if string != ""]
172166

173167
def manager(self, func):
174168
"""Get the Casbin Enforcer Object to manager Casbin"""

tests/test_casbin_enforcer.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def update_callback(self):
5959
("X-Idp-Groups", "admin", "GET", 401, "X-User"),
6060
("X-Idp-Groups", "users", "GET", 200, None),
6161
("X-Idp-Groups", "noexist,testnoexist,users", "GET", 200, None),
62-
("X-Idp-Groups", "noexist testnoexist users", "GET", 200, None),
62+
# ("X-Idp-Groups", "noexist testnoexist users", "GET", 200, None),
6363
("X-Idp-Groups", "noexist, testnoexist, users", "GET", 200, None),
6464
("Authorization", "Basic Ym9iOnBhc3N3b3Jk", "GET", 200, "Authorization"),
6565
("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZGVudGl0eSI6ImJvYiJ9."
@@ -205,3 +205,33 @@ def owner_loader():
205205
caller = getattr(c, method.lower())
206206
rv = caller("/item")
207207
assert rv.status_code == status
208+
209+
210+
@pytest.mark.parametrize(
211+
"header_string, expected_list",
212+
[
213+
("noexist,testnoexist,users ", ["noexist", "testnoexist", "users"]),
214+
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
215+
("noexist, testnoexist, users", ["noexist", "testnoexist", "users"]),
216+
("somegroup, group with space", ["somegroup", "group with space"]),
217+
("group with space", ["group with space"])
218+
]
219+
)
220+
def test_sanitize_group_headers(header_string, expected_list):
221+
header_list = CasbinEnforcer.sanitize_group_headers(header_string)
222+
assert header_list == expected_list
223+
224+
225+
@pytest.mark.parametrize(
226+
"header_string, expected_list",
227+
[
228+
("noexist testnoexist users ", ["noexist", "testnoexist", "users"]),
229+
("noexist testnoexist users", ["noexist", "testnoexist", "users"]),
230+
("noexist, testnoexist, users", ["noexist,", "testnoexist,", "users"]),
231+
("somegroup, group with space", ["somegroup,", "group", "with", "space"]),
232+
("group with space", ["group", "with", "space"])
233+
]
234+
)
235+
def test_sanitize_group_headers_with_whitespace(header_string, expected_list):
236+
header_list = CasbinEnforcer.sanitize_group_headers(header_string, ' ')
237+
assert header_list == expected_list

tests/test_casbin_enforcer_init_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def update_callback(self):
6565
("X-Idp-Groups", "admin", "GET", 401, "X-User"),
6666
("X-Idp-Groups", "users", "GET", 200, None),
6767
("X-Idp-Groups", "noexist,testnoexist,users", "GET", 200, None),
68-
("X-Idp-Groups", "noexist testnoexist users", "GET", 200, None),
68+
# ("X-Idp-Groups", "noexist testnoexist users", "GET", 200, None),
6969
("X-Idp-Groups", "noexist, testnoexist, users", "GET", 200, None),
7070
("Authorization", "Basic Ym9iOnBhc3N3b3Jk", "GET", 200, "Authorization"),
7171
("Authorization", "Unsupported Ym9iOnBhc3N3b3Jk", "GET", 401, None),

0 commit comments

Comments
 (0)