Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use bleach to sanitize the HTML of the tinyMCE editor

Roland Geidera68cee9643a1
diff --git a/requirements.txt b/requirements.txt
index cf7fc8987..793342773 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,4 @@ selenium
django_mobile
django-discover-runner
pep8
+bleach
diff --git a/wger/manager/templates/mobile/template.html b/wger/manager/templates/mobile/template.html
index dde66251e..4d9898ddd 100644
--- a/wger/manager/templates/mobile/template.html
+++ b/wger/manager/templates/mobile/template.html
@@ -63,7 +63,7 @@
{% if user.is_authenticated %}
<li {% if active_tab == 'workout' %}class="active"{% endif %}>
- <a href="{% url manager.views.overview %}">{% trans "Workouts" %}</a>
+ <a href="{% url wgrt.manager.views.overview %}">{% trans "Workouts" %}</a>
</li>
<li class="dropdown {% if active_tab == 'nutrition' %}active{% endif %}">
<a href="{% url nutrition.views.overview %}">{% trans "Nutrition" %}</a>
@@ -89,40 +89,40 @@
</li>
{% if user.is_authenticated %}
- <li {% if active_tab == 'workout' %}class="active"{% endif %}><a href="{% url manager.views.overview %}">{% trans "Workouts" %}</a></li>
+ <li {% if active_tab == 'workout' %}class="active"{% endif %}><a href="{% url wger.manager.views.overview %}">{% trans "Workouts" %}</a></li>
{% endif %}
<li>
- <a href="{% url exercises.views.exercise_overview %}"
+ <a href="{% url wger.exercises.views.exercise_overview %}"
class="dropdown-toggle"
data-toggle="dropdown">{% trans "Exercises" %} <b class="caret"></b></a>
<ul class="dropdown-menu">
- <li><a href="{% url exercises.views.exercise_overview %}">{% trans "Exercises" %}</a></li>
+ <li><a href="{% url wger.exercises.views.exercise_overview %}">{% trans "Exercises" %}</a></li>
<li><a href="{% url muscle-overview %}">{% trans "Muscle overview" %}</a></li>
</ul>
</li>
{% if user.is_authenticated %}
<li>
- <a href="{% url weight.views.overview %}"
+ <a href="{% url wger.weight.views.overview %}"
class="dropdown-toggle"
data-toggle="dropdown">{% trans "Weight" %} <b class="caret"></b></a>
<ul class="dropdown-menu">
- <li><a href="{% url weight.views.overview %}">{% trans "Weight overview" %} </a></li>
+ <li><a href="{% url wger.weight.views.overview %}">{% trans "Weight overview" %} </a></li>
<li><a href="{% url weight-add %}">{% trans "Add weight entry" %}</a></li>
</ul>
</li>
{% endif %}
<li>
- <a href="{% url nutrition.views.overview %}"
+ <a href="{% url wger.nutrition.views.overview %}"
class="dropdown-toggle"
data-toggle="dropdown">{% trans "Nutrition" %} <b class="caret"></b></a>
<ul class="dropdown-menu">
{% if user.is_authenticated %}
- <li><a href="{% url nutrition.views.overview %}">{% trans "Nutrition plans" %} </a></li>
+ <li><a href="{% url wger.nutrition.views.overview %}">{% trans "Nutrition plans" %} </a></li>
{% endif %}
- <li><a href="{% url nutrition.views.ingredient_overview %}">{% trans "Ingredient overview" %}</a></li>
+ <li><a href="{% url wger.nutrition.views.ingredient_overview %}">{% trans "Ingredient overview" %}</a></li>
</ul>
</li>
diff --git a/wger/workout_manager/constants.py b/wger/workout_manager/constants.py
index ed2a326d7..a3a07d125 100644
--- a/wger/workout_manager/constants.py
+++ b/wger/workout_manager/constants.py
@@ -32,3 +32,18 @@ DATE_FORMATS = ['%d.%m.%Y', # '25.10.2012'
'%Y-%m-%d', # '2012-10-25'
]
+
+# Allowed tags, attributes and styles allowed in textareas edited with a JS
+# editor. Everything not in these whitelists is stripped.
+HTML_TAG_WHITELIST = ('strong',
+ 'em',
+ 'ul',
+ 'ol',
+ 'li',
+ 'p')
+
+HTML_ATTRIBUTES_WHITELIST = {
+ '*': 'style'
+}
+
+HTML_STYLES_WHITELIST = ('text-decoration',)
diff --git a/wger/workout_manager/generic_views.py b/wger/workout_manager/generic_views.py
index 614c10773..e0dd2a8d9 100644
--- a/wger/workout_manager/generic_views.py
+++ b/wger/workout_manager/generic_views.py
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
import logging
+import bleach
from django.forms import models
from django.http import HttpResponseForbidden
@@ -24,6 +25,10 @@ from django.core.urlresolvers import reverse
from django.core.context_processors import csrf
from django.views.generic.edit import ModelFormMixin
+from wger.workout_manager.constants import HTML_TAG_WHITELIST
+from wger.workout_manager.constants import HTML_ATTRIBUTES_WHITELIST
+from wger.workout_manager.constants import HTML_STYLES_WHITELIST
+
logger = logging.getLogger('workout_manager.custom')
@@ -37,6 +42,7 @@ class YamlFormMixin(ModelFormMixin):
title = ''
owner_object = False
submit_text = ugettext_lazy('Save')
+ clean_html = ()
def get_context_data(self, **kwargs):
'''
@@ -116,6 +122,24 @@ class YamlFormMixin(ModelFormMixin):
# Dispatch normally
return super(YamlFormMixin, self).dispatch(request, *args, **kwargs)
+ def form_valid(self, form):
+ '''
+ Pre-process the form, cleaning up the HTML code found in the fields
+ given in clean_html. All HTML tags, attributes and styles not in the
+ whitelists are stripped from the output, leaving only the text content:
+
+ <table><tr><td>foo</td></tr></table> simply becomes 'foo'
+ '''
+
+ for field in self.clean_html:
+ setattr(form.instance, field, bleach.clean(getattr(form.instance, field),
+ tags=HTML_TAG_WHITELIST,
+ attributes=HTML_ATTRIBUTES_WHITELIST,
+ styles=HTML_STYLES_WHITELIST,
+ strip=True))
+
+ return super(YamlFormMixin, self).form_valid(form)
+
class YamlDeleteMixin(ModelFormMixin):
template_name = 'delete.html'