Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clickjacking.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Clickjacking Protection Middleware.
  3. This module provides a middleware that implements protection against a
  4. malicious site loading resources from your site in a hidden frame.
  5. """
  6. from django.conf import settings
  7. from django.utils.deprecation import MiddlewareMixin
  8. class XFrameOptionsMiddleware(MiddlewareMixin):
  9. """
  10. Set the X-Frame-Options HTTP header in HTTP responses.
  11. Do not set the header if it's already set or if the response contains
  12. a xframe_options_exempt value set to True.
  13. By default, set the X-Frame-Options header to 'SAMEORIGIN', meaning the
  14. response can only be loaded on a frame within the same site. To prevent the
  15. response from being loaded in a frame in any site, set X_FRAME_OPTIONS in
  16. your project's Django settings to 'DENY'.
  17. """
  18. def process_response(self, request, response):
  19. # Don't set it if it's already in the response
  20. if response.get('X-Frame-Options') is not None:
  21. return response
  22. # Don't set it if they used @xframe_options_exempt
  23. if getattr(response, 'xframe_options_exempt', False):
  24. return response
  25. response['X-Frame-Options'] = self.get_xframe_options_value(request,
  26. response)
  27. return response
  28. def get_xframe_options_value(self, request, response):
  29. """
  30. Get the value to set for the X_FRAME_OPTIONS header. Use the value from
  31. the X_FRAME_OPTIONS setting, or 'SAMEORIGIN' if not set.
  32. This method can be overridden if needed, allowing it to vary based on
  33. the request or response.
  34. """
  35. return getattr(settings, 'X_FRAME_OPTIONS', 'SAMEORIGIN').upper()