Ruby: fix qhelp

This commit is contained in:
Nick Rolfe
2021-10-28 10:42:56 +01:00
parent 11154a9409
commit bd92403b42
3 changed files with 19 additions and 21 deletions

View File

@@ -26,31 +26,12 @@ special meaning.
The following examples construct regular expressions from an HTTP request
parameter without sanitizing it first:
</p>
<sample language="ruby">
class UsersController < ActionController::Base
def first_example
# BAD: Unsanitized user input is used to construct a regular expression
regex = /#{ params[:key] }/
end
def second_example
# BAD: Unsanitized user input is used to construct a regular expression
regex = Regexp.new(params[:key])
end
end
</sample>
<sample src="examples/regexp_injection_bad.rb" />
<p>
Instead, the request parameter should be sanitized first. This ensures that the
user cannot insert characters that have special meanings in regular expressions.
</p>
<sample language="ruby">
class UsersController < ActionController::Base
def example
# GOOD: User input is sanitized before constructing the regular expression
regex = Regexp.new(Regex.escape(params[:key]))
end
end
</sample>
<sample src="examples/regexp_injection_good.rb" />
</example>
<references>

View File

@@ -0,0 +1,11 @@
class UsersController < ActionController::Base
def first_example
# BAD: Unsanitized user input is used to construct a regular expression
regex = /#{ params[:key] }/
end
def second_example
# BAD: Unsanitized user input is used to construct a regular expression
regex = Regexp.new(params[:key])
end
end

View File

@@ -0,0 +1,6 @@
class UsersController < ActionController::Base
def example
# GOOD: User input is sanitized before constructing the regular expression
regex = Regexp.new(Regex.escape(params[:key]))
end
end