From da1b5765c7e1d03809a864fa18449b32327ef49c Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 7 Jul 2017 11:43:16 -0700 Subject: [PATCH] Prevent URI parse errors when checking box name Fixes #8758 --- lib/vagrant/action/builtin/box_add.rb | 8 +++++-- .../vagrant/action/builtin/box_add_test.rb | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 5f2aa907c..b951a805d 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -31,8 +31,12 @@ module Vagrant @download_interrupted = false unless env[:box_name].nil? - if URI.parse(env[:box_name]).kind_of?(URI::HTTP) - env[:ui].warn(I18n.t("vagrant.box_add_url_warn")) + begin + if URI.parse(env[:box_name]).kind_of?(URI::HTTP) + env[:ui].warn(I18n.t("vagrant.box_add_url_warn")) + end + rescue URI::InvalidURIError + # do nothing end end diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index d7c6efa97..d3dd8ab33 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -305,6 +305,28 @@ describe Vagrant::Action::Builtin::BoxAdd, :skip_windows do end end + context "with a box name containing invalid URI characters" do + it "should not raise an error" do + box_path = iso_env.box2_file(:virtualbox) + with_web_server(box_path) do |port| + + box_url_name = "box name with spaces" + env[:box_name] = box_url_name + + expect(box_collection).to receive(:add).with { |path, name, version, **opts| + expect(name).to eq(box_url_name) + expect(version).to eq("0") + expect(opts[:metadata_url]).to be_nil + true + }.and_return(box) + + expect(app).to receive(:call).with(env) + + subject.call(env) + end + end + end + context "with URL containing credentials" do let(:username){ "box-username" } let(:password){ "box-password" }