Skip to content

Commit

Permalink
Handle IPv6 addresses when removing port numbers in request.location
Browse files Browse the repository at this point in the history
  • Loading branch information
abevoelker committed Jan 2, 2018
1 parent b00d68b commit 650add8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/geocoder/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ def geocoder_reject_trusted_ip_addresses(ip_addresses)
end

def geocoder_remove_port_from_addresses(ip_addresses)
ip_addresses.map { |ip| ip.split(':').first }
ip_addresses.map do |ip|
# IPv4
if ip.count('.') > 0
ip.split(':').first
# IPv6 bracket notation
elsif match = ip.match(/\[(\S+)\]/)
match.captures.first
# IPv6 bare notation
else
ip
end
end
end

def geocoder_reject_non_ipv4_addresses(ip_addresses)
Expand Down
6 changes: 6 additions & 0 deletions test/unit/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def test_geocoder_remove_port_from_addresses_with_port
req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end
def test_geocoder_remove_port_from_ipv6_addresses_with_port
expected_ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '2001:db8:1f70::999:de8:7648:6e8', '10.128.0.2']
ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '[2001:db8:1f70::999:de8:7648:6e8]:100', '10.128.0.2']
req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end
def test_geocoder_remove_port_from_addresses_without_port
expected_ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
Expand Down

0 comments on commit 650add8

Please sign in to comment.