from BeautifulSoup import BeautifulSoup, Comment
register = template.Library()
def sanitize_html(value):
valid_tags = 'p i strong b u a h1 h2 h3 pre br img'.split()
valid_attrs = 'href src'.split()
soup = BeautifulSoup(value)
for comment in soup.findAll(
text=lambda text: isinstance(text, Comment)):
comment.extract()
for tag in soup.findAll(True):
if tag.name not in valid_tags:
tag.hidden = True
tag.attrs = [(attr, val) for attr, val in tag.attrs
if attr in valid_attrs]
return soup.renderContents().decode('utf8').replace('javascript:', '')
register.filter('santize', sanitize_html)
Comments
This filter does not prevent case "insensitive XSS attack vector" and "embedded tab to break up the cross site scripting attack" documented here: http://ha.ckers.org/xss.html
#
Might want to try changing the 'javascript' regex in the second-to-last line with this regex instead:
(all on one line, of course)
It's long and unwieldy but I think it catches the above issues, and then some (embedded entity tabs/linebreaks, for example).
#
Beware also of the typo on the last line. Author presumably meant to write "sanitize" instead of "santize".
#
Here's how I solved the javascript link problem:
Admittedly, not the best solution (looping over attrs twice isn't elegant), but it both solves the case-sensitivity problem and actually removes the
<a href="javascript:">tag, rather than just removing thehrefattribute.#
I added a new filter, which prevent better for XXS attacks. No configuration needed http://djangosnippets.org/snippets/2444/
#
All people deserve wealthy life and loan or just commercial loan would make it better. Just because people's freedom relies on money state.
#