Possible SQL injection Open
results = ActiveRecord::Base.connection.execute sql
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Injection is #1 on the 2013 OWASP Top Ten web security risks. SQL injection is when a user is able to manipulate a value which is used unsafely inside a SQL query. This can lead to data leaks, data loss, elevation of privilege, and other unpleasant outcomes.
Brakeman focuses on ActiveRecord methods dealing with building SQL statements.
A basic (Rails 2.x) example looks like this:
User.first(:conditions => "username = '#{params[:username]}'")
Brakeman would produce a warning like this:
Possible SQL injection near line 30: User.first(:conditions => ("username = '#{params[:username]}'"))
The safe way to do this query is to use a parameterized query:
User.first(:conditions => ["username = ?", params[:username]])
Brakeman also understands the new Rails 3.x way of doing things (and local variables and concatenation):
username = params[:user][:name].downcase
password = params[:user][:password]
User.first.where("username = '" + username + "' AND password = '" + password + "'")
This results in this kind of warning:
Possible SQL injection near line 37:
User.first.where((((("username = '" + params[:user][:name].downcase) + "' AND password = '") + params[:user][:password]) + "'"))
See the Ruby Security Guide for more information and Rails-SQLi.org for many examples of SQL injection in Rails.
Class has too many lines. [255/250] Open
class Instrument < ApplicationRecord
# This model is exportable as DDI
include Exportable
# This model can be tracked using an Identifier
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks if the length a class exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Method copy
has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring. Open
def copy(new_prefix, other_vals = {})
new_i = self.dup
new_i.prefix = new_prefix
other_vals.select { |key, val| new_i[key] = val }
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method has too many lines. [46/30] Open
def copy(new_prefix, other_vals = {})
new_i = self.dup
new_i.prefix = new_prefix
other_vals.select { |key, val| new_i[key] = val }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Class Instrument
has 27 methods (exceeds 20 allowed). Consider refactoring. Open
class Instrument < ApplicationRecord
# This model is exportable as DDI
include Exportable
# This model can be tracked using an Identifier
- Create a ticketCreate a ticket
File instrument.rb
has 256 lines of code (exceeds 250 allowed). Consider refactoring. Open
class Instrument < ApplicationRecord
# This model is exportable as DDI
include Exportable
# This model can be tracked using an Identifier
- Create a ticketCreate a ticket
Cyclomatic complexity for copy is too high. [10/6] Open
def copy(new_prefix, other_vals = {})
new_i = self.dup
new_i.prefix = new_prefix
other_vals.select { |key, val| new_i[key] = val }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.
An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.
Method copy
has 46 lines of code (exceeds 25 allowed). Consider refactoring. Open
def copy(new_prefix, other_vals = {})
new_i = self.dup
new_i.prefix = new_prefix
other_vals.select { |key, val| new_i[key] = val }
- Create a ticketCreate a ticket
Useless assignment to variable - ccs
. Open
ccs = {}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end