Skip to content

Commit 283ba4c

Browse files
committed
(PUP-11772) Resolve Security/Open
When opening a file path, use File.open When opening a URL, use URI.parse(..).open The Windows package class includes our Registry module which defines `open`. Use the fully qualified name to avoid rubocop confusion.
1 parent 03b3df3 commit 283ba4c

File tree

9 files changed

+10
-18
lines changed

9 files changed

+10
-18
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -926,15 +926,6 @@ Security/Eval:
926926
- 'lib/puppet/pops/loader/ruby_function_instantiator.rb'
927927
- 'lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb'
928928

929-
Security/Open:
930-
Exclude:
931-
- 'lib/puppet/file_system/file_impl.rb'
932-
- 'lib/puppet/file_system/posix.rb'
933-
- 'lib/puppet/provider/package/appdmg.rb'
934-
- 'lib/puppet/provider/package/windows/package.rb'
935-
- 'lib/puppet/util/command_line/trollop.rb'
936-
- 'lib/puppet/util/execution.rb'
937-
938929
# Configuration parameters: EnforcedStyle, AllowModifiersOnSymbols.
939930
# SupportedStyles: inline, group
940931
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
@@ -151,7 +151,7 @@ def lstat(path)
151151
end
152152

153153
def compare_stream(path, stream)
154-
open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
154+
::File.open(path, 0, 'rb') { |this| FileUtils.compare_stream(this, stream) }
155155
end
156156

157157
def chmod(mode, path)

lib/puppet/file_system/posix.rb

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

lib/puppet/provider/package/appdmg.rb

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

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

lib/puppet/provider/package/windows/package.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def self.with_key(&block)
4444
[KEY64, KEY32].each do |mode|
4545
mode |= KEY_READ
4646
begin
47-
open(hive, 'Software\Microsoft\Windows\CurrentVersion\Uninstall', mode) do |uninstall|
47+
self.open(hive, 'Software\Microsoft\Windows\CurrentVersion\Uninstall', mode) do |uninstall|
4848
each_key(uninstall) do |name, wtime|
49-
open(hive, "#{uninstall.keyname}\\#{name}", mode) do |key|
49+
self.open(hive, "#{uninstall.keyname}\\#{name}", mode) do |key|
5050
yield key, values_by_name(key, reg_value_names_to_load)
5151
end
5252
end

lib/puppet/util/command_line/trollop.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ def parse_io_parameter param, arg
650650
else
651651
require 'open-uri'
652652
begin
653-
open param
653+
URI.parse(param).open
654654
rescue SystemCallError => e
655655
raise CommandlineError, _("file or url for option '%{arg}' cannot be opened: %{value0}") % { arg: arg, value0: e.message }, e.backtrace
656656
end

lib/puppet/util/execution.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def self.execpipe(command, failonfail = true)
7878
# a predictable output
7979
english_env = ENV.to_hash.merge( {'LANG' => 'C', 'LC_ALL' => 'C'} )
8080
output = Puppet::Util.withenv(english_env) do
81-
open("| #{command_str} 2>&1") do |pipe|
81+
# We are intentionally using 'pipe' with open to launch a process
82+
open("| #{command_str} 2>&1") do |pipe| # rubocop:disable Security/Open
8283
yield pipe
8384
end
8485
end

spec/unit/provider/package/appdmg_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
before do
1212
fh = double('filehandle', path: '/tmp/foo')
1313
resource[:source] = "foo.dmg"
14-
allow(described_class).to receive(:open).and_yield(fh)
14+
allow(File).to receive(:open).and_yield(fh)
1515
allow(Dir).to receive(:mktmpdir).and_return("/tmp/testtmp123")
1616
allow(FileUtils).to receive(:remove_entry_secure)
1717
end

0 commit comments

Comments
 (0)