Add RSS feed to comparison.xhtml, and svn:eol-style=native
[htmlpurifier-web.git] / comparison.xhtml
blob7074b1d54cff3bbe80ec6333dbecb40ce29297a7
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml"
5 xmlns:xi="http://www.w3.org/2001/XInclude"
6 xmlns:xc="urn:xhtml-compiler"
7 xmlns:svn="urn:xhtml-compiler:Subversion"
8 svn:head-url="$HeadURL$"
9 svn:revision="$Revision$"
10 xml:lang="en" lang="en">
11 <head>
12 <title>Comparison - HTML Purifier</title>
13 <xi:include href="common-meta.xml" xpointer="xpointer(/*/node())" />
14 <link rel="stylesheet" href="comparison.css" type="text/css" />
15 <meta name="keywords" content="HTMLPurifier, HTML Purifier, HTML, filter, filtering, HTML_Safe, PEAR, comparison, kses, striptags, SafeHTMLChecker" />
16 </head>
17 <body>
19 <xi:include href="common-header.xml" xpointer="xpointer(/*/node())" />
20 <h1 id="title">Comparison</h1>
22 <div id="content">
24 <p>With the advent of
25 <a href="http://en.wikipedia.org/wiki/Web_2.0">Web 2.0</a>, the end user has
26 gone from passive consumer to active producer of content on the World Wide
27 Web. <a href="http://en.wikipedia.org/wiki/Wiki">Wikis</a>,
28 <a href="http://en.wikipedia.org/wiki/Social_software">Social Software</a> and
29 <a href="http://en.wikipedia.org/wiki/Blog">Blogs</a> all
30 put the user in control.</p>
32 <p>Give the user too much control, however, and you set yourself up
33 for <a href="http://en.wikipedia.org/wiki/Cross-site_scripting"><abbr>XSS</abbr></a> attacks. For this reason,
34 <abbr>HTML</abbr>'s flexibility
35 has proven to be both a blessing and a curse, and the software that processes
36 it must strike a fine balance between security and usability. How do
37 we prevent users from injecting JavaScript or inserting malformed
38 <abbr>HTML</abbr> while allowing
39 a rich syntax of tags, attributes and <abbr>CSS</abbr>? How do we put
40 <abbr>HTML</abbr> inside
41 <abbr>RSS</abbr> feed without worrying
42 about sloppy coding messing up <abbr>XML</abbr> parsing?
43 Almost every <abbr>PHP</abbr>
44 developer has come across this problem before, and many have tried
45 (albeit unsuccessfully) to solve this problem. We will analyze existing
46 libraries to demonstrate how they are ineffective and, of course,
47 how <strong>HTML Purifier</strong> solves all our problems and achieves
48 standards-compliance.</p>
50 <p>I will take no quarter and pull no punches: as of the time of writing,
51 no other library comes even <em>close</em> to solving the problem effectively
52 for richly formatted documents. But, nonetheless, there is a necessary
53 disclaimer:</p>
55 <p class="disclaimer">
56 This comparison document was written by the author of HTML Purifier,
57 and clearly is <strong>in favor</strong> of HTML Purifier. However, that doesn't
58 mean that it is biased: I have made every attempt to be <strong>factual and
59 fair</strong>, and I hope that you will agree, by the time you finish reading
60 this document, that HTML Purifier is the only satisfactory <abbr>HTML</abbr>
61 filter out there today.
62 </p>
64 <div id="toc" />
66 <h2 id="Summary">Summary</h2>
68 <p>A table summarizing the differences for the impatient.</p>
70 <div class="wide-table">
71 <table cellspacing="0">
73 <thead>
74 <tr>
75 <th>Library</th>
76 <th>Version</th>
77 <th>Date</th>
78 <th>License</th>
79 <th>Whitelist</th>
80 <th>Removal</th>
81 <th>Well-formed</th>
82 <th>Nesting</th>
83 <th>Attributes</th>
84 <th>XSS&nbsp;safe</th>
85 <th>Standards&nbsp;safe</th>
86 </tr>
87 </thead>
89 <tbody>
91 <tr>
92 <td>striptags</td>
93 <td>n/a</td>
94 <td>n/a</td>
95 <td>n/a</td>
96 <td class="impl-almostyes">Yes (user)</td>
97 <td class="impl-partial">Buggy</td>
98 <td class="impl-no">No</td>
99 <td class="impl-no">No</td>
100 <td class="impl-no">No</td>
101 <td class="impl-no">No</td>
102 <td class="impl-no">No</td>
103 </tr>
105 <tr>
106 <td>PHP Input Filter</td>
107 <td>1.2.2</td>
108 <td>2005-10-05</td>
109 <td>GPL</td>
110 <td class="impl-almostyes">Yes (user)</td>
111 <td class="impl-yes">Yes</td>
112 <td class="impl-no">No</td>
113 <td class="impl-no">No</td>
114 <td class="impl-partial">Partial</td>
115 <td class="impl-almostyes">Probably</td>
116 <td class="impl-no">No</td>
117 </tr>
119 <tr>
120 <td>HTML_Safe</td>
121 <td>0.9.9beta</td>
122 <td>2005-12-21</td>
123 <td>BSD (3)</td>
124 <td class="impl-no">Mostly No</td>
125 <td class="impl-yes">Yes</td>
126 <td class="impl-yes">Yes</td>
127 <td class="impl-no">No</td>
128 <td class="impl-partial">Partial</td>
129 <td class="impl-almostyes">Probably</td>
130 <td class="impl-no">No</td>
131 </tr>
133 <tr>
134 <td>kses</td>
135 <td>0.2.2</td>
136 <td>2005-02-06</td>
137 <td>GPL</td>
138 <td class="impl-almostyes">Yes (user)</td>
139 <td class="impl-yes">Yes</td>
140 <td class="impl-no">No</td>
141 <td class="impl-no">No</td>
142 <td class="impl-partial">Partial</td>
143 <td class="impl-almostyes">Probably</td>
144 <td class="impl-no">No</td>
145 </tr>
147 <tr>
148 <td>Safe HTML Checker</td>
149 <td>n/a</td>
150 <td>2003-09-15</td>
151 <td>n/a</td>
152 <td class="impl-almostyes">Yes (bare)</td>
153 <td class="impl-yes">Yes</td>
154 <td class="impl-yes">Yes</td>
155 <td class="impl-almostyes">Almost</td>
156 <td class="impl-partial">Partial</td>
157 <td class="impl-yes">Yes</td>
158 <td class="impl-almostyes">Almost</td>
159 </tr>
161 <tr>
162 <td>HTML Purifier</td>
163 <td>1.6.0</td>
164 <td>2007-04-01</td>
165 <td>LGPL</td>
166 <td class="impl-yes">Yes</td>
167 <td class="impl-yes">Yes</td>
168 <td class="impl-yes">Yes</td>
169 <td class="impl-yes">Yes</td>
170 <td class="impl-yes">Yes</td>
171 <td class="impl-yes">Yes</td>
172 <td class="impl-yes">Yes</td>
173 </tr>
175 </tbody>
177 </table>
178 </div>
180 <p><a href="#Tidy">HTML Tidy</a> is omitted from this list because it is not an <abbr>HTML</abbr>
181 filter.</p>
183 <h2 id="AltMarkup">Look Ma, No <abbr>HTML</abbr>!</h2>
185 <blockquote class="fancy">
186 <div class="quote" style="text-align:center;">
187 A clever person solves a problem.
188 A wise person avoids it.
189 </div>
190 <div class="origin">&mdash; Albert Einstein</div>
191 </blockquote>
193 <p>Before we jump into the weird and not-so-wonderful world
194 of <abbr>HTML</abbr> filters, we must first consider another domain: non-<abbr>HTML</abbr>
195 markup libraries. While libraries of this type really shouldn't be
196 considered <abbr>HTML</abbr> filters,
197 they are the number one method of taking user input and processing it into
198 something more than plain old text. These libraries forgo
199 <abbr>HTML</abbr> and define their
200 own markup syntax. <a href="http://en.wikipedia.org/wiki/BBCode">BBCode</a>,
201 <a href="http://en.wikipedia.org/wiki/Wikitext">Wikitext</a>,
202 <a href="http://daringfireball.net/projects/markdown/">Markdown</a> and
203 <a href="http://textism.com/tools/textile/">Textile</a> are all examples of
204 such markup languages (although it should be noted that
205 Wikitext and Markdown can allow
206 <abbr>HTML</abbr> within them).
207 The benefits (to those who use it, anyway) are clear: simplicity and
208 security.
209 </p>
211 <table cellspacing="0">
212 <thead>
213 <tr>
214 <th>Markup language</th>
215 <th>Sample</th>
216 </tr>
217 </thead>
218 <tbody>
219 <tr>
220 <th>BBCode</th>
221 <td><tt>[b]B[/b] [i]i[/i] [url = http://www.example.com/]link[/url].</tt></td>
222 </tr>
223 <tr>
224 <th>Wikitext<sup>1</sup></th>
225 <td><tt>'''B''' ''i'' [http://www.example.com/ link]</tt></td>
226 </tr>
227 <tr>
228 <th>Markdown<sup>2</sup></th>
229 <td><tt>**B** *i* [link](http://www.example.com/)</tt></td>
230 </tr>
231 <tr>
232 <th>Textile</th>
233 <td><tt>*B* _i_ &quot;link&quot;:http://www.example.com/</tt></td>
234 </tr>
235 <tr>
236 <th><abbr>HTML</abbr></th>
237 <td><tt>&lt;b&gt;B&lt;/b&gt; &lt;i&gt;i&lt;/i&gt; &lt;a href=&quot;http://www.example.com/&quot;&gt;link&lt;/a&gt;</tt></td>
238 </tr>
239 <tr>
240 <th><acronym>WYSIWYG</acronym></th>
241 <td><b>B</b> <i>i</i> <a href="http://www.example.com/">link</a></td>
242 </tr>
243 </tbody>
244 </table>
246 <ol class="notes">
247 <li>Wikitext shown is modeled after <a
248 href="http://www.mediawiki.org/wiki/MediaWiki">MediaWiki</a> style.
249 There are many variants of Wikitext currently extant.</li>
250 <li>Strictly speaking, the Markdown syntax is not equivalent: bold text
251 is expressed as <code>&lt;strong&gt;</code> and italicized text is
252 expressed as <code>&lt;em&gt;</code>. Most browser default stylesheets,
253 however, map those two semantic tags to the associated styling, so
254 many users assume that it really is italics (and use it improperly for,
255 say, book titles.)</li>
256 </ol>
258 <h3 id="AltMarkup:Simplicity">Simplicity</h3>
260 <p><abbr>HTML</abbr>
261 source code is often criticized for being difficult to read. For example,
262 compare:</p>
264 <pre>
265 * Item 1
266 * Item 2
267 </pre>
269 <p>...versus:</p>
271 <pre>
272 &lt;ul&gt;
273 &lt;li&gt;Item 1&lt;/li&gt;
274 &lt;li&gt;Item 2&lt;/li&gt;
275 &lt;/ul&gt;
276 </pre>
278 <p>Which would you prefer to edit? The answer seems obvious, but be careful
279 not to fall into the fallacy of <a
280 href="http://en.wikipedia.org/wiki/False_dilemma">false dilemma</a>.
281 There <em>is</em> a third choice: the
282 <acronym>WYSIWYG</acronym> (rich text)
283 editor, which blows earlier choices out of the water in terms
284 of usability.</p>
286 <p>Note that rich text editors and alternate markup syntaxes are not
287 mutually exclusive, but, when push comes to shove, it's easier
288 implement this sort of editor on top of <abbr>HTML</abbr> than some obscure
289 markup language. And in the cases when it is done, you usually end up with
290 a live preview, not a true rich text editor.</p>
292 <blockquote class="digression">
293 <p><q>Now just wait a second,</q> you may be saying,
294 <q><acronym>WYSIWYG</acronym>
295 editors aren't all that great.</q> There are many good arguments
296 against these editors, and <a
297 href="http://www.ideography.co.uk/library/seybold/WYSIWYG.html">intelligent
298 people have written essays</a> devoted to
299 criticizing <acronym>WYSIWYG</acronym>.
300 In addition to the usual arguments against said editors, the web poses
301 another limitation: no JavaScript means no
302 editor, and no editor means... (gasp) manually typing in code.</p>
304 <p>Even the most dogmatic purist, however, should recognize that for all
305 its faults, prospective clients <em>really</em> want rich text editors.
306 There are steps you can take to mitigate the associated drawbacks of
307 these editors.</p>
309 <p>It is often asserted that
310 <acronym>WYSIWYG</acronym> editors
311 <em>encourage excessive presentational markup</em>. As it turns out,
312 this is the case with any markup language that allows the smallest
313 iota of presentational tags, be it <tt>&lt;font&gt;</tt> or
314 <tt>[color=red]</tt>.
315 A good way to reduce this trouble is to simply eliminate the
316 dialogue boxes that allow users to change colors or fonts (which
317 usually have no legitimate use) and adopt a
318 <acronym>WYSIWYM</acronym> scheme,
319 allowing users to select contextually correct formatting styles
320 for segments of text.</p>
321 </blockquote>
323 <p>Simplicity is also a double-edged sword. The moment any remotely
324 complex markup is needed, these lightweight markup languages fail to
325 produce. Sure you can make '''this text bold''' with Wikitext, but that
326 infobox all <q>rendered nicely in aqua blue</q> will require a gaggle of
327 &lt;div&gt;s and <abbr>CSS</abbr>.
328 These languages face the same troubles as regular <abbr>HTML</abbr>
329 filters in that their whitelist is too restrictive (besides the fact that
330 their table markup is extraordinarily complex).</p>
332 <h3 id="AltMarkup:Security">Security</h3>
334 <p>BBCode can be boiled down to a <q>wanna-be</q> version of
335 <abbr>HTML</abbr>. I mean, replacing
336 the angled brackets with square brackets and omitting the occasional parameter
337 name? How much more un-original can you get? Somehow, I don't think BBCode
338 was meant to readable. <a
339 href="http://en.wikipedia.org/wiki/BBCode">Wikipedia</a> agrees:</p>
341 <blockquote>
342 BBCode was devised and put to use in order to provide a safer, easier
343 and more limited way of allowing users to format their messages.
344 Previously, many message boards allowed the users to include <abbr>HTML</abbr>,
345 which could be used to break/imitate parts of the layout, or run
346 JavaScript. Some implementations of BBCode have suffered problems related
347 to the way they translate the BBCode into <abbr>HTML</abbr>, which could negate the
348 security that was intended to be given by BBCode.
349 </blockquote>
351 <p>Or, put more simply:</p>
353 <blockquote>
354 BBCode came to life when developers where too
355 lazy to parse <abbr>HTML</abbr> correctly
356 and decided to invent their own markup language. As with all products of
357 laziness, the result is completely inconsistent, unstandardized, and
358 widely adopted.
359 </blockquote>
361 <p>Well, developers, the whole point of HTML Purifier is that I do the
362 work so you can just execute the ridiculously simple
363 <tt>$purifier->purify($html)</tt> call and go on to do, well, whatever
364 you developers do. <tt>:-P</tt></p>
366 <h3 id="AltMarkup:Conclusion">Conclusion</h3>
368 <p>These alternative markup languages have their shiny points, and HTML
369 Purifier is not meant to replace them. However, a major reason for
370 their existence has been called into question. Why are <em>you</em>
371 using these languages?</p>
373 <h2 id="Tidy">HTML Tidy</h2>
375 <p>Dave Raggett's
376 <a href="http://www.w3.org/People/Raggett/tidy/">HTML Tidy</a> is a program;
377 neat enough, at least, to make it into <abbr>PHP</abbr> as a
378 <a href="http://us2.php.net/manual/en/ref.tidy.php"><abbr>PECL</abbr> extension.</a>
379 The premise is simple, the execution effective. Tidy is, in short, a great
380 <em>tool</em>.</p>
382 <p>It is not, however, a filter. I am often surprised when people ask
383 me, <q>What about Tidy?</q> There's nothing against Tidy: Tidy tackles
384 a different problem set. Let's see what <tt>man tidy</tt> has to say:</p>
386 <blockquote cite="http://tidy.sourceforge.net/docs/tidy_man.html">
387 Tidy reads <abbr>HTML</abbr>, <abbr>XHTML</abbr> and
388 <abbr>XML</abbr> files and writes cleaned up markup. For
389 <abbr>HTML</abbr> variants, it detects and corrects many common coding errors and
390 strives to produce visually equivalent markup that is both <abbr>W3C</abbr> compliant
391 and works on most browsers. A common use of Tidy is to convert plain <abbr>HTML</abbr>
392 to <abbr>XHTML</abbr>.
393 </blockquote>
395 <p>Hmm... why do I not see the words <q>filter</q> or
396 <q><abbr>XSS</abbr></q> in here? Perhaps it's
397 because Tidy accepts <em>any</em> valid
398 <abbr>HTML</abbr>. Including
399 <tt>script</tt> tags. Which leads us to our second part: Tidy parses
400 <em>documents</em>, not document <em>fragments</em>.</p>
402 <p>This is not to say that I haven't seen Tidy be used in this sort of
403 fashion. MediaWiki, for instance, uses Tidy to cleanup the final <abbr>HTML</abbr>
404 output before shuttling it off to the browser. The developers, nevertheless,
405 agree that this is only a band-aid solution, and that the real way
406 to fix it is to fix the parser. Tidy's great, but in terms of security,
407 it's not suitable for untrusted sources.</p>
409 <h2 id="Preface">Preface</h2>
411 <p>I've ordered my analyses according to how bad a library is. The worst
412 is first, and then we move up the spectrum. I will point out the most
413 flagrant problems with the libraries, but note that I will omit more
414 advanced vulnerabilities: if you can't catch an <tt>onmouseover</tt>
415 attribute, I really shouldn't reprimand you for letting non-<abbr>SGML</abbr> code
416 points through. The ideal solution, however, must do all these things.</p>
418 <p>Note that besides striptags,
419 most of the libraries are moderately effective against the most common <abbr>XSS</abbr>
420 attacks. None of them (save Safe HTML Checker) fare very well
421 in the standards-compliance department though.</p>
423 <h2 id="striptags">striptags()</h2>
425 <table class="summary">
426 <tr><th>Whitelist</th> <td class="impl-yes">Yes, user-specified</td></tr>
427 <tr><th>Removes foreign tags</th> <td class="impl-partial">Buggy</td></tr>
428 <tr><th>Makes well-formed</th> <td class="impl-no">No</td></tr>
429 <tr><th>Fixes nesting</th> <td class="impl-no">No</td></tr>
430 <tr><th>Validates attributes</th> <td class="impl-no">No</td></tr>
431 </table>
433 <p>The <abbr>PHP</abbr> function
434 <a href="http://php.net/manual/en/function.strip-tags.php">striptags()</a> is
435 the classic solution for attempting to clean up
436 <abbr>HTML</abbr>. It
437 is also the <em>worst</em> solution, and should be avoided like the plague.
438 The fact that it doesn't validate attributes at all means that anyone can
439 insert an <tt>onmouseover='xss();'</tt> and exploit your application.</p>
441 <p>While
442 this can be bandaided with a series of regular expressions that strip out
443 on[event] (you're still vulnerable to <abbr>XSS</abbr> and at the mercy of
444 quirky browser behavior), striptags() is fundamentally flawed and should not be
445 used.
446 </p>
448 <h2 id="Input_Filter">PHP Input Filter</h2>
450 <p>Though its title may not imply it,
451 <a href="http://www.phpclasses.org/browse/package/2189.html">PHP Input Filter</a>
452 is a souped up version of striptags() with the ability to inspect
453 attributes. (Don't mind the hastily tacked on query escaping function).</p>
455 <table class="summary">
456 <tr><th>Version</th> <td class="impl-yes">1.2.2</td></tr>
457 <tr><th>Last update</th> <td class="impl-irrelevant">2005-10-05</td></tr>
458 <tr><th>License</th> <td class="impl-irrelevant">GPL</td></tr>
459 <tr><th>Whitelist</th> <td class="impl-yes">Yes, user defined</td></tr>
460 <tr><th>Removes foreign tags</th> <td class="impl-yes">Yes</td></tr>
461 <tr><th>Makes well-formed</th> <td class="impl-no">No</td></tr>
462 <tr><th>Fixes nesting</th> <td class="impl-no">No</td></tr>
463 <tr><th>Validates attributes</th> <td class="impl-partial">Partial</td></tr>
464 <tr><th>XSS safe</th> <td class="impl-almostyes">Probably</td></tr>
465 <tr><th>Standards safe</th> <td class="impl-no">No</td></tr>
466 </table>
468 <p>PHP Input Filter implements an
469 <abbr>HTML</abbr> parser, and
470 performs very basic checks on whether or not tags and attributes have
471 been defined in the whitelist as well as some
472 smarter <abbr>XSS</abbr> checks. It is left up to
473 the user to define what they'll permit.</p>
475 <p>With absolutely no checking of well-formedness, it is trivially easy
476 to trick the filter into leaving unclosed tags lying around. While to some
477 standards-compliance may be viewed by some as a <q>nice feature</q>,
478 basic sanity checks like this must be implemented, otherwise a user
479 can mangle a website's layout.</p>
481 <p>More troubles: Woe to
482 any user that allows the <tt>style</tt> attribute: you can't simply
483 just let <abbr>CSS</abbr> through and expect your
484 layout not to be badly mutilated. To top things off,
485 the filter doesn't even preserve data properly: attributes have all
486 spaces stripped out of them. Stay away, stay away!</p>
488 <h2 id="HTML_Safe">HTML_Safe/SafeHTML</h2>
490 <p><a href="http://pear.php.net/package/HTML_Safe">HTML_Safe</a> is
491 <acronym>PEAR</acronym>'s <abbr>HTML</abbr> filtering library.
492 It should be noted that this is the same library as
493 <a href="http://pixel-apes.com/safehtml/">SafeHTML</a>, though with different
494 branding (and a different version number).</p>
496 <table class="summary">
497 <tr><th>Version</th> <td class="impl-almostyes">0.9.9beta</td></tr>
498 <tr><th>Last update</th> <td class="impl-irrelevant">2005-12-21</td></tr>
499 <tr><th>License</th> <td class="impl-irrelevant">BSD (3 clause)</td></tr>
500 <tr><th>Whitelist</th> <td class="impl-no">Mostly No</td></tr>
501 <tr><th>Removes foreign tags</th> <td class="impl-yes">Yes</td></tr>
502 <tr><th>Makes well-formed</th> <td class="impl-yes">Yes</td></tr>
503 <tr><th>Fixes nesting</th> <td class="impl-no">No</td></tr>
504 <tr><th>Validates attributes</th> <td class="impl-partial">Partial</td></tr>
505 <tr><th>XSS safe</th> <td class="impl-almostyes">Probably</td></tr>
506 <tr><th>Standards safe</th> <td class="impl-no">No</td></tr>
507 </table>
509 <p>HTML_Safe's mechanism of action involves parsing
510 <abbr>HTML</abbr> with a
511 <acronym>SAX</acronym> parser and performing
512 validation and filtering as the handlers are called. HTML_Safe does a lot
513 of things right, which is why I say it <em>probably</em> isn't vulnerable
514 to <abbr>XSS</abbr>, but its approach
515 is fundamentally flawed: blacklists.</p>
517 <p>This library maintains arrays of dangerous tags, attributes and
518 <abbr>CSS</abbr> properties. (It also
519 has a blacklist of dangerous <abbr>URI</abbr> protocols, but this is
520 intelligently disabled by default in favor of a protocol whitelist.)
521 What this means is that HTML_Safe has no qualms of accepting input
522 like <tt>&lt;foobar&gt; Bang &lt;/foobar&gt;</tt>. Anything goes except
523 the tags in those arrays. Scratch standards-compliance (and that was
524 without even considering proper nesting).</p>
526 <p>For now, HTML_Safe might be safe from
527 <abbr>XSS</abbr>.
528 In the future, however, one of the infinitely many tags that HTML_Safe lets
529 through might just possibly be given special functionality by browser vendors.
530 And it might just turn out that this can be exploited. <em>Any</em> blacklist
531 solution puts you at a perpetual arms race against crackers who are constantly
532 discovering new and inventive ways to abuse tags and attributes that you
533 didn't blacklist.</p>
535 <h2 id="kses">kses</h2>
537 <p><a href="http://sourceforge.net/projects/kses/">kses</a> appears to
538 be the de-facto solution for cleaning <abbr>HTML</abbr>, having found
539 its way into applications such as <a href="http://wordpress.org/">WordPress</a>
540 and being the number one search result for <q>php html filter</q>.</p>
542 <table class="summary">
543 <tr><th>Version</th> <td class="impl-partial">0.2.2</td></tr>
544 <tr><th>Last update</th> <td class="impl-irrelevant">2005-02-06</td></tr>
545 <tr><th>License</th> <td class="impl-irrelevant">GPL</td></tr>
546 <tr><th>Whitelist</th> <td class="impl-yes">Yes, user defined</td></tr>
547 <tr><th>Removes foreign tags</th> <td class="impl-yes">Yes</td></tr>
548 <tr><th>Makes well-formed</th> <td class="impl-no">No</td></tr>
549 <tr><th>Fixes nesting</th> <td class="impl-no">No</td></tr>
550 <tr><th>Validates attributes</th> <td class="impl-partial">Partial</td></tr>
551 <tr><th>XSS safe</th> <td class="impl-almostyes">Probably</td></tr>
552 <tr><th>Standards safe</th> <td class="impl-no">No</td></tr>
553 </table>
555 <p>To be truthful, I didn't do as comprehensive a code survey for kses
556 as I did for some of the other libraries. Out of
557 all the classes I've reviewed so far, kses was definitely the hardest to
558 understand.</p>
560 <p>kses's modus operandi is splitting up html with a monster regexp
561 and then validating each section with <tt>kses_split2()</tt>. It
562 suffers from the same problems as Input Filter: no well-formedness
563 checks leading to rampant runaway tags (and no standards-compliance).
564 WordPress, the primary user of kses today, had to implement their
565 own custom tag-balancing code to fix this problem: don't use this
566 library without some equivalent!</p>
568 <p>Its whitelist syntax, however, is the most complex of all these libraries,
569 so I'm going to take some time to argue why this particular implementation
570 is bad. The author of this library was thoughtful enough to provide some
571 basic constraint checks on attributes like maxlen and maxval. Now, barring
572 the fact that there simply aren't enough checks, and the fact that they are
573 all lumped together in one function, we now must wonder whether or not
574 the user will go through the trouble of specifying the maximum length
575 of a title attribute.</p>
577 <p>I have my opinions about inherent human laziness, but perhaps WordPress's
578 default filterset is the most telling example:</p>
580 <pre>
581 $allowedposttags = array (
582 /* formatted and trimmed */
583 'hr' => array (
584 'align' => array (),
585 'noshade' => array (),
586 'size' => array (),
587 'width' => array ()
590 </pre>
592 <p>Hmm... do I see a blatant lack of attribute constraints? Conclusion:
593 if the user can get away with not doing work, they will! The biggest
594 problem in all these whitelists filters is that they forgot to <em>supply</em>
595 the whitelist. The whitelist is just as important as the code that uses
596 the whitelist to filter <abbr>HTML</abbr>.</p>
598 <h2 id="Safe_HTML_Checker">Safe HTML Checker</h2>
601 <a href="http://simon.incutio.com/archive/2003/02/23/safeHtmlChecker">Safe
602 HTML Checker</a> is (to my knowledge) the first attempt to make a filter
603 that also outputs standards-compliant <abbr>XHTML</abbr>. It wasn't even released or
604 licensed officially, but we'll let that slide: a 4<sup>th</sup> place
605 search result must have done something right.</p>
607 <table class="summary">
608 <tr><th>Version</th> <td class="impl-partial">in-house</td></tr>
609 <tr><th>Last update</th> <td class="impl-almostyes">2003-09-15</td></tr>
610 <tr><th>License</th> <td class="impl-no">undefined</td></tr>
611 <tr><th>Whitelist</th> <td class="impl-almostyes">Yes (bare-bones)</td></tr>
612 <tr><th>Removes foreign tags</th> <td class="impl-yes">Yes</td></tr>
613 <tr><th>Makes well-formed</th> <td class="impl-yes">Yes</td></tr>
614 <tr><th>Fixes nesting</th> <td class="impl-almostyes">Almost</td></tr>
615 <tr><th>Validates attributes</th> <td class="impl-partial">Partial</td></tr>
616 <tr><th>XSS safe</th> <td class="impl-yes">Yes</td></tr>
617 <tr><th>Standards safe</th> <td class="impl-almostyes">Almost</td></tr>
618 </table>
620 <p>Indeed, it is quite a well-written piece of code. It demonstrates
621 knowledge of inline versus block elements, thus almost nearly getting
622 nesting correct (the only exception is an unimplemented omitted SGML
623 exclusion for <tt>&lt;a&gt;</tt> tags, and that's easy to fix).</p>
625 <p>Unfortunately, part of the reason why it works so well is that it's
626 extremely restrictive. No styling, no tables, very few attributes.
627 Perfectly appropriate for blog comments, but then again, there's always
628 BBCode. This probably means that Safe HTML Checker has a different
629 goal than HTML Purifier.</p>
631 <p>The <abbr>XML</abbr> parser
632 is also quite strict. Accidentally missed a &lt; sign? The parser will
633 complain with the cryptic message:
634 <q><abbr>XHTML</abbr>
635 is not well-formed</q>.
636 The solution is not as simple as just switching to a more permissive
637 parser: Safe HTML Checker relies on the fact that the parser will have
638 matched up the tags for them.</p>
640 <h2 id="HTMLPurifier">HTML Purifier</h2>
642 <table class="summary">
643 <tr><th>Version</th> <td class="impl-yes">1.6.0</td></tr>
644 <tr><th>Last update</th> <td class="impl-yes">2007-04-01</td></tr>
645 <tr><th>License</th> <td class="impl-irrelevant">LGPL</td></tr>
646 <tr><th>Whitelist</th> <td class="impl-yes">Yes</td></tr>
647 <tr><th>Removes foreign tags</th> <td class="impl-yes">Yes</td></tr>
648 <tr><th>Makes well-formed</th> <td class="impl-yes">Yes</td></tr>
649 <tr><th>Fixes nesting</th> <td class="impl-yes">Yes</td></tr>
650 <tr><th>Validates attributes</th> <td class="impl-yes">Yes</td></tr>
651 <tr><th>XSS safe</th> <td class="impl-yes">Yes</td></tr>
652 <tr><th>Standards safe</th> <td class="impl-yes">Yes</td></tr>
653 </table>
655 <p>That table should say it all, but I'll add a few more features:</p>
657 <table class="summary">
658 <tr><th>UTF-8 aware</th><td class="impl-yes">Yes</td></tr>
659 <tr><th>Object-Oriented</th><td class="impl-yes">Yes</td></tr>
660 <tr><th>Validates CSS</th><td class="impl-yes">Yes</td></tr>
661 <tr><th>Tables</th><td class="impl-yes">Yes</td></tr>
662 <tr><th>PHP 5 aware</th><td class="impl-yes">Yes</td></tr>
663 <tr><th>E_STRICT compliant</th><td class="impl-yes">Yes (use -strict)</td></tr>
664 </table>
666 <p>This is not to say that HTML Purifier doesn't have problems of its own.
667 It's a fairly nascent library (that doesn't mean its buggy though), it's big
668 (while the others usually fit in one file, this one requires a huge
669 include list), and it's <a href="http://hp.jpsband.org/live/TODO">missing
670 features.</a> But even in its current state,
671 HTML Purifier is far better than the other libraries.</p>
673 <p>So... <a href="./#Download">what are you waiting for?</a></p>
675 </div>
676 </body>
677 </html>