-
Notifications
You must be signed in to change notification settings - Fork 102
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
ReDOS possible in the sanitize_html function #73
Comments
Apparently CVE-2023-31606 is assigned for this issue. |
@e23e Thanks so much for investigating this! We're using it on our platform, and would like to stay with Textile if possible. Do you have a fix for the issue already? If not, I think I can help with the RegExp, and then we could at least create a fork 🤔 |
Changing the regex to: text.gsub!( /<(\/*)(?>[A-Za-z]\w*)([^>]*?)(\s?\/?)>/ ) do |m| already speeds this up by 30x. Edit: The version below doesn't work! Use this one instead: #73 (comment). Allowing open tags (without a text.gsub!( /<(\/*)(?>[A-Za-z]\w*)([^>]*?)(\s?\/?>?)/ ) do |m|
…
"<#{raw[1]}#{pcs.join " "}#{raw[4]}" |
Hi @korny
The Could you please let me know your thoughts on above? |
After a bit of testing, I'm positive that my first version already fixes the problem for good. The use of Atomic Grouping effectively prevents backtracking in the second group (which matches the tag). The same can be achieved with a possessive quantifier: # v-- This "+" does the trick
text.gsub!( /<(\/*)([A-Za-z]\w*+)([^>]*?)(\s?\/?)>/ ) do |m| In this case, the By the way: Ruby 3.2 doesn't even need this patch because it uses some smart caching. |
Hi @korny, I gave below as input.
With the old regex, it returns the below output.
But with the new regex, it returns the below output.
I have added attached below the test code I ran
|
I'm sorry, it seems I had a copy/paste error in my comment. The only change I propose is the Here's the PR: https://github.com/jgarber/redcloth/pull/75/files. Thanks for testing this! |
Hi @korny It works fine and fixes the ReDOS issue, |
@korny : while working on adding tests to ensure the regexp will not take a long time, I've noticed your proposed regexp fails on Recheck playgound (https://makenowjust-labs.github.io/recheck/playground/). However, on my development laptop, I was able to replicate the time spent between the old regexp and the one you provided in #75 . Do you have any thoughts? |
Recheck can’t really read Ruby regexps. It doesn’t support all of the syntax that Ruby provides. Don’t use it to verify Ruby regexps. |
Thank you @korny . |
RedCloth v4.3.3 has been release to RubyGems! 🎉 |
There is a security vulnerability in this gem. I tried to communicate with the maintainers in an email, but still haven't got a response, I'm raising the issue here.
Vulnerable Code:
https://github.com/jgarber/redcloth/blob/v4.3.2/lib/redcloth/formatters/html.rb#L327
The above code has /<(/)([A-Za-z]\w*)([^>]?)(\s?/?)>/ regex and it matches user-provided input with the regex pattern, the above regex is vulnerable to ReDOS
time ruby test.rb
command and observe the time and CPU taken to process itTest Output:
Fix:
The text was updated successfully, but these errors were encountered: