fix regexp#6
Merged
k0kubun merged 1 commit intoruby:masterfrom Jan 15, 2022
Merged
Conversation
k0kubun
reviewed
Jan 14, 2022
| s.scan(re) do | ||
| comment = $+ | ||
| comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/] | ||
| comment = $1 if comment[/-\*-\s*([^\s].*?)\s*-*-$/] |
Member
There was a problem hiding this comment.
Is it intentional that you changed the behavior of the regexp?
"-*--"[/-\*-\s*(.*?)\s*-*-$/] #=> "-*--"
"-*--"[/-\*-\s*([^\s].*?)\s*-*-$/] #=> nilFirst of all, I believe the magic comment syntax is -*- xxx -*-, so it's weird that the last * is not escaped. Would just escaping it fix the ReDoS problem?
Contributor
Author
There was a problem hiding this comment.
No, the change in regexp behavior was unintentional.
Even if escape last *, the ReDoS problem seems to remain.
# current
❯ time ruby -e '"-*-#{" "* 3456}"[/-\*-\s*(.*?)\s*-*-$/]'
ruby -e '"-*-#{" "* 3456}"[/-\*-\s*(.*?)\s*-*-$/]' 41.47s user 0.14s system 99% cpu 41.773 total
# escape last `*`
❯ time ruby -e '"-*-#{" "* 3456}"[/-\*-\s*(.*?)\s*-\*-$/]'
ruby -e '"-*-#{" "* 3456}"[/-\*-\s*(.*?)\s*-\*-$/]' 30.50s user 0.12s system 99% cpu 30.741 total
# fix ReDoS
❯ time ruby -e '"-*-#{" "* 3456}"[/-\*-\s*([^\s].*?)\s*-*-$/]'
ruby -e '"-*-#{" "* 3456}"[/-\*-\s*([^\s].*?)\s*-*-$/]' 0.05s user 0.05s system 73% cpu 0.129 total
# fix ReDoS and escape last `*`
❯ time ruby -e '"-*-#{" "* 3456}"[/-\*-\s*([^\s].*?)\s*-\*-$/]'
ruby -e '"-*-#{" "* 3456}"[/-\*-\s*([^\s].*?)\s*-\*-$/]' 0.05s user 0.05s system 75% cpu 0.122 total
/-\*-\s*([^\s].*?)\s*-\*-$/ seems to be a regular expression with the correct intent.
Member
There was a problem hiding this comment.
I was only looking at this line, but looking at how comment is used, the change seems fine. So I'll merge your change, also addressing \* separately.
k0kubun
approved these changes
Jan 15, 2022
matzbot
pushed a commit
to ruby/ruby
that referenced
this pull request
Jan 15, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I used Regexploit to find a regular expression that could be ReDoS for erb.
It's not a security issue, and I think it's extremely rare that it could affect performance.
sample
Results of Regexploit