0

Is there an easy way to remove with php PHP 文字列の任意のタイプのイベント HTML。たとえば、 submit、mouseOut、mouseOver、click、blur、focus などのイベントの場合です。次のような場合に JavaScript インジェクションを防ぐに は: たとえば、 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

4

1 に答える 1

4

この種の問題を解決する最善の方法は、ブラックリストではなくホワイトリストに登録することです。アイデアは、悪いものを除外しようとするのではなく、許可するタグ/属性を定義することです.

これを処理する優れたライブラリは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>
于 2013-01-22T12:48:43.313 に答える