You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
7.6 KiB

  1. ======
  2. Bleach
  3. ======
  4. .. image:: https://travis-ci.org/mozilla/bleach.png?branch=master
  5. :target: https://travis-ci.org/mozilla/bleach
  6. .. image:: https://badge.fury.io/py/Bleach.svg
  7. :target: http://badge.fury.io/py/Bleach
  8. Bleach is a whitelist-based HTML sanitizing library that escapes or strips
  9. markup and attributes.
  10. Bleach can also linkify text safely, applying filters that Django's ``urlize``
  11. filter cannot, and optionally setting ``rel`` attributes, even on links already
  12. in the text.
  13. Bleach is intended for sanitizing text from *untrusted* sources. If you find
  14. yourself jumping through hoops to allow your site administrators to do lots of
  15. things, you're probably outside the use cases. Either trust those users, or
  16. don't.
  17. Because it relies on html5lib_, Bleach is as good as modern browsers at dealing
  18. with weird, quirky HTML fragments. And *any* of Bleach's methods will fix
  19. unbalanced or mis-nested tags.
  20. The version on GitHub_ is the most up-to-date and contains the latest bug
  21. fixes. You can find full documentation on `ReadTheDocs`_.
  22. :Code: https://github.com/mozilla/bleach
  23. :Documentation: https://bleach.readthedocs.io/
  24. :Issue tracker: https://github.com/mozilla/bleach/issues
  25. :IRC: ``#bleach`` on irc.mozilla.org
  26. :License: Apache License v2; see LICENSE file
  27. Reporting Bugs
  28. ==============
  29. For regular bugs, please report them `in our issue tracker
  30. <https://github.com/mozilla/bleach/issues>`_.
  31. If you believe that you've found a security vulnerability, please `file a secure
  32. bug report in our bug tracker
  33. <https://bugzilla.mozilla.org/enter_bug.cgi?assigned_to=nobody%40mozilla.org&product=Webtools&component=Bleach-security&groups=webtools-security>`_
  34. or send an email to *security AT mozilla DOT org*.
  35. For more information on security-related bug disclosure and the PGP key to use
  36. for sending encrypted mail or to verify responses received from that address,
  37. please read our wiki page at
  38. `<https://www.mozilla.org/en-US/security/#For_Developers>`_.
  39. Installing Bleach
  40. =================
  41. Bleach is available on PyPI_, so you can install it with ``pip``::
  42. $ pip install bleach
  43. Or with ``easy_install``::
  44. $ easy_install bleach
  45. Or by cloning the repo from GitHub_::
  46. $ git clone git://github.com/mozilla/bleach.git
  47. Then install it by running::
  48. $ python setup.py install
  49. Upgrading Bleach
  50. ================
  51. .. warning::
  52. Before doing any upgrades, read through `Bleach Changes
  53. <https://bleach.readthedocs.io/en/latest/changes.html>`_ for backwards
  54. incompatible changes, newer versions, etc.
  55. Basic use
  56. =========
  57. The simplest way to use Bleach is:
  58. .. code-block:: python
  59. >>> import bleach
  60. >>> bleach.clean('an <script>evil()</script> example')
  61. u'an &lt;script&gt;evil()&lt;/script&gt; example'
  62. >>> bleach.linkify('an http://example.com url')
  63. u'an <a href="http://example.com" rel="nofollow">http://example.com</a> url
  64. .. _html5lib: https://github.com/html5lib/html5lib-python
  65. .. _GitHub: https://github.com/mozilla/bleach
  66. .. _ReadTheDocs: https://bleach.readthedocs.io/
  67. .. _PyPI: http://pypi.python.org/pypi/bleach
  68. Bleach Changes
  69. ==============
  70. Version 1.5 (November 4th, 2016)
  71. --------------------------------
  72. **Backwards incompatible changes**
  73. - clean: The list of ``ALLOWED_PROTOCOLS`` now defaults to http, https and
  74. mailto. Previously it was a long list of protocols something like ed2k, ftp,
  75. http, https, irc, mailto, news, gopher, nntp, telnet, webcal, xmpp, callto,
  76. feed, urn, aim, rsync, tag, ssh, sftp, rtsp, afs, data. #149
  77. **Changes**
  78. - clean: Added ``protocols`` to arguments list to let you override the list of
  79. allowed protocols. Thank you, Andreas Malecki! #149
  80. - linkify: Fix a bug involving periods at the end of an email address. Thank you,
  81. Lorenz Schori! #219
  82. - linkify: Fix linkification of non-ascii ports. Thank you Alexandre, Macabies!
  83. #207
  84. - linkify: Fix linkify inappropriately removing node tails when dropping nodes.
  85. #132
  86. - Fixed a test that failed periodically. #161
  87. - Switched from nose to py.test. #204
  88. - Add test matrix for all supported Python and html5lib versions. #230
  89. - Limit to html5lib ``>=0.999,!=0.9999,!=0.99999,<0.99999999`` because 0.9999
  90. and 0.99999 are busted.
  91. - Add support for ``python setup.py test``. #97
  92. Version 1.4.3 (May 23rd, 2016)
  93. ------------------------------
  94. **Changes**
  95. - Limit to html5lib ``>=0.999,<0.99999999`` because of impending change to
  96. sanitizer api. #195
  97. Version 1.4.2 (September 11, 2015)
  98. ----------------------------------
  99. **Changes**
  100. - linkify: Fix hang in linkify with ``parse_email=True``. #124
  101. - linkify: Fix crash in linkify when removing a link that is a first-child. #136
  102. - Updated TLDs.
  103. - linkify: Don't remove exterior brackets when linkifying. #146
  104. Version 1.4.1 (December 15, 2014)
  105. ---------------------------------
  106. **Changes**
  107. - Consistent order of attributes in output.
  108. - Python 3.4 support.
  109. Version 1.4 (January 12, 2014)
  110. ------------------------------
  111. **Changes**
  112. - linkify: Update linkify to use etree type Treewalker instead of simpletree.
  113. - Updated html5lib to version ``>=0.999``.
  114. - Update all code to be compatible with Python 3 and 2 using six.
  115. - Switch to Apache License.
  116. Version 1.3
  117. -----------
  118. - Used by Python 3-only fork.
  119. Version 1.2.2 (May 18, 2013)
  120. ----------------------------
  121. - Pin html5lib to version 0.95 for now due to major API break.
  122. Version 1.2.1 (February 19, 2013)
  123. ---------------------------------
  124. - clean() no longer considers ``feed:`` an acceptable protocol due to
  125. inconsistencies in browser behavior.
  126. Version 1.2 (January 28, 2013)
  127. ------------------------------
  128. - linkify() has changed considerably. Many keyword arguments have been
  129. replaced with a single callbacks list. Please see the documentation
  130. for more information.
  131. - Bleach will no longer consider unacceptable protocols when linkifying.
  132. - linkify() now takes a tokenizer argument that allows it to skip
  133. sanitization.
  134. - delinkify() is gone.
  135. - Removed exception handling from _render. clean() and linkify() may now
  136. throw.
  137. - linkify() correctly ignores case for protocols and domain names.
  138. - linkify() correctly handles markup within an <a> tag.
  139. Version 1.1.5
  140. -------------
  141. Version 1.1.4
  142. -------------
  143. Version 1.1.3 (July 10, 2012)
  144. -----------------------------
  145. - Fix parsing bare URLs when parse_email=True.
  146. Version 1.1.2 (June 1, 2012)
  147. ----------------------------
  148. - Fix hang in style attribute sanitizer. (#61)
  149. - Allow '/' in style attribute values.
  150. Version 1.1.1 (February 17, 2012)
  151. ---------------------------------
  152. - Fix tokenizer for html5lib 0.9.5.
  153. Version 1.1.0 (October 24, 2011)
  154. --------------------------------
  155. - linkify() now understands port numbers. (#38)
  156. - Documented character encoding behavior. (#41)
  157. - Add an optional target argument to linkify().
  158. - Add delinkify() method. (#45)
  159. - Support subdomain whitelist for delinkify(). (#47, #48)
  160. Version 1.0.4 (September 2, 2011)
  161. ---------------------------------
  162. - Switch to SemVer git tags.
  163. - Make linkify() smarter about trailing punctuation. (#30)
  164. - Pass exc_info to logger during rendering issues.
  165. - Add wildcard key for attributes. (#19)
  166. - Make linkify() use the HTMLSanitizer tokenizer. (#36)
  167. - Fix URLs wrapped in parentheses. (#23)
  168. - Make linkify() UTF-8 safe. (#33)
  169. Version 1.0.3 (June 14, 2011)
  170. -----------------------------
  171. - linkify() works with 3rd level domains. (#24)
  172. - clean() supports vendor prefixes in style values. (#31, #32)
  173. - Fix linkify() email escaping.
  174. Version 1.0.2 (June 6, 2011)
  175. ----------------------------
  176. - linkify() supports email addresses.
  177. - clean() supports callables in attributes filter.
  178. Version 1.0.1 (April 12, 2011)
  179. ------------------------------
  180. - linkify() doesn't drop trailing slashes. (#21)
  181. - linkify() won't linkify 'libgl.so.1'. (#22)