As part of a online forum I'm setting up I need to display user submitted content, which is stored in a database and then displayed.

Because the site uses cookies for authentification, and as a general preventative measure I wish to strip out dangerous tags, javascript, images etc.

I think that I would be safe just leaving a minimal subset of HTML, such as the following tags P, B, I, A (with only a subset of attributes, HREF and TITLE for example).

I realise that a regular expression approach is unlikely to be workable, so my two choices seem to be HTML::Sanitizer and HTML::Scrubber. Both of these will do the job, without too much effort. (I'm still suprised this isn't done here on the home nodes, maybe its a hard thing to do efficiently? Either that or its not yet been considered important enough)

As they do a real parse of the HTML they rely upon the various parsing modules, HTML::Tree and HTML::Parser respectively.

Is there another approach I'm missing, with less dependencies? Or a simpler system I could use instead?

Whilst I can use either of the two packages above I'm keen on using something that's less hungry - so that I can keep it upto date on my Debian Stable webhost.

Steve
---
steve.org.uk

In reply to Sanitizing HTML by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.