# HG changeset patch # User Mads Kiilerich # Date 2018-10-21 15:44:06 # Node ID 5746cc3b3fa5a1b8735ba914823b44550b406c15 # Parent 2927d568f71403b0257b5855364ee54caf9b34a0 lib: use bleach to sanitize HTML generated from markdown - fix XSS issue when repo front page shows README.md Reported by Bob Hogg . diff --git a/kallithea/lib/markup_renderer.py b/kallithea/lib/markup_renderer.py --- a/kallithea/lib/markup_renderer.py +++ b/kallithea/lib/markup_renderer.py @@ -31,6 +31,7 @@ import logging import traceback import markdown as markdown_mod +import bleach from kallithea.lib.utils2 import safe_unicode, MENTIONS_REGEX @@ -142,28 +143,40 @@ class MarkupRenderer(object): @classmethod def markdown(cls, source, safe=True, flavored=False): """ - Convert Markdown (possibly GitHub Flavored) to HTML, possibly + Convert Markdown (possibly GitHub Flavored) to XSS safe HTML, possibly with "safe" fall-back to plaintext. >>> MarkupRenderer.markdown('''''') - u'

' + u'

' >>> MarkupRenderer.markdown('''''') - u'

' + u'

' >>> MarkupRenderer.markdown('''foo''') u'

foo

' >>> MarkupRenderer.markdown('''''') - u'' + u'<script>alert(1)</script>' >>> MarkupRenderer.markdown('''
yo
''') - u'
yo
' + u'
yo
' >>> MarkupRenderer.markdown('''yo''') - u'

yo

' + u'

yo

' """ source = safe_unicode(source) try: if flavored: source = cls._flavored_markdown(source) markdown_html = markdown_mod.markdown(source, ['codehilite', 'extra']) - return markdown_html + # Allow most HTML, while preventing XSS issues: + # no