How do I filter out Dangerous HTML like SO does? [duplicate]

你。 提交于 2020-01-03 18:51:38

问题


I want to provide an HTML editor on my site, but don't want to open myself up to xss or other attacks that come with allowing user-generated HTML.

This is pretty similar to what Stack Overflow does. How is the HTML checked/sanitized here so that the styling information still remains, while other, more dangerous stuff (like javascript, iframes, etc.) are kept out?

Are there any libraries (preferably in PHP) that already do this?


回答1:


PHP has a function strip_tags that strips HTML and PHP tags from a string, and allows you to specify certain allowable tags. But as @webarto states, there are libraries that do this better.

From the PHP Manual.




回答2:


Your can use

strip_tags($yourData,"<a><p><div><i>") // more tags you want to keep;

If your using SQL too use

mysql_real_escape_string($data);

This is really all you need to not get injected. Do keep in mind, when using mySQL real escape you need to use strip slashes to remove them when you echo them out.

Here are the docs for strip tags and the docs for mysql escape.




回答3:


If you wish to allow some (X)HTML and restrict only tags viewed as unsafe, you can use something like KSES. Wordpress uses a solution like this.

http://sourceforge.net/projects/kses/




回答4:


In addendum to Whymarrh's post, suggestion is to have the code work take place in a subfolder of your site, and auto-alter any code that has "..", or "http://" or any mysql commands.



来源:https://stackoverflow.com/questions/9853239/how-do-i-filter-out-dangerous-html-like-so-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!