Skip to content

Commit

Permalink
Replace bleach with nh3
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Feb 16, 2024
1 parent bd94621 commit 8f3ff05
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Hotfix for wrong font urls
- Bump Summernote to 0.8.13
- Drop support for Python<3.8 and Django<3.2
- Replaced (deprecated) bleach sanitation usage with nh3. Note that the
styles content sanitation is no longer doable.

0.8.19.0
--------
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ Last, please don't forget to use `safe` templatetag while displaying in template

{{ foobar|safe }}

__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:
__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 nh3 package:

In `forms`,
```python
Expand Down
14 changes: 8 additions & 6 deletions django_summernote/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.db import models
from django.forms import fields

import bleach
from django_summernote.settings import ALLOWED_TAGS, ATTRIBUTES, STYLES
import nh3
from django_summernote.settings import ALLOWED_TAGS, ATTRIBUTES
from django_summernote.widgets import SummernoteWidget

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

def to_python(self, value):
value = super().to_python(value)
return bleach.clean(
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)
return nh3.clean(
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES
)


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

def to_python(self, value):
value = super().to_python(value)
return bleach.clean(
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, styles=STYLES)
return nh3.clean(
value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES
)
12 changes: 4 additions & 8 deletions django_summernote/settings.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
ALLOWED_TAGS = [
ALLOWED_TAGS = {
'a', 'div', 'p', 'span', 'img', 'em', 'i', 'li', 'ol', 'ul', 'strong', 'br',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'table', 'tbody', 'thead', 'tr', 'td',
'abbr', 'acronym', 'b', 'blockquote', 'code', 'strike', 'u', 'sup', 'sub',
]

STYLES = [
'background-color', 'font-size', 'line-height', 'color', 'font-family'
]
}

ATTRIBUTES = {
'*': ['style', 'align', 'title', ],
'a': ['href', ],
'*': {'style', 'align', 'title'},
'a': {'href'},
}
8 changes: 4 additions & 4 deletions django_summernote/test_django_summernote.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ class SimpleForm(forms.Form):
assert url in html
assert 'id="id_foobar"' in html

illegal_tags = '<script></script>'
illegal_tags = '<unknown>dangerous</unknown>'
form_field = SummernoteTextFormField()
cleaned_text = form_field.clean(illegal_tags)
self.assertEqual(cleaned_text, '&lt;script&gt;&lt;/script&gt;')
self.assertEqual(cleaned_text, 'dangerous')

def test_field(self):
from django import forms
Expand All @@ -112,11 +112,11 @@ class Meta:
assert url in html
assert 'id="id_foobar"' in html

illegal_tags = '<script></script>'
illegal_tags = '<unknown>dangerous</unknown>'
model_field = SummernoteTextField()
model_instance = SimpleModel1()
cleaned_text = model_field.clean(illegal_tags, model_instance)
self.assertEqual(cleaned_text, '&lt;script&gt;&lt;/script&gt;')
self.assertEqual(cleaned_text, 'dangerous')

def test_empty(self):
from django import forms
Expand Down
2 changes: 2 additions & 0 deletions django_summernote/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
MEDIA_URL = '/media/'
MEDIA_ROOT = 'test_media'

USE_TZ = True

SECRET_KEY = 'django_summernote'

ROOT_URLCONF = 'django_summernote.urls'
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
django
bleach
nh3
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
description='Summernote plugin for Django',
classifiers=CLASSIFIERS,

install_requires=['django', 'bleach'],
install_requires=['django', 'nh3'],
extras_require={
'dev': [
'django-dummy-plug',
Expand Down

0 comments on commit 8f3ff05

Please sign in to comment.