Ticket #37131: security_xss.patch

File security_xss.patch, 5.4 KB (added by blighj, 109 minutes ago)
  • docs/topics/security.txt

    diff --git a/docs/topics/security.txt b/docs/topics/security.txt
    index 088e936361..3f7c9f561e 100644
    a b Security in Django  
    55This document is an overview of Django's security features. It includes advice
    66on securing a Django-powered site.
    77
     8..  note::
     9
     10    Django's web security implementations have been designed with security for
     11    real-world applications in mind. Django is a general-purpose web
     12    application framework, and its defaults reflect this - they will not be the
     13    best solution for every particular case. Special cases deserve special
     14    attention to their needs.
     15
     16    Web security requires a multi-layered approach. Neither a single layer of
     17    protection, nor an apparent weakness in one, can on its own reflect on the
     18    overall security of a Django website. This needs to be borne in mind
     19    particularly when assessing individual points in security audits.
     20
    821.. _sanitize-user-input:
    922
    1023Always sanitize user input
    details on validating user inputs in Django.  
    2033Cross site scripting (XSS) protection
    2134=====================================
    2235
    23 XSS attacks allow a user to inject client side scripts into the browsers of
    24 other users. This is usually achieved by storing the malicious scripts in the
    25 database where it will be retrieved and displayed to other users, or by getting
    26 users to click a link which will cause the attacker's JavaScript to be executed
    27 by the user's browser. However, XSS attacks can originate from any untrusted
    28 source of data, such as cookies or web services, whenever the data is not
    29 sufficiently sanitized before including in a page.
     36In a cross site scripting attack, malicious code in the form of a client-side
     37script is injected into another user's web browser, where it will be executed.
     38
     39This is typically done by:
     40
     41* storing the malicious script in the database where it will be retrieved and
     42  presented to other users in their browsers, or
     43* getting users to click a link which will cause the attacker's JavaScript to
     44  be executed by the user's browser.
     45
     46Cross site scripting attacks can originate from any untrusted source of data,
     47including cookies or web services, if the data are not adequately sanitized
     48before being published in a page.
    3049
    31 Using Django templates protects you against the majority of XSS attacks.
    32 However, it is important to understand what protections it provides
    33 and its limitations.
     50Django templates provide protection against the majority of cross site
     51scripting attacks by :ref:`automatically escaping characters that represent a
     52risk <automatic-html-escaping>` (that is, HTML characters that could be
     53*interpreted* by the browser to malicious effect are instead safely
     54*displayed*).
    3455
    35 Django templates :ref:`escape specific characters <automatic-html-escaping>`
    36 which are particularly dangerous to HTML. While this protects users from most
    37 malicious input, it is not entirely foolproof. For example, it will not
    38 protect the following:
     56However, the extent of this protection and its limitations should be understood.
     57
     58Ambiguity, in browsers' interpretation of HTML and in developers' own
     59intentions, can open a gap in which autoescaping can fail. Suppose that a
     60developer creates:
    3961
    4062.. code-block:: text
    4163
    protect the following:  
    4365
    4466.. highlighting as html+django fails due to intentionally missing quotes.
    4567
    46 If ``var`` is set to ``'class1 onmouseover=javascript:func()'``, this can
    47 result in unauthorized JavaScript execution, depending on how the browser
    48 renders imperfect HTML. (Quoting the attribute value would fix this case.)
     68where a ``var`` containing something like ``'class1'`` seems to work. Not only
     69might it fail to work at all in other browsers, if ``var`` were set to
     70``'class1 onmouseover=javascript:func()'``, this could result in unauthorized
     71JavaScript execution, depending on how the browser renders imperfect HTML.
    4972
    50 It is also important to be particularly careful when using ``is_safe`` with
    51 custom template tags, the :tfilter:`safe` template tag, :mod:`mark_safe
    52 <django.utils.safestring>`, and when autoescape is turned off.
     73Explicit template design, in which the quotes are not left to the variable to
     74provide:
    5375
    54 In addition, if you are using the template system to output something other
    55 than HTML, there may be entirely separate characters and words which require
    56 escaping.
     76.. code-block:: html+django
    5777
    58 You should also be very careful when storing HTML in the database, especially
    59 when that HTML is retrieved and displayed.
     78    <style class="{{ var }}">...</style>
     79
     80would eliminate this possibility.
    6081
     82It is also important to be particularly careful when using the ``is_safe``
     83attribute with custom template tags, the :tfilter:`safe` template tag,
     84:mod:`mark_safe <django.utils.safestring>`, and when autoescape is turned off.
     85
     86Django's built-in escaping is intended to protect HTML output. If you are using
     87the template system to output something other than HTML, the characters and
     88strings that require escaping might be entirely different.
     89
     90You should also be very careful when storing HTML in the database, especially
     91when that HTML is retrieved and displayed. Unless the HTML is guaranteed to
     92come from a trusted source - user input is *not* a trusted source - stored HTML
     93should be checked and sanitized, preferably on input as well as output.
    6194
    6295Cross site request forgery (CSRF) protection
    6396============================================
Back to Top