Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Escape several fields in our template macros.
Browse files Browse the repository at this point in the history
Django won't auto-escape these values, so we'll need to handle it ourselves.
  • Loading branch information
CM Lubinski committed Aug 24, 2017
1 parent 3a21b40 commit fc59e01
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions regulations/templatetags/macros.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""Macros used to make a few common tasks in templates easier"""
from django import template
from django.utils.html import escape

register = template.Library()


@register.inclusion_tag('regulations/macros/external_link.html')
def external_link(url, text, classes="", title=""):
return {"url": url, "text": text, "classes": classes, "title": title}
return {"url": escape(url), "text": escape(text),
"classes": escape(classes), "title": escape(title)}


@register.inclusion_tag('regulations/macros/search_for.html')
def search_for(terms, reg, version, text=None):
if text is None:
text = terms
return {"terms": terms, "reg": reg, "version": version, "text": text}
return {"terms": terms, "reg": reg, "version": version,
"text": escape(text)}
9 changes: 9 additions & 0 deletions regulations/tests/templatetags_macros_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_external_link_classes_title():
assert 'some here' in anchor.get('class')


def test_external_link_is_escaped():
# LXML decodes the value for us, so let's look at the raw markup
text = ('{% load macros %}'
'{% external_link url="http://example.com/?param=1&other=value" '
'text="value" %}')
as_str = Template(text).render(Context({}))
assert 'http://example.com/?param=1&other=value' in as_str


def test_search_for():
"""Macro should url-encode search terms."""
anchor = _gen_link(
Expand Down

0 comments on commit fc59e01

Please sign in to comment.