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.

enduser-overview.txt 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. HTML Purifier
  2. by Edward Z. Yang
  3. There are a number of ad hoc HTML filtering solutions out there on the web
  4. (some examples including HTML_Safe, kses and SafeHtmlChecker.class.php) that
  5. claim to filter HTML properly, preventing malicious JavaScript and layout
  6. breaking HTML from getting through the parser. None of them, however,
  7. demonstrates a thorough knowledge of neither the DTD that defines the HTML
  8. nor the caveats of HTML that cannot be expressed by a DTD. Configurable
  9. filters (such as kses or PHP's built-in striptags() function) have trouble
  10. validating the contents of attributes and can be subject to security attacks
  11. due to poor configuration. Other filters take the naive approach of
  12. blacklisting known threats and tags, failing to account for the introduction
  13. of new technologies, new tags, new attributes or quirky browser behavior.
  14. However, HTML Purifier takes a different approach, one that doesn't use
  15. specification-ignorant regexes or narrow blacklists. HTML Purifier will
  16. decompose the whole document into tokens, and rigorously process the tokens by:
  17. removing non-whitelisted elements, transforming bad practice tags like <font>
  18. into <span>, properly checking the nesting of tags and their children and
  19. validating all attributes according to their RFCs.
  20. To my knowledge, there is nothing like this on the web yet. Not even MediaWiki,
  21. which allows an amazingly diverse mix of HTML and wikitext in its documents,
  22. gets all the nesting quirks right. Existing solutions hope that no JavaScript
  23. will slip through, but either do not attempt to ensure that the resulting
  24. output is valid XHTML or send the HTML through a draconic XML parser (and yet
  25. still get the nesting wrong: SafeHtmlChecker.class.php does not prevent <a>
  26. tags from being nested within each other).
  27. This document no longer is a detailed description of how HTMLPurifier works,
  28. as those descriptions have been moved to the appropriate code. The first
  29. draft was drawn up after two rough code sketches and the implementation of a
  30. forgiving lexer. You may also be interested in the unit tests located in the
  31. tests/ folder, which provide a living document on how exactly the filter deals
  32. with malformed input.
  33. In summary (see corresponding classes for more details):
  34. 1. Parse document into an array of tag and text tokens (Lexer)
  35. 2. Remove all elements not on whitelist and transform certain other elements
  36. into acceptable forms (i.e. <font>)
  37. 3. Make document well formed while helpfully taking into account certain quirks,
  38. such as the fact that <p> tags traditionally are closed by other block-level
  39. elements.
  40. 4. Run through all nodes and check children for proper order (especially
  41. important for tables).
  42. 5. Validate attributes according to more restrictive definitions based on the
  43. RFCs.
  44. 6. Translate back into a string. (Generator)
  45. HTML Purifier is best suited for documents that require a rich array of
  46. HTML tags. Things like blog comments are, in all likelihood, most appropriately
  47. written in an extremely restrictive set of markup that doesn't require
  48. all this functionality (or not written in HTML at all), although this may
  49. be changing in the future with the addition of levels of filtering.
  50. vim: et sw=4 sts=4