-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor usage of Dir[] to eliminate unsafe glob construction #331
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -261,7 +261,7 @@ def self.all_load_paths | ||
# Return all the partial paths in +gemdir+. | # Return all the partial paths in +gemdir+. | ||
|
|
||
def self.all_partials(gemdir) | def self.all_partials(gemdir) | ||
Dir[File.join(gemdir, "gems/*")] | Gem.glob(gemdir, "gems/*") | ||
end | end | ||
|
|
||
private_class_method :all_partials | private_class_method :all_partials | ||
|
@@ -469,7 +469,7 @@ def self.find_files(glob, check_load_path=true) | ||
|
|
||
if check_load_path | if check_load_path | ||
files = $LOAD_PATH.map { |load_path| | files = $LOAD_PATH.map { |load_path| | ||
Dir["#{File.expand_path glob, load_path}#{Gem.suffix_pattern}"] | Gem.glob(load_path, "#{glob}#{Gem.suffix_pattern}") | ||
}.flatten.select { |file| File.file? file.untaint } | }.flatten.select { |file| File.file? file.untaint } | ||
end | end | ||
|
|
||
|
@@ -524,6 +524,21 @@ def self.find_home | ||
|
|
||
private_class_method :find_home | private_class_method :find_home | ||
|
|
||
## | |||
# Dir.glob wrapper that takes a base directory | |||
|
|||
def self.glob(dir, pattern) | |||
# TODO: move to utils | |||
dir = File.expand_path(dir) | |||
return [] unless File.directory?(dir) | |||
|
|||
Dir.chdir dir do | |||
Dir.glob(pattern).map do |filename| | |||
File.join(dir, filename) | |||
end | |||
end | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing directories feels wrong, for multithreaded programs it randomly disrupts Dir.pwd for other threads. Checking if a directory exists and rebuilding paths feels like unnecessary work that glob can do for us. Why not the simpler: def self.glob dir, pattern
dir = dir.gsub(/[*?{}\[\]]/, '\\\\\&')
pattern = File.join dir, pattern
Dir.glob pattern
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what's I was originally going to do - but I discovered jruby/jruby#172 and @tenderlove suggested I use |
|||
end | |||
|
|||
## | ## | ||
# Zlib::GzipReader wrapper that unzips +data+. | # Zlib::GzipReader wrapper that unzips +data+. | ||
|
|
||
|
@@ -1099,11 +1114,9 @@ def self.load_plugins | ||
# Find all 'rubygems_plugin' files in $LOAD_PATH and load them | # Find all 'rubygems_plugin' files in $LOAD_PATH and load them | ||
|
|
||
def self.load_env_plugins | def self.load_env_plugins | ||
path = "rubygems_plugin" | |||
|
|||
files = [] | files = [] | ||
$LOAD_PATH.each do |load_path| | $LOAD_PATH.each do |load_path| | ||
globbed = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"] | globbed = Gem.glob(load_path, "rubygems_plugin#{Gem.suffix_pattern}") | ||
|
|
||
globbed.each do |load_path_file| | globbed.each do |load_path_file| | ||
files << load_path_file if File.file?(load_path_file.untaint) | files << load_path_file if File.file?(load_path_file.untaint) | ||
|
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -107,7 +107,7 @@ def refute_path_exists path, msg = nil | ||
# or <tt>i686-darwin8.10.1</tt> otherwise. | # or <tt>i686-darwin8.10.1</tt> otherwise. | ||
# | # | ||
# If the +KEEP_FILES+ environment variable is set the files will not be | # If the +KEEP_FILES+ environment variable is set the files will not be | ||
# removed from <tt>/tmp/test_rubygems_#{$$}.#{Time.now.to_i}</tt>. | # removed from <tt>/tmp/test_rubygems_{#{$$}}.#{Time.now.to_i}</tt>. | ||
|
|
||
def setup | def setup | ||
super | super | ||
|
@@ -122,9 +122,9 @@ def setup | ||
tmpdir = File.expand_path("tmp/test") | tmpdir = File.expand_path("tmp/test") | ||
|
|
||
if ENV['KEEP_FILES'] then | if ENV['KEEP_FILES'] then | ||
@tempdir = File.join(tmpdir, "test_rubygems_#{$$}.#{Time.now.to_i}") | @tempdir = File.join(tmpdir, "test_rubygems_{#{$$}}.#{Time.now.to_i}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you've wrapped the value with {}. Could you explain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To test that glob characters don't cause a problem - writing a whole bunch of extra tests seemed excessive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not excessive when someone comes along and removes that because they don't know why it's there |
|||
else | else | ||
@tempdir = File.join(tmpdir, "test_rubygems_#{$$}") | @tempdir = File.join(tmpdir, "test_rubygems_{#{$$}}") | ||
end | end | ||
@tempdir.untaint | @tempdir.untaint | ||
@gemhome = File.join @tempdir, 'gemhome' | @gemhome = File.join @tempdir, 'gemhome' | ||
|
@@ -298,7 +298,7 @@ def uninstall_gem spec | ||
def create_tmpdir | def create_tmpdir | ||
tmpdir = nil | tmpdir = nil | ||
Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp | Dir.chdir Dir.tmpdir do tmpdir = Dir.pwd end # HACK OSX /private/tmp | ||
tmpdir = File.join tmpdir, "test_rubygems_#{$$}" | tmpdir = File.join tmpdir, "test_rubygems_{#{$$}}" | ||
FileUtils.mkdir_p tmpdir | FileUtils.mkdir_p tmpdir | ||
return tmpdir | return tmpdir | ||
end | end | ||
|
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -292,8 +292,11 @@ def test_download_local_read_only | ||
inst = Gem::RemoteFetcher.fetcher | inst = Gem::RemoteFetcher.fetcher | ||
end | end | ||
|
|
||
assert_equal(File.join(@tempdir, @a1.file_name), | uri = URI.const_defined?(:DEFAULT_PARSER) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this here? It seems unrelated to the rest of this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The url returned by |
|||
inst.download(@a1, local_path)) | URI::DEFAULT_PARSER.escape(File.join(@tempdir, @a1.file_name)) : | ||
URI.escape(File.join(@tempdir, @a1.file_name)) | |||
|
|||
assert_equal(uri, inst.download(@a1, local_path)) | |||
ensure | ensure | ||
FileUtils.chmod 0755, @a1.cache_dir | FileUtils.chmod 0755, @a1.cache_dir | ||
end | end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o_O
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming at some point a whole bunch of these methods would be moved to Gem::Utils - that doesn't exist at the moment does it? I didn't want to pollute the pull request with unrelated changes.