Skip to content

Commit 0b2adc0

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. (cherry picked from commit 283ba4c)
1 parent 4e6aa1f commit 0b2adc0

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
@@ -935,15 +935,6 @@ Security/Eval:
935935
- 'lib/puppet/pops/loader/ruby_function_instantiator.rb'
936936
- 'lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb'
937937

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-
947938
# Configuration parameters: EnforcedStyle, AllowModifiersOnSymbols.
948939
# SupportedStyles: inline, group
949940
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/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.

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

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

lib/puppet/util/command_line/trollop.rb

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

lib/puppet/util/execution.rb

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