Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add new field for rich text editor

akjanikd2c9a1239600
diff --git a/requirements.in b/requirements.in
index aee3353fb9..c9fd6b7923 100644
--- a/requirements.in
+++ b/requirements.in
@@ -1,4 +1,5 @@
Babel>=2.5
+bleach<3.0
boto3
cairosvg
Django>=1.11
diff --git a/requirements.txt b/requirements.txt
index 32ed31a5ea..c4352cc71c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -9,6 +9,7 @@ asn1crypto==0.24.0 # via cryptography
attrs==17.4.0 # via pytest
babel==2.5.3
billiard==3.5.0.3 # via celery
+bleach==2.1.2
boto3==1.5.18
botocore==1.8.32 # via boto3, s3transfer
braintree==3.39.1 # via django-payments
@@ -56,7 +57,7 @@ graphene-django==2.0.0
graphene==2.0.1 # via graphene-django
graphql-core==2.0 # via graphene, graphql-relay
graphql-relay==0.4.5 # via graphene
-html5lib==1.0.1 # via weasyprint
+html5lib==1.0.1 # via bleach, weasyprint
idna==2.6 # via cryptography, requests, yarl
iso8601==0.1.12 # via graphene-django
jmespath==0.9.3 # via boto3, botocore
@@ -94,7 +95,7 @@ rx==1.6.0 # via graphql-core
s3transfer==0.1.12 # via boto3
satchless==1.1.3
singledispatch==3.4.0.3 # via graphene-django
-six==1.11.0 # via cryptography, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpy
+six==1.11.0 # via bleach, cryptography, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpy
social-auth-app-django==2.1.0
social-auth-core==1.6.0 # via social-auth-app-django
stripe==1.77.1 # via django-payments
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index aebdef2005..ca13df61ff 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -1,3 +1,4 @@
+from bleach.sanitizer import ALLOWED_TAGS
from django import forms
from django.db.models import Count
from django.forms.models import ModelChoiceIterator
@@ -5,6 +6,7 @@ from django.forms.widgets import CheckboxSelectMultiple
from django.utils.encoding import smart_text
from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
+import bleach
from . import ProductBulkAction
from ...product.models import (
@@ -14,6 +16,20 @@ from ..widgets import RichTextEditorWidget
from .widgets import ImagePreviewWidget
+class RichTextField(forms.CharField):
+ """
+ A field for rich text editor, providing backend sanitization
+ """
+
+ widget = RichTextEditorWidget
+ ALLOWED_TAGS.extend([u'br', u'p', u'h2', u'h3'])
+
+ def to_python(self, value):
+ value = super().to_python(value)
+ value = bleach.clean(value, tags=ALLOWED_TAGS)
+ return value
+
+
class ProductTypeSelectorForm(forms.Form):
"""Form that allows selecting product type."""
@@ -131,6 +147,7 @@ class ProductForm(forms.ModelForm):
collections = forms.ModelMultipleChoiceField(
required=False, queryset=Collection.objects.all())
+ description = RichTextField()
def __init__(self, *args, **kwargs):
self.product_attributes = []
@@ -140,7 +157,6 @@ class ProductForm(forms.ModelForm):
self.product_attributes = self.product_attributes.prefetch_related(
'values')
self.prepare_fields_for_attributes()
- self.fields['description'].widget = RichTextEditorWidget()
self.fields["collections"].initial = Collection.objects.filter(
products__name=self.instance)
diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.py
index 65aacb7242..45b050c3b5 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -7,6 +7,7 @@ from PIL import Image
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import HiddenInput
+from django.forms.models import model_to_dict
from django.urls import reverse
from django.utils.encoding import smart_text
import pytest
@@ -678,3 +679,24 @@ def test_assign_collection_to_product(product_in_stock):
form.save()
assert product.collections.first().name == 'test_collections'
assert collection.products.first().name == product.name
+
+
+def test_sanitize_product_description(product_type, default_category):
+ product = Product.objects.create(
+ name='Test Product', price=10, description='', pk=10,
+ product_type=product_type, category=default_category)
+ data = model_to_dict(product)
+ data['description'] = '<b>bold</b><p><i>italic</i></p><h2>Header</h2>' \
+ '<h3>subheader</h3><blockquote>quote</blockquote>' \
+ '<p><a href="www.mirumee.com">link</a></p>' \
+ '<p>an <script>evil()</script> example</p>'
+ data['price'] = 20
+ form = ProductForm(data, instance=product)
+ assert form.is_valid()
+ form.save()
+ assert product.description == '<b>bold</b><p><i>italic</i></p>' \
+ '<h2>Header</h2><h3>subheader</h3>' \
+ '<blockquote>quote</blockquote>' \
+ '<p><a href="www.mirumee.com">link</a></p>' \
+ '<p>an &lt;script&gt;evil()&lt;/script&gt;' \
+ ' example</p>'