-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Regular expression catastrophic backtracking in bottle.Router.rule_syntax #1194
Comments
The pattern should match "everything up until the next non-excaped I'm not sure if this is really an issue, because route definitions are usually short, controlled by the developer, and evaluated only once. #1155 is way more serious and I have no idea how to fix that yet. Any help would be welcomed. For this one, would removing the inner
|
Forgot to mention: Tools being able to detect these issues are a HUGE win and I hope python adds ways around these errors in the future. Most mitigations against catastrophic backtracking are not possible in python because the regular expression engine lacks support for certain features. No idea how to fix #1155 because of that :/ |
Unfortunately removing the inner
The issue is that the alternation or branch (
To match "everything up until the next non-escaped >" could we simply do I'm confused about significance of the literal dot. If they aren't important we can just let Thoughts? |
I think you misinterpret what the regular expression actually does. Count the backslashes. The python String Removing the inner repetition should fix the backtracking issue.
That seems to work as expected, and no longer hang for large invalid input. |
Ah, I see. Normally I use raw strings (e.g. |
I finally understood this issue a bit better today and was able to (hopefully) fix three backtracking issues in bottle with your help. Thanks a lot for that! |
Glad we got everything figured out! I know, catastrophic backtracking regular expressions caused some catastrophic backtracking in my brain for a long time before I fully understood them :) |
Related to bottlepy#1194 This backports the patch from 332215b to the 0.12 release branch.
…ttle.Router.rule_syntax This backports the patch from aaee93a to the 0.12 release branch.
Related to bottlepy#1194 This backports the patch from 332215b to the 0.12 release branch. This fix can be validated using the following repl commands: >>> import bottle >>> bottle.template("""<img src="{{usar_webp(''/static/images/branco400.jpg')}}" alt="Sem imagem"/>""")
…ttle.Router.rule_syntax This backports the patch from aaee93a to the 0.12 release branch. This fix can be validated with the following command from the issue: python -c "import bottle; list(bottle.Router.rule_syntax.finditer('<abc:def:' + '.' * 64 + '<'))"
Related to bottlepy#1194 This backports the patch from 332215b to the 0.12 release branch. This fix can be validated using the following repl commands: >>> import bottle >>> bottle.template("""<img src="{{usar_webp(''/static/images/branco400.jpg')}}" alt="Sem imagem"/>""")
…ttle.Router.rule_syntax This backports the patch from aaee93a to the 0.12 release branch. This fix can be validated with the following command from the issue: python -c "import bottle; list(bottle.Router.rule_syntax.finditer('<abc:def:' + '.' * 64 + '<'))"
…ter.rule_syntax This backports the patch from aaee93a to the 0.12 release branch. This fix can be validated with the following command from the issue: python -c "import bottle; list(bottle.Router.rule_syntax.finditer('<abc:def:' + '.' * 64 + '<'))"
Hi there!
I'm working on a static analysis tool called Dlint. A rule I've recently written looks for catastrophic backtracking in the Python
re
module. Looks like this has come up before in #1155.After running this rule against your code base I found one issue:
If we look at the
rule_syntax
expression we can indeed confirm it has catastrophic cases:Under normal cases the expression runs quickly:
But if we craft a special subject string we can cause it to catastrophically backtrack:
The culprit here is this portion of the expression:
Due to mutually inclusive alternation, a long string of dots (
.
) will backtrack this expression. Changing the expression to the following should fix the issue:If this breaks intended functionality there may be other ways to fix the expression.
Let me know if you have any questions, and I hope you'll check out Dlint! Note that the
DUO138
catastrophic backtracking check hasn't been released to PyPI yet as I'm still ironing out some kinks. Installing from the Github repository itself will provide the new rule, though.The text was updated successfully, but these errors were encountered: