Skip to content

Commit ad661f7

Browse files
author
Feanil Patel
authored
Merge pull request #308 from openedx/feanil/update_bleach
feat!: Update to the latest version of bleach.
2 parents 831cea1 + db6130a commit ad661f7

File tree

5 files changed

+19
-72
lines changed

5 files changed

+19
-72
lines changed

Changelog.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
Drag and Drop XBlock changelog
22
==============================
33

4+
Version 3.1.0 (2023-01-31)
5+
--------------------------
6+
7+
* Upgrade to be compatible with `bleach==6.0.0` and `bleach<6.0.0`
8+
* Make the dependency on the `css` extras explicit.
9+
410
Version 3.0.0 (2022-11-18)
511
---------------------------
612

7-
* Sanitize HTML tags to prevent XSS vulnerabilities.
8-
BREAKING CHANGE: Disallowed HTML tags (e.g. `<script>`) will no longer be rendered in LMS and Studio.
13+
* Sanitize HTML tags to prevent XSS vulnerabilities.
14+
15+
BREAKING CHANGE: Disallowed HTML tags (e.g. `<script>`) will no longer be rendered in LMS and Studio.
916

1017
Version 2.7.0 (2022-11-15)
1118
---------------------------
@@ -20,7 +27,7 @@ Version 2.6.0 (2022-10-24)
2027
Version 2.5.0 (2022-10-13)
2128
---------------------------
2229

23-
* Make the "Show Answer" condition customizable (like in the Problem XBlock).
30+
* Make the "Show Answer" condition customizable (like in the Problem XBlock).
2431

2532
Version 2.4.2 (2022-10-13)
2633
---------------------------

drag_and_drop_v2/utils.py

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88

99
import bleach
1010

11-
try:
12-
from bleach.css_sanitizer import CSSSanitizer
13-
except (ImportError, ModuleNotFoundError):
14-
# pylint: disable=fixme
15-
# TODO: The bleach library changes the way CSS Styles are cleaned in version 5.0.0. The edx-platform uses version
16-
# 4.1.0 in Maple, so this import is handled within a try block. This can be removed for the Nutmeg release.
17-
CSSSanitizer = None
11+
from bleach.css_sanitizer import CSSSanitizer
1812

1913

2014
def _(text):
@@ -37,7 +31,8 @@ def _clean_data(data):
3731
return cleaned_text
3832

3933

40-
ALLOWED_TAGS = bleach.ALLOWED_TAGS + [
34+
# Convert `bleach.ALLOWED_TAGS` to a set because it is a list in `bleach<6.0.0`.
35+
ALLOWED_TAGS = set(bleach.ALLOWED_TAGS) | {
4136
'br',
4237
'caption',
4338
'dd',
@@ -68,7 +63,7 @@ def _clean_data(data):
6863
'thead',
6964
'tr',
7065
'u',
71-
]
66+
}
7267
ALLOWED_ATTRIBUTES = {
7368
'*': ['class', 'style', 'id'],
7469
'a': ['href', 'title', 'target', 'rel'],
@@ -79,54 +74,6 @@ def _clean_data(data):
7974
'table': ['border', 'cellspacing', 'cellpadding'],
8075
'td': ['style', 'scope'],
8176
}
82-
ALLOWED_STYLES = [
83-
"azimuth",
84-
"background-color",
85-
"border-bottom-color",
86-
"border-collapse",
87-
"border-color",
88-
"border-left-color",
89-
"border-right-color",
90-
"border-top-color",
91-
"clear",
92-
"color",
93-
"cursor",
94-
"direction",
95-
"display",
96-
"elevation",
97-
"float",
98-
"font",
99-
"font-family",
100-
"font-size",
101-
"font-style",
102-
"font-variant",
103-
"font-weight",
104-
"height",
105-
"letter-spacing",
106-
"line-height",
107-
"overflow",
108-
"pause",
109-
"pause-after",
110-
"pause-before",
111-
"pitch",
112-
"pitch-range",
113-
"richness",
114-
"speak",
115-
"speak-header",
116-
"speak-numeral",
117-
"speak-punctuation",
118-
"speech-rate",
119-
"stress",
120-
"text-align",
121-
"text-decoration",
122-
"text-indent",
123-
"unicode-bidi",
124-
"vertical-align",
125-
"voice-family",
126-
"volume",
127-
"white-space",
128-
"width",
129-
]
13077

13178

13279
def sanitize_html(raw_body: str) -> str:
@@ -139,14 +86,7 @@ def sanitize_html(raw_body: str) -> str:
13986
strip=True,
14087
attributes=ALLOWED_ATTRIBUTES,
14188
)
142-
if CSSSanitizer:
143-
bleach_options['css_sanitizer'] = CSSSanitizer()
144-
else:
145-
# pylint: disable=fixme
146-
# TODO: This is maintaining backward compatibility with bleach 4.1.0 used in Maple release of edx-platform.
147-
# This can be removed for the Nutmeg release which uses bleach 5.0.0
148-
bleach_options['styles'] = ALLOWED_STYLES
149-
89+
bleach_options['css_sanitizer'] = CSSSanitizer()
15090
return bleach.clean(raw_body, **bleach_options)
15191

15292

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ edx-i18n-tools==0.4.7
44
pycodestyle==2.6.0
55
django-statici18n==1.9.0
66
transifex-client==0.14.2
7-
bleach==3.3.1
7+
bleach[css]==6.0.0
88
xblock-utils==1.2.2
99
selenium==2.53.6
1010
pylint==2.4.2

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def package_data(pkg, root_list):
2323

2424
setup(
2525
name='xblock-drag-and-drop-v2',
26-
version='3.0.0',
26+
version='3.1.0',
2727
description='XBlock - Drag-and-Drop v2',
2828
packages=['drag_and_drop_v2'],
2929
install_requires=[
3030
'XBlock',
3131
'xblock-utils',
32-
'bleach',
32+
'bleach[css]',
3333
],
3434
entry_points={
3535
'xblock.v1': 'drag-and-drop-v2 = drag_and_drop_v2:DragAndDropBlock',

tests/integration/test_custom_data_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_items_rendering(self):
2222
self.assertEqual(len(items), 3)
2323
self.assertIn('<b>1</b>', self.get_element_html(items[0]))
2424
self.assertIn('<i>2</i>', self.get_element_html(items[1]))
25-
self.assertIn('<span style="color: red;">X</span>', self.get_element_html(items[2]))
25+
self.assertIn('<span style="color:red;">X</span>', self.get_element_html(items[2]))
2626

2727
def test_html_title_renders_properly(self):
2828
"""

0 commit comments

Comments
 (0)