-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 的理想代码......是真的吗?

4

3 回答 3

0

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

于 2013-05-23T05:14:14.403 回答
0

htmlentities()将保护您免受 XSS 攻击。

您的代码看起来还想允许一些 HTML 代码。您仍然应该运行htmlentities()并在此运行之后str_replace()允许您需要一些 html 标记。

于 2012-03-13T11:35:07.033 回答
0
htmlspecialchars($str, ENT_QUOTES, 'UTF-8');

应该足够安全

http://php.net/manual/en/function.htmlspecialchars.php

于 2012-03-13T12:52:14.963 回答