use bleach instead of lxml.html.clean for sanitize_html OEE
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import logging
|
||||
from lxml.html.clean import Cleaner, autolink_html
|
||||
import re
|
||||
import bleach
|
||||
from xmodule.progress import Progress
|
||||
import capa.xqueue_interface as xqueue_interface
|
||||
from capa.util import *
|
||||
@@ -50,24 +50,14 @@ def upload_to_s3(file_to_upload, keyname, s3_interface):
|
||||
|
||||
return public_url
|
||||
|
||||
class WhiteListCleaner(Cleaner):
|
||||
"""
|
||||
By default, lxml cleaner strips out all links that are not in a defined whitelist.
|
||||
We want to allow all links, and rely on the peer grading flagging mechanic to catch
|
||||
the "bad" ones. So, don't define a whitelist at all.
|
||||
"""
|
||||
def allow_embedded_url(self, el, url):
|
||||
"""
|
||||
Override the Cleaner allow_embedded_url method to remove the whitelist url requirement.
|
||||
Ensure that any tags not in the whitelist are stripped beforehand.
|
||||
"""
|
||||
|
||||
# Tell cleaner to strip any element with a tag that isn't whitelisted.
|
||||
if self.whitelist_tags is not None and el.tag not in self.whitelist_tags:
|
||||
return False
|
||||
|
||||
# Tell cleaner to allow all urls.
|
||||
return True
|
||||
# Used by sanitize_html
|
||||
ALLOWED_HTML_ATTRS = {
|
||||
'*': ['id', 'class', 'height', 'width', 'alt'],
|
||||
'a': ['href', 'title', 'rel'],
|
||||
'embed': ['src'],
|
||||
'iframe': ['src'],
|
||||
'img': ['src'],
|
||||
}
|
||||
|
||||
|
||||
class OpenEndedChild(object):
|
||||
@@ -228,22 +218,19 @@ class OpenEndedChild(object):
|
||||
answer - any string
|
||||
return - a cleaned version of the string
|
||||
"""
|
||||
try:
|
||||
answer = autolink_html(answer)
|
||||
cleaner = WhiteListCleaner(
|
||||
style=True,
|
||||
links=True,
|
||||
add_nofollow=False,
|
||||
page_structure=True,
|
||||
safe_attrs_only=True,
|
||||
whitelist_tags=('embed', 'iframe', 'a', 'img', 'br',)
|
||||
)
|
||||
clean_html = cleaner.clean_html(answer)
|
||||
clean_html = re.sub(r'</p>$', '', re.sub(r'^<p>', '', clean_html))
|
||||
clean_html = re.sub("\n","<br/>", clean_html)
|
||||
except Exception:
|
||||
clean_html = answer
|
||||
return clean_html
|
||||
clean_html = bleach.clean(answer,
|
||||
tags=['embed', 'iframe', 'a', 'img', 'br'],
|
||||
attributes=ALLOWED_HTML_ATTRS,
|
||||
strip=True)
|
||||
return OpenEndedChild.replace_newlines(clean_html)
|
||||
|
||||
@staticmethod
|
||||
def replace_newlines(html):
|
||||
"""
|
||||
Replaces "\n" newlines with <br/>
|
||||
"""
|
||||
retv = re.sub(r'</p>$', '', re.sub(r'^<p>', '', html))
|
||||
return re.sub("\n","<br/>", retv)
|
||||
|
||||
def new_history_entry(self, answer):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user