2
0

clickjacking.txt 4.4 KB

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