Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Escape user text before rendering it into the PDF exports

Jan Kahmen0e0cc4e27833
diff --git a/wger/manager/tests/test_pdf.py b/wger/manager/tests/test_pdf.py
index 534d8b2b7..e008165f6 100644
--- a/wger/manager/tests/test_pdf.py
+++ b/wger/manager/tests/test_pdf.py
@@ -12,11 +12,18 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Standard Library
+from unittest.mock import patch
+
# Django
from django.urls import reverse
+# Third Party
+from reportlab.platypus import Paragraph
+
# wger
from wger.core.tests.base_testcase import WgerTestCase
+from wger.manager.models import Routine
class RoutinePdfLogExportTestCase(WgerTestCase):
@@ -127,3 +134,48 @@ class RoutinePdfTableExportTestCase(WgerTestCase):
self.user_login('admin')
self.export_pdf(fail=True)
+
+
+class RoutinePdfEscapingTestCase(WgerTestCase):
+ """
+ Tests that user submitted text is escaped before it is passed to reportlab
+ """
+
+ name = 'Squat <img src=x.png>'
+ description = 'Bench <img src="http://localhost:1/x.png"/>'
+
+ def setUp(self):
+ super().setUp()
+
+ routine = Routine.objects.get(pk=3)
+ routine.name = self.name
+ routine.description = self.description
+ routine.save()
+
+ self.user_login('test')
+
+ def paragraph_markup(self, url_name):
+ """
+ Returns the markup of every Paragraph the view builds itself
+ """
+ with patch('wger.manager.views.pdf.Paragraph', side_effect=Paragraph) as paragraph:
+ response = self.client.get(reverse(url_name, kwargs={'pk': 3}))
+
+ self.assertEqual(response.status_code, 200)
+ return [call.args[0] for call in paragraph.call_args_list]
+
+ def test_log_pdf_escapes_routine_text(self):
+ markup = self.paragraph_markup('manager:routine:pdf-log')
+
+ self.assertTrue(markup)
+ for entry in markup:
+ self.assertNotIn('<img', entry)
+ self.assertIn('&lt;img', ' '.join(markup))
+
+ def test_table_pdf_escapes_routine_text(self):
+ markup = self.paragraph_markup('manager:routine:pdf-table')
+
+ self.assertTrue(markup)
+ for entry in markup:
+ self.assertNotIn('<img', entry)
+ self.assertIn('&lt;img', ' '.join(markup))
diff --git a/wger/manager/views/pdf.py b/wger/manager/views/pdf.py
index d1950c105..269e2c6b6 100644
--- a/wger/manager/views/pdf.py
+++ b/wger/manager/views/pdf.py
@@ -16,6 +16,7 @@
# Standard Library
import logging
+from xml.sax.saxutils import escape
# Django
from django.http import (
@@ -86,12 +87,13 @@ def workout_log(request, pk: int):
# Set the title
p = Paragraph(
- f'<para align="center"><strong>{routine.name}</strong></para>', styleSheet['HeaderBold']
+ f'<para align="center"><strong>{escape(routine.name)}</strong></para>',
+ styleSheet['HeaderBold'],
)
elements.append(p)
elements.append(Spacer(10 * cm, 0.5 * cm))
if routine.description:
- p = Paragraph(f'<para align="center">{routine.description}</para>')
+ p = Paragraph(f'<para align="center">{escape(routine.description)}</para>')
elements.append(p)
elements.append(Spacer(10 * cm, 1.5 * cm))
@@ -155,7 +157,8 @@ def workout_view(request, pk):
# Set the title
p = Paragraph(
- '<para align="center"><strong>%(description)s</strong></para>' % {'description': routine},
+ '<para align="center"><strong>%(description)s</strong></para>'
+ % {'description': escape(str(routine))},
styleSheet['HeaderBold'],
)
elements.append(p)
diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.py
index 2b45fae53..17646faed 100644
--- a/wger/nutrition/views/plan.py
+++ b/wger/nutrition/views/plan.py
@@ -16,6 +16,7 @@
# Standard Library
import logging
+from xml.sax.saxutils import escape
# Django
from django.contrib.auth.decorators import login_required
@@ -164,9 +165,9 @@ def export_pdf(request, id: int):
for item in meal.mealitem_set.select_related():
ingredient_markers.append(len(data))
- p = Paragraph(f'<para>{item.ingredient.name}</para>', styleSheet['Normal'])
+ p = Paragraph(f'<para>{escape(item.ingredient.name)}</para>', styleSheet['Normal'])
if item.weight_unit:
- unit_name = ' × ' + item.weight_unit.name
+ unit_name = ' × ' + escape(item.weight_unit.name)
else:
unit_name = 'g'
@@ -210,7 +211,7 @@ def export_pdf(request, id: int):
if plan.description:
p = Paragraph(
'<para align="center"><strong>%(description)s</strong></para>'
- % {'description': plan.description},
+ % {'description': escape(plan.description)},
styleSheet['HeaderBold'],
)
elements.append(p)