当我需要从所见即所得编辑器渲染 HTML 时,如何防止 XSS 攻击?
非技术背景信息:我在一所学校工作,我们正在使用 Django 构建一个新网站。学校的老师在技术上没有足够的能力使用另一种标记语言,例如 MarkDown。我们最终决定应该使用所见即所得编辑器,这会带来安全缺陷。我们不太担心老师本身,而是更担心恶意的学生可能会得到老师的凭证。
技术背景信息:我们正在使用 Django 1.3 运行,尚未选择特定的编辑器。我们倾向于使用 JavaScript,例如 TINYMCE,但可以说服使用任何允许安全性和易用性的东西。因为所见即所得编辑器将输出要渲染到文档中的 HTML,所以我们不能简单地转义它。
防止恶意代码同时又能让非技术教师轻松撰写帖子的最佳方法是什么?
Non-Technical Background info: I am working for a school and we are building a new website using Django. The teachers that work for the school aren't technologically competent enough to use another MarkUp language such as MarkDown. We eventually decided that we should use a WYSIWYG editor, which poses security flaws. We aren't too worried about the teachers themselves, but more malicious students that might get the teacher's credentials.
Technical Background info: We are running using Django 1.3 and have not chosen a specific editor yet. We are leaning towards a javascript one such as TINYMCE, but can be persuaded to use anything that allows security and ease of use. Because the WYSIWYG editor will output HTML to be rendered into the document, we cannot simply escape it.
What is the best way to prevent malicious code while still making it easy for non-technical teachers to write posts?
发布评论
评论(4)
虽然已经晚了,但您可以尝试 Bleach,它在幕后使用 html5lib,您还可以获得标签平衡。
这是一个完整的片段:
settings.py
app/forms.py
您可以阅读 bleach 文档,以便您可以根据自己的需要进行调整。
This is late, but you can try Bleach, under the hood it uses the html5lib, and you'll also get tag balancing.
Here is a complete snippet:
settings.py
app/forms.py
You can read the bleach docs, so you can adapt it to your needs.
您需要在服务器上解析 HTML 并删除任何不符合严格白名单的标签和属性。
您应该将其解析(或至少重新呈现)为严格的 XML,以防止攻击者利用模糊解析器之间的差异。
白名单不得包含
、