Possible unprotected redirect Open
redirect_to params.merge(action: topic_slideshow? ? 'show' : 'index')
- Read upRead up
- Exclude checks
Unvalidated redirects and forwards are #10 on the OWASP Top Ten.
Redirects which rely on user-supplied values can be used to "spoof" websites or hide malicious links in otherwise harmless-looking URLs. They can also allow access to restricted areas of a site if the destination is not validated.
Brakeman will raise warnings whenever redirect_to
appears to be used with a user-supplied value that may allow them to change the :host
option.
For example,
redirect_to params.merge(:action => :home)
will create a warning like
Possible unprotected redirect near line 46: redirect_to(params)
This is because params
could contain :host => 'evilsite.com'
which would redirect away from your site and to a malicious site.
If the first argument to redirect_to
is a hash, then adding :only_path => true
will limit the redirect to the current host. Another option is to specify the host explicitly.
redirect_to params.merge(:only_path => true)
redirect_to params.merge(:host => 'myhost.com')
If the first argument is a string, then it is possible to parse the string and extract the path:
redirect_to URI.parse(some_url).path
If the URL does not contain a protocol (e.g., http://
), then you will probably get unexpected results, as redirect_to
will prepend the current host name and a protocol.
Module has too many lines. [127/100] Open
module ImageSlideshow
unless included_modules.include? ImageSlideshow
def self.included(klass)
if klass.name == 'TopicsController'
klass.send :before_filter, :prepare_slideshow, only: ['selected_image']
- Read upRead up
- Exclude checks
This cop checks if the length a module exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.
Assignment Branch Condition size for prepare_slideshow is too high. [36.62/15] Open
def prepare_slideshow
# Reset a few instance vars
@current_id = @selected_still_image = @previous_url = @next_url = nil
# create a url based on the current request. It only contains an id when the user has
- 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 prepare_slideshow
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
def prepare_slideshow
# Reset a few instance vars
@current_id = @selected_still_image = @previous_url = @next_url = nil
# create a url based on the current request. It only contains an id when the user has
- 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
Assignment Branch Condition size for populate_slideshow is too high. [24.68/15] Open
def populate_slideshow
# Get either this baskets images, or the related images if we're in a topic
@still_image_ids = topic_slideshow? ? find_related_images : find_basket_images
session[:image_slideshow] = nil
if !@still_image_ids.blank?
- 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. [18/10] Open
def prepare_slideshow
# Reset a few instance vars
@current_id = @selected_still_image = @previous_url = @next_url = nil
# create a url based on the current request. It only contains an id when the user has
- 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.
Perceived complexity for prepare_slideshow is too high. [11/7] Open
def prepare_slideshow
# Reset a few instance vars
@current_id = @selected_still_image = @previous_url = @next_url = nil
# create a url based on the current request. It only contains an id when the user has
- 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
Method has too many lines. [13/10] Open
def populate_slideshow
# Get either this baskets images, or the related images if we're in a topic
@still_image_ids = topic_slideshow? ? find_related_images : find_basket_images
session[:image_slideshow] = nil
if !@still_image_ids.blank?
- 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.
Cyclomatic complexity for prepare_slideshow is too high. [9/6] Open
def prepare_slideshow
# Reset a few instance vars
@current_id = @selected_still_image = @previous_url = @next_url = nil
# create a url based on the current request. It only contains an id when the user has
- 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.
Method has too many lines. [11/10] Open
def slideshow_updater
update_id = (topic_slideshow? ? 'related_items_slideshow' : 'selected-image-display')
@template.periodically_call_remote(
update: update_id,
url: {
- 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 selected_image
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
def selected_image
if request.xhr?
render text: (topic_slideshow? ? slideshow_div : render_selected_image)
else
redirect_to params.merge(action: topic_slideshow? ? 'show' : 'index')
- 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 find_related_images
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
def find_related_images(limit = 20)
raise 'ERROR: Tried to populate topic slideshow without passing in params[:topic_id]' unless params[:topic_id]
find_args_hash = { select: 'still_images.id, still_images.title, still_images.created_at, still_images.basket_id, still_images.file_private', limit: limit }
find_args_hash.merge!(public_conditions) unless display_private_items?
find_args_hash[:order] = 'still_images.created_at desc'
- 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 slideshow_updater
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def slideshow_updater
update_id = (topic_slideshow? ? 'related_items_slideshow' : 'selected-image-display')
@template.periodically_call_remote(
update: update_id,
url: {
- 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 find_basket_images
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def find_basket_images(limit = 20)
find_args_hash = { select: 'id, title, created_at, basket_id, file_private', limit: limit }
find_args_hash.merge!(public_conditions) unless display_private_items?
# Order results acording to the basket setting
find_args_hash[:order] = @current_basket.index_page_image_as == 'random' ? 'RANDOM()' : 'created_at desc'
- 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
Avoid more than 3 levels of block nesting. Open
@current_id = $1 if @current_url.split('/').last =~ /([0-9]+)(.*)/
- 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.
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if !@still_image_ids.blank?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Redundant curly braces around a hash parameter. Open
html = @template.content_tag('div', render_selected_image, { id: 'selected-image-display' })
- Read upRead up
- Exclude checks
This cop checks for braces around the last parameter in a method call
if the last parameter is a hash.
It supports braces
, no_braces
and context_dependent
styles.
Example: EnforcedStyle: braces
# The `braces` style enforces braces around all method
# parameters that are hashes.
# bad
some_method(x, y, a: 1, b: 2)
# good
some_method(x, y, {a: 1, b: 2})
Example: EnforcedStyle: no_braces (default)
# The `no_braces` style checks that the last parameter doesn't
# have braces around it.
# bad
some_method(x, y, {a: 1, b: 2})
# good
some_method(x, y, a: 1, b: 2)
Example: EnforcedStyle: context_dependent
# The `context_dependent` style checks that the last parameter
# doesn't have braces around it, but requires braces if the
# second to last parameter is also a hash literal.
# bad
some_method(x, y, {a: 1, b: 2})
some_method(x, y, {a: 1, b: 2}, a: 1, b: 2)
# good
some_method(x, y, a: 1, b: 2)
some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2})
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
{ basket_ids: @basket_access_hash.collect { |b| b[1][:id] }, file_private: false }] }
- 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 a guard clause instead of wrapping the code inside a conditional expression. Open
if slideshow_has_results?
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Favor unless
over if
for negative conditions. Open
if !@still_image_ids.blank?
total_images = @still_image_ids.size
image_slideshow.key = slideshow_key
image_slideshow.results = @still_image_ids.collect { |id| url_for(url_hash.merge(id: id)) }
image_slideshow.total = total_images
- Read upRead up
- Exclude checks
Checks for uses of if with a negated condition. Only ifs without else are considered. There are three different styles:
- both
- prefix
- postfix
Example: EnforcedStyle: both (default)
# enforces `unless` for `prefix` and `postfix` conditionals
# bad
if !foo
bar
end
# good
unless foo
bar
end
# bad
bar if !foo
# good
bar unless foo
Example: EnforcedStyle: prefix
# enforces `unless` for just `prefix` conditionals
# bad
if !foo
bar
end
# good
unless foo
bar
end
# good
bar if !foo
Example: EnforcedStyle: postfix
# enforces `unless` for just `postfix` conditionals
# bad
bar if !foo
# good
bar unless foo
# good
if !foo
bar
end
Avoid the use of Perl-style backrefs. Open
@current_id = $1 if @current_url.split('/').last =~ /([0-9]+)(.*)/
- Read upRead up
- Exclude checks
This cop looks for uses of Perl-style regexp match backreferences like $1, $2, etc.
Example:
# bad
puts $1
# good
puts Regexp.last_match(1)