Skip to content

Commit 7e0a01a

Browse files
authored
Merge pull request #9171 from joshcooper/rubocop_7x_security
(PUP-1172) Resolve Security cops 7.x
2 parents 4e6aa1f + 3e669ca commit 7e0a01a

File tree

14 files changed

+21
-40
lines changed

14 files changed

+21
-40
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -927,23 +927,6 @@ Naming/VariableName:
927927
Naming/VariableNumber:
928928
Enabled: false
929929

930-
Security/Eval:
931-
Exclude:
932-
- 'lib/puppet/interface/action.rb'
933-
- 'lib/puppet/interface/action_builder.rb'
934-
- 'lib/puppet/pops/loader/ruby_data_type_instantiator.rb'
935-
- 'lib/puppet/pops/loader/ruby_function_instantiator.rb'
936-
- 'lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb'
937-
938-
Security/Open:
939-
Exclude:
940-
- 'lib/puppet/file_system/file_impl.rb'
941-
- 'lib/puppet/file_system/posix.rb'
942-
- 'lib/puppet/provider/package/appdmg.rb'
943-
- 'lib/puppet/provider/package/windows/package.rb'
944-
- 'lib/puppet/util/command_line/trollop.rb'
945-
- 'lib/puppet/util/execution.rb'
946-
947930
# Configuration parameters: EnforcedStyle, AllowModifiersOnSymbols.
948931
# SupportedStyles: inline, group
949932
Style/AccessModifierDeclarations:

examples/enc/regexp_nodes/regexp_nodes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def matched_in_patternfile?(filepath, matchthis)
133133
patternlist = []
134134

135135
begin
136-
open(filepath).each do |l|
136+
File.open(filepath).each do |l|
137137
l.chomp!
138138

139139
next if l =~ /^$/

lib/puppet/file_system/file_impl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def lstat(path)
150150
end
151151

152152
def compare_stream(path, stream)
153-
open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
153+
::File.open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
154154
end
155155

156156
def chmod(mode, path)

lib/puppet/file_system/posix.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def binread(path)
1010
# issue this method reimplements the faster 2.0 version that will correctly
1111
# compare binary File and StringIO streams.
1212
def compare_stream(path, stream)
13-
open(path, 0, 'rb') do |this|
13+
::File.open(path, 'rb') do |this|
1414
bsize = stream_blksize(this, stream)
1515
sa = "".force_encoding('ASCII-8BIT')
1616
sb = "".force_encoding('ASCII-8BIT')

lib/puppet/interface/action.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ def #{@name}(#{decl.join(", ")})
264264
end
265265
WRAPPER
266266

267+
# It should be possible to rewrite this code to use `define_method`
268+
# instead of `class/instance_eval` since Ruby 1.8 is long dead.
267269
if @face.is_a?(Class)
268-
@face.class_eval do eval wrapper, nil, file, line end
270+
@face.class_eval do eval wrapper, nil, file, line end # rubocop:disable Security/Eval
269271
@face.send(:define_method, internal_name, &block)
270272
@when_invoked = @face.instance_method(name)
271273
else
272-
@face.instance_eval do eval wrapper, nil, file, line end
274+
@face.instance_eval do eval wrapper, nil, file, line end # rubocop:disable Security/Eval
273275
@face.meta_def(internal_name, &block)
274276
@when_invoked = @face.method(name).unbind
275277
end

lib/puppet/interface/action_builder.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# within the context of a new instance of this class.
55
# @api public
66
class Puppet::Interface::ActionBuilder
7+
extend Forwardable
8+
79
# The action under construction
810
# @return [Puppet::Interface::Action]
911
# @api private
@@ -141,15 +143,8 @@ def render_as(value = nil)
141143
property = setter.to_s.chomp('=')
142144

143145
unless method_defined? property
144-
# Using eval because the argument handling semantics are less awful than
145-
# when we use the define_method/block version. The later warns on older
146-
# Ruby versions if you pass the wrong number of arguments, but carries
147-
# on, which is totally not what we want. --daniel 2011-04-18
148-
eval <<-METHOD
149-
def #{property}(value)
150-
@action.#{property} = value
151-
end
152-
METHOD
146+
# ActionBuilder#<property> delegates to Action#<setter>
147+
def_delegator :@action, setter, property
153148
end
154149
end
155150

lib/puppet/pops/loader/ruby_data_type_instantiator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def self.create(loader, typed_name, source_ref, ruby_code_string)
1919
# make the private loader available in a binding to allow it to be passed on
2020
loader_for_type = loader.private_loader
2121
here = get_binding(loader_for_type)
22-
created = eval(ruby_code_string, here, source_ref, 1)
22+
created = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval
2323
unless created.is_a?(Puppet::Pops::Types::PAnyType)
2424
raise ArgumentError, _("The code loaded from %{source_ref} did not produce a data type when evaluated. Got '%{klass}'") % { source_ref: source_ref, klass: created.class }
2525
end

lib/puppet/pops/loader/ruby_function_instantiator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def self.create(loader, typed_name, source_ref, ruby_code_string)
1919
# make the private loader available in a binding to allow it to be passed on
2020
loader_for_function = loader.private_loader
2121
here = get_binding(loader_for_function)
22-
created = eval(ruby_code_string, here, source_ref, 1)
22+
created = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval
2323
unless created.is_a?(Class)
2424
raise ArgumentError, _("The code loaded from %{source_ref} did not produce a Function class when evaluated. Got '%{klass}'") % { source_ref: source_ref, klass: created.class }
2525
end

lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def self.create(loader, typed_name, source_ref, ruby_code_string)
3737
# This will do the 3x loading and define the "function_<name>" and "real_function_<name>" methods
3838
# in the anonymous module used to hold function definitions.
3939
#
40-
func_info = eval(ruby_code_string, here, source_ref, 1)
40+
func_info = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval
4141

4242
# Validate what was loaded
4343
unless func_info.is_a?(Hash)

lib/puppet/provider/package/appdmg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def self.installpkgdmg(source, name)
6666
end
6767
end
6868

69-
open(cached_source) do |dmg|
69+
File.open(cached_source) do |dmg|
7070
xml_str = hdiutil "mount", "-plist", "-nobrowse", "-readonly", "-mountrandom", "/tmp", dmg.path
7171
ptable = Puppet::Util::Plist::parse_plist(xml_str)
7272
# JJM Filter out all mount-paths into a single array, discard the rest.

0 commit comments

Comments
 (0)