Possible unprotected redirect Open
redirect_to redirect_location
- 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.
Class has too many lines. [245/100] Open
class MembersController < ApplicationController
permit 'site_admin or admin of :current_basket', except: %i[index list join remove rss]
before_filter :permitted_to_view_memberlist, only: %i[index list rss]
- Read upRead up
- 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.
Assignment Branch Condition size for list_members_in is too high. [45.12/15] Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
- 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. [38/10] Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
- 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 has too many lines. [30/10] Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
- 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.
Assignment Branch Condition size for join is too high. [31.4/15] Open
def join
if !@basket_access_hash[@current_basket.urlified_name.to_sym].blank?
flash[:error] = t('members_controller.join.already_joined')
else
case @current_basket.join_policy_with_inheritance
- 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
Assignment Branch Condition size for change_membership_type is too high. [30.82/15] Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
- 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
Assignment Branch Condition size for add_members is too high. [29.58/15] Open
def add_members
if !params[:user]
params[:user] = {}
params[:user][params[:id]] = 1
end
- 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
Assignment Branch Condition size for list is too high. [27.39/15] Open
def list
if !params[:type].blank? && @basket_admin
@listing_type = params[:type]
else
@listing_type = 'member'
- 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. [21/10] Open
def join
if !@basket_access_hash[@current_basket.urlified_name.to_sym].blank?
flash[:error] = t('members_controller.join.already_joined')
else
case @current_basket.join_policy_with_inheritance
- 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 has too many lines. [20/10] Open
def list
if !params[:type].blank? && @basket_admin
@listing_type = params[:type]
else
@listing_type = 'member'
- 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.
Assignment Branch Condition size for change_request_status is too high. [23.43/15] Open
def change_request_status
@user = User.find(params[:id])
@current_basket.delete_roles_for(@user)
approved = (params[:status] && params[:status] == 'approved')
- 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 change_membership_type
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
- 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 has too many lines. [16/10] Open
def add_members
if !params[:user]
params[:user] = {}
params[:user][params[:id]] = 1
end
- 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 change_membership_type is too high. [13/7] Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
- 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. [15/10] Open
def remove
@user ||= User.find(params[:id])
# make sure we arn't trying to remove from site basket (destroy is the correct action for that)
if @current_basket == @site_basket
- 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 change_membership_type is too high. [11/6] Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
- 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 list_members_in is too high. [11/7] Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
- 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
Assignment Branch Condition size for remove is too high. [19.72/15] Open
def remove
@user ||= User.find(params[:id])
# make sure we arn't trying to remove from site basket (destroy is the correct action for that)
if @current_basket == @site_basket
- 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 list_members_in
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
- 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 destroy is too high. [18.55/15] Open
def destroy
@user = User.find(params[:id])
if @user.contributions.size > 0
flash[:error] = t('members_controller.destroy.has_contributions', user_name: @user.user_name)
elsif !@site_basket.more_than_one_site_admin?
- 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. [12/10] Open
def change_request_status
@user = User.find(params[:id])
@current_basket.delete_roles_for(@user)
approved = (params[:status] && params[:status] == 'approved')
- 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.
Assignment Branch Condition size for potential_new_members is too high. [17.75/15] Open
def potential_new_members
@existing_users = User.joins(:roles_users).where('roles_users.role_id in (?)', @current_basket.accepted_roles)
# don't allow, at least for now, anonymous users to be added to other baskets
# besides site
- 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
Cyclomatic complexity for list_members_in is too high. [7/6] Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
- 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.
Assignment Branch Condition size for become_user is too high. [15.43/15] Open
def become_user
return unless request.post?
# logout the old user first
# stolen from account_controller.logout, should make DRY
- 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 list_members_in
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
def list_members_in(role_name, order = 'users.login asc')
@non_member_roles_plural = {}
@possible_roles = {
'admin' => t('members_controller.list_members_in.admin'),
'moderator' => t('members_controller.list_members_in.moderator'),
Method change_membership_type
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
def change_membership_type
membership_type = params[:role]
@user = User.find(params[:id])
can_change = false
Method list
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def list
if !params[:type].blank? && @basket_admin
@listing_type = params[:type]
else
@listing_type = 'member'
- 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 remove
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def remove
@user ||= User.find(params[:id])
# make sure we arn't trying to remove from site basket (destroy is the correct action for that)
if @current_basket == @site_basket
- 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
Use each_key
instead of keys.each
. Open
params[:user].keys.each do |user|
- Read upRead up
- Exclude checks
This cop checks for uses of each_key
and each_value
Hash methods.
Note: If you have an array of two-element arrays, you can put parentheses around the block arguments to indicate that you're not working with a hash, and suppress RuboCop offenses.
Example:
# bad
hash.keys.each { |k| p k }
hash.values.each { |v| p v }
hash.each { |k, _v| p k }
hash.each { |_k, v| p v }
# good
hash.each_key { |k| p k }
hash.each_value { |v| p v }
Use the return of the conditional for variable assignment and comparison. Open
if current_user_can_see_memberlist_for?(@current_basket)
redirect_location = { action: 'list' }
else
redirect_location = "/#{@site_basket.urlified_name}/"
end
- Exclude checks
Use the return of the conditional for variable assignment and comparison. Open
unless site_admin?
@members = @role.users.where(not_anonymous_condition).order('roles_users.created_at desc').limit(50)
else
@members = @role.users.order('roles_users.created_at desc').limit(50)
end
- Exclude checks
Use the return of the conditional for variable assignment and comparison. Open
if params[:user].size > 1
flash[:notice] = t('members_controller.add_members.added_plural')
else
flash[:notice] = t('members_controller.add_members.added_singular')
end
- Exclude checks
Use the return of the conditional for variable assignment and comparison. Open
if !params[:type].blank? && @basket_admin
@listing_type = params[:type]
else
@listing_type = 'member'
end
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless params[:search_name].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
Use @user.contributions.size.positive?
instead of @user.contributions.size > 0
. Open
if @user.contributions.size > 0
- Read upRead up
- Exclude checks
This cop checks for usage of comparison operators (==
,
>
, <
) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
The cop can also be configured to do the reverse.
The cop disregards #nonzero?
as it its value is truthy or falsey,
but not true
and false
, and thus not always interchangeable with
!= 0
.
The cop ignores comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Interger
polymorphic.
Example: EnforcedStyle: predicate (default)
# bad
foo == 0
0 > foo
bar.baz > 0
# good
foo.zero?
foo.negative?
bar.baz.positive?
Example: EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
# good
foo == 0
0 > foo
bar.baz > 0
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if logged_in?
- 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
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless current_user_can_see_memberlist_for?(@current_basket)
- 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
Do not use unless
with else
. Rewrite these with the positive case first. Open
unless site_admin?
@members = @role.users.where(not_anonymous_condition).order('roles_users.created_at desc').limit(50)
else
@members = @role.users.order('roles_users.created_at desc').limit(50)
end
- Read upRead up
- Exclude checks
This cop looks for unless expressions with else clauses.
Example:
# bad
unless foo_bar.nil?
# do something...
else
# do a different thing...
end
# good
if foo_bar.present?
# do something...
else
# do a different thing...
end
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
. Open
if clear_roles
- Read upRead up
- Exclude checks
Checks for if and unless statements that would fit on one line
if written as a modifier if/unless. The maximum line length is
configured in the Metrics/LineLength
cop.
Example:
# bad
if condition
do_stuff(bar)
end
unless qux.empty?
Foo.do_something
end
# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?
Use self-assignment shorthand +=
. Open
@existing_users = @existing_users + @users_to_exclude
- Read upRead up
- Exclude checks
This cop enforces the use the shorthand for self-assignment.
Example:
# bad
x = x + 1
# good
x += 1
Favor unless
over if
for negative conditions. Open
if !params[:user]
params[:user] = {}
params[:user][params[:id]] = 1
end
- 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
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if @user.save
- 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
Use !empty?
instead of size > 0
. Open
if @user.contributions.size > 0
- Read upRead up
- Exclude checks
This cop checks for numeric comparisons that can be replaced by a predicate method, such as receiver.length == 0, receiver.length > 0, receiver.length != 0, receiver.length < 1 and receiver.size == 0 that can be replaced by receiver.empty? and !receiver.empty.
Example:
# bad
[1, 2, 3].length == 0
0 == "foobar".length
array.length < 1
{a: 1, b: 2}.length != 0
string.length > 0
hash.size > 0
# good
[1, 2, 3].empty?
"foobar".empty?
array.empty?
!{a: 1, b: 2}.empty?
!string.empty?
!hash.empty?
Use a guard clause instead of wrapping the code inside a conditional expression. Open
if @user.save
- 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
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless logged_in? && (permit?('site_admin or admin of :current_basket') || @current_user == @user)
- 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