mirror of https://github.com/django/django.git
Fixed #28115 -- Fixed IP address validation in geoip2 module
Regression in 277a4dd4b4
.
Thanks Tim Graham for the test.
This commit is contained in:
parent
257075d4ad
commit
3f1ba76851
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue