2015-12-14 19:43:39 +01:00
|
|
|
from HTMLParser import HTMLParser
|
|
|
|
import inspect
|
2016-02-29 00:47:07 +01:00
|
|
|
import re
|
2015-12-14 19:43:39 +01:00
|
|
|
from urllib import quote, quote_plus
|
2016-02-25 10:13:39 +01:00
|
|
|
from urlparse import urlparse
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
class SafeHTMLParser(HTMLParser):
|
|
|
|
# from html5lib.sanitiser
|
|
|
|
acceptable_elements = ['a', 'abbr', 'acronym', 'address', 'area',
|
|
|
|
'article', 'aside', 'audio', 'b', 'big', 'blockquote', 'br', 'button',
|
|
|
|
'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
|
|
|
|
'command', 'datagrid', 'datalist', 'dd', 'del', 'details', 'dfn',
|
|
|
|
'dialog', 'dir', 'div', 'dl', 'dt', 'em', 'event-source', 'fieldset',
|
|
|
|
'figcaption', 'figure', 'footer', 'font', 'header', 'h1',
|
|
|
|
'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'ins',
|
|
|
|
'keygen', 'kbd', 'label', 'legend', 'li', 'm', 'map', 'menu', 'meter',
|
|
|
|
'multicol', 'nav', 'nextid', 'ol', 'output', 'optgroup', 'option',
|
|
|
|
'p', 'pre', 'progress', 'q', 's', 'samp', 'section', 'select',
|
|
|
|
'small', 'sound', 'source', 'spacer', 'span', 'strike', 'strong',
|
|
|
|
'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'time', 'tfoot',
|
|
|
|
'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'video']
|
2017-02-28 22:47:56 +01:00
|
|
|
replaces_pre = [["&", "&"], ["\"", """], ["<", "<"], [">", ">"]]
|
|
|
|
replaces_post = [["\n", "<br/>"], ["\t", " "], [" ", " "], [" ", " "], ["<br/> ", "<br/> "]]
|
2016-02-25 10:13:39 +01:00
|
|
|
src_schemes = [ "data" ]
|
2016-03-18 14:09:28 +01:00
|
|
|
uriregex1 = re.compile(r'(?i)\b((?:(https?|ftp|bitcoin):(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?]))')
|
2016-02-29 00:47:07 +01:00
|
|
|
uriregex2 = re.compile(r'<a href="([^"]+)&')
|
|
|
|
emailregex = re.compile(r'\b([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})\b')
|
2015-12-16 14:20:51 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2017-02-28 22:47:56 +01:00
|
|
|
def replace_pre(text):
|
|
|
|
for a in SafeHTMLParser.replaces_pre:
|
|
|
|
text = text.replace(a[0], a[1])
|
|
|
|
return text
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def replace_post(text):
|
|
|
|
for a in SafeHTMLParser.replaces_post:
|
2015-12-16 14:20:51 +01:00
|
|
|
text = text.replace(a[0], a[1])
|
2016-01-23 09:53:14 +01:00
|
|
|
if len(text) > 1 and text[0] == " ":
|
|
|
|
text = " " + text[1:]
|
2015-12-16 14:20:51 +01:00
|
|
|
return text
|
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
HTMLParser.__init__(self, *args, **kwargs)
|
2016-01-23 09:53:14 +01:00
|
|
|
self.reset_safe()
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
def reset_safe(self):
|
2015-12-15 03:51:06 +01:00
|
|
|
self.elements = set()
|
2015-12-14 19:43:39 +01:00
|
|
|
self.raw = u""
|
|
|
|
self.sanitised = u""
|
2015-12-15 03:51:06 +01:00
|
|
|
self.has_html = False
|
2016-02-25 10:13:39 +01:00
|
|
|
self.allow_picture = False
|
|
|
|
self.allow_external_src = False
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
def add_if_acceptable(self, tag, attrs = None):
|
2017-06-24 12:13:35 +02:00
|
|
|
if tag not in SafeHTMLParser.acceptable_elements:
|
2015-12-14 19:43:39 +01:00
|
|
|
return
|
|
|
|
self.sanitised += "<"
|
|
|
|
if inspect.stack()[1][3] == "handle_endtag":
|
|
|
|
self.sanitised += "/"
|
|
|
|
self.sanitised += tag
|
2017-06-24 12:13:35 +02:00
|
|
|
if attrs is not None:
|
2016-02-25 10:13:39 +01:00
|
|
|
for attr, val in attrs:
|
|
|
|
if tag == "img" and attr == "src" and not self.allow_picture:
|
|
|
|
val = ""
|
|
|
|
elif attr == "src" and not self.allow_external_src:
|
|
|
|
url = urlparse(val)
|
|
|
|
if url.scheme not in SafeHTMLParser.src_schemes:
|
2017-06-24 12:13:35 +02:00
|
|
|
val = ""
|
2016-02-25 10:13:39 +01:00
|
|
|
self.sanitised += " " + quote_plus(attr)
|
|
|
|
if not (val is None):
|
2017-02-22 09:05:05 +01:00
|
|
|
self.sanitised += "=\"" + val + "\""
|
2015-12-14 19:43:39 +01:00
|
|
|
if inspect.stack()[1][3] == "handle_startendtag":
|
|
|
|
self.sanitised += "/"
|
|
|
|
self.sanitised += ">"
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
2016-02-25 10:13:39 +01:00
|
|
|
if tag in SafeHTMLParser.acceptable_elements:
|
2015-12-14 19:43:39 +01:00
|
|
|
self.has_html = True
|
|
|
|
self.add_if_acceptable(tag, attrs)
|
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
|
|
|
self.add_if_acceptable(tag)
|
|
|
|
|
|
|
|
def handle_startendtag(self, tag, attrs):
|
2016-02-25 10:13:39 +01:00
|
|
|
if tag in SafeHTMLParser.acceptable_elements:
|
2015-12-14 19:43:39 +01:00
|
|
|
self.has_html = True
|
|
|
|
self.add_if_acceptable(tag, attrs)
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
2017-02-22 09:05:05 +01:00
|
|
|
self.sanitised += data
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
def handle_charref(self, name):
|
|
|
|
self.sanitised += "&#" + name + ";"
|
|
|
|
|
|
|
|
def handle_entityref(self, name):
|
|
|
|
self.sanitised += "&" + name + ";"
|
2015-12-16 14:20:51 +01:00
|
|
|
|
|
|
|
def feed(self, data):
|
2017-02-22 09:05:05 +01:00
|
|
|
try:
|
|
|
|
data = unicode(data, 'utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
data = unicode(data, 'utf-8', errors='replace')
|
2015-12-16 14:20:51 +01:00
|
|
|
HTMLParser.feed(self, data)
|
2017-02-28 22:47:56 +01:00
|
|
|
tmp = SafeHTMLParser.replace_pre(data)
|
2017-02-22 09:05:05 +01:00
|
|
|
tmp = SafeHTMLParser.uriregex1.sub(
|
|
|
|
r'<a href="\1">\1</a>',
|
|
|
|
tmp)
|
|
|
|
tmp = SafeHTMLParser.uriregex2.sub(r'<a href="\1&', tmp)
|
|
|
|
tmp = SafeHTMLParser.emailregex.sub(r'<a href="mailto:\1">\1</a>', tmp)
|
2017-02-28 22:47:56 +01:00
|
|
|
tmp = SafeHTMLParser.replace_post(tmp)
|
2016-02-29 00:47:07 +01:00
|
|
|
self.raw += tmp
|
2015-12-16 14:20:51 +01:00
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
def is_html(self, text = None, allow_picture = False):
|
|
|
|
if text:
|
|
|
|
self.reset()
|
|
|
|
self.reset_safe()
|
2016-02-25 10:13:39 +01:00
|
|
|
self.allow_picture = allow_picture
|
2015-12-14 19:43:39 +01:00
|
|
|
self.feed(text)
|
|
|
|
self.close()
|
2016-02-25 10:13:39 +01:00
|
|
|
return self.has_html
|