-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
regex URL converter matches incorrect value #2481
Comments
Werkzeug does not provide a |
Apologies for that, wasn't aware that this wrapped some other code. Here's a minimal reproducible pytest example which works in 2.1.2 but not in 2.2.0 nor 2.2.1: from werkzeug import routing as r
from werkzeug.routing import BaseConverter
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
def test_regex():
m = r.Map(
[
r.Rule("/<regex('(privacy|jamstack|capabilities)'):folder>/<image>", endpoint="simple"),
], converters={'regex': RegexConverter}
)
a = m.bind("localhost", path_info="/jamstack/random.png")
assert a.match() == ("simple", {"folder": "jamstack", "image": "random.png"}) Result from 2.2.1:
You can see it's incorrectly providing the |
If you change the rule to |
That works find for me so I'm happy enough with that. However, I do wonder if it limits routing somewhat for more complex regexs? For example if you wanted to match 2020-2021 you might use the following: r.Rule("/<regex('20(20|21|22)'):folder>/<image>", endpoint="simple") Here the grouping is used to group the options from anything before or after them. Anyway, I'm happy to close this issue and will let you make the call on that. |
You can use the non-grouping syntax if you need parens. |
You could do |
Yeah I've confirmed that
That doesn't seem to work in 2.1.2 nor 2.2.x Anyway, think with the first option, we've enough to close this. Or want to keep it open until you update the docs? |
This works for me, class RegexConverter(r.BaseConverter):
def __init__(self, url_map, *items):
super().__init__(url_map)
self.regex = items[0]
def test_regex():
m = r.Map(
[
r.Rule("/20<regex('21|22|23'):folder>/<image>", endpoint="simple"),
], converters={'regex': RegexConverter}
)
a = m.bind("localhost", path_info="/2021/random.png")
assert a.match() == ("simple", {"folder": "21", "image": "random.png"}) What didn't work for you? |
Oh, didn't notice that @pgjones wanted to document it. |
The |
This demonstrates why we don't have a For the original example, you'd be better off using the |
Yup today I learned! Will convert all my Regex converters to inbuilt ones as they seem to be sufficient for my needs. |
This should clarify that the regex itself is matched as a group and hence discourage gropuing within. See also pallets#2481.
See #2488 |
Thanks for this! I'm seeing a related failure in werkzeug 2.2.0's new router on the same custom I'm only using |
It looks like that's the same issue, you have a capturing group in your regex. The regexes for converters are intended to be fairly simple, with further validation done in |
@davidism thanks! Just FYI, I still see the hang even if I remove the capturing group, eg: app.add_url_rule(r'/<regex("[^/:]+\.[^/:]+")>', view_func=lambda: 'foo') |
Looks like the app.add_url_rule(r'/<regex("[X]")>', view_func=lambda: 'foo') (Still just FYI! I'm still happy to fix it on my end by simplifying or dropping my |
@pgjones this seems like an issue, because I'd expect a custom converter might legitimately want to use a character group |
|
Thanks for looking! Apologies, the missing variable names were oversights on my part, trying a bit too hard to minimize test cases. It does hang similarly with eg |
Added a second commit to #2489 I think fixes these issues. |
@pgjones this fix is great, all of my tests are passing again. Thank you! Looking forward to seing it released! |
they broke us due to pallets/werkzeug#2481 (comment) . 2.2.2 (pallets/werkzeug#2489) fixed this.
I have the following code in a Flask App using Werkzeug
prior to 2.2.0 this would allow the following redirect:
to
Now however it incorrectly sends us to:
Pinning werkzeug to 2.1.2 fixes the issue.
Environment:
The text was updated successfully, but these errors were encountered: