From efd3a62ffe0be76ce5819f786b9765be1943a100 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 21 Nov 2019 09:56:58 -0800 Subject: [PATCH] Fixes #11207: Do not validate checksums if options are empty string Prior to this commit, if Vagrant received checksum options from Vagrant Cloud that were simply empty strings, it would try to validate its checksum with those options. This commit fixes that by ignoring empty string values. --- lib/vagrant/action/builtin/box_add.rb | 12 +++++++--- lib/vagrant/util/file_checksum.rb | 3 ++- templates/locales/en.yml | 2 +- .../vagrant/action/builtin/box_add_test.rb | 23 +++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 60ac6d597..dcd3a8ce4 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -348,9 +348,15 @@ module Vagrant end if opts[:checksum] && opts[:checksum_type] - env[:ui].detail(I18n.t("vagrant.actions.box.add.checksumming")) - validate_checksum( - opts[:checksum_type], opts[:checksum], box_url) + if opts[:checksum].to_s.strip.empty? + @logger.warn("Given checksum is empty, cannot validate checksum for box") + elsif opts[:checksum_type].to_s.strip.empty? + @logger.warn("Given checksum type is empty, cannot validate checksum for box") + else + env[:ui].detail(I18n.t("vagrant.actions.box.add.checksumming")) + validate_checksum( + opts[:checksum_type], opts[:checksum], box_url) + end end # Add the box! diff --git a/lib/vagrant/util/file_checksum.rb b/lib/vagrant/util/file_checksum.rb index 2b195a7b0..060250a8c 100644 --- a/lib/vagrant/util/file_checksum.rb +++ b/lib/vagrant/util/file_checksum.rb @@ -66,7 +66,8 @@ class FileChecksum digest = CHECKSUM_MAP[type.to_s.to_sym] if digest.nil? raise Vagrant::Errors::BoxChecksumInvalidType, - type: type.to_s + type: type.to_s, + types: CHECKSUM_MAP.keys.join(', ') end digest end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 0339cf220..4d045e32b 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -578,7 +578,7 @@ en: The specified checksum type is not supported by Vagrant: %{type}. Vagrant supports the following checksum types: - md5, sha1, sha256 + %{types} box_checksum_mismatch: |- The checksum of the downloaded box did not match the expected value. Please verify that you have the proper URL setup and that diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index 8f648bcf1..91bbc27a2 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -209,6 +209,29 @@ describe Vagrant::Action::Builtin::BoxAdd, :skip_windows, :bsdtar do to raise_error(Vagrant::Errors::BoxChecksumMismatch) end + it "ignores checksums if empty string" do + box_path = iso_env.box2_file(:virtualbox) + with_web_server(box_path) do |port| + env[:box_name] = "foo" + env[:box_url] = "http://127.0.0.1:#{port}/#{box_path.basename}" + env[:box_checksum] = "" + env[:box_checksum_type] = "" + + + expect(box_collection).to receive(:add).with(any_args) { |path, name, version, **opts| + expect(checksum(path)).to eq(checksum(box_path)) + expect(name).to eq("foo") + 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 + it "does not raise an error if the checksum has different case" do box_path = iso_env.box2_file(:virtualbox)