From ebf1fa2fb1be8f7ba755d7d47feee1dd3dbc4241 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 8 Oct 2010 18:23:38 -0700 Subject: [PATCH] Check if file exists on root path traversal [closes GH-182] --- CHANGELOG.md | 2 +- lib/vagrant/environment.rb | 2 +- test/vagrant/environment_test.rb | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fdad08a6..77bdabcbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.6.6 (unreleased) - + - Fix potential infinite loop with root path if bad CWD is given to environment. ## 0.6.5 (October 8, 2010) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index a11bcea12..be88cce3e 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -243,7 +243,7 @@ module Vagrant root_finder = lambda do |path| 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) end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 9ede33d4e..f2634debd 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -242,6 +242,7 @@ class EnvironmentTest < Test::Unit::TestCase search_seq = sequence("search_seq") 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).returns(true).in_sequence(search_seq) if !path.root? end 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 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 env = @klass.new File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once