Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
ecf64c26
Commit
ecf64c26
authored
Apr 23, 2021
by
Mark Chao
Committed by
Igor Drozdov
Apr 23, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rubocop check for direct manipulation of ActiveModel errors hash
parent
ba41e132
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
132 additions
and
7 deletions
+132
-7
.rubocop.yml
.rubocop.yml
+3
-0
app/services/users/destroy_service.rb
app/services/users/destroy_service.rb
+1
-1
app/validators/branch_filter_validator.rb
app/validators/branch_filter_validator.rb
+2
-2
app/validators/same_project_association_validator.rb
app/validators/same_project_association_validator.rb
+1
-1
ee/app/services/vulnerability_feedback/create_service.rb
ee/app/services/vulnerability_feedback/create_service.rb
+3
-3
rubocop/cop/active_model_errors_direct_manipulation.rb
rubocop/cop/active_model_errors_direct_manipulation.rb
+60
-0
spec/rubocop/cop/active_model_errors_direct_manipulation_spec.rb
...bocop/cop/active_model_errors_direct_manipulation_spec.rb
+62
-0
No files found.
.rubocop.yml
View file @
ecf64c26
...
@@ -307,6 +307,9 @@ Cop/ActiveRecordAssociationReload:
...
@@ -307,6 +307,9 @@ Cop/ActiveRecordAssociationReload:
-
'
spec/**/*'
-
'
spec/**/*'
-
'
ee/spec/**/*'
-
'
ee/spec/**/*'
Cop/ActiveModelErrorsDirectManipulation
:
Enabled
:
true
Gitlab/AvoidFeatureGet
:
Gitlab/AvoidFeatureGet
:
Enabled
:
true
Enabled
:
true
...
...
app/services/users/destroy_service.rb
View file @
ecf64c26
...
@@ -31,7 +31,7 @@ module Users
...
@@ -31,7 +31,7 @@ module Users
end
end
if
!
delete_solo_owned_groups
&&
user
.
solo_owned_groups
.
present?
if
!
delete_solo_owned_groups
&&
user
.
solo_owned_groups
.
present?
user
.
errors
[
:base
]
<<
'You must transfer ownership or delete groups before you can remove user'
user
.
errors
.
add
(
:base
,
'You must transfer ownership or delete groups before you can remove user'
)
return
user
return
user
end
end
...
...
app/validators/branch_filter_validator.rb
View file @
ecf64c26
...
@@ -20,11 +20,11 @@ class BranchFilterValidator < ActiveModel::EachValidator
...
@@ -20,11 +20,11 @@ class BranchFilterValidator < ActiveModel::EachValidator
value_without_wildcards
=
value
.
tr
(
'*'
,
'x'
)
value_without_wildcards
=
value
.
tr
(
'*'
,
'x'
)
unless
Gitlab
::
GitRefValidator
.
validate
(
value_without_wildcards
)
unless
Gitlab
::
GitRefValidator
.
validate
(
value_without_wildcards
)
record
.
errors
[
attribute
]
<<
"is not a valid branch name"
record
.
errors
.
add
(
attribute
,
"is not a valid branch name"
)
end
end
unless
value
.
length
<=
4000
unless
value
.
length
<=
4000
record
.
errors
[
attribute
]
<<
"is longer than the allowed length of 4000 characters."
record
.
errors
.
add
(
attribute
,
"is longer than the allowed length of 4000 characters."
)
end
end
end
end
end
end
...
...
app/validators/same_project_association_validator.rb
View file @
ecf64c26
...
@@ -16,6 +16,6 @@ class SameProjectAssociationValidator < ActiveModel::EachValidator
...
@@ -16,6 +16,6 @@ class SameProjectAssociationValidator < ActiveModel::EachValidator
def
validate_each
(
record
,
attribute
,
value
)
def
validate_each
(
record
,
attribute
,
value
)
return
if
record
.
project
==
value
&
.
project
return
if
record
.
project
==
value
&
.
project
record
.
errors
[
attribute
]
<<
'must associate the same project'
record
.
errors
.
add
(
attribute
,
'must associate the same project'
)
end
end
end
end
ee/app/services/vulnerability_feedback/create_service.rb
View file @
ecf64c26
...
@@ -68,7 +68,7 @@ module VulnerabilityFeedback
...
@@ -68,7 +68,7 @@ module VulnerabilityFeedback
.
execute
.
execute
if
result
[
:status
]
==
:error
if
result
[
:status
]
==
:error
vulnerability_feedback
.
errors
[
:issue
]
<<
result
[
:message
]
vulnerability_feedback
.
errors
.
add
(
:issue
,
result
[
:message
])
raise
ActiveRecord
::
Rollback
raise
ActiveRecord
::
Rollback
end
end
...
@@ -78,7 +78,7 @@ module VulnerabilityFeedback
...
@@ -78,7 +78,7 @@ module VulnerabilityFeedback
issue_link_result
=
create_vulnerability_issue_link
(
vulnerability_feedback
.
vulnerability_data
[
:vulnerability_id
],
issue
)
issue_link_result
=
create_vulnerability_issue_link
(
vulnerability_feedback
.
vulnerability_data
[
:vulnerability_id
],
issue
)
if
issue_link_result
&
.
error?
if
issue_link_result
&
.
error?
vulnerability_feedback
.
errors
[
:issue_link
]
<<
issue_link_result
.
message
vulnerability_feedback
.
errors
.
add
(
:issue_link
,
issue_link_result
.
message
)
raise
ActiveRecord
::
Rollback
raise
ActiveRecord
::
Rollback
end
end
...
@@ -100,7 +100,7 @@ module VulnerabilityFeedback
...
@@ -100,7 +100,7 @@ module VulnerabilityFeedback
vulnerability_feedback
.
save
vulnerability_feedback
.
save
else
else
vulnerability_feedback
.
errors
[
:merge_request
]
<<
result
[
:message
]
vulnerability_feedback
.
errors
.
add
(
:merge_request
,
result
[
:message
])
end
end
end
end
...
...
rubocop/cop/active_model_errors_direct_manipulation.rb
0 → 100644
View file @
ecf64c26
# frozen_string_literal: true
module
RuboCop
module
Cop
# Cop that avoid direct manipulation of ActiveModel#errors hash,
# in preparation to upgrade to Rails 6.1
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/225874
class
ActiveModelErrorsDirectManipulation
<
RuboCop
::
Cop
::
Cop
MSG
=
'Avoid manipulating errors hash directly. For more details check https://gitlab.com/gitlab-org/gitlab/-/issues/225874'
MANIPULATIVE_METHODS
=
":<< :append :clear :collect! :compact! :concat :delete :delete_at :delete_if :drop :drop_while :fill :filter! :keep_if :flatten! :insert :map! :pop :prepend :push :reject! :replace :reverse! :rotate! :select! :shift :shuffle! :slice! :sort! :sort_by! :uniq! :unshift"
def_node_matcher
:active_model_errors_root_manipulation?
,
<<~
PATTERN
(send
(send
(send {send ivar lvar} :errors)
:[]
...)
{
#{
MANIPULATIVE_METHODS
}
}
...)
PATTERN
def_node_matcher
:active_model_errors_root_assignment?
,
<<~
PATTERN
(send
(send {send ivar lvar} :errors)
:[]=
...)
PATTERN
def_node_matcher
:active_model_errors_manipulation?
,
<<~
PATTERN
(send
(send
(send
(send {send ivar lvar} :errors)
{:messages :details})
:[]
...)
{
#{
MANIPULATIVE_METHODS
}
}
...)
PATTERN
def_node_matcher
:active_model_errors_assignment?
,
<<~
PATTERN
(send
(send
(send {send ivar lvar} :errors)
{:messages :details})
:[]=
...)
PATTERN
def
on_send
(
node
)
add_offense
(
node
,
location: :expression
)
if
active_model_errors_root_assignment?
(
node
)
add_offense
(
node
,
location: :expression
)
if
active_model_errors_root_manipulation?
(
node
)
add_offense
(
node
,
location: :expression
)
if
active_model_errors_manipulation?
(
node
)
add_offense
(
node
,
location: :expression
)
if
active_model_errors_assignment?
(
node
)
end
end
end
end
spec/rubocop/cop/active_model_errors_direct_manipulation_spec.rb
0 → 100644
View file @
ecf64c26
# frozen_string_literal: true
require
'fast_spec_helper'
require_relative
'../../../rubocop/cop/active_model_errors_direct_manipulation'
RSpec
.
describe
RuboCop
::
Cop
::
ActiveModelErrorsDirectManipulation
do
subject
(
:cop
)
{
described_class
.
new
}
context
'when modifying errors'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors[:name] << 'msg'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
context
'when assigning'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors[:name] = []
^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
end
end
context
'when modifying errors.messages'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors.messages[:name] << 'msg'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
context
'when assigning'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors.messages[:name] = []
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
end
end
context
'when modifying errors.details'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors.details[:name] << {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
context
'when assigning'
do
it
'registers an offense'
do
expect_offense
(
<<~
PATTERN
)
user.errors.details[:name] = []
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating errors hash directly. [...]
PATTERN
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment