clickjacking.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 site and clicks "I Like Ponies" he will inadvertently
  19. click on the online store's "Buy Now" button and unknowingly purchase 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. .. versionchanged:: 1.6
  48. This middleware is enabled in the settings file generated by
  49. :djadmin:`startproject`.
  50. By default, the middleware will set the ``X-Frame-Options`` header to
  51. ``SAMEORIGIN`` for every outgoing ``HttpResponse``. If you want ``DENY``
  52. instead, set the :setting:`X_FRAME_OPTIONS` setting::
  53. X_FRAME_OPTIONS = 'DENY'
  54. When using the middleware there may be some views where you do **not** want the
  55. ``X-Frame-Options`` header set. For those cases, you can use a view decorator
  56. that tells the middleware not to set the header::
  57. from django.http import HttpResponse
  58. from django.views.decorators.clickjacking import xframe_options_exempt
  59. @xframe_options_exempt
  60. def ok_to_load_in_a_frame(request):
  61. return HttpResponse("This page is safe to load in a frame on any site.")
  62. Setting X-Frame-Options per view
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. To set the ``X-Frame-Options`` header on a per view basis, Django provides these
  65. decorators::
  66. from django.http import HttpResponse
  67. from django.views.decorators.clickjacking import xframe_options_deny
  68. from django.views.decorators.clickjacking import xframe_options_sameorigin
  69. @xframe_options_deny
  70. def view_one(request):
  71. return HttpResponse("I won't display in any frame!")
  72. @xframe_options_sameorigin
  73. def view_two(request):
  74. return HttpResponse("Display in a frame if it's from the same origin as me.")
  75. Note that you can use the decorators in conjunction with the middleware. Use of
  76. a decorator overrides the middleware.
  77. Limitations
  78. ===========
  79. The ``X-Frame-Options`` header will only protect against clickjacking in a
  80. modern browser. Older browsers will quietly ignore the header and need `other
  81. clickjacking prevention techniques`_.
  82. Browsers that support X-Frame-Options
  83. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. * Internet Explorer 8+
  85. * Firefox 3.6.9+
  86. * Opera 10.5+
  87. * Safari 4+
  88. * Chrome 4.1+
  89. See also
  90. ~~~~~~~~
  91. A `complete list`_ of browsers supporting ``X-Frame-Options``.
  92. .. _complete list: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header#Browser_compatibility
  93. .. _other clickjacking prevention techniques: http://en.wikipedia.org/wiki/Clickjacking#Prevention