-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Ruby: add regex injection query #6978
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
Conversation
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelp |
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
I still need to get a successful DCA run to check performance, but otherwise I think this ready for review. |
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
I still need to add a missing QLDoc comment. More worryingly, the DCA runs show that this query causes significant slowdowns. I'll have to investigate when I have time. |
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
As far as I can tell from DCA, the new query takes about the same amount of time as any other dataflow query. |
@@ -335,3 +336,18 @@ class ModuleEvalCallCodeExecution extends CodeExecution::Range, DataFlow::CallNo | |||
|
|||
override DataFlow::Node getCode() { result = this.getArgument(0) } | |||
} | |||
|
|||
/** Flow summary for `Regexp.escape`. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not one for this PR, but I wonder if we ought to split out StandardLibrary.qll
soon? Maybe something like (following the structure of https://ruby-doc.com/):
ql/lib/codeql/ruby/frameworks
├── core
│ ├── Kernel.qll
│ └── Object.qll
│ └── Regexp.qll
└── std-lib
├── Date.qll
└── Open3.qll
RegexpEscapeSummary() { this = "Regexp.escape" } | ||
|
||
override MethodCall getACall() { | ||
result = API::getTopLevelMember("Regexp").getAMethodCall("escape").asExpr().getExpr() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regexp.quote
is an alias for Regexp.escape
, so we could add that here too (and in RegExpInjectionCustomizations
).
class ConstructedRegExpAsSink extends Sink { | ||
ConstructedRegExpAsSink() { | ||
this = | ||
API::getTopLevelMember("Regexp").getAnInstantiation().(DataFlow::CallNode).getArgument(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few other ways I've found for creating regexes:
Regexp.compile
, which is an alias forRegexp.new
Regexp.union
, which will create a regex matching a union of its arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah but Regexp.union
escapes its arguments, so we don't have to bother with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me! I only have a couple of small suggestions for additional sinks/sanitizers.
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
@nickrolfe it looks like some consistency tests are failing now, which I assume is unrelated to this change. Not sure what we can do to fix them, but ping me if you get this green and I'll 👍 it. |
QHelp previews: ruby/ql/src/queries/security/cwe-1333/RegExpInjection.qhelpRegular expression injectionConstructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack. RecommendationBefore embedding user input into a regular expression, use a sanitization function such as ExampleThe following examples construct regular expressions from an HTTP request parameter without sanitizing it first: 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 Instead, the request parameter should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions. 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 References
|
@hmac it's all green now! |
No description provided.