Created
July 11, 2014 10:54
-
-
Save bajubullet/2c19479e31630402b107 to your computer and use it in GitHub Desktop.
Sanitize HTML(XSS checking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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