Ticket #14201: 14201-security.diff

File 14201-security.diff, 8.6 KB (added by David Fischer, 13 years ago)

updated security doc with sql injection and clickjacking

  • docs/index.txt

     
    185185    * :doc:`Messages <ref/contrib/messages>`
    186186    * :doc:`Pagination <topics/pagination>`
    187187    * :doc:`Redirects <ref/contrib/redirects>`
     188    * :doc:`Security <topics/security>`
    188189    * :doc:`Serialization <topics/serialization>`
    189190    * :doc:`Sessions <topics/http/sessions>`
    190191    * :doc:`Signals <topics/signals>`
  • docs/topics/db/sql.txt

     
    1313__ `performing raw queries`_
    1414__ `executing custom SQL directly`_
    1515
     16.. _executing-raw-queries:
     17
    1618Performing raw queries
    1719======================
    1820
     
    190192
    191193__ http://en.wikipedia.org/wiki/SQL_injection
    192194
     195.. _executing-custom-sql:
     196
    193197Executing custom SQL directly
    194198=============================
    195199
  • docs/topics/index.txt

     
    2323   i18n/index
    2424   logging
    2525   pagination
     26   security
    2627   serialization
    2728   settings
    2829   signals
  • docs/topics/security.txt

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

     
    2626user visits the attacker site and clicks "I Like Ponies" he will inadvertently
    2727click on the online store's "Buy Now" button and unknowingly purchase the item.
    2828
     29.. _clickjacking-prevention:
     30
    2931Preventing clickjacking
    3032=======================
    3133
  • docs/ref/contrib/csrf.txt

     
    2121.. _Cross Site Request Forgeries: http://www.squarefree.com/securitytips/web-developers.html#CSRF
    2222.. _9.1.1 Safe Methods, HTTP 1.1, RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
    2323
     24.. _using-csrf:
     25
    2426How to use it
    2527=============
    2628
     
    182184own view for handling this condition.  To do this, simply set the
    183185:setting:`CSRF_FAILURE_VIEW` setting.
    184186
     187.. _how-csrf-works:
     188
    185189How it works
    186190============
    187191
Back to Top