简体   繁体   English

存储HTML时避免XSS攻击

[英]Avoiding XSS attacks when storing HTML

My site stores HTML that is generated by the user.我的网站存储用户生成的 HTML。 Then of course this data is rendered on a web page.然后当然这些数据会呈现在 web 页面上。 What are the best practices for rending the HTML and avoiding XSS attacks?渲染 HTML 和避免 XSS 攻击的最佳实践是什么? Is stripping <script> and <iframe> tags enough?剥离<script><iframe>标签是否足够? Will this cover all browsers?这会涵盖所有浏览器吗? I heard of old browsers rendering HTML from weird encoding... how can I handle this?我听说旧浏览器会通过奇怪的编码呈现 HTML ......我该如何处理?

I would like a general answer, not related to any languages or technologies.我想要一个一般性的答案,与任何语言或技术无关。

You could use libraries like Jsoup especially their whitelist-sanitizer to prevent XSS.您可以使用Jsoup 之类的库,尤其是它们的白名单清理程序来防止 XSS。

In general, I think it is a better/safer aproach to use a white list , rather than filtering black listed tags.一般来说,我认为使用白名单而不是过滤黑名单标签是一种更好/更安全的方法。 Besides, HTML should be avoided in the first place.此外,首先应避免使用 HTML。 Instead, some simple markup, like markdown , should be used.相反,应该使用一些简单的标记,例如markdown

I can assure you there is no way to safely do what you are asking.我可以向你保证,没有办法安全地做你所要求的。 A crafty hacker will always be able to exploit a system that allows arbitrary html from the user, even when cleaned up by stripping away certain tags.狡猾的黑客总是能够利用允许用户任意 html 的系统,即使通过剥离某些标签进行清理也是如此。 This is exactly why tools like markdown and BBcode exist.这正是存在markdownBBcode等工具的原因。 Why look, even StackOverflow is using similar.为什么看,甚至 StackOverflow 也在使用类似的。

The road you are considering traveling down is littered with 10,000 exploited web sites.您正在考虑的道路上散布着 10,000 个被利用的 web 站点。

You're lost when will allow your user do set HTML on your page, there a re wide range of possibilities like onmouseover"myEvilJS" or当您允许您的用户在您的页面上设置 HTML 时,您会迷路,有很多可能性,例如 onmouseover"myEvilJS" 或

Minimum you must convert this symbols.最低限度您必须转换此符号。
& -> &amp; & -> &amp;
< -> &lt; < -> <
> -> &gt; > -> &gt;
“ -> &quot; “->”
' -> &#x27; ' -> &#x27;
/ -> &#x2F; / -> &#x2F;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM