Is there an easy way to remove with php任何类型的事件HTML中的php字符串。例如,对于events submit、mouseOut、mouseOver、click、blur、focus等用于防止javascript注入的,对于以下情况:是否有一种简单的方法可以用php删除--任何类型的事件HTML --在php中。例如,对于events submit、mouseOut、mouseOver、click、blur、focus等用于防止javascript注入的,如下所示:
$text= 'mi secure html <div id="javascript_injection" onfocus=function(){SomeJavaScriptCode}></div> <p> Im interested in showing the resulting html </p>'
echo $text = 'mi secure html <div id="javascript_injection" > example </div> <b> Im interested in showing the resulting html </b>我也有兴趣展示这个:
'mi secure html <div id="javascript_injection" > example </div> <b> Im interested in showing the resulting html </b>PD:我无法转义所有文本或删除所有标记,因为如果我想在html中显示这些部分。**Imagine you want to show a user creates html to another and want to avoid the injection of javascript*
发布于 2013-01-22 12:48:43
解决这类问题的最好办法是通过白名单而不是黑名单。这样做的想法是定义允许的标记/属性,而不是试图过滤掉不好的东西。
一个处理这个问题的好库是http://htmlpurifier.org/。您可以自定义它,使它允许您想要保留的东西。
require_once '/path/to/HTMLPurifier.auto.php';
$dirty_html = '<a href="test.html" onclick="alert()">Test</a>'
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
echo $clean_html输出:
<a href="test.html">Test</a>https://stackoverflow.com/questions/14458323
复制相似问题