Fixed #28115 -- Fixed IP address validation in geoip2 module

Regression in 277a4dd4b4.
Thanks Tim Graham for the test.
This commit is contained in:
Claude Paroz 2017-04-22 16:44:24 +02:00
parent 257075d4ad
commit 3f1ba76851
2 changed files with 12 additions and 3 deletions

View File

@ -4,8 +4,8 @@ import socket
import geoip2.database import geoip2.database
from django.conf import settings from django.conf import settings
from django.core.validators import ipv4_re from django.core.exceptions import ValidationError
from django.utils.ipv6 import is_valid_ipv6_address from django.core.validators import validate_ipv46_address
from .resources import City, Country from .resources import City, Country
@ -157,7 +157,9 @@ class GeoIP2:
raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file) raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file)
# Return the query string back to the caller. GeoIP2 only takes IP addresses. # Return the query string back to the caller. GeoIP2 only takes IP addresses.
if not (ipv4_re.match(query) or is_valid_ipv6_address(query)): try:
validate_ipv46_address(query)
except ValidationError:
query = socket.gethostbyname(query) query = socket.gethostbyname(query)
return query return query

View File

@ -161,3 +161,10 @@ class GeoIPTest(unittest.TestCase):
'city': city_path, 'city': city_path,
} }
self.assertEqual(repr(g), expected) self.assertEqual(repr(g), expected)
@mock.patch('socket.gethostbyname', return_value='expected')
def test_check_query(self, gethostbyname):
g = GeoIP2()
self.assertEqual(g._check_query('127.0.0.1'), '127.0.0.1')
self.assertEqual(g._check_query('2002:81ed:c9a5::81ed:c9a5'), '2002:81ed:c9a5::81ed:c9a5')
self.assertEqual(g._check_query('invalid-ip-address'), 'expected')