README 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. kses 0.2.2 README [kses strips evil scripts!]
  2. =================
  3. * INTRODUCTION *
  4. Welcome to kses - an HTML/XHTML filter written in PHP. It removes all unwanted
  5. HTML elements and attributes, no matter how malformed HTML input you give it.
  6. It also does several checks on attribute values. kses can be used to avoid
  7. Cross-Site Scripting (XSS), Buffer Overflows and Denial of Service attacks,
  8. among other things.
  9. The program is released under the terms of the GNU General Public License. You
  10. should look into what that means, before using kses in your programs. You can
  11. find the full text of the license in the file COPYING.
  12. * FEATURES *
  13. Some of kses' current features are:
  14. * It will only allow the HTML elements and attributes that it was explicitly
  15. told to allow.
  16. * Element and attribute names are case-insensitive (a href vs A HREF).
  17. * It will understand and process whitespace correctly.
  18. * Attribute values can be surrounded with quotes, apostrophes or nothing.
  19. * It will accept valueless attributes with just names and no values (selected).
  20. * It will accept XHTML's closing " /" marks.
  21. * Attribute values that are surrounded with nothing will get quotes to avoid
  22. producing non-W3C conforming HTML
  23. (<a href=http://sourceforge.net/projects/kses> works but isn't valid HTML).
  24. * It handles lots of types of malformed HTML, by interpreting the existing
  25. code the best it can and then rebuilding new code from it. That's a better
  26. approach than trying to process existing code, as you're bound to forget about
  27. some weird special case somewhere. It handles problems like never-ending
  28. quotes and tags gracefully.
  29. * It will remove additional "<" and ">" characters that people may try to
  30. sneak in somewhere.
  31. * It supports checking attribute values for minimum/maximum length and
  32. minimum/maximum value, to protect against Buffer Overflows and Denial of
  33. Service attacks against WWW clients and various servers. You can stop
  34. <iframe src= width= height=> from having too high values for width and height,
  35. for instance.
  36. * It has got a system for whitelisting URL protocols. You can say that
  37. attribute values may only start with http:, https:, ftp: and gopher:, but no
  38. other URL protocols (javascript:, java:, about:, telnet:..). The functions that
  39. do this work handle whitespace, upper/lower case, HTML entities
  40. ("jav&#97;script:") and repeated entries ("javascript:javascript:alert(57)").
  41. It also normalizes HTML entities as a nice side effect.
  42. * It removes Netscape 4's JavaScript entities ("&{alert(57)};").
  43. * It handles NULL bytes and Opera's chr(173) whitespace characters.
  44. * There is a procedural version and two object-oriented versions (for PHP 4
  45. and PHP 5) of kses.
  46. * USE IT *
  47. It's very easy to use kses in your own PHP web application! Basic usage looks
  48. like this:
  49. <?php
  50. include 'kses.php';
  51. $allowed = array('b' => array(),
  52. 'i' => array(),
  53. 'a' => array('href' => 1, 'title' => 1),
  54. 'p' => array('align' => 1),
  55. 'br' => array());
  56. $val = $_POST['val'];
  57. if (get_magic_quotes_gpc())
  58. $val = stripslashes($val);
  59. # You must strip slashes from magic quotes, or kses will get confused.
  60. $val = kses($val, $allowed); # The filtering takes place here.
  61. # Do something with $val.
  62. ?>
  63. This definition of $allowed means that only the elements B, I, A, P and BR are
  64. allowed (along with their closing tags /B, /I, /A, /P and /BR). B, I and BR
  65. may not have any attributes. A may only have the attributes HREF and TITLE,
  66. while P may only have the attribute ALIGN. You can list the elements and
  67. attributes in the array in any mixture of upper and lower case. kses will also
  68. recognize HTML code that uses both lower and upper case.
  69. It's important to select the right allowed attributes, so you won't open up
  70. an XSS hole by mistake. Some important attributes that you mustn't allow
  71. include but are not limited to: 1) style, and 2) all intrinsic events
  72. attributes (onMouseOver and so on, on* really). I'll write more about this in
  73. the documentation that will be distributed with future versions of kses.
  74. It's also important to note that kses' HTML input must be cleaned of all
  75. slashes coming from magic quotes. If the rest of your code requires these
  76. slashes to be present, you can always add them again after calling kses with
  77. a simple addslashes() call.
  78. You should take a look at the documentation in the docs/ directory and the
  79. examples in the examples/ directory, to get more information on how to use
  80. kses. The object-oriented versions of kses are also worth checking out, and
  81. they're included in the oop/ directory.
  82. * UPGRADING TO 0.2.2 *
  83. kses 0.2.2 is backwards compatible with all previous releases, so upgrading
  84. should just be a matter of using a new version of kses.php instead of an old
  85. one.
  86. * NEW VERSIONS, MAILING LISTS AND BUG REPORTS *
  87. If you want to download new versions, subscribe to the kses-general mailing
  88. list or even take part in the development of kses, we refer you to its
  89. homepage at http://sourceforge.net/projects/kses . New developers and beta
  90. testers are more than welcome!
  91. If you have any bug reports, suggestions for improvement or simply want to tell
  92. us that you use kses for some project, feel free to post to the kses-general
  93. mailing list. If you have found any security problems (particularly XSS,
  94. naturally) in kses, please contact Ulf privately at metaur at users dot
  95. sourceforge dot net so he can correct it before you or someone else tells the
  96. public about it.
  97. (No, it's not a security problem in kses if some program that uses it allows a
  98. bad attribute, silly. If kses is told to accept the element body with the
  99. attributes style and onLoad, it will accept them, even if that's a really bad
  100. idea, securitywise.)
  101. * OTHER HTML FILTERS *
  102. Here are the other stand-alone, open source HTML filters that we currently know
  103. of:
  104. * Htmlfilter for PHP - the filter from Squirrelmail
  105. PHP
  106. Konstantin Riabitsev
  107. http://linux.duke.edu/projects/mini/htmlfilter/
  108. * HTML::StripScripts and related CPAN modules
  109. Perl
  110. Nick Cleaton
  111. http://search.cpan.org/perldoc?HTML%3A%3AStripScripts
  112. * SafeHtmlChecker [is this really open source?]
  113. PHP
  114. Simon Willison
  115. http://simon.incutio.com/archive/2003/02/23/safeHtmlChecker
  116. There are also a lot of HTML filters that were written specifically for some
  117. program. Some of them are better than others.
  118. Please write to the kses-general mailing list if you know of any other
  119. stand-alone, open-source filters.
  120. * DEDICATION *
  121. kses 0.2.2 is dedicated to Audrey Tautou and Jean-Pierre Jeunet.
  122. * MISC *
  123. The kses code is based on an HTML filter that Ulf wrote on his own back in 2002
  124. for the open-source project Gnuheter ( http://savannah.nongnu.org/projects/
  125. gnuheter ). Gnuheter is a fork from PHP-Nuke. The HTML filter has been
  126. improved a lot since then.
  127. To stop people from having sleepless nights, we feel the urgent need to state
  128. that kses doesn't have anything to do with the KDE project, despite having a
  129. name that starts with a K.
  130. In case someone was wondering, Ulf is available for kses-related consulting.
  131. Finally, the name kses comes from the terms XSS and access. It's also a
  132. recursive acronym (every open-source project should have one!) for "kses
  133. strips evil scripts".
  134. // Ulf and the kses development group, February 2005