Skip to content

Commit

Permalink
Added escaping to activity_list_select and test
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Aug 10, 2019
1 parent 6b08be5 commit 7c8dd55
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
22 changes: 12 additions & 10 deletions ckan/lib/helpers.py
Expand Up @@ -36,6 +36,7 @@
from werkzeug.routing import BuildError as FlaskRouteBuildError
import i18n
from six import string_types, text_type
import jinja2

import ckan.exceptions
import ckan.model as model
Expand Down Expand Up @@ -2735,19 +2736,20 @@ def activity_list_select(pkg_activity_list, current_activity_id):
on the "Changes" summary page.
'''
select_list = []
template = jinja2.Template(
u'<option value="{{activity_id}}" {{selected}}>'
'{{timestamp}}</option>',
autoescape=True)
for activity in pkg_activity_list:
entry = render_datetime(activity['timestamp'],
with_hours=True,
with_seconds=True)
if activity['id'] == current_activity_id:
select_list.append(
"<option value=\"" + activity['id'] +
"\" selected>" + entry + "</option>"
)
else:
select_list.append(
"<option value=\"" + activity['id'] + "\">" +
entry + "</option>"
)
select_list.append(Markup(
template
.render(activity_id=activity['id'], timestamp=entry,
selected='selected'
if activity['id'] == current_activity_id
else '')
))

return select_list
4 changes: 2 additions & 2 deletions ckan/templates/package/changes.html
Expand Up @@ -27,12 +27,12 @@ <h1 class="page-heading">{{ _('Changes') }}</h1>
View changes from
<select class="form-control select-time" form="range_form" name="oldest_id">
<pre>
{{ select_list1[1:]|safe }}
{{ select_list1[1:]|join }}
</pre>
</select> to
<select class="form-control select-time" form="range_form" name="newest_id">
<pre>
{{ select_list2|safe }}
{{ select_list2|join }}
</pre>
</select>
</form>
Expand Down
46 changes: 46 additions & 0 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -790,3 +790,49 @@ def helper_as_attribute(self):

def helper_as_item(self):
return base.render('tests/helper_as_item.html')


class TestActivityListSelect(object):

def setup(self):
helpers.reset_db()

def test_simple(self):
pkg_activity = {
'id': 'id1',
'timestamp': datetime.datetime(2018, 2, 1, 10, 58, 59),
}

out = h.activity_list_select([pkg_activity], '')

html = out[0]
eq_(str(html),
'<option value="id1" >February 1, 2018, 10:58:59 (UTC)'
'</option>')
assert hasattr(html, '__html__') # shows it is safe Markup

def test_selected(self):
pkg_activity = {
'id': 'id1',
'timestamp': datetime.datetime(2018, 2, 1, 10, 58, 59),
}

out = h.activity_list_select([pkg_activity], 'id1')

html = out[0]
print html
eq_(str(html),
'<option value="id1" selected>February 1, 2018, 10:58:59 (UTC)'
'</option>')
assert hasattr(html, '__html__') # shows it is safe Markup

def test_escaping(self):
pkg_activity = {
'id': '">', # hacked somehow
'timestamp': datetime.datetime(2018, 2, 1, 10, 58, 59),
}

out = h.activity_list_select([pkg_activity], '')

html = out[0]
assert str(html).startswith(u'<option value="&#34;&gt;" >')

0 comments on commit 7c8dd55

Please sign in to comment.