clickjacking.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ========================
  2. Clickjacking Protection
  3. ========================
  4. .. module:: django.middleware.clickjacking
  5. :synopsis: Protects against Clickjacking
  6. The clickjacking middleware and decorators provide easy-to-use protection
  7. against `clickjacking`_. This type of attack occurs when a malicious site
  8. tricks a user into clicking on a concealed element of another site which they
  9. have loaded in a hidden frame or iframe.
  10. .. _clickjacking: http://en.wikipedia.org/wiki/Clickjacking
  11. An example of clickjacking
  12. ==========================
  13. Suppose an online store has a page where a logged in user can click "Buy Now" to
  14. purchase an item. A user has chosen to stay logged into the store all the time
  15. for convenience. An attacker site might create an "I Like Ponies" button on one
  16. of their own pages, and load the store's page in a transparent iframe such that
  17. the "Buy Now" button is invisibly overlaid on the "I Like Ponies" button. If the
  18. user visits the attacker's site, clicking "I Like Ponies" will cause an
  19. inadvertent click on the "Buy Now" button and an unknowing purchase of the item.
  20. .. _clickjacking-prevention:
  21. Preventing clickjacking
  22. =======================
  23. Modern browsers honor the `X-Frame-Options`_ HTTP header that indicates whether
  24. or not a resource is allowed to load within a frame or iframe. If the response
  25. contains the header with a value of ``SAMEORIGIN`` then the browser will only
  26. load the resource in a frame if the request originated from the same site. If
  27. the header is set to ``DENY`` then the browser will block the resource from
  28. loading in a frame no matter which site made the request.
  29. .. _X-Frame-Options: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
  30. Django provides a few simple ways to include this header in responses from your
  31. site:
  32. 1. A simple middleware that sets the header in all responses.
  33. 2. A set of view decorators that can be used to override the middleware or to
  34. only set the header for certain views.
  35. How to use it
  36. =============
  37. Setting X-Frame-Options for all responses
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. To set the same ``X-Frame-Options`` value for all responses in your site, put
  40. ``'django.middleware.clickjacking.XFrameOptionsMiddleware'`` to
  41. :setting:`MIDDLEWARE_CLASSES`::
  42. MIDDLEWARE_CLASSES = (
  43. ...
  44. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  45. ...
  46. )
  47. This middleware is enabled in the settings file generated by
  48. :djadmin:`startproject`.
  49. By default, the middleware will set the ``X-Frame-Options`` header to
  50. ``SAMEORIGIN`` for every outgoing ``HttpResponse``. If you want ``DENY``
  51. instead, set the :setting:`X_FRAME_OPTIONS` setting::
  52. X_FRAME_OPTIONS = 'DENY'
  53. When using the middleware there may be some views where you do **not** want the
  54. ``X-Frame-Options`` header set. For those cases, you can use a view decorator
  55. that tells the middleware not to set the header::
  56. from django.http import HttpResponse
  57. from django.views.decorators.clickjacking import xframe_options_exempt
  58. @xframe_options_exempt
  59. def ok_to_load_in_a_frame(request):
  60. return HttpResponse("This page is safe to load in a frame on any site.")
  61. Setting X-Frame-Options per view
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. To set the ``X-Frame-Options`` header on a per view basis, Django provides these
  64. decorators::
  65. from django.http import HttpResponse
  66. from django.views.decorators.clickjacking import xframe_options_deny
  67. from django.views.decorators.clickjacking import xframe_options_sameorigin
  68. @xframe_options_deny
  69. def view_one(request):
  70. return HttpResponse("I won't display in any frame!")
  71. @xframe_options_sameorigin
  72. def view_two(request):
  73. return HttpResponse("Display in a frame if it's from the same origin as me.")
  74. Note that you can use the decorators in conjunction with the middleware. Use of
  75. a decorator overrides the middleware.
  76. Limitations
  77. ===========
  78. The ``X-Frame-Options`` header will only protect against clickjacking in a
  79. modern browser. Older browsers will quietly ignore the header and need `other
  80. clickjacking prevention techniques`_.
  81. Browsers that support X-Frame-Options
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. * Internet Explorer 8+
  84. * Firefox 3.6.9+
  85. * Opera 10.5+
  86. * Safari 4+
  87. * Chrome 4.1+
  88. See also
  89. ~~~~~~~~
  90. A `complete list`_ of browsers supporting ``X-Frame-Options``.
  91. .. _complete list: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header#Browser_compatibility
  92. .. _other clickjacking prevention techniques: http://en.wikipedia.org/wiki/Clickjacking#Prevention