Skip to content

Commit 6d04bb9

Browse files
authored
Merge pull request #111 from CentreForDigitalHumanities/fix/xss-vulnerability-transformat
fix: fixed an XSS exploit in transformat
2 parents 74a3b3a + 3bd85c2 commit 6d04bb9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/cdh/core/templatetags/transformat.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist
22
from django.template.base import render_value_in_context
3+
from django.utils.html import escape, format_html
34
from django.utils.safestring import SafeData, mark_safe
45

56
register = Library()
@@ -27,20 +28,28 @@ def render(self, context):
2728
value = render_value_in_context(output, context)
2829
# Restore percent signs. Percent signs in template text are doubled
2930
# so they are not interpreted as string format flags.
30-
is_safe = isinstance(value, SafeData)
3131
value = value.replace('%%', '%')
3232

3333
formatvalues = []
3434
for formatvalue in self.formatvalues:
35+
variable_value = None
3536
try:
37+
# Try to see if this value is actually a variable reference
3638
variable = Variable(formatvalue)
37-
formatvalues.append(variable.resolve(context))
39+
40+
variable_value = variable.resolve(context)
3841
except VariableDoesNotExist:
39-
formatvalues.append(formatvalue)
42+
# If not, it's just a plain value specified directly
43+
variable_value = formatvalue
44+
45+
# Check if the string isn't marked as safe, in which case, escape.
46+
if not isinstance(variable_value, SafeData):
47+
variable_value = escape(variable_value)
48+
49+
formatvalues.append(variable_value)
4050

4151
value = value.format(*formatvalues)
4252

43-
value = mark_safe(value) if is_safe else value
4453
if self.asvar:
4554
context[self.asvar] = value
4655
return ''

0 commit comments

Comments
 (0)