Check if file exists on root path traversal [closes GH-182]

This commit is contained in:
Mitchell Hashimoto 2010-10-08 18:23:38 -07:00
parent 29a5cada78
commit ebf1fa2fb1
3 changed files with 7 additions and 2 deletions

View File

@ -1,6 +1,6 @@
## 0.6.6 (unreleased) ## 0.6.6 (unreleased)
- Fix potential infinite loop with root path if bad CWD is given to environment.
## 0.6.5 (October 8, 2010) ## 0.6.5 (October 8, 2010)

View File

@ -243,7 +243,7 @@ module Vagrant
root_finder = lambda do |path| root_finder = lambda do |path|
return path if File.exist?(File.join(path.to_s, ROOTFILE_NAME)) return path if File.exist?(File.join(path.to_s, ROOTFILE_NAME))
return nil if path.root? return nil if path.root? || !File.exist?(path)
root_finder.call(path.parent) root_finder.call(path.parent)
end end

View File

@ -242,6 +242,7 @@ class EnvironmentTest < Test::Unit::TestCase
search_seq = sequence("search_seq") search_seq = sequence("search_seq")
paths.each do |path| paths.each do |path|
File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(false).in_sequence(search_seq) File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(false).in_sequence(search_seq)
File.expects(:exist?).with(path).returns(true).in_sequence(search_seq) if !path.root?
end end
assert !@klass.new(:cwd => paths.first).root_path assert !@klass.new(:cwd => paths.first).root_path
@ -254,6 +255,10 @@ class EnvironmentTest < Test::Unit::TestCase
assert_equal path, @klass.new(:cwd => path).root_path assert_equal path, @klass.new(:cwd => path).root_path
end end
should "not infinite loop on relative paths" do
assert @klass.new(:cwd => "../test").root_path.nil?
end
should "only load the root path once" do should "only load the root path once" do
env = @klass.new env = @klass.new
File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once