Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Replace bleach with nh3 and implement Markdown support for exercises

Mannai9606d5fec0bb
diff --git a/pyproject.toml b/pyproject.toml
index 37e910cf8..a9f90b533 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -28,7 +28,6 @@ classifiers = [
]
dependencies = [
- "bleach[css]~=6.3",
"celery[redis]~=5.5.3",
"crispy-bootstrap5==2025.6",
"django-activity-stream~=2.0.0",
@@ -58,6 +57,9 @@ dependencies = [
"icalendar~=6.3.2",
"invoke~=2.2.1",
"lingua-language-detector~=2.1.1",
+ "markdownify~=1.2",
+ "markdown-it-py~=4.0",
+ "nh3~=0.3",
"openfoodfacts~=3.3.0",
"packaging~=25.0",
"pillow~=12.0.0",
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index be6750a04..7c4cee4ce 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -468,6 +468,7 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False, read_only=True)
uuid = serializers.UUIDField(required=False, read_only=True)
+ description_source = serializers.CharField(required=False, allow_blank=True)
exercise = serializers.PrimaryKeyRelatedField(
queryset=Exercise.objects.all(),
required=True,
@@ -481,10 +482,12 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
'name',
'exercise',
'description',
+ 'description_source',
'created',
'language',
'license_author',
)
+ read_only_fields = ('description') # Prevents API from accepting raw HTML
def validate(self, value):
"""
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 5739904b3..db71a798c 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -29,9 +29,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 bleach.css_sanitizer import CSSSanitizer
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import (
OpenApiParameter,
@@ -173,15 +171,7 @@ class ExerciseTranslationViewSet(ModelViewSet):
"""
Save entry to activity stream
"""
- # 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,
- css_sanitizer=CSSSanitizer(allowed_css_properties=HTML_STYLES_WHITELIST),
- strip=True,
- )
+
super().perform_create(serializer)
actstream_action.send(
@@ -202,17 +192,8 @@ class ExerciseTranslationViewSet(ModelViewSet):
if serializer.validated_data.get('language'):
del serializer.validated_data['language']
- # 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,
- css_sanitizer=CSSSanitizer(allowed_css_properties=HTML_STYLES_WHITELIST),
- strip=True,
- )
-
super().perform_update(serializer)
+
actstream_action.send(
self.request.user,
verb=StreamVerbs.UPDATED.value,
diff --git a/wger/exercises/models/translation.py b/wger/exercises/models/translation.py
index 97efbd1da..2867cce44 100644
--- a/wger/exercises/models/translation.py
+++ b/wger/exercises/models/translation.py
@@ -26,12 +26,13 @@ from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
# Third Party
-import bleach
+import nh3
from simple_history.models import HistoricalRecords
# wger
from wger.core.models import Language
from wger.exercises.models import Exercise
+from wger.utils.markdown import render_markdown, sanitize_html
from wger.utils.cache import reset_exercise_api_cache
from wger.utils.models import (
AbstractHistoryMixin,
@@ -51,6 +52,13 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
)
"""Description on how to perform the exercise"""
+ description_source = models.TextField(
+ verbose_name=_('Description (Source)'),
+ blank=True,
+ null=True
+ )
+ """The raw Markdown source"""
+
name = models.CharField(
max_length=200,
verbose_name=_('Name'),
@@ -128,6 +136,9 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
"""
Reset all cached infos
"""
+ if self.description_source:
+ self.description = render_markdown(self.description_source)
+
super().save(*args, **kwargs)
# Api cache
@@ -203,7 +214,7 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
"""
Return the exercise description with all markup removed
"""
- return bleach.clean(self.description, strip=True)
+ return sanitize_html(self.description)
def get_owner_object(self):
"""
diff --git a/wger/utils/generic_views.py b/wger/utils/generic_views.py
index 41d8a25bf..b449e8256 100644
--- a/wger/utils/generic_views.py
+++ b/wger/utils/generic_views.py
@@ -31,8 +31,6 @@ from django.views.generic import TemplateView
from django.views.generic.edit import ModelFormMixin
# Third Party
-import bleach
-from bleach.css_sanitizer import CSSSanitizer
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
ButtonHolder,
@@ -41,6 +39,7 @@ from crispy_forms.layout import (
)
# wger
+from wger.utils.markdown import sanitize_html
from wger.utils.constants import (
HTML_ATTRIBUTES_WHITELIST,
HTML_STYLES_WHITELIST,
@@ -147,7 +146,7 @@ class WgerFormMixin(ModelFormMixin):
clean_html = ()
"""
- List of form fields that should be passed to bleach to clean the html
+ List of form fields that should be passed to wger.utils.markdown to clean the html
"""
messages = ''
@@ -237,17 +236,9 @@ class WgerFormMixin(ModelFormMixin):
"""
for field in self.clean_html:
- setattr(
- form.instance,
- field,
- bleach.clean(
- getattr(form.instance, field),
- tags=HTML_TAG_WHITELIST,
- attributes=HTML_ATTRIBUTES_WHITELIST,
- css_sanitizer=CSSSanitizer(allowed_css_properties=HTML_STYLES_WHITELIST),
- strip=True,
- ),
- )
+ raw_value = getattr(form.instance, field)
+ clean_value = sanitize_html(raw_value)
+ setattr(form.instance, field, clean_value)
if self.get_messages():
messages.success(self.request, self.get_messages())
diff --git a/wger/utils/markdown.py b/wger/utils/markdown.py
new file mode 100644
index 000000000..0ce59e590
--- /dev/null
+++ b/wger/utils/markdown.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Third party
+import nh3
+from markdown_it import MarkdownIt
+
+def render_markdown(text):
+ """
+ Renders markdown text to HTML and sanitizes it to allow only basic markup.
+ """
+ if not text:
+ return ""
+
+ # Render Markdown to HTML
+ md = MarkdownIt("commonmark", {"breaks": True, "html": True})
+ raw_html = md.render(text)
+
+ # Sanitize HTML
+ ALLOWED_TAGS = {'b', 'strong', 'i', 'em', 'ul', 'ol', 'li', 'p'}
+
+ clean_html = nh3.clean(
+ raw_html,
+ tags=ALLOWED_TAGS,
+ attributes={}
+ )
+
+ return clean_html
+
+def sanitize_html(text):
+ """
+ Directly sanitizes HTML (for legacy fields or non-markdown inputs)
+ """
+ if not text:
+ return ""
+
+ ALLOWED_TAGS = {'b', 'strong', 'i', 'em', 'ul', 'ol', 'li', 'p'}
+ return nh3.clean(text, tags=ALLOWED_TAGS, attributes={})
\ No newline at end of file
diff --git a/wger/utils/tests/test_markdown.py b/wger/utils/tests/test_markdown.py
new file mode 100644
index 000000000..d2643e943
--- /dev/null
+++ b/wger/utils/tests/test_markdown.py
@@ -0,0 +1,112 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Standard Library
+import unittest
+
+# wger
+from wger.utils.markdown import (
+ render_markdown,
+ sanitize_html,
+)
+
+
+class TestMarkdown(unittest.TestCase):
+ """
+ Test the markdown rendering and HTML sanitization utilities.
+ Allowed tags: b, strong, i, em, ul, ol, li, p
+ """
+
+ def test_render_markdown_basic(self):
+ """
+ Test that basic markdown syntax is rendered to HTML.
+ """
+ # Bold and Italic
+ text = "I like **osaka** and eating *sata-andagi*."
+ output = render_markdown(text)
+
+ self.assertTrue('<strong>osaka</strong>' in output or '<b>osaka</b>' in output)
+ self.assertTrue('<em>sata-andagi</em>' in output or '<i>sata-andagi</i>' in output)
+ self.assertIn('<p>', output)
+
+ def test_render_markdown_lists(self):
+ """
+ Test that lists are rendered correctly.
+ """
+ text = "- Item 1\n- Item 2"
+ output = render_markdown(text)
+
+ self.assertIn('<ul>', output)
+ self.assertIn('<li>Item 1</li>', output)
+ self.assertIn('<li>Item 2</li>', output)
+
+ def test_render_markdown_strips_disallowed_tags(self):
+ """
+ Test that valid markdown generating disallowed tags (e.g. headings, images)
+ results in the tag being stripped.
+ """
+ # Heading 1 (<h1> is not allowed)
+ text = "# Big Title"
+ output = render_markdown(text)
+
+ self.assertNotIn('<h1>', output)
+ self.assertIn('Big Title', output) # Content remains, tag is removed
+
+ # Image (<img> is not allowed)
+ text = "![Alt text](http://example.com/img.jpg)"
+ output = render_markdown(text)
+
+ self.assertNotIn('<img', output)
+
+ def test_render_markdown_xss_prevention(self):
+ """
+ Test that script tags and dangerous attributes are removed.
+ """
+ # Script injection via Markdown/HTML mix
+ text = "Hello <script>alert('XSS')</script>"
+ output = render_markdown(text)
+
+ self.assertNotIn('<script>', output)
+ self.assertNotIn('alert', output)
+
+ # Dangerous attributes
+ text = '<p onclick="maliciousCode()">Click me</p>'
+ output = render_markdown(text)
+
+ self.assertNotIn('onclick', output)
+ self.assertIn('<p>Click me</p>', output)
+
+ def test_sanitize_html_legacy(self):
+ """
+ Test the direct HTML sanitizer used for legacy fields.
+ """
+ html = '<b>Bold</b> and <a href="http://google.com">Link</a>'
+ output = sanitize_html(html)
+
+ self.assertIn('<b>Bold</b>', output)
+ self.assertNotIn('<a href', output) # Links are not in the allowed list
+ self.assertIn('Link', output) # Content usually preserved
+
+ def test_empty_inputs(self):
+ """
+ Test handling of None or empty strings.
+ """
+ self.assertEqual(render_markdown(None), "")
+ self.assertEqual(render_markdown(""), "")
+ self.assertEqual(sanitize_html(None), "")
+ self.assertEqual(sanitize_html(""), "")
+
+
+if __name__ == '__main__':
+ unittest.main()
\ No newline at end of file