2012-03-13 111 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)" />