Skip to content

Commit 8f3ff05

Browse files
committed
Replace bleach with nh3
1 parent bd94621 commit 8f3ff05

File tree

8 files changed

+27
-21
lines changed

8 files changed

+27
-21
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- Hotfix for wrong font urls
44
- Bump Summernote to 0.8.13
55
- Drop support for Python<3.8 and Django<3.2
6+
- Replaced (deprecated) bleach sanitation usage with nh3. Note that the
7+
styles content sanitation is no longer doable.
68

79
0.8.19.0
810
--------

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ Last, please don't forget to use `safe` templatetag while displaying in template
119119

120120
{{ foobar|safe }}
121121

122-
__Warning__: Please mind, that the widget does not provide any escaping. If you expose the widget to external users without taking care of this, it could potentially lead to an injection vulnerability. Therefore you can use the SummernoteTextFormField or SummernoteTextField, which escape all harmful tags through mozilla's package bleach:
122+
__Warning__: Please mind, that the widget does not provide any escaping. If
123+
you expose the widget to external users without taking care of this, it could
124+
potentially lead to an injection vulnerability. Therefore you can use the
125+
SummernoteTextFormField or SummernoteTextField, which escape all harmful tags
126+
through nh3 package:
123127

124128
In `forms`,
125129
```python

django_summernote/fields.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from django.db import models
22
from django.forms import fields
33

4-
import bleach
5-
from django_summernote.settings import ALLOWED_TAGS, ATTRIBUTES, STYLES
4+
import nh3
5+
from django_summernote.settings import ALLOWED_TAGS, ATTRIBUTES
66
from django_summernote.widgets import SummernoteWidget
77

88
# code based on https://github.com/shaunsephton/django-ckeditor
@@ -15,8 +15,9 @@ def __init__(self, *args, **kwargs):
1515

1616
def to_python(self, value):
1717
value = super().to_python(value)
18-
return bleach.clean(
19-
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)
18+
return nh3.clean(
19+
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES
20+
)
2021

2122

2223
class SummernoteTextField(models.TextField):
@@ -26,5 +27,6 @@ def formfield(self, **kwargs):
2627

2728
def to_python(self, value):
2829
value = super().to_python(value)
29-
return bleach.clean(
30-
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)
30+
return nh3.clean(
31+
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES
32+
)

django_summernote/settings.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
ALLOWED_TAGS = [
1+
ALLOWED_TAGS = {
22
'a', 'div', 'p', 'span', 'img', 'em', 'i', 'li', 'ol', 'ul', 'strong', 'br',
33
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
44
'table', 'tbody', 'thead', 'tr', 'td',
55
'abbr', 'acronym', 'b', 'blockquote', 'code', 'strike', 'u', 'sup', 'sub',
6-
]
7-
8-
STYLES = [
9-
'background-color', 'font-size', 'line-height', 'color', 'font-family'
10-
]
6+
}
117

128
ATTRIBUTES = {
13-
'*': ['style', 'align', 'title', ],
14-
'a': ['href', ],
9+
'*': {'style', 'align', 'title'},
10+
'a': {'href'},
1511
}

django_summernote/test_django_summernote.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ class SimpleForm(forms.Form):
8888
assert url in html
8989
assert 'id="id_foobar"' in html
9090

91-
illegal_tags = '<script></script>'
91+
illegal_tags = '<unknown>dangerous</unknown>'
9292
form_field = SummernoteTextFormField()
9393
cleaned_text = form_field.clean(illegal_tags)
94-
self.assertEqual(cleaned_text, '&lt;script&gt;&lt;/script&gt;')
94+
self.assertEqual(cleaned_text, 'dangerous')
9595

9696
def test_field(self):
9797
from django import forms
@@ -112,11 +112,11 @@ class Meta:
112112
assert url in html
113113
assert 'id="id_foobar"' in html
114114

115-
illegal_tags = '<script></script>'
115+
illegal_tags = '<unknown>dangerous</unknown>'
116116
model_field = SummernoteTextField()
117117
model_instance = SimpleModel1()
118118
cleaned_text = model_field.clean(illegal_tags, model_instance)
119-
self.assertEqual(cleaned_text, '&lt;script&gt;&lt;/script&gt;')
119+
self.assertEqual(cleaned_text, 'dangerous')
120120

121121
def test_empty(self):
122122
from django import forms

django_summernote/test_settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
MEDIA_URL = '/media/'
1919
MEDIA_ROOT = 'test_media'
2020

21+
USE_TZ = True
22+
2123
SECRET_KEY = 'django_summernote'
2224

2325
ROOT_URLCONF = 'django_summernote.urls'

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
django
2-
bleach
2+
nh3

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
description='Summernote plugin for Django',
3939
classifiers=CLASSIFIERS,
4040

41-
install_requires=['django', 'bleach'],
41+
install_requires=['django', 'nh3'],
4242
extras_require={
4343
'dev': [
4444
'django-dummy-plug',

0 commit comments

Comments
 (0)