diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e4064fd1..3ada69624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,6 +91,7 @@ BUG FIXES: `which` since that doesn't exist on some systems. [GH-5170] - provisioners/chef-zero: support more chef-zero/local mode attributes [GH-5339] - provisioners/docker: use docker.com instead of docker.io [GH-5216] + - provisioners/file: validation of source is relative to Vagrantfile [GH-5252] - pushes/atlas: send additional box metadata [GH-5283] - pushes/ftp: improve check for remote directory existence [GH-5549] - synced\_folders/rsync: add `IdentitiesOnly=yes` to the rsync command. [GH-5175] diff --git a/plugins/provisioners/file/config.rb b/plugins/provisioners/file/config.rb index 2af27e67b..e8e2d6c92 100644 --- a/plugins/provisioners/file/config.rb +++ b/plugins/provisioners/file/config.rb @@ -1,3 +1,5 @@ +require "pathname" + require "vagrant" module VagrantPlugins @@ -15,10 +17,10 @@ module VagrantPlugins errors << I18n.t("vagrant.provisioners.file.no_dest_file") end if source - s = File.expand_path(source) - if ! File.exist?(s) + s = Pathname.new(source).expand_path(machine.env.root_path) + if !s.exist? errors << I18n.t("vagrant.provisioners.file.path_invalid", - path: s) + path: s.to_s) end end diff --git a/test/unit/plugins/provisioners/file/config_test.rb b/test/unit/plugins/provisioners/file/config_test.rb index e41cac966..4302a0a9c 100644 --- a/test/unit/plugins/provisioners/file/config_test.rb +++ b/test/unit/plugins/provisioners/file/config_test.rb @@ -7,7 +7,13 @@ describe VagrantPlugins::FileUpload::Config do subject { described_class.new } - let(:machine) { double("machine") } + let(:env) do + iso_env = isolated_environment + iso_env.vagrantfile("") + iso_env.create_vagrant_env + end + + let(:machine) { double("machine", env: env) } describe "#validate" do it "returns an error if destination is not specified" do @@ -33,7 +39,7 @@ describe VagrantPlugins::FileUpload::Config do end it "returns an error if source file does not exist" do - non_existing_file = "this/does/not/exist" + non_existing_file = "/this/does/not/exist" subject.source = non_existing_file subject.destination = "/tmp/foo" @@ -58,7 +64,10 @@ describe VagrantPlugins::FileUpload::Config do end it "passes with relative source path" do - existing_relative_path = __FILE__ + path = env.root_path.join("foo") + path.open("w+") { |f| f.write("hello") } + + existing_relative_path = "foo" subject.source = existing_relative_path subject.destination = "/tmp/foo"