markdown: better input sanitization

This commit is contained in:
Darks 2021-02-23 12:00:34 +01:00
parent 7e7e865430
commit eba1b7dd3b
Signed by: Darks
GPG Key ID: 7515644268BE1433
2 changed files with 10 additions and 9 deletions

View File

@ -6,6 +6,7 @@ from markdown.extensions.footnotes import FootnoteExtension
from markdown.extensions.toc import TocExtension
from app.utils.markdown_extensions.pclinks import PCLinkExtension
from app.utils.markdown_extensions.escape_html import EscapeHtml
@app.template_filter('md')
@ -22,19 +23,12 @@ def md(text):
'sane_lists',
'tables',
CodeHiliteExtension(linenums=True, use_pygments=True),
EscapeHtml(),
FootnoteExtension(UNIQUE_IDS=True),
TocExtension(baselevel=2),
PCLinkExtension(),
]
def escape(text):
text = text.replace("&", "&")
text = text.replace("<", "&lt;")
text = text.replace(">", "&gt;")
return text
# Escape html chars because markdown does not
safe = escape(text)
out = markdown(safe, options=options, extensions=extensions)
out = markdown(text, options=options, extensions=extensions)
return Markup(out)

View File

@ -0,0 +1,7 @@
from markdown.extensions import Extension
class EscapeHtml(Extension):
def extendMarkdown(self, md):
md.preprocessors.deregister('html_block')
md.inlinePatterns.deregister('html')