Skip to content

Commit

Permalink
[#8536] use h.clean_html and |safe_html
Browse files Browse the repository at this point in the history
  • Loading branch information
brondsem authored and webjunkie01 committed Feb 23, 2024
1 parent 63f12b9 commit b5333e2
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions Allura/allura/config/app_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def create(cls, config, app_globals):
jinja2_env.filters['filter'] = lambda s, t=None: list(filter(t and jinja2_env.tests[t], s))
jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter
jinja2_env.filters['subrender'] = helpers.subrender_jinja_filter
jinja2_env.filters['safe_html'] = helpers.clean_html
jinja2_env.globals.update({
'hasattr': hasattr,
'h': helpers,
Expand Down
2 changes: 1 addition & 1 deletion Allura/allura/ext/admin/templates/project_trove.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h3>{{base.fullname}}</h3>
{% set help_text = config.get('trovecategories.admin.help.'+base.shortname, '') %}
{% if help_text %}
<div class="grid-19">
{{ help_text|safe }}
{{ help_text|safe_html }}
<br><br>
</div>
{% endif %}
Expand Down
9 changes: 8 additions & 1 deletion Allura/allura/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ def subrender_jinja_filter(context, html_tmpl: str) -> Markup:
log.exception(f'Could not replace {var} in jinja "subrender" for site notification')
continue
html_tmpl = html_tmpl.replace(var, val)
return Markup(html_tmpl)
return clean_html(html_tmpl)


def nl2br_jinja_filter(value):
Expand Down Expand Up @@ -1378,3 +1378,10 @@ def pluralize_tool_name(tool_name: string, count: int):
def parse_fediverse_address(username: str):
pieces = username.split('@')
return f'https://{pieces[-1]}/@{pieces[1]}'


def clean_html(value: str) -> Markup:
from allura.lib.markdown_extensions import HTMLSanitizer
return Markup(
HTMLSanitizer().run(value)
)
4 changes: 2 additions & 2 deletions Allura/allura/templates/jinja_master/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@

{% if c.project and c.project.neighborhood.css %}
<style type="text/css">
{{c.project.neighborhood.get_custom_css()|safe}}
{{ c.project.neighborhood.get_custom_css()|safe_html }}
</style>
{% elif neighborhood|default and neighborhood.css %}
<style type="text/css">
{{neighborhood.get_custom_css()}}
{{ neighborhood.get_custom_css()|safe_html }}
</style>
{% endif %}
{% block extra_css %}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion Allura/allura/templates/neighborhood_project_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{ text }}
{% endif %}
{% if neighborhood.homepage %}
{{neighborhood.homepage|safe}}
{{neighborhood.homepage|safe_html}}
{% endif %}
{% if neighborhood.allow_browse %}
{% if not projects %}
Expand Down
4 changes: 2 additions & 2 deletions Allura/allura/templates_responsive/jinja_master/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@

{% if c.project and c.project.neighborhood.css %}
<style type="text/css">
{{c.project.neighborhood.get_custom_css()|safe}}
{{ c.project.neighborhood.get_custom_css()|safe_html }}
</style>
{% elif neighborhood|default and neighborhood.css %}
<style type="text/css">
{{neighborhood.get_custom_css()}}
{{ neighborhood.get_custom_css()|safe_html }}
</style>
{% endif %}
{% block extra_css %}{% endblock %}
Expand Down
5 changes: 5 additions & 0 deletions Allura/allura/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,8 @@ def test_querystring():
'https://mysite.com/p/test/foobar/p/test/foobar?page=2&limit=5&count=100')
assert (h.querystring(req, dict(page=5, limit=2, count=None)) ==
'https://mysite.com/p/test/foobar/p/test/foobar?page=5&limit=2')

def test_clean_html():
assert h.clean_html('<script>alert(1)</script>') == '&lt;script&gt;alert(1)&lt;/script&gt;'
assert h.clean_html('<b style="color: red; right: 0">ok</b>') == '<b style="color: red;">ok</b>'
assert isinstance(h.clean_html('foo'), Markup)

0 comments on commit b5333e2

Please sign in to comment.