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