2012-03-13 71 views
-1
function xap ($in, $format=false) { 
    if ($format == 'html') { 
     //Делаем безопасный html 
    $in = preg_replace('/(<(link|script|iframe|object|applet|embed).*?>[^<]*(<\/(link|script|iframe|object|applet|embed).*?>)?)/i', '', $in); //Удаляем стили, скрипты, фреймы и flash 
    $in = preg_replace('/(script:)|(expression\()/i', '\\1&nbsp;', $in); //Обезвреживаем скрипты, что остались 
    $in = preg_replace('/(onblur|onchange|onclick|ondblclick|onfocus|onkeydown|onkeypress|onkeyup|onload|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|onreset|onselect|onsubmit|onunload)=?/i', '', $in); 
    $in = preg_replace('/((src|href).*?=.*?)(http:\/\/)/i', '\\1redirect/\\2', $in); 
    return $in; 
} else { 
    return htmlentities($in); 
    } 
} 
echo xap($text); //for read 
echo xap($text, "html"); //for read html tags 

作者说这是保护XSS的理想代码......是真的吗?保护XSS(理想功能)?

+3

没有“理想代码”这样的东西。有很多令人讨厌的方法来规避这种XSS过滤器,包括滥用鲜为人知的编码,如UTF-7。我会寻找一个公开记录的,知名的同行评议过的图书馆,而不是信任一位作者提供“理想的代码”。 – 2012-03-13 10:00:39

回答

0

htmlentities()将保护您免受XSS。

您的代码看起来像您还想要允许某些HTML代码。您仍然应该运行htmlentities(),然后运行str_replace()以允许您需要的某些html标记。

0

你已经忘记的onerror。 => <img src="http://idontexists.neke/notexists.jpg" onerror="alert(document.cookie)" />