Skip to content

Commit fd92ccb

Browse files
committed
utils/html: Remove the hard-coded list of allowed elements and attributes
These changes provide full control over the management of "allowed-elements" and "allowed-attributes" through the configuration file. Fixes #751
1 parent eb35b17 commit fd92ccb

File tree

6 files changed

+31
-50
lines changed

6 files changed

+31
-50
lines changed

CHANGES.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ New Features
1616
Breaking Changes
1717
^^^^^^^^^^^^^^^^
1818

19+
- Provide full control of allowed-elements and allowed-attributes via the configuration
20+
file (`#1007`_, pkvach)
21+
Now the configuration options ``allowed-elements`` and ``allowed-attributes`` are not additional.
22+
That is, they specify the full list of allowed elements and attributes.
23+
1924
- TBD
2025

2126
Bugfixes & Improvements
@@ -27,13 +32,16 @@ Bugfixes & Improvements
2732
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
2833
- Fix W3C Validation issues (`#999`_, pkvach)
2934
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
35+
- Provide full control of allowed-elements and allowed-attributes via the configuration
36+
file (`#1007`_, pkvach)
3037

3138
.. _#951: https://github.com/posativ/isso/pull/951
3239
.. _#967: https://github.com/posativ/isso/pull/967
3340
.. _#983: https://github.com/posativ/isso/pull/983
3441
.. _#995: https://github.com/isso-comments/isso/pull/995
3542
.. _#999: https://github.com/isso-comments/isso/pull/999
3643
.. _#994: https://github.com/isso-comments/isso/pull/994
44+
.. _#1007: https://github.com/isso-comments/isso/pull/1007
3745

3846
0.13.1.dev0 (2023-02-05)
3947
------------------------

contrib/isso-dev.cfg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ reply-to-self = true
3737
[markup]
3838
options = autolink, fenced-code, no-intra-emphasis, strikethrough, superscript
3939
flags =
40-
allowed-elements =
41-
allowed-attributes =
40+
allowed-elements = a, p, hr, br, ol, ul, li, pre, code, blockquote, del, ins,
41+
strong, em, h1, h2, h3, h4, h5, h6, sub, sup, table, thead, tbody, th, td
42+
allowed-attributes = align, href
4243

4344
[hash]
4445
salt = Eech7co8Ohloopo9Ol6baimi

docs/docs/reference/server-config.rst

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -428,37 +428,19 @@ flags
428428
.. versionadded:: 0.12.4
429429

430430
allowed-elements
431-
**Additional** HTML tags to allow in the generated output, comma-separated.
432-
433-
By default, only ``a``, ``blockquote``, ``br``, ``code``, ``del``, ``em``,
434-
``h1``, ``h2``, ``h3``, ``h4``, ``h5``, ``h6``, ``hr``, ``ins``, ``li``,
435-
``ol``, ``p``, ``pre``, ``strong``, ``table``, ``tbody``, ``td``, ``th``,
436-
``thead`` and ``ul`` are allowed.
431+
HTML tags to allow in the generated output, comma-separated.
437432

438433
For a more detailed explanation, see :doc:`/docs/reference/markdown-config`.
439434

440-
.. warning::
441-
442-
This option (together with ``allowed-attributes``) is frequently
443-
misunderstood. Setting e.g. this list to only ``a, blockquote`` will
444-
mean that ``br, code, del, ...`` and all other default allowed tags are
445-
still allowed. You can only add *additional* elements here.
446-
447-
It is planned to change this behavior, see
448-
`this issue <https://github.com/isso-comments/isso/issues/751>`_.
449-
450-
Default: (empty)
435+
Default: ``a, p, hr, br, ol, ul, li, pre, code, blockquote, del, ins, strong, em, h1, h2, h3, h4, h5, h6, sub, sup, table, thead, tbody, th, td``
451436

452437
allowed-attributes
453-
**Additional** HTML attributes (independent from elements) to allow in the
438+
HTML attributes (independent from elements) to allow in the
454439
generated output, comma-separated.
455440

456-
By default, only ``align`` and ``href`` are allowed (same caveats as for
457-
``allowed-elements`` above apply)
458-
459441
For a more detailed explanation, see :doc:`/docs/reference/markdown-config`.
460442

461-
Default: (empty)
443+
Default: ``align, href``
462444

463445
.. note:: To allow images in comments, you need to add
464446
``allowed-elements = img`` and *also* ``allowed-attributes = src``.

isso/isso.cfg

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,13 @@ options = autolink, fenced-code, no-intra-emphasis, strikethrough, superscript
211211
# Per Misaka's defaults, no flags are set.
212212
flags =
213213

214-
# Additional HTML tags to allow in the generated output, comma-separated. By
215-
# default, only a, blockquote, br, code, del, em, h1, h2, h3, h4, h5, h6, hr,
216-
# ins, li, ol, p, pre, strong, table, tbody, td, th, thead and ul are allowed.
217-
allowed-elements =
218-
219-
# Additional HTML attributes (independent from elements) to allow in the
220-
# generated output, comma-separated. By default, only align and href are
221-
# allowed.
222-
allowed-attributes =
214+
# HTML tags to allow in the generated output, comma-separated.
215+
allowed-elements = a, p, hr, br, ol, ul, li, pre, code, blockquote, del, ins,
216+
strong, em, h1, h2, h3, h4, h5, h6, sub, sup, table, thead, tbody, th, td
217+
218+
# HTML attributes (independent from elements) to allow in the generated output,
219+
# comma-separated.
220+
allowed-attributes = align, href
223221

224222

225223
[hash]

isso/tests/test_html.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_github_flavoured_markdown(self):
6060
</code></pre>""")
6161

6262
def test_sanitizer(self):
63-
sanitizer = html.Sanitizer(elements=[], attributes=[])
63+
sanitizer = html.Sanitizer(elements=["p", "a", "code"], attributes=["href"])
6464
examples = [
6565
('Look: <img src="..." />', 'Look: '),
6666
('<a href="http://example.org/">Ha</a>',
@@ -94,8 +94,8 @@ def test_render(self):
9494
"markup": {
9595
"options": "autolink",
9696
"flags": "",
97-
"allowed-elements": "",
98-
"allowed-attributes": ""
97+
"allowed-elements": "a, p",
98+
"allowed-attributes": "href"
9999
}
100100
})
101101
renderer = html.Markup(conf.section("markup")).render
@@ -109,7 +109,7 @@ def test_sanitized_render_extensions(self):
109109
"markup": {
110110
"options": "no_intra_emphasis", # Deliberately snake_case
111111
"flags": "",
112-
"allowed-elements": "",
112+
"allowed-elements": "p",
113113
"allowed-attributes": ""
114114
}
115115
})

isso/utils/html.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,17 @@ def allow_attribute_class(tag, name, value):
1717
return name == "class" and bool(Sanitizer.code_language_pattern.match(value))
1818

1919
def __init__(self, elements, attributes):
20-
# attributes found in Sundown's HTML serializer [1]
21-
# - except for <img> tag, because images are not generated anyways.
22-
# - sub and sup added
23-
#
24-
# [1] https://github.com/vmg/sundown/blob/master/html/html.c
25-
self.elements = ["a", "p", "hr", "br", "ol", "ul", "li",
26-
"pre", "code", "blockquote",
27-
"del", "ins", "strong", "em",
28-
"h1", "h2", "h3", "h4", "h5", "h6", "sub", "sup",
29-
"table", "thead", "tbody", "th", "td"] + elements
20+
self.elements = elements
3021

3122
# allowed attributes for tags
3223
self.attributes = {
33-
"table": ["align"],
34-
"a": ["href"],
35-
"code": Sanitizer.allow_attribute_class,
3624
"*": attributes
3725
}
3826

27+
# If "code" elements are allowed, allow "language-*" CSS classes for syntax highlighting
28+
if "code" in self.elements:
29+
self.attributes["code"] = Sanitizer.allow_attribute_class
30+
3931
def sanitize(self, text):
4032
clean_html = bleach.clean(text, tags=self.elements, attributes=self.attributes, strip=True)
4133

0 commit comments

Comments
 (0)