Allows the use of IP ranges in CIDR format in the SEARCH_CLIENTS
parameter. Individual addresses can still be specified and continue to
work like before.
import email.policy
from io import BytesIO
from urllib.parse import quote
+import ipaddress
import json
return HttpResponseForbidden('Not public archives')
# Only certain hosts are allowed to call the search API
- if not request.META['REMOTE_ADDR'] in settings.SEARCH_CLIENTS:
+ allowed = False
+ for ip_range in settings.SEARCH_CLIENTS:
+ if ipaddress.ip_address(request.META['REMOTE_ADDR']) in ipaddress.ip_network(ip_range):
+ allowed = True
+ break
+ if not allowed:
return HttpResponseForbidden('Invalid host')
curs = connection.cursor()