Skip to content

Commit

Permalink
Merge pull request #8394 from koic/use_cop_base_api_for_security_depa…
Browse files Browse the repository at this point in the history
…rtment

Use `Cop::Base` API for `Security` department
  • Loading branch information
koic committed Jul 24, 2020
2 parents 9b3ff64 + 5802b9a commit ca9483a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/security/eval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Security
#
# eval(something)
# binding.eval(something)
class Eval < Cop
class Eval < Base
MSG = 'The use of `eval` is a serious security risk.'

def_node_matcher :eval?, <<~PATTERN
Expand All @@ -22,7 +22,7 @@ def on_send(node)
eval?(node) do |code|
return if code.dstr_type? && code.recursive_literal?

add_offense(node, location: :selector)
add_offense(node.loc.selector)
end
end
end
Expand Down
14 changes: 6 additions & 8 deletions lib/rubocop/cop/security/json_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ module Security
# # good
# JSON.parse("{}")
#
class JSONLoad < Cop
class JSONLoad < Base
extend AutoCorrector

MSG = 'Prefer `JSON.parse` over `JSON.%<method>s`.'

def_node_matcher :json_load, <<~PATTERN
Expand All @@ -31,15 +33,11 @@ class JSONLoad < Cop

def on_send(node)
json_load(node) do |method|
add_offense(node,
location: :selector,
message: format(MSG, method: method))
add_offense(node.loc.selector, message: format(MSG, method: method)) do |corrector|
corrector.replace(node.loc.selector, 'parse')
end
end
end

def autocorrect(node)
->(corrector) { corrector.replace(node.loc.selector, 'parse') }
end
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/security/marshal_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Security
# # okish - deep copy hack
# Marshal.load(Marshal.dump({}))
#
class MarshalLoad < Cop
class MarshalLoad < Base
MSG = 'Avoid using `Marshal.%<method>s`.'

def_node_matcher :marshal_load, <<~PATTERN
Expand All @@ -28,9 +28,7 @@ class MarshalLoad < Cop

def on_send(node)
marshal_load(node) do |method|
add_offense(node,
location: :selector,
message: format(MSG, method: method))
add_offense(node.loc.selector, message: format(MSG, method: method))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/security/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Security
# File.open(something)
# IO.popen(something)
# URI.parse(something).open
class Open < Cop
class Open < Base
MSG = 'The use of `Kernel#open` is a serious security risk.'

def_node_matcher :open?, <<~PATTERN
Expand All @@ -30,7 +30,7 @@ def on_send(node)
open?(node) do |code|
return if safe?(code)

add_offense(node, location: :selector)
add_offense(node.loc.selector)
end
end

Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/security/yaml_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Security
# YAML.safe_load("--- foo")
# YAML.dump("foo")
#
class YAMLLoad < Cop
class YAMLLoad < Base
extend AutoCorrector

MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'

def_node_matcher :yaml_load, <<~PATTERN
Expand All @@ -24,13 +26,11 @@ class YAMLLoad < Cop

def on_send(node)
yaml_load(node) do
add_offense(node, location: :selector)
add_offense(node.loc.selector) do |corrector|
corrector.replace(node.loc.selector, 'safe_load')
end
end
end

def autocorrect(node)
->(corrector) { corrector.replace(node.loc.selector, 'safe_load') }
end
end
end
end
Expand Down

0 comments on commit ca9483a

Please sign in to comment.