Skip to content

Commit

Permalink
fix: Update HTTP headers and CSRF sessions usage
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Jan 3, 2024
1 parent b41ec43 commit facb212
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions deploy/middleware/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
class CustomSecurityMiddleware(SecurityMiddleware):
"""
Extends Django's Security Middleware.
See https://docs.djangoproject.com/en/2.2/_modules/django/middleware/security/ for
the source code, as well as https://docs.djangoproject.com/en/2.2/ref/middleware/#module-django.middleware.security
See https://docs.djangoproject.com/en/3.2/_modules/django/middleware/security/ for
the source code, as well as https://docs.djangoproject.com/en/3.2/ref/middleware/#module-django.middleware.security
for docs on security middleware.
"""

def process_response(self, request, response):
"""
Extends the original security middleware to ensure the X-XSS-Protection header
is set to 0.
is set to 1.
"""
super().process_response(request, response)

if self.xss_filter:
response["X-XSS-Protection"] = "0"
response["X-XSS-Protection"] = "1"

return response
1 change: 1 addition & 0 deletions example_project/portal_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_REFERRER_POLICY = "strict-origin-when-cross-origin"
CSRF_USE_SESSIONS = True
RECAPTCHA_DOMAIN = "www.recaptcha.net"
AUTHENTICATION_BACKENDS = ["django.contrib.auth.backends.ModelBackend", "portal.backends.StudentLoginBackend"]
USE_TZ = True
Expand Down
1 change: 1 addition & 0 deletions example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_REFERRER_POLICY = "strict-origin-when-cross-origin"
CSRF_USE_SESSIONS = True
RECAPTCHA_DOMAIN = "www.recaptcha.net"
AUTHENTICATION_BACKENDS = ["django.contrib.auth.backends.ModelBackend", "portal.backends.StudentLoginBackend"]
USE_TZ = True
Expand Down
2 changes: 1 addition & 1 deletion portal/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_security_headers(self):
assert response.headers["cache-control"] == "private"
assert response.headers["x-content-type-options"] == "nosniff"
assert response.headers["x-frame-options"] == "DENY"
assert response.headers["x-xss-protection"] == "0"
assert response.headers["x-xss-protection"] == "1"


class TestSessionTimeoutMiddleware(TestCase):
Expand Down

0 comments on commit facb212

Please sign in to comment.