Skip to content

Commit 813847e

Browse files
committed
GH-29: Move the content type check to the primary middleware
- this will not let requests such as AJAX and static files pass through the checks
1 parent 992d701 commit 813847e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/django_forbid/middleware.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from .skills.forbid_device import ForbidDeviceMiddleware
24
from .skills.forbid_location import ForbidLocationMiddleware
35
from .skills.forbid_network import ForbidNetworkMiddleware
@@ -14,8 +16,11 @@ class ForbidMiddleware:
1416

1517
def __init__(self, get_response):
1618
self.get_response = get_response
19+
self.regex = re.compile(r"\w+/(?:html|xhtml\+xml|xml)")
1720

1821
def __call__(self, request):
19-
for skill in __skills__:
20-
self.get_response = skill(self.get_response)
21-
return self.get_response(request)
22+
get_response = self.get_response
23+
if self.regex.search(request.META.get("HTTP_ACCEPT")):
24+
for skill in __skills__:
25+
get_response = skill(get_response)
26+
return get_response(request)

src/django_forbid/skills/forbid_network.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import re
32

43
from django.http import HttpResponse
54
from django.http import HttpResponseForbidden
@@ -31,20 +30,11 @@ def forbidden_page():
3130
geoip2_tz = request.session.get("GEOIP2_TZ")
3231
verified_tz = request.session.get("VERIFIED_TZ", "")
3332

34-
if any([
35-
verified_tz == geoip2_tz,
36-
# Checks if VPN is False or not set.
37-
not Settings.get("OPTIONS.VPN", False),
38-
# Checks if the request is an AJAX request.
39-
not re.search(
40-
r"\w+\/(?:html|xhtml\+xml|xml)",
41-
request.META.get("HTTP_ACCEPT"),
42-
),
43-
]):
33+
# Checks if the user's timezone match with the last accessed one.
34+
if verified_tz == geoip2_tz or not Settings.get("OPTIONS.VPN", False):
4435
return self.get_response(request)
45-
4636
# Checks if GEOIP2_TZ and VERIFIED_TZ don't exist.
47-
if verified_tz and geoip2_tz != "N/A":
37+
elif verified_tz and geoip2_tz != "N/A":
4838
return forbidden_page()
4939

5040
if all(map(request.session.has_key, ("GEOIP2_TZ", *response_attributes))):

0 commit comments

Comments
 (0)