Possible SQL injection Open
matching_extended_fields = ContentType.find_by_class_name(self.class.name).form_fields.find(:all, conditions: "import_synonyms like \'%#{key}%\'")
- Read upRead up
- 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.
Assignment Branch Condition size for populate_attributes_from_embedded_in is too high. [86.18/15] Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
- Read upRead up
- Exclude checks
This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
Method has too many lines. [78/10] Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
- Read upRead up
- 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.
Method populate_attributes_from_embedded_in
has a Cognitive Complexity of 53 (exceeds 5 allowed). Consider refactoring. Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
- Read upRead up
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 populate_attributes_from_embedded_in
has 78 lines of code (exceeds 25 allowed). Consider refactoring. Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
Cyclomatic complexity for populate_attributes_from_embedded_in is too high. [16/6] Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
- Read upRead up
- 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.
Perceived complexity for populate_attributes_from_embedded_in is too high. [17/7] Open
def populate_attributes_from_embedded_in(file_path)
# if there is no file we just leave it up to validation
# to sort out what needs doing
return unless File.exist?(file_path)
- Read upRead up
- Exclude checks
This cop tries to produce a complexity score that's a measure of the
complexity the reader experiences when looking at a method. For that
reason it considers when
nodes as something that doesn't add as much
complexity as an if
or a &&
. Except if it's one of those special
case
/when
constructs where there's no expression after case
. Then
the cop treats it as an if
/elsif
/elsif
... and lets all the when
nodes count. In contrast to the CyclomaticComplexity cop, this cop
considers else
nodes as adding complexity.
Example:
def my_method # 1
if cond # 1
case var # 2 (0.8 + 4 * 0.2, rounded)
when 1 then func_one
when 2 then func_two
when 3 then func_three
when 4..10 then func_other
end
else # 1
do_something until a && b # 2
end # ===
end # 7 complexity points
Block has too many lines. [50/25] Open
embedded.each do |key, value|
# get rid of any extra white space at beginning or end of value
value = value.strip if value.is_a?(String)
# accept ; as demarkation of separate values
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Avoid deeply nested control flow statements. Open
if current_value.is_a?(String)
current_value += ' '
value = value.to_s
end
Block has too many lines. [26/25] Open
standard_attribute_synonyms.each do |a_name, synonyms|
# if the embedded key in the list of the attribute's synonyms
# we have a match and should assign the value of the embedded key's value
if synonyms.include?(key)
case a_name
- Read upRead up
- Exclude checks
This cop checks if the length of a block exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable. The cop can be configured to ignore blocks passed to certain methods.
Avoid more than 3 levels of block nesting. Open
if current_value.blank? || current_value =~ /^-replace-/
send("#{a_name}=", value)
else
if current_value.is_a?(String)
current_value += ' '
- Read upRead up
- Exclude checks
This cop checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
TODO found Open
# TODO: this may be MySQL specific, test with PostgreSQL
- Exclude checks
TODO found Open
# TODO: wrap this handling of name variants
- Exclude checks
Shadowing outer local variable - value
. Open
value.to_a.each do |value|
- Read upRead up
- Exclude checks
This cop looks for use of the same name as outer local variables
for block arguments or block local variables.
This is a mimic of the warning
"shadowing outer local variable - foo" from ruby -cw
.
Example:
# bad
def some_method
foo = 1
2.times do |foo| # shadowing outer `foo`
do_something(foo)
end
end
Example:
# good
def some_method
foo = 1
2.times do |bar|
do_something(bar)
end
end
%w
-literals should be delimited by [
and ]
. Open
conditions += " AND name NOT LIKE 'Short Summary%'" unless %w(Topic Document).include?(self.class.name)
- Read upRead up
- Exclude checks
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Example:
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Closing array brace must be on the line after the last array element when opening brace is on a separate line from the first array element. Open
raw_attribute_name]
- Read upRead up
- Exclude checks
This cop checks that the closing brace in an array literal is either on the same line as the last array element, or a new line.
When using the symmetrical
(default) style:
If an array's opening brace is on the same line as the first element of the array, then the closing brace should be on the same line as the last element of the array.
If an array's opening brace is on the line above the first element of the array, then the closing brace should be on the line below the last element of the array.
When using the new_line
style:
The closing brace of a multi-line array literal must be on the line after the last element of the array.
When using the same_line
style:
The closing brace of a multi-line array literal must be on the same line as the last element of the array.
Example: EnforcedStyle: symmetrical (default)
# bad
[ :a,
:b
]
# bad
[
:a,
:b ]
# good
[ :a,
:b ]
# good
[
:a,
:b
]
Example: EnforcedStyle: new_line
# bad
[
:a,
:b ]
# bad
[ :a,
:b ]
# good
[ :a,
:b
]
# good
[
:a,
:b
]
Example: EnforcedStyle: same_line
# bad
[ :a,
:b
]
# bad
[
:a,
:b
]
# good
[
:a,
:b ]
# good
[ :a,
:b ]
Use next
to skip iteration. Open
if synonyms.include?(key)
- Read upRead up
- Exclude checks
Use next
to skip iteration instead of a condition at the end.
Example: EnforcedStyle: skipmodifierifs (default)
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
# good
[1, 2].each do |o|
puts o unless o == 1
end
Example: EnforcedStyle: always
# With `always` all conditions at the end of an iteration needs to be
# replaced by next - with `skip_modifier_ifs` the modifier if like
# this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
# bad
[1, 2].each do |o|
puts o unless o == 1
end
# bad
[1, 2].each do |a|
if a == 1
puts a
end
end
# good
[1, 2].each do |a|
next unless a == 1
puts a
end
Pass &:strip
as an argument to collect
instead of a block. Open
value = value.split(';').collect { |i| i.strip }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Pass &:blank?
as an argument to reject
instead of a block. Open
all_tags = all_tags.reject { |i| i.blank? }
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
Avoid rescuing without specifying an error class. Open
rescue
- Read upRead up
- Exclude checks
This cop checks for rescuing StandardError
. There are two supported
styles implicit
and explicit
. This cop will not register an offense
if any error other than StandardError
is specified.
Example: EnforcedStyle: implicit
# `implicit` will enforce using `rescue` instead of
# `rescue StandardError`.
# bad
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Example: EnforcedStyle: explicit (default)
# `explicit` will enforce using `rescue StandardError`
# instead of `rescue`.
# bad
begin
foo
rescue
bar
end
# good
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Prefer $ERROR_INFO
from the stdlib 'English' module (don't forget to require it) over $!
. Open
logger.info('Embedded metadata harvesting skipped. Details are: ' + $!.message)
- Exclude checks
Pass &:blank?
as an argument to reject
instead of a block. Open
value = value.reject { |i| i.blank? } if value.is_a?(Array)
- Read upRead up
- Exclude checks
Use symbols as procs when possible.
Example:
# bad
something.map { |s| s.upcase }
# good
something.map(&:upcase)
%
-literals should be delimited by (
and )
. Open
if %{ map map_address }.include?(field.ftype)
- Read upRead up
- Exclude checks
This cop enforces the consistent usage of %
-literal delimiters.
Specify the 'default' key to set all preferred delimiters at once. You can continue to specify individual preferred delimiters to override the default.
Example:
# Style/PercentLiteralDelimiters:
# PreferredDelimiters:
# default: '[]'
# '%i': '()'
# good
%w[alpha beta] + %i(gamma delta)
# bad
%W(alpha #{beta})
# bad
%I(alpha beta)
Use self-assignment shorthand +=
. Open
all_tags = all_tags + value.to_a
- Read upRead up
- Exclude checks
This cop enforces the use the shorthand for self-assignment.
Example:
# bad
x = x + 1
# good
x += 1