Skip to content

Commit fa76cbe

Browse files
committed
Ensure that rules without strict slashes match paths with slashes
A rule defined as `/path` with strict-slashes == False should match a path `/path/` but only if there isn't a rule that directly matches `/path/`. This restores the previous 2.2.0 behaviour.
1 parent 6a04415 commit fa76cbe

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Version 2.2.1
55

66
Unreleased
77

8+
- Fix router so that ``/path/`` will match a rule ``/path`` if strict
9+
slashes mode is disabled for the rule. :issue:`2467`
10+
811

912
Version 2.2.0
1013
-------------

src/werkzeug/routing/matcher.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ def _match(
129129
rv = _match(new_state, remaining, values + list(match.groups()))
130130
if rv is not None:
131131
return rv
132+
133+
# If there is no match and the only part left is a
134+
# trailing slash ("") consider rules that aren't
135+
# strict-slashes as these should match if there is a final
136+
# slash part.
137+
if parts == [""]:
138+
for rule in state.rules:
139+
if rule.strict_slashes:
140+
continue
141+
if rule.methods is not None and method not in rule.methods:
142+
have_match_for.update(rule.methods)
143+
elif rule.websocket != websocket:
144+
websocket_mismatch = True
145+
else:
146+
return rule, values
147+
132148
return None
133149

134150
try:

tests/test_routing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def test_strict_slashes_leaves_dont_consume():
219219
r.Rule("/path3/", endpoint="branch", strict_slashes=False),
220220
r.Rule("/path4", endpoint="leaf", strict_slashes=False),
221221
r.Rule("/path4/", endpoint="branch", strict_slashes=False),
222+
r.Rule("/path5", endpoint="leaf"),
222223
],
223224
strict_slashes=False,
224225
)
@@ -233,6 +234,7 @@ def test_strict_slashes_leaves_dont_consume():
233234
assert adapter.match("/path3/", method="GET") == ("branch", {})
234235
assert adapter.match("/path4", method="GET") == ("leaf", {})
235236
assert adapter.match("/path4/", method="GET") == ("branch", {})
237+
assert adapter.match("/path5/", method="GET") == ("leaf", {})
236238

237239

238240
def test_environ_defaults():

0 commit comments

Comments
 (0)