Skip to content

Commit 3334f91

Browse files
committed
[CVE-2024-6839] Sort Paths by Regex Specificity
1 parent 5da9be4 commit 3334f91

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

flask_cors/core.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ def parse_resources(resources):
6969
# resource of '*', which is not actually a valid regexp.
7070
resources = [(re_fix(k), v) for k, v in resources.items()]
7171

72-
# Sort by regex length to provide consistency of matching and
73-
# to provide a proxy for specificity of match. E.G. longer
74-
# regular expressions are tried first.
75-
def pattern_length(pair):
76-
maybe_regex, _ = pair
77-
return len(get_regexp_pattern(maybe_regex))
78-
79-
return sorted(resources, key=pattern_length, reverse=True)
72+
# Sort patterns with static (literal) paths first, then by regex specificity
73+
def sort_key(pair):
74+
pattern, _ = pair
75+
if isinstance(pattern, RegexObject):
76+
return (1, 0, pattern.pattern.count("/"), -len(pattern.pattern))
77+
elif probably_regex(pattern):
78+
return (1, 1, pattern.count("/"), -len(pattern))
79+
else:
80+
return (0, 0, pattern.count("/"), -len(pattern))
81+
82+
return sorted(resources, key=sort_key)
8083

8184
elif isinstance(resources, str):
8285
return [(re_fix(resources), {})]

tests/core/helper_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_parse_resources_sorted(self):
7878

7979
self.assertEqual(
8080
[r[0] for r in resources],
81-
[re.compile(r'/api/v1/.*'), '/foo', re.compile(r'/.*')]
81+
['/foo', re.compile(r'/api/v1/.*'), re.compile(r'/.*')]
8282
)
8383

8484
def test_probably_regex(self):

0 commit comments

Comments
 (0)