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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from functools import wraps
  2. def xframe_options_deny(view_func):
  3. """
  4. Modify a view function so its response has the X-Frame-Options HTTP
  5. header set to 'DENY' as long as the response doesn't already have that
  6. header set. Usage:
  7. @xframe_options_deny
  8. def some_view(request):
  9. ...
  10. """
  11. def wrapped_view(*args, **kwargs):
  12. resp = view_func(*args, **kwargs)
  13. if resp.get('X-Frame-Options') is None:
  14. resp['X-Frame-Options'] = 'DENY'
  15. return resp
  16. return wraps(view_func)(wrapped_view)
  17. def xframe_options_sameorigin(view_func):
  18. """
  19. Modify a view function so its response has the X-Frame-Options HTTP
  20. header set to 'SAMEORIGIN' as long as the response doesn't already have
  21. that header set. Usage:
  22. @xframe_options_sameorigin
  23. def some_view(request):
  24. ...
  25. """
  26. def wrapped_view(*args, **kwargs):
  27. resp = view_func(*args, **kwargs)
  28. if resp.get('X-Frame-Options') is None:
  29. resp['X-Frame-Options'] = 'SAMEORIGIN'
  30. return resp
  31. return wraps(view_func)(wrapped_view)
  32. def xframe_options_exempt(view_func):
  33. """
  34. Modify a view function by setting a response variable that instructs
  35. XFrameOptionsMiddleware to NOT set the X-Frame-Options HTTP header. Usage:
  36. @xframe_options_exempt
  37. def some_view(request):
  38. ...
  39. """
  40. def wrapped_view(*args, **kwargs):
  41. resp = view_func(*args, **kwargs)
  42. resp.xframe_options_exempt = True
  43. return resp
  44. return wraps(view_func)(wrapped_view)