From 270462fc89322758eb3af34bf47108a758d037d5 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Wed, 27 May 2020 19:29:19 -0400 Subject: [PATCH 1/3] Fix #11640: Check port of loopback address If the given host_ip is a loopback address, proceed with the regular port check. --- .../builtin/handle_forwarded_port_collisions.rb | 3 ++- .../handle_forwarded_port_collisions_test.rb | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index 917cbde8d..1cd2efbff 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -269,7 +269,8 @@ module Vagrant end else # Do a regular check - if test_host_ip == "0.0.0.0" || Vagrant::Util::IPv4Interfaces.ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } + if test_host_ip == "0.0.0.0" || Addrinfo.ip(test_host_ip).ipv4_loopback? || + ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } Vagrant::Util::IsPortOpen.is_port_open?(test_host_ip, host_port) else raise Errors::ForwardPortHostIPNotFound, name: machine.name, host_ip: host_ip diff --git a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb index 8f16954af..b0e1a5d43 100644 --- a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb +++ b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb @@ -195,12 +195,23 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do end context "when host ip does not exist" do - let(:host_ip) { "127.0.0.2" } + let(:host_ip) { "192.168.99.100" } let(:name) { "default" } it "should raise an error including the machine name" do allow(machine).to receive(:name).and_return(name) - expect{ instance.send(:port_check, host_ip, host_port) }.to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound, /#{name}/) + expect{ instance.send(:port_check, host_ip, host_port) }. + to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound, /#{name}/) + end + end + + context "with loopback address" do + let (:host_ip) { "127.1.2.40" } + let (:addrinfo) { double("addrinfo", ipv4_loopback?: true) } + + it "should check if the port is open" do + expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) + instance.send(:port_check, host_ip, host_port) end end end From e7d48b0b935af6fc95b6aedf328fa2f0654ae754 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Thu, 28 May 2020 09:40:15 -0400 Subject: [PATCH 2/3] Remove test double We don't actually need a test double here because `#ipv4_loopback?` will return true for any address in the `127.0.0.0/8` space. --- .../action/builtin/handle_forwarded_port_collisions_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb index b0e1a5d43..98937f040 100644 --- a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb +++ b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb @@ -207,7 +207,6 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do context "with loopback address" do let (:host_ip) { "127.1.2.40" } - let (:addrinfo) { double("addrinfo", ipv4_loopback?: true) } it "should check if the port is open" do expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) From 73130e3ff61accd8a75b463cdf6a838845f73c87 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Fri, 29 May 2020 15:51:24 -0400 Subject: [PATCH 3/3] Switch to Vagrant::Util methods --- lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb | 2 +- .../action/builtin/handle_forwarded_port_collisions_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index 1cd2efbff..aafb1e396 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -270,7 +270,7 @@ module Vagrant else # Do a regular check if test_host_ip == "0.0.0.0" || Addrinfo.ip(test_host_ip).ipv4_loopback? || - ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } + Vagrant::Util::IPv4Interfaces.ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } Vagrant::Util::IsPortOpen.is_port_open?(test_host_ip, host_port) else raise Errors::ForwardPortHostIPNotFound, name: machine.name, host_ip: host_ip diff --git a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb index 98937f040..151c27202 100644 --- a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb +++ b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb @@ -209,7 +209,7 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do let (:host_ip) { "127.1.2.40" } it "should check if the port is open" do - expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) + expect(Vagrant::Util::IsPortOpen).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) instance.send(:port_check, host_ip, host_port) end end