security.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ====================
  2. Security in Django
  3. ====================
  4. This document will show you the security features of Django as well
  5. as give some advice about securing a Django site.
  6. .. _cross-site-scripting:
  7. Cross site scripting (XSS) protection
  8. =====================================
  9. .. highlightlang:: html+django
  10. XSS attacks allow a user to inject client side scripts into the
  11. browsers of other users. This is usually achieved by storing the malicious
  12. scripts to the database where it will be retrieved and displayed to other users
  13. or to get users to click a link containing variables containing scripts that
  14. will be rendered by the user's browser. However, XSS attacks can originate
  15. from any untrusted source of data such as cookies or web services.
  16. Using Django templates protects you against the majority of XSS attacks.
  17. However, it is important to understand what protections it provides
  18. and its limitations.
  19. Django templates :ref:`escape specific characters <automatic-html-escaping>`
  20. which are particularly dangerous to HTML. While this protects users from most
  21. malicious input, it is not entirely foolproof. For example, it will not
  22. protect the following:
  23. .. code-block:: html+django
  24. <style class={{ var }}>...</style>
  25. If ``var`` is set to ``'class1 onmouseover=javascript:func()'``, this can result
  26. in unauthorized javascript execution depending on how the browser renders
  27. imperfect HTML.
  28. It is also important to be particularly careful when using ``is_safe`` with
  29. custom template tags, the :ttag:`safe` template tag, :mod:`mark_safe
  30. <django.utils.safestring>`, and when autoescape is turned off.
  31. In addition, if you are using the template system to output something other
  32. than HTML, there may be entirely separate characters and words which require
  33. escaping.
  34. You should also be very careful when storing HTML to the database especially
  35. when that HTML will be retrieved and displayed.
  36. Cross site request forgery (CSRF) protection
  37. ============================================
  38. CSRF attacks allow a malicious user to execute actions using the credentials
  39. of another user without that user's knowledge or consent.
  40. Django has built-in protection against most types of CSRF attacks, providing you
  41. have :ref:`enabled and used it <using-csrf>` where appropriate. However, as with
  42. any mitigation technique, there are limitations. For example, it is possible to
  43. disable the CSRF module globally or for particular views. You should only do
  44. this if you know what you are doing. There are other :ref:`limitations
  45. <csrf-limitations>` if your site has subdomains that are outside of your
  46. control.
  47. :ref:`CSRF protection works <how-csrf-works>` by checking for a nonce in each
  48. POST request. This ensures that a malicious user cannot simply "replay" a form
  49. POST to your website and have another logged in user unwittingly submit that
  50. form. The malicious user would have to know the nonce, which is user specific
  51. (using a cookie).
  52. Be very careful with marking views with the ``csrf_exempt`` decorator unless
  53. it is absolutely necessary.
  54. SQL injection protection
  55. ========================
  56. SQL injection is a type of attack where a malicious user is able to execute
  57. arbitrary SQL code on a database. This can result in records
  58. being deleted or data leakage.
  59. By using Django's querysets, the resulting SQL will be properly escaped by
  60. the underlying database driver. However, Django also gives developers power to
  61. write :ref:`raw queries <executing-raw-queries>` or execute
  62. :ref:`custom sql <executing-custom-sql>`. These capabilities should be used
  63. sparingly and you should always be careful to properly escape any parameters
  64. that the user can control. In addition, you should exercise caution when using
  65. :meth:`extra() <django.db.models.query.QuerySet.extra>`.
  66. Clickjacking protection
  67. =======================
  68. Clickjacking is a type of attack where a malicious site wraps another site
  69. in a frame. This attack can result in an unsuspecting user being tricked
  70. into performing unintended actions on the target site.
  71. Django contains :ref:`clickjacking protection <clickjacking-prevention>` in
  72. the form of the
  73. :mod:`X-Frame-Options middleware <django.middleware.clickjacking.XFrameOptionsMiddleware>`
  74. which in a supporting browser can prevent a site from being rendered inside
  75. a frame. It is possible to disable the protection on a per view basis
  76. or to configure the exact header value sent.
  77. The middleware is strongly recommended for any site that does not need to have
  78. its pages wrapped in a frame by third party sites, or only needs to allow that
  79. for a small section of the site.
  80. SSL/HTTPS
  81. =========
  82. It is always better for security, though not always practical in all cases, to
  83. deploy your site behind HTTPS. Without this, it is possible for malicious
  84. network users to sniff authentication credentials or any other information
  85. transferred between client and server, and in some cases - **active** network
  86. attackers - to alter data that is sent in either direction.
  87. If you want the protection that HTTPS provides, and have enabled it on your
  88. server, there are some additional steps to consider to ensure that sensitive
  89. information is not leaked:
  90. * Set up redirection so that requests over HTTP are redirected to HTTPS.
  91. It is possible to do this with a piece of Django middleware. However, this has
  92. problems for the common case of a Django app running behind a reverse
  93. proxy. Often, reverse proxies are configured to set the ``X-Forwarded-SSL``
  94. header (or equivalent) if the incoming connection was HTTPS, and the absence
  95. of this header could be used to detect a request that was not HTTPS. However,
  96. this method usually cannot be relied on, as a client, or a malicious active
  97. network attacker, could also set this header.
  98. So, for the case of a reverse proxy, it is recommended that the main web
  99. server should be configured to do the redirect to HTTPS, or configured to send
  100. HTTP requests to an app that unconditionally redirects to HTTPS.
  101. * Use 'secure' cookies.
  102. If a browser connects initially via HTTP, which is the default for most
  103. browsers, it is possible for existing cookies to be leaked. For this reason,
  104. you should set your :setting:`SESSION_COOKIE_SECURE` and
  105. :setting:`CSRF_COOKIE_SECURE` settings to ``True``. This instructs the browser
  106. to only send these cookies over HTTPS connections. Note that this will mean
  107. that sessions will not work over HTTP, and the CSRF protection will prevent
  108. any POST data being accepted over HTTP (which will be fine if you are
  109. redirecting all HTTP traffic to HTTPS).
  110. .. _additional-security-topics:
  111. Additional security topics
  112. ==========================
  113. While Django provides good security protection out of the box, it is still
  114. important to properly deploy your application and take advantage of the
  115. security protection of the web server, operating system and other components.
  116. * Make sure that your Python code is outside of the web server's root. This
  117. will ensure that your Python code is not accidentally served as plain text.
  118. * Django does not throttle requests to authenticate users. To protect against
  119. brute-force attacks against the authentication system, you may consider
  120. deploying a Django plugin or web server module to throttle these requests.
  121. * If your site accepts file uploads, it is strongly advised that you limit
  122. these uploads in your web server configuration to a reasonable
  123. size in order to prevent denial of service (DOS) attacks. In Apache, this
  124. can be easily set using the LimitRequestBody_ directive.
  125. * Keep your :setting:`SECRET_KEY` a secret.
  126. * It is a good idea to limit the accessibility of your caching system and
  127. database using a firewall.
  128. .. _LimitRequestBody: http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody