Skip to content

Commit 1eec140

Browse files
Célestin Mattemhagander
authored andcommitted
Allow use of IP ranges for SEARCH_CLIENTS
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.
1 parent 6acb869 commit 1eec140

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

django/archives/mailarchives/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import email.policy
2121
from io import BytesIO
2222
from urllib.parse import quote
23+
import ipaddress
2324

2425
import json
2526

@@ -709,7 +710,12 @@ def search(request):
709710
return HttpResponseForbidden('Not public archives')
710711

711712
# Only certain hosts are allowed to call the search API
712-
if not request.META['REMOTE_ADDR'] in settings.SEARCH_CLIENTS:
713+
allowed = False
714+
for ip_range in settings.SEARCH_CLIENTS:
715+
if ipaddress.ip_address(request.META['REMOTE_ADDR']) in ipaddress.ip_network(ip_range):
716+
allowed = True
717+
break
718+
if not allowed:
713719
return HttpResponseForbidden('Invalid host')
714720

715721
curs = connection.cursor()

0 commit comments

Comments
 (0)