From 3f1ba76851e1dc140d4544fa50a463425be197b2 Mon Sep 17 00:00:00 2001
From: Claude Paroz <claude@2xlibre.net>
Date: Sat, 22 Apr 2017 16:44:24 +0200
Subject: [PATCH] Fixed #28115 -- Fixed IP address validation in geoip2 module

Regression in 277a4dd4b4cc2a2cad77139882f084480751a95a.
Thanks Tim Graham for the test.
---
 django/contrib/gis/geoip2/base.py | 8 +++++---
 tests/gis_tests/test_geoip2.py    | 7 +++++++
 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')