kernel/v2: validate forwarded ports [GH-3187]

This commit is contained in:
Mitchell Hashimoto 2014-03-13 08:31:59 -07:00
parent 03c15343e4
commit 012c28606f
4 changed files with 30 additions and 0 deletions

View File

@ -19,6 +19,8 @@ BUG FIXES:
- core: PowerShell scripts work when they're in a directory with - core: PowerShell scripts work when they're in a directory with
spaces. [GH-3100] spaces. [GH-3100]
- core: If you add a box path that doesn't exist, error earlier. [GH-3091] - core: If you add a box path that doesn't exist, error earlier. [GH-3091]
- core: Validation on forwarded ports to make sure they're between
0 and 65535. [GH-3187]
- guests/darwin: Fix an exception when configuring networks. [GH-3143] - guests/darwin: Fix an exception when configuring networks. [GH-3143]
- hosts/linux: Unusual sed delimiter to avoid conflicts. [GH-3167] - hosts/linux: Unusual sed delimiter to avoid conflicts. [GH-3167]
- providers/virtualbox: Make more internal interactions with VBoxManage - providers/virtualbox: Make more internal interactions with VBoxManage

View File

@ -548,6 +548,7 @@ module VagrantPlugins
fp_used = Set.new fp_used = Set.new
valid_network_types = [:forwarded_port, :private_network, :public_network] valid_network_types = [:forwarded_port, :private_network, :public_network]
port_range=(1..65535)
networks.each do |type, options| networks.each do |type, options|
if !valid_network_types.include?(type) if !valid_network_types.include?(type)
errors << I18n.t("vagrant.config.vm.network_type_invalid", errors << I18n.t("vagrant.config.vm.network_type_invalid",
@ -570,6 +571,10 @@ module VagrantPlugins
fp_used.add(key) fp_used.add(key)
end end
if !port_range.include?(options[:host]) || !port_range.include?(options[:guest])
errors << I18n.t("vagrant.config.vm.network_fp_invalid_port")
end
end end
if type == :private_network if type == :private_network

View File

@ -1082,6 +1082,8 @@ en:
properly, try changing this IP. properly, try changing this IP.
network_ip_required: |- network_ip_required: |-
An IP is required for a private network. An IP is required for a private network.
network_fp_invalid_port: |-
Ports to forward must be 1 to 65535
network_fp_host_not_unique: |- network_fp_host_not_unique: |-
Forwarded port '%{host}' (host port) is declared multiple times Forwarded port '%{host}' (host port) is declared multiple times
with the protocol '%{protocol}'. with the protocol '%{protocol}'.

View File

@ -7,6 +7,13 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
let(:machine) { double("machine") } let(:machine) { double("machine") }
def assert_invalid
errors = subject.validate(machine)
if !errors.values.any? { |v| !v.empty? }
raise "No errors: #{errors.inspect}"
end
end
def assert_valid def assert_valid
errors = subject.validate(machine) errors = subject.validate(machine)
if !errors.values.all? { |v| v.empty? } if !errors.values.all? { |v| v.empty? }
@ -125,6 +132,20 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
expect(n[1][:guest]).to eq(45) expect(n[1][:guest]).to eq(45)
expect(n[1][:host]).to eq(4545) expect(n[1][:host]).to eq(4545)
end end
it "is an error if forwarding a port too low" do
subject.network "forwarded_port",
guest: "45", host: "-5"
subject.finalize!
assert_invalid
end
it "is an error if forwarding a port too high" do
subject.network "forwarded_port",
guest: "45", host: "74545"
subject.finalize!
assert_invalid
end
end end
describe "#provider and #get_provider_config" do describe "#provider and #get_provider_config" do