mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-04-25 09:16:02 +03:00
[GH-ISSUE #657] Polynomial backtracking in some regexes #491
Labels
No labels
bug
enhancement
enhancement
enhancement
fixed in next
fixed in next
fixed in next
new rule
new rule
new rule
pull-request
question
refactoring
refactoring
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/markdownlint#491
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @RunDevelopment on GitHub (Nov 28, 2022).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/657
Hi!
We recently added markdownlint to our project, and so I took a look at the README which contained this regex:
I noticed
\s*$[^]*?. This part of the pattern can be exploited to create attack strings that take O(n^2) time to process. So luckily it's not exponential time ReDoS, but it should still be fixed.How to fix it:
The problem is that
\sand[^]*?can exchange characters,\r,\n, and 2 others to be exact. In your case, the problem is that the\s*can consume characters after the first line break and beyond. E.g. In---\n\n\n\nfoo\n---, the\swill consume the 3 first\nafter the starting---. The fix is to ensure that the\s*can't consume line ends, so replacing\s*with[ \t]*will fix the issue.While
[ \t]*will fix the issue, it's not an exact fix.\s*matches many kinds of spaces, not just ASCII space and tab. Whether you want to include those spaces as well is up to you. The easiest way to say "all spaces but no line ends" is(?:(?=.)\s)*btw.This fix also has to be applied to the 2 other occurrences of
\s*$[^]*?in the regex.Detecting ReDoS (and other regex problems)
Remember the project we added markdown lint to? That one actually detected this problem :)
(I might be able to see that
\s*$[^]*?is a problem from looking at it, but I can't tell you exactly which characters are the problem.)It's an ESLint plugin to detect problems in regexes, enforce best practices and some other stuff. You can use its
regexp/no-super-linear-backtrackingrule to find all cases of polynomial and exponential backtracking in your regexes (at least, all that it can detect).I recommend you add it to your code base, because this regex wasn't the only one with polynomial backtracking it found, and it likely won't be the last if you intend to use regexes in your code base.
If you need help fixing case of ReDoS, feel free to ask me on GitHub or per email.
Despite ReDoS being a security vulnerability, I opened this public issue. I did this for 3 reasons:
I would still recommend setting up a security policy that at least contains contact information.
@DavidAnson commented on GitHub (Nov 28, 2022):
Thanks for bringing this to my attention! As you probably know, CodeQL raises issues for this problem, and I spent time one or two releases ago addressing all of the issues it found.
It looks like it flagged this scenario, but only associated with the test case and so I dismissed it. https://github.com/DavidAnson/markdownlint/security/code-scanning/16
I will try your plug-in and address the issues it raises. As you say, this particular scenario does not seem overly concerning – especially because this is something the user can override via configuration.
@DavidAnson commented on GitHub (Dec 8, 2022):