You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2025/08/20 15:44:22 UTC
(allura) 02/02: allow certain classes and ids, sanitize others
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch db/fix_markdown_cleaning
in repository https://gitbox.apache.org/repos/asf/allura.git
commit 0c42e7483d5c8356876443d95aed1356325d32d4
Author: Dave Brondsema <db...@slashdotmedia.com>
AuthorDate: Wed Aug 20 11:43:39 2025 -0400
allow certain classes and ids, sanitize others
---
Allura/allura/lib/utils.py | 34 +++++++++++++++++++++++++++++--
Allura/allura/public/nf/js/allura-base.js | 5 +++++
Allura/allura/tests/test_globals.py | 4 ++--
3 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 26126bf83..7955d3891 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -555,12 +555,27 @@ def __init__(self, *args, **kwargs):
# srcset is used in our own project_list/project_summary widgets
# which are used as macros so go through markdown
- excluded_attributes = {(None, 'class'), (None, 'id')}
- self.allowed_attributes = (html5lib.filters.sanitizer.allowed_attributes | {(None, 'srcset')}) - excluded_attributes
+ self.allowed_attributes = (html5lib.filters.sanitizer.allowed_attributes | {(None, 'srcset')})
self.valid_iframe_srcs = ('https://www.youtube.com/embed/',
'https://www.youtube-nocookie.com/embed/',
)
+
+ self.valid_class_values = {
+ 'markdown_content', # our wrapper class
+ # standard extensions:
+ 'codehilite', 'footnote', 'checklist', 'toc', 'footnote-ref', 'footnote-backref',
+ 'alink', # ForgeLinkPattern
+ 'user-mention', # UserMentionExtension
+ # our macros:
+ 'neighborhood_feed_entry', 'md-users-list', 'md-users-list-more',
+ # codehilite classes:
+ 'hll', 'c', 'k', 'o', 'cm', 'cp', 'c1', 'cs', 'gd', 'ge', 'gr', 'gh', 'gi', 'go', 'gp', 'gs', 'gu', 'gt', 'kc', 'kd', 'kn', 'kp', 'kr', 'kt', 'm', 's', 'na', 'nb', 'nc', 'no', 'nd', 'ni', 'ne', 'nf', 'nl', 'nn', 'nt', 'nv', 'ow', 'w', 'mf', 'mh', 'mi', 'mo', 'sb', 'sc', 'sd', 's2', 'se', 'sh', 'si', 'sx', 'sr', 's1', 'ss', 'bp', 'vc', 'vg', 'vi', 'il', 'code_block', 'lineno',
+ }
+ self.valid_id_prefixes = {
+ 'h:', # see toc_slugify_with_prefix
+ 'fn:', 'fnref:', # from footnotes extension
+ }
self._prev_token_was_ok_iframe = False
def sanitize_token(self, token):
@@ -585,6 +600,21 @@ def sanitize_token(self, token):
elif token.get('type') == "EndTag" and self._prev_token_was_ok_iframe:
self.allowed_elements.add(iframe_el)
+ # sanitize classes and ids
+ if token.get('type') == 'StartTag':
+ classes = token.get('data', {}).get((None, 'class'), '')
+ if classes:
+ classes = classes.split()
+ cleaned_classes = [c for c in classes if c in self.valid_class_values]
+ if cleaned_classes != classes:
+ log.info(f'Removed invalid classes: {classes} => {cleaned_classes}')
+ token['data'][(None, 'class')] = ' '.join(cleaned_classes)
+
+ id = token.get('data', {}).get((None, 'id'), '')
+ if id:
+ if not any(id.startswith(prefix) for prefix in self.valid_id_prefixes):
+ token['data'][(None, 'id')] = 'user-content-' + id
+
self._prev_token_was_ok_iframe = ok_opening_iframe
if token.get('name') == 'input':
diff --git a/Allura/allura/public/nf/js/allura-base.js b/Allura/allura/public/nf/js/allura-base.js
index d41bd5bd7..7a8b15cdd 100644
--- a/Allura/allura/public/nf/js/allura-base.js
+++ b/Allura/allura/public/nf/js/allura-base.js
@@ -245,6 +245,11 @@ $(function(){
var new_hash = window.location.hash.replace('#', '#h:');
if (document.querySelector(new_hash)) {
window.location.hash = new_hash;
+ } else {
+ new_hash = window.location.hash.replace('#h:', '#user-content-');
+ if (document.querySelector(new_hash)) {
+ window.location.hash = new_hash;
+ }
}
}
});
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index ba04c13ce..2ae86f4c0 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -606,8 +606,8 @@ def test_markdown_extremely_slow(self):
assert True # finished!
def test_macro_include(self):
- r = g.markdown.convert('[[include ref=Home id=foo]]')
- assert '<div id="foo">' in r, r
+ r = g.markdown.convert('[[include ref=Home id=foo class=modal data-a=b]]')
+ assert '<div class="" id="user-content-foo">' in r, r
assert 'href="../foo"' in g.markdown.convert('[My foo](foo)')
assert 'href="..' not in g.markdown.convert('[My foo](./foo)')