Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Clean HTML in exercise description when creating or updating over the API

Roland Geider4c985d552607
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index e2e9cb8ee..73ab13635 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -26,6 +26,7 @@ from django.utils.translation import gettext as _
from django.views.decorators.cache import cache_page
# Third Party
+import bleach
from actstream import action as actstream_action
from easy_thumbnails.alias import aliases
from easy_thumbnails.files import get_thumbnailer
@@ -68,6 +69,11 @@ from wger.exercises.models import (
Variation,
)
from wger.exercises.views.helper import StreamVerbs
+from wger.utils.constants import (
+ HTML_ATTRIBUTES_WHITELIST,
+ HTML_STYLES_WHITELIST,
+ HTML_TAG_WHITELIST,
+)
from wger.utils.language import load_item_languages
@@ -134,6 +140,17 @@ class ExerciseTranslationViewSet(CreateUpdateModelViewSet):
Save entry to activity stream
"""
super().perform_create(serializer)
+
+ # Clean the description HTML
+ if serializer.validated_data.get('description'):
+ serializer.validated_data['description'] = bleach.clean(
+ serializer.validated_data['description'],
+ tags=HTML_TAG_WHITELIST,
+ attributes=HTML_ATTRIBUTES_WHITELIST,
+ styles=HTML_STYLES_WHITELIST,
+ strip=True
+ )
+
actstream_action.send(
self.request.user,
verb=StreamVerbs.CREATED.value,
@@ -149,6 +166,16 @@ class ExerciseTranslationViewSet(CreateUpdateModelViewSet):
if serializer.validated_data.get('exercise_base'):
del serializer.validated_data['exercise_base']
+ # Clean the description HTML
+ if serializer.validated_data.get('description'):
+ serializer.validated_data['description'] = bleach.clean(
+ serializer.validated_data['description'],
+ tags=HTML_TAG_WHITELIST,
+ attributes=HTML_ATTRIBUTES_WHITELIST,
+ styles=HTML_STYLES_WHITELIST,
+ strip=True
+ )
+
super().perform_create(serializer)
actstream_action.send(
self.request.user,
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index 9f6d01f5e..6f1b56640 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -25,9 +25,10 @@ from django.template import (
)
from django.urls import reverse
-# wger
+# Third Party
from rest_framework import status
+# wger
from wger.core.tests import api_base_test
from wger.core.tests.api_base_test import ApiBaseTestCase
from wger.core.tests.base_testcase import (
@@ -607,3 +608,29 @@ class ExerciseCustomApiTestCase(api_base_test.BaseTestCase, ApiBaseTestCase):
exercise = Exercise.objects.get(pk=self.pk)
self.assertEqual(exercise.exercise_base_id, 1)
+
+ def test_patch_clean_html(self):
+ """
+ Test that the description field has its HTML stripped before saving
+ """
+
+ description = '<script>alert();</script> The wild boar is a suid native...'
+ self.get_credentials('test')
+ response = self.client.patch(self.url_detail, data={'description': description})
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.description, 'alert(); The wild boar is a suid native...')
+
+ def test_post_clean_html(self):
+ """
+ Test that the description field has its HTML stripped before creating an exercise
+ """
+
+ description = '<script>alert();</script> The wild boar is a suid native...'
+ self.get_credentials('test')
+ response = self.client.patch(self.url_detail, data={'description': description})
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.description, 'alert(); The wild boar is a suid native...')