Skip to content

Instantly share code, notes, and snippets.

@bajubullet
Created July 11, 2014 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajubullet/2c19479e31630402b107 to your computer and use it in GitHub Desktop.
Save bajubullet/2c19479e31630402b107 to your computer and use it in GitHub Desktop.
Sanitize HTML(XSS checking)
from html5lib import html5parser
from html5lib import sanitizer
_HTML_PARSER = html5parser.HTMLParser(tokenizer=sanitizer.HTMLSanitizer)
def Sanitize(text):
"""Sanitizes given text to remove XSS vulnerabilities.
Args:
text: The text to sanitize.
Returns:
The sanitized string.
"""
return ''.join((
token.toxml() for token in _HTML_PARSER.parseFragment(text).childNodes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment