diff --git a/django/contrib/gis/geoip2/base.py b/django/contrib/gis/geoip2/base.py index 545bd12a25..40b45fd72b 100644 --- a/django/contrib/gis/geoip2/base.py +++ b/django/contrib/gis/geoip2/base.py @@ -4,8 +4,8 @@ import socket import geoip2.database from django.conf import settings -from django.core.validators import ipv4_re -from django.utils.ipv6 import is_valid_ipv6_address +from django.core.exceptions import ValidationError +from django.core.validators import validate_ipv46_address from .resources import City, Country @@ -157,7 +157,9 @@ class GeoIP2: raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file) # 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) return query diff --git a/tests/gis_tests/test_geoip2.py b/tests/gis_tests/test_geoip2.py index a362aed953..7d6b965d12 100644 --- a/tests/gis_tests/test_geoip2.py +++ b/tests/gis_tests/test_geoip2.py @@ -161,3 +161,10 @@ class GeoIPTest(unittest.TestCase): 'city': city_path, } 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')