diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index ba67350867b..f077f2cc9e6 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -24,7 +24,7 @@ import webhelpers.text as whtext import webhelpers.date as date from markdown import markdown -from bleach import clean as clean_html, ALLOWED_TAGS +from bleach import clean as clean_html, ALLOWED_TAGS, ALLOWED_ATTRIBUTES from pylons import url as _pylons_default_url from ckan.common import config, is_flask_request from flask import redirect as _flask_redirect @@ -54,6 +54,9 @@ 'sup', 'sub', 'strike', 'br', 'hr' ]).union(ALLOWED_TAGS) +MARKDOWN_ATTRIBUTES = copy.deepcopy(ALLOWED_ATTRIBUTES) +MARKDOWN_ATTRIBUTES.setdefault('img', []).extend(['src', 'alt', 'title']) + class HelperAttributeDict(dict): def __init__(self, *args, **kwargs): @@ -1865,7 +1868,9 @@ def render_markdown(data, auto_link=True, allow_html=False): data = markdown(data.strip()) else: data = RE_MD_HTML_TAGS.sub('', data.strip()) - data = clean_html(markdown(data), strip=True, tags=MARKDOWN_TAGS) + data = clean_html( + markdown(data), strip=True, + tags=MARKDOWN_TAGS, attributes=MARKDOWN_ATTRIBUTES) # tags can be added by tag:... or tag:"...." and a link will be made # from it if auto_link: diff --git a/ckan/tests/lib/test_helpers.py b/ckan/tests/lib/test_helpers.py index 010f817f096..461a360ed3d 100644 --- a/ckan/tests/lib/test_helpers.py +++ b/ckan/tests/lib/test_helpers.py @@ -205,6 +205,45 @@ def test_render_naughty_markdown(self): output = u'' eq_(h.render_markdown(data), output) + def test_render_markdown_with_js(self): + data = u'[text](javascript: alert(1))' + output = u'

text

' + eq_(h.render_markdown(data), output) + + def test_event_attributes(self): + data = u'

and text

' + output = u'

and text

' + eq_(h.render_markdown(data), output) + + def test_ampersand_in_links(self): + data = u'[link](/url?a=1&b=2)' + output = u'

link

' + eq_(h.render_markdown(data), output) + + data = u'http://example.com/page?a=1&b=2' + output = u'

http://example.com/page?a=1&b=2

' + eq_(h.render_markdown(data), output) + + def test_tags_h1(self): + data = u'#heading' + output = u'

heading

' + eq_(h.render_markdown(data), output) + + def test_tags_h2(self): + data = u'##heading' + output = u'

heading

' + eq_(h.render_markdown(data), output) + + def test_tags_h3(self): + data = u'###heading' + output = u'

heading

' + eq_(h.render_markdown(data), output) + + def test_tags_img(self): + data = u'![image](/image.png)' + output = u'

image

' + eq_(h.render_markdown(data), output) + class TestHelpersRemoveLineBreaks(object):