From 9afd9a29feb80acd30ce9fd77ddcf49bf624f94e Mon Sep 17 00:00:00 2001 From: pgjones Date: Tue, 26 Jul 2022 19:20:37 +0100 Subject: [PATCH] Bugfix ensure that the entire part is matched Previously the router would accept a partial match of a part, which is contrary to how the router is meant to work. For example if the rule to be matched was `d+` the router would match `2dfd` as `2` and silently forget the rest. --- src/werkzeug/routing/rules.py | 2 +- tests/test_routing.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/werkzeug/routing/rules.py b/src/werkzeug/routing/rules.py index f16e5668a2..5a33f894d7 100644 --- a/src/werkzeug/routing/rules.py +++ b/src/werkzeug/routing/rules.py @@ -625,7 +625,7 @@ def _parse_rule(self, rule: str) -> t.Iterable[RulePart]: -len(argument_weights), argument_weights, ) - if final: + if not static: content += r"$\Z" yield RulePart( content=content, final=final, static=static, weight=weight diff --git a/tests/test_routing.py b/tests/test_routing.py index 0c28a18673..4f9fc2cc33 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -1400,3 +1400,18 @@ def test_newline_match(): with pytest.raises(NotFound): a.match("/hello\n") + + +def test_weighting(): + m = r.Map( + [ + r.Rule("/", endpoint="int"), + r.Rule("/", endpoint="uuid"), + ] + ) + a = m.bind("localhost") + + assert a.match("/2b5b0911-fdcf-4dd2-921b-28ace88db8a0") == ( + "uuid", + {"value": uuid.UUID("2b5b0911-fdcf-4dd2-921b-28ace88db8a0")}, + )