summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2017-04-22 16:44:24 +0200
committerClaude Paroz <claude@2xlibre.net>2017-04-22 20:10:24 +0200
commit3f1ba76851e1dc140d4544fa50a463425be197b2 (patch)
treed690d69775e0d44f856b33e9ef2ab4c69eb3bf43
parent257075d4adf9ce0fae8ca1ec3c680dc071fa5a72 (diff)
Fixed #28115 -- Fixed IP address validation in geoip2 module
Regression in 277a4dd4b4cc2a2cad77139882f084480751a95a. Thanks Tim Graham for the test.
-rw-r--r--django/contrib/gis/geoip2/base.py8
-rw-r--r--tests/gis_tests/test_geoip2.py7
2 files changed, 12 insertions, 3 deletions
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')