pytr-markdown对不受信任的输入安全吗?

时间:2011-12-07 01:23:21

标签: python security markdown

Python-Markdown包括转义原始HTML的功能,这些功能显然是为了使其在不受信任的输入上安全,并且一般来说Markdown通常用于呈现用户输入,例如就在SO上。

但这种实施真的值得信赖吗?有没有人在这里研究它以决定在任意输入上运行是否安全?

我看到有Markdown in Django XSS safeSecure Python Markdown Library,但“安全”模式真的很安全吗?

2 个答案:

答案 0 :(得分:5)

据我所知,Python Markdown库似乎是安全的if you use it properly。有关如何安全使用它的详细信息,请参阅链接,但简短版本是:使用最新版本,设置safe_mode和设置enable_attributes=False非常重要。

更新:safe_mode现在应该被弃用,因为它存在安全问题。见https://github.com/Python-Markdown/markdown/commit/7db56daedf8a6006222f55eeeab748e7789fba89。相反,请使用单独的HTML清理程序,例如HTML Purifier。

答案 1 :(得分:0)

您可以使用bleach

import bleach

text = "<a href='https://example.com'>Example</a><script>alert('message');</script>"

sanitized_text = bleach.clean(text,
            tags=['p','a','code','pre','blockquote'],
            attributes={'code': ['class'],'a': ['href']}
)

阅读documentation了解更多信息。