Escape pattern before creating a regular expression from it#123
Escape pattern before creating a regular expression from it#123parndt wants to merge 1 commit intobbenno:masterfrom parndt:bugfix/regexp-escaping
pattern before creating a regular expression from it#123Conversation
|
After opening and looking to add a test it occurred to me that maybe it's a necessary feature that the input is not escaped, as the caller can easily do this before calling Thanks for the gem! |
|
@bbenno or we could do this: def search(pattern, case_sensitive: true)
unless case_sensitive
pattern = Regexp.escape(pattern) if pattern.is_a?(String)
pattern = Regexp.new(pattern, Regexp::IGNORECASE).freeze
end
all.select { |l| l.name.match? pattern }
endLet me know if you want this patch and I'll reopen with tests. |
|
Hi @parndt, Thank you for your contribution. Your initiative is highly appreciated. I propose delineating the security concerns into two distinct categories: Regular Expression Denial of Service (ReDoS) prevention and input validation. Regarding ReDoS prevention, the current design permits the passing of arbitrary regular expressions as arguments, which is an intentional feature rather than a flaw. Escaping the Concerning input validation, if Any thoughts on this? |
|
thanks @bbenno that is great feedback Do you mean like this: def search(pattern)
raise ArgumentError, "Pattern must be a Regexp" unless pattern.is_a?(Regexp)
all.select { |l| l.name.match?(pattern) }
endI think this is probably a good idea. It was definitely surprising that the string turned into a Regexp, until I read the source code. |
|
OK, this will be included in the next version that will be released 🔜. |
By creating a regular expression from what might be user input we are
allowing the system to run arbitrary regular expressions, which could be
a security concern.
We can simply escape it to avoid potential problems. Benchmarking shows
that this runs comparatively quickly: