Skip to content
Snippets Groups Projects
Commit 67eafec5 authored by Ahtisham Shahid's avatar Ahtisham Shahid
Browse files

Added img to bleach safe tags

updated bleach

Added img to bleach safe tags

Added img to bleach safe tags

Added img to bleach safe tags

added test

added test

added test

added test

added test for attr image

added test for attr image

fixed style bug

Updated format xoe

removed extra formatting
parent 0eb0499e
No related branches found
No related tags found
No related merge requests found
...@@ -574,8 +574,8 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -574,8 +574,8 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock):
try: try:
lti_id, key, secret = [i.strip() for i in lti_passport.split(':')] lti_id, key, secret = [i.strip() for i in lti_passport.split(':')]
except ValueError: except ValueError:
msg = self.ugettext('Could not parse LTI passport: {lti_passport}. Should be "id:key:secret" string.').\ msg = 'Could not parse LTI passport: {lti_passport!r}. Should be "id:key:secret" string.'
format(lti_passport='{0!r}'.format(lti_passport)) msg = self.ugettext(msg).format(lti_passport=lti_passport)
raise LtiError(msg) raise LtiError(msg)
if lti_id == self.lti_id.strip(): if lti_id == self.lti_id.strip():
...@@ -694,9 +694,8 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -694,9 +694,8 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock):
param_name, param_value = [p.strip() for p in custom_parameter.split('=', 1)] param_name, param_value = [p.strip() for p in custom_parameter.split('=', 1)]
except ValueError: except ValueError:
_ = self.runtime.service(self, "i18n").ugettext _ = self.runtime.service(self, "i18n").ugettext
# pylint: disable=line-too-long msg = 'Could not parse custom parameter: {custom_parameter!r}. Should be "x=y" string.'
msg = self.ugettext('Could not parse custom parameter: {custom_parameter}. Should be "x=y" string.').\ msg = self.ugettext(msg).format(custom_parameter=custom_parameter)
format(custom_parameter="{0!r}".format(custom_parameter))
raise LtiError(msg) raise LtiError(msg)
# LTI specs: 'custom_' should be prepended before each custom parameter, as pointed in link above. # LTI specs: 'custom_' should be prepended before each custom parameter, as pointed in link above.
...@@ -923,17 +922,12 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -923,17 +922,12 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock):
dict: Context variables for templates dict: Context variables for templates
""" """
# use bleach defaults. see https://github.com/jsocol/bleach/blob/master/bleach/__init__.py # For more context on ALLOWED_TAGS and ALLOWED_ATTRIBUTES
# ALLOWED_TAGS are # Look into this documentation URL see https://bleach.readthedocs.io/en/latest/clean.html#allowed-tags-tags
# ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul']
#
# ALLOWED_ATTRIBUTES are
# 'a': ['href', 'title'],
# 'abbr': ['title'],
# 'acronym': ['title'],
#
# This lets all plaintext through. # This lets all plaintext through.
sanitized_comment = bleach.clean(self.score_comment) allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['img']
allowed_attributes = dict(bleach.sanitizer.ALLOWED_ATTRIBUTES, **{'img': ['src', 'alt']})
sanitized_comment = bleach.clean(self.score_comment, tags=allowed_tags, attributes=allowed_attributes)
return { return {
'launch_url': self.launch_url.strip(), 'launch_url': self.launch_url.strip(),
......
...@@ -303,6 +303,7 @@ class TestEditableFields(TestLtiConsumerXBlock): ...@@ -303,6 +303,7 @@ class TestEditableFields(TestLtiConsumerXBlock):
""" """
Unit tests for LtiConsumerXBlock.editable_fields Unit tests for LtiConsumerXBlock.editable_fields
""" """
def get_mock_lti_configuration(self, editable): def get_mock_lti_configuration(self, editable):
""" """
Returns a mock object of lti-configuration service Returns a mock object of lti-configuration service
...@@ -805,6 +806,7 @@ class TestParseSuffix(TestLtiConsumerXBlock): ...@@ -805,6 +806,7 @@ class TestParseSuffix(TestLtiConsumerXBlock):
self.assertEqual(parsed, FAKE_USER_ID) self.assertEqual(parsed, FAKE_USER_ID)
@ddt.ddt
class TestGetContext(TestLtiConsumerXBlock): class TestGetContext(TestLtiConsumerXBlock):
""" """
Unit tests for LtiConsumerXBlock._get_context_for_template() Unit tests for LtiConsumerXBlock._get_context_for_template()
...@@ -825,6 +827,27 @@ class TestGetContext(TestLtiConsumerXBlock): ...@@ -825,6 +827,27 @@ class TestGetContext(TestLtiConsumerXBlock):
for key in context_keys: for key in context_keys:
self.assertIn(key, context) self.assertIn(key, context)
@ddt.data('a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul', 'img')
def test_comment_allowed_tags(self, tag):
"""
Test that allowed tags are not escaped in context['comment']
"""
comment = u'<{0}>This is a comment</{0}>!'.format(tag)
self.xblock.set_user_module_score(Mock(), 0.92, 1.0, comment)
context = self.xblock._get_context_for_template() # pylint: disable=protected-access
self.assertIn('<{}>'.format(tag), context['comment'])
def test_comment_retains_image_src(self):
"""
Test that image tag has src and other attrs are sanitized
"""
comment = u'<img src="example.com/image.jpeg" onerror="myFunction()">'
self.xblock.set_user_module_score(Mock(), 0.92, 1.0, comment)
context = self.xblock._get_context_for_template() # pylint: disable=protected-access
self.assertIn(u'<img src="example.com/image.jpeg">', context['comment'])
@ddt.ddt @ddt.ddt
class TestProcessorSettings(TestLtiConsumerXBlock): class TestProcessorSettings(TestLtiConsumerXBlock):
......
...@@ -25,7 +25,7 @@ def package_data(pkg, roots): ...@@ -25,7 +25,7 @@ def package_data(pkg, roots):
setup( setup(
name='lti_consumer-xblock', name='lti_consumer-xblock',
version='1.2.3', version='1.2.4',
description='This XBlock implements the consumer side of the LTI specification.', description='This XBlock implements the consumer side of the LTI specification.',
packages=[ packages=[
'lti_consumer', 'lti_consumer',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment