1. ==================
    
  2. Security in Django
    
  3. ==================
    
  4. 
    
  5. This document is an overview of Django's security features. It includes advice
    
  6. on securing a Django-powered site.
    
  7. 
    
  8. .. _cross-site-scripting:
    
  9. 
    
  10. Cross site scripting (XSS) protection
    
  11. =====================================
    
  12. 
    
  13. .. highlight:: html+django
    
  14. 
    
  15. XSS attacks allow a user to inject client side scripts into the browsers of
    
  16. other users. This is usually achieved by storing the malicious scripts in the
    
  17. database where it will be retrieved and displayed to other users, or by getting
    
  18. users to click a link which will cause the attacker's JavaScript to be executed
    
  19. by the user's browser. However, XSS attacks can originate from any untrusted
    
  20. source of data, such as cookies or web services, whenever the data is not
    
  21. sufficiently sanitized before including in a page.
    
  22. 
    
  23. Using Django templates protects you against the majority of XSS attacks.
    
  24. However, it is important to understand what protections it provides
    
  25. and its limitations.
    
  26. 
    
  27. Django templates :ref:`escape specific characters <automatic-html-escaping>`
    
  28. which are particularly dangerous to HTML. While this protects users from most
    
  29. malicious input, it is not entirely foolproof. For example, it will not
    
  30. protect the following:
    
  31. 
    
  32. .. code-block:: text
    
  33. 
    
  34.     <style class={{ var }}>...</style>
    
  35. 
    
  36. .. highlighting as html+django fails due to intentionally missing quotes.
    
  37. 
    
  38. If ``var`` is set to ``'class1 onmouseover=javascript:func()'``, this can result
    
  39. in unauthorized JavaScript execution, depending on how the browser renders
    
  40. imperfect HTML. (Quoting the attribute value would fix this case.)
    
  41. 
    
  42. It is also important to be particularly careful when using ``is_safe`` with
    
  43. custom template tags, the :tfilter:`safe` template tag, :mod:`mark_safe
    
  44. <django.utils.safestring>`, and when autoescape is turned off.
    
  45. 
    
  46. In addition, if you are using the template system to output something other
    
  47. than HTML, there may be entirely separate characters and words which require
    
  48. escaping.
    
  49. 
    
  50. You should also be very careful when storing HTML in the database, especially
    
  51. when that HTML is retrieved and displayed.
    
  52. 
    
  53. 
    
  54. Cross site request forgery (CSRF) protection
    
  55. ============================================
    
  56. 
    
  57. CSRF attacks allow a malicious user to execute actions using the credentials
    
  58. of another user without that user's knowledge or consent.
    
  59. 
    
  60. Django has built-in protection against most types of CSRF attacks, providing you
    
  61. have :ref:`enabled and used it <using-csrf>` where appropriate. However, as with
    
  62. any mitigation technique, there are limitations. For example, it is possible to
    
  63. disable the CSRF module globally or for particular views. You should only do
    
  64. this if you know what you are doing. There are other :ref:`limitations
    
  65. <csrf-limitations>` if your site has subdomains that are outside of your
    
  66. control.
    
  67. 
    
  68. :ref:`CSRF protection works <how-csrf-works>` by checking for a secret in each
    
  69. POST request. This ensures that a malicious user cannot "replay" a form POST to
    
  70. your website and have another logged in user unwittingly submit that form. The
    
  71. malicious user would have to know the secret, which is user specific (using a
    
  72. cookie).
    
  73. 
    
  74. When deployed with :ref:`HTTPS <security-recommendation-ssl>`,
    
  75. ``CsrfViewMiddleware`` will check that the HTTP referer header is set to a
    
  76. URL on the same origin (including subdomain and port). Because HTTPS
    
  77. provides additional security, it is imperative to ensure connections use HTTPS
    
  78. where it is available by forwarding insecure connection requests and using
    
  79. HSTS for supported browsers.
    
  80. 
    
  81. Be very careful with marking views with the ``csrf_exempt`` decorator unless
    
  82. it is absolutely necessary.
    
  83. 
    
  84. .. _sql-injection-protection:
    
  85. 
    
  86. SQL injection protection
    
  87. ========================
    
  88. 
    
  89. SQL injection is a type of attack where a malicious user is able to execute
    
  90. arbitrary SQL code on a database. This can result in records
    
  91. being deleted or data leakage.
    
  92. 
    
  93. Django's querysets are protected from SQL injection since their queries are
    
  94. constructed using query parameterization. A query's SQL code is defined
    
  95. separately from the query's parameters. Since parameters may be user-provided
    
  96. and therefore unsafe, they are escaped by the underlying database driver.
    
  97. 
    
  98. Django also gives developers power to write :ref:`raw queries
    
  99. <executing-raw-queries>` or execute :ref:`custom sql <executing-custom-sql>`.
    
  100. These capabilities should be used sparingly and you should always be careful to
    
  101. properly escape any parameters that the user can control. In addition, you
    
  102. should exercise caution when using :meth:`~django.db.models.query.QuerySet.extra`
    
  103. and :class:`~django.db.models.expressions.RawSQL`.
    
  104. 
    
  105. Clickjacking protection
    
  106. =======================
    
  107. 
    
  108. Clickjacking is a type of attack where a malicious site wraps another site
    
  109. in a frame. This attack can result in an unsuspecting user being tricked
    
  110. into performing unintended actions on the target site.
    
  111. 
    
  112. Django contains :ref:`clickjacking protection <clickjacking-prevention>` in
    
  113. the form of the
    
  114. :mod:`X-Frame-Options middleware <django.middleware.clickjacking.XFrameOptionsMiddleware>`
    
  115. which in a supporting browser can prevent a site from being rendered inside
    
  116. a frame. It is possible to disable the protection on a per view basis
    
  117. or to configure the exact header value sent.
    
  118. 
    
  119. The middleware is strongly recommended for any site that does not need to have
    
  120. its pages wrapped in a frame by third party sites, or only needs to allow that
    
  121. for a small section of the site.
    
  122. 
    
  123. .. _security-recommendation-ssl:
    
  124. 
    
  125. SSL/HTTPS
    
  126. =========
    
  127. 
    
  128. It is always better for security to deploy your site behind HTTPS. Without
    
  129. this, it is possible for malicious network users to sniff authentication
    
  130. credentials or any other information transferred between client and server, and
    
  131. in some cases -- **active** network attackers -- to alter data that is sent in
    
  132. either direction.
    
  133. 
    
  134. If you want the protection that HTTPS provides, and have enabled it on your
    
  135. server, there are some additional steps you may need:
    
  136. 
    
  137. * If necessary, set :setting:`SECURE_PROXY_SSL_HEADER`, ensuring that you have
    
  138.   understood the warnings there thoroughly. Failure to do this can result
    
  139.   in CSRF vulnerabilities, and failure to do it correctly can also be
    
  140.   dangerous!
    
  141. 
    
  142. * Set :setting:`SECURE_SSL_REDIRECT` to ``True``, so that requests over HTTP
    
  143.   are redirected to HTTPS.
    
  144. 
    
  145.   Please note the caveats under :setting:`SECURE_PROXY_SSL_HEADER`. For the
    
  146.   case of a reverse proxy, it may be easier or more secure to configure the
    
  147.   main web server to do the redirect to HTTPS.
    
  148. 
    
  149. * Use 'secure' cookies.
    
  150. 
    
  151.   If a browser connects initially via HTTP, which is the default for most
    
  152.   browsers, it is possible for existing cookies to be leaked. For this reason,
    
  153.   you should set your :setting:`SESSION_COOKIE_SECURE` and
    
  154.   :setting:`CSRF_COOKIE_SECURE` settings to ``True``. This instructs the browser
    
  155.   to only send these cookies over HTTPS connections. Note that this will mean
    
  156.   that sessions will not work over HTTP, and the CSRF protection will prevent
    
  157.   any POST data being accepted over HTTP (which will be fine if you are
    
  158.   redirecting all HTTP traffic to HTTPS).
    
  159. 
    
  160. * Use :ref:`http-strict-transport-security` (HSTS)
    
  161. 
    
  162.   HSTS is an HTTP header that informs a browser that all future connections
    
  163.   to a particular site should always use HTTPS. Combined with redirecting
    
  164.   requests over HTTP to HTTPS, this will ensure that connections always enjoy
    
  165.   the added security of SSL provided one successful connection has occurred.
    
  166.   HSTS may either be configured with :setting:`SECURE_HSTS_SECONDS`,
    
  167.   :setting:`SECURE_HSTS_INCLUDE_SUBDOMAINS`, and :setting:`SECURE_HSTS_PRELOAD`,
    
  168.   or on the web server.
    
  169. 
    
  170. .. _host-headers-virtual-hosting:
    
  171. 
    
  172. Host header validation
    
  173. ======================
    
  174. 
    
  175. Django uses the ``Host`` header provided by the client to construct URLs in
    
  176. certain cases. While these values are sanitized to prevent Cross Site Scripting
    
  177. attacks, a fake ``Host`` value can be used for Cross-Site Request Forgery,
    
  178. cache poisoning attacks, and poisoning links in emails.
    
  179. 
    
  180. Because even seemingly-secure web server configurations are susceptible to fake
    
  181. ``Host`` headers, Django validates ``Host`` headers against the
    
  182. :setting:`ALLOWED_HOSTS` setting in the
    
  183. :meth:`django.http.HttpRequest.get_host()` method.
    
  184. 
    
  185. This validation only applies via :meth:`~django.http.HttpRequest.get_host()`;
    
  186. if your code accesses the ``Host`` header directly from ``request.META`` you
    
  187. are bypassing this security protection.
    
  188. 
    
  189. For more details see the full :setting:`ALLOWED_HOSTS` documentation.
    
  190. 
    
  191. .. warning::
    
  192. 
    
  193.    Previous versions of this document recommended configuring your web server to
    
  194.    ensure it validates incoming HTTP ``Host`` headers. While this is still
    
  195.    recommended, in many common web servers a configuration that seems to
    
  196.    validate the ``Host`` header may not in fact do so. For instance, even if
    
  197.    Apache is configured such that your Django site is served from a non-default
    
  198.    virtual host with the ``ServerName`` set, it is still possible for an HTTP
    
  199.    request to match this virtual host and supply a fake ``Host`` header. Thus,
    
  200.    Django now requires that you set :setting:`ALLOWED_HOSTS` explicitly rather
    
  201.    than relying on web server configuration.
    
  202. 
    
  203. Additionally, Django requires you to explicitly enable support for the
    
  204. ``X-Forwarded-Host`` header (via the :setting:`USE_X_FORWARDED_HOST` setting)
    
  205. if your configuration requires it.
    
  206. 
    
  207. Referrer policy
    
  208. ===============
    
  209. 
    
  210. Browsers use the ``Referer`` header as a way to send information to a site
    
  211. about how users got there. By setting a *Referrer Policy* you can help to
    
  212. protect the privacy of your users, restricting under which circumstances the
    
  213. ``Referer`` header is set. See :ref:`the referrer policy section of the
    
  214. security middleware reference <referrer-policy>` for details.
    
  215. 
    
  216. Cross-origin opener policy
    
  217. ==========================
    
  218. 
    
  219. .. versionadded:: 4.0
    
  220. 
    
  221. The cross-origin opener policy (COOP) header allows browsers to isolate a
    
  222. top-level window from other documents by putting them in a different context
    
  223. group so that they cannot directly interact with the top-level window. If a
    
  224. document protected by COOP opens a cross-origin popup window, the popup’s
    
  225. ``window.opener`` property will be ``null``. COOP protects against cross-origin
    
  226. attacks. See :ref:`the cross-origin opener policy section of the security
    
  227. middleware reference <cross-origin-opener-policy>` for details.
    
  228. 
    
  229. Session security
    
  230. ================
    
  231. 
    
  232. Similar to the :ref:`CSRF limitations <csrf-limitations>` requiring a site to
    
  233. be deployed such that untrusted users don't have access to any subdomains,
    
  234. :mod:`django.contrib.sessions` also has limitations. See :ref:`the session
    
  235. topic guide section on security <topics-session-security>` for details.
    
  236. 
    
  237. .. _user-uploaded-content-security:
    
  238. 
    
  239. User-uploaded content
    
  240. =====================
    
  241. 
    
  242. .. note::
    
  243.     Consider :ref:`serving static files from a cloud service or CDN
    
  244.     <staticfiles-from-cdn>` to avoid some of these issues.
    
  245. 
    
  246. * If your site accepts file uploads, it is strongly advised that you limit
    
  247.   these uploads in your web server configuration to a reasonable
    
  248.   size in order to prevent denial of service (DOS) attacks. In Apache, this
    
  249.   can be easily set using the LimitRequestBody_ directive.
    
  250. 
    
  251. * If you are serving your own static files, be sure that handlers like Apache's
    
  252.   ``mod_php``, which would execute static files as code, are disabled. You don't
    
  253.   want users to be able to execute arbitrary code by uploading and requesting a
    
  254.   specially crafted file.
    
  255. 
    
  256. * Django's media upload handling poses some vulnerabilities when that media is
    
  257.   served in ways that do not follow security best practices. Specifically, an
    
  258.   HTML file can be uploaded as an image if that file contains a valid PNG
    
  259.   header followed by malicious HTML. This file will pass verification of the
    
  260.   library that Django uses for :class:`~django.db.models.ImageField` image
    
  261.   processing (Pillow). When this file is subsequently displayed to a
    
  262.   user, it may be displayed as HTML depending on the type and configuration of
    
  263.   your web server.
    
  264. 
    
  265.   No bulletproof technical solution exists at the framework level to safely
    
  266.   validate all user uploaded file content, however, there are some other steps
    
  267.   you can take to mitigate these attacks:
    
  268. 
    
  269.   #. One class of attacks can be prevented by always serving user uploaded
    
  270.      content from a distinct top-level or second-level domain. This prevents
    
  271.      any exploit blocked by `same-origin policy`_ protections such as cross
    
  272.      site scripting. For example, if your site runs on ``example.com``, you
    
  273.      would want to serve uploaded content (the :setting:`MEDIA_URL` setting)
    
  274.      from something like ``usercontent-example.com``. It's *not* sufficient to
    
  275.      serve content from a subdomain like ``usercontent.example.com``.
    
  276. 
    
  277.   #. Beyond this, applications may choose to define a list of allowable
    
  278.      file extensions for user uploaded files and configure the web server
    
  279.      to only serve such files.
    
  280. 
    
  281. .. _same-origin policy: https://en.wikipedia.org/wiki/Same-origin_policy
    
  282. 
    
  283. .. _additional-security-topics:
    
  284. 
    
  285. Additional security topics
    
  286. ==========================
    
  287. 
    
  288. While Django provides good security protection out of the box, it is still
    
  289. important to properly deploy your application and take advantage of the
    
  290. security protection of the web server, operating system and other components.
    
  291. 
    
  292. * Make sure that your Python code is outside of the web server's root. This
    
  293.   will ensure that your Python code is not accidentally served as plain text
    
  294.   (or accidentally executed).
    
  295. * Take care with any :ref:`user uploaded files <file-upload-security>`.
    
  296. * Django does not throttle requests to authenticate users. To protect against
    
  297.   brute-force attacks against the authentication system, you may consider
    
  298.   deploying a Django plugin or web server module to throttle these requests.
    
  299. * Keep your :setting:`SECRET_KEY`, and :setting:`SECRET_KEY_FALLBACKS` if in
    
  300.   use, secret.
    
  301. * It is a good idea to limit the accessibility of your caching system and
    
  302.   database using a firewall.
    
  303. * Take a look at the Open Web Application Security Project (OWASP) `Top 10
    
  304.   list`_ which identifies some common vulnerabilities in web applications. While
    
  305.   Django has tools to address some of the issues, other issues must be
    
  306.   accounted for in the design of your project.
    
  307. * Mozilla discusses various topics regarding `web security`_. Their
    
  308.   pages also include security principles that apply to any system.
    
  309. 
    
  310. .. _LimitRequestBody: https://httpd.apache.org/docs/2.4/mod/core.html#limitrequestbody
    
  311. .. _Top 10 list: https://owasp.org/Top10/
    
  312. .. _web security: https://infosec.mozilla.org/guidelines/web_security.html