0

How to remove unsafe contents of WYSIWYG editors like script tags or iframe tags and events of other tags before use it?

<script>
// Dangerous contents
</script>

<iframe>
// bad web pages
</iframe>

<span onclick="javascript://do bad work here !!!">click me</span>
4

2 回答 2

1

您不应该尝试自己编写此类保护。

特别是,您不应将保护放在客户端(javascript)上,而应使用服务器端过滤,例如http://htmlpurifier.org/

于 2013-07-22T07:29:41.367 回答
-1

我们可以使用像这样的javascript函数来中和

function neutralizeText(text) {
    var i,mtch=text.match(/(<[a-z]+\s+[^>]*\s+|<[a-z]+\s+)on[a-z]+(\s)*=(\b|\s|[^>])*>/gi);
    if(mtch!=null) {
        for(i=0;i<mtch.length;i++) {
            text=text.replace(mtch[i],mtch[i].replace(/\s+on[a-z]+(\s)*=/gi,' data-neutralized='));
        }
    }
    text=text.replace(/<script(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/script>/ig,'');
    text=text.replace(/<iframe(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/iframe>/ig,'');
    text=text.replace(/<object(\b|\s)[^>]*>(([ \t\r\n]|.)*?)<\/object>/ig,'');
    text=text.replace(/<\!\-\-(.*?)\-\->/g,'');
    text=text.replace(/<\!\-\-/g,'');
    return text;
}

但这还不够,您必须在服务器端检查它。因为可以在浏览器中禁用 javascript。

于 2013-07-22T07:15:53.730 回答