Skip to content

Commit b5333e2

Browse files
brondsemwebjunkie01
authored andcommitted
[#8536] use h.clean_html and |safe_html
1 parent 63f12b9 commit b5333e2

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed

Allura/allura/config/app_cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def create(cls, config, app_globals):
143143
jinja2_env.filters['filter'] = lambda s, t=None: list(filter(t and jinja2_env.tests[t], s))
144144
jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter
145145
jinja2_env.filters['subrender'] = helpers.subrender_jinja_filter
146+
jinja2_env.filters['safe_html'] = helpers.clean_html
146147
jinja2_env.globals.update({
147148
'hasattr': hasattr,
148149
'h': helpers,

Allura/allura/ext/admin/templates/project_trove.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h3>{{base.fullname}}</h3>
2727
{% set help_text = config.get('trovecategories.admin.help.'+base.shortname, '') %}
2828
{% if help_text %}
2929
<div class="grid-19">
30-
{{ help_text|safe }}
30+
{{ help_text|safe_html }}
3131
<br><br>
3232
</div>
3333
{% endif %}

Allura/allura/lib/helpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ def subrender_jinja_filter(context, html_tmpl: str) -> Markup:
809809
log.exception(f'Could not replace {var} in jinja "subrender" for site notification')
810810
continue
811811
html_tmpl = html_tmpl.replace(var, val)
812-
return Markup(html_tmpl)
812+
return clean_html(html_tmpl)
813813

814814

815815
def nl2br_jinja_filter(value):
@@ -1378,3 +1378,10 @@ def pluralize_tool_name(tool_name: string, count: int):
13781378
def parse_fediverse_address(username: str):
13791379
pieces = username.split('@')
13801380
return f'https://{pieces[-1]}/@{pieces[1]}'
1381+
1382+
1383+
def clean_html(value: str) -> Markup:
1384+
from allura.lib.markdown_extensions import HTMLSanitizer
1385+
return Markup(
1386+
HTMLSanitizer().run(value)
1387+
)

Allura/allura/templates/jinja_master/master.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656

5757
{% if c.project and c.project.neighborhood.css %}
5858
<style type="text/css">
59-
{{c.project.neighborhood.get_custom_css()|safe}}
59+
{{ c.project.neighborhood.get_custom_css()|safe_html }}
6060
</style>
6161
{% elif neighborhood|default and neighborhood.css %}
6262
<style type="text/css">
63-
{{neighborhood.get_custom_css()}}
63+
{{ neighborhood.get_custom_css()|safe_html }}
6464
</style>
6565
{% endif %}
6666
{% block extra_css %}{% endblock %}

Allura/allura/templates/neighborhood_project_list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
{{ text }}
4646
{% endif %}
4747
{% if neighborhood.homepage %}
48-
{{neighborhood.homepage|safe}}
48+
{{neighborhood.homepage|safe_html}}
4949
{% endif %}
5050
{% if neighborhood.allow_browse %}
5151
{% if not projects %}

Allura/allura/templates_responsive/jinja_master/master.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858

5959
{% if c.project and c.project.neighborhood.css %}
6060
<style type="text/css">
61-
{{c.project.neighborhood.get_custom_css()|safe}}
61+
{{ c.project.neighborhood.get_custom_css()|safe_html }}
6262
</style>
6363
{% elif neighborhood|default and neighborhood.css %}
6464
<style type="text/css">
65-
{{neighborhood.get_custom_css()}}
65+
{{ neighborhood.get_custom_css()|safe_html }}
6666
</style>
6767
{% endif %}
6868
{% block extra_css %}{% endblock %}

Allura/allura/tests/test_helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,8 @@ def test_querystring():
707707
'https://mysite.com/p/test/foobar/p/test/foobar?page=2&limit=5&count=100')
708708
assert (h.querystring(req, dict(page=5, limit=2, count=None)) ==
709709
'https://mysite.com/p/test/foobar/p/test/foobar?page=5&limit=2')
710+
711+
def test_clean_html():
712+
assert h.clean_html('<script>alert(1)</script>') == '&lt;script&gt;alert(1)&lt;/script&gt;'
713+
assert h.clean_html('<b style="color: red; right: 0">ok</b>') == '<b style="color: red;">ok</b>'
714+
assert isinstance(h.clean_html('foo'), Markup)

0 commit comments

Comments
 (0)