首页 新闻 赞助 找找看

编辑器与html编码问题。

0
[已解决问题] 解决于 2012-12-05 14:52

最近在开发一个项目的时候。需要做留言功能。并且客户要求可以在留言里发表情。就像qq 表情那样的。

于是使用了 xheditor 编辑器 。有用怕被加入如 js 脚本。于是 找了如下的代码来进行过滤。

 //删 除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

            //删 除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "delete from", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "drop table", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "truncate", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "asc", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "exec master", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "net localgroup administrators", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, "net user", "", RegexOptions.IgnoreCase);

                    Htmlstring = Htmlstring.Replace("*", "");
                   Htmlstring = Htmlstring.Replace(";", "");
            Htmlstring = Htmlstring.Replace("*/", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
           
           Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

 

结果发现通过编辑器传进去的值中的 <script> 类似这样的标签以及被现实成了。 &amp;lt;script&amp;gt; 这个样子。 而img 标签则保持原样 没有改变 插入的图片 还是

<img src='' >这个样子的。  然而通过这段代码 过滤后并使用HtmlEncode 进行编码后。好像 返回的值 却变成了&lt;script&gt; 这个样子。 然后页面执行的时候 还是会把这个代码当成了 js标签来执行。而不是现实出来。   如果我没采用上面的过滤方法 而是直接 使用 HtmlEncode 进行编码。后。则不会吧代码当成js 来执行 而是会显示成<script> 在页面上。这里还是有的 糊涂。不知道是怎么回事。。高手帮忙解释下。

yzy的主页 yzy | 菜鸟二级 | 园豆:317
提问于:2010-11-20 02:30
< >
分享
最佳答案
0

如果不想JS执行,可以加上<pre>标签,具体用法参考:

http://www.w3school.com.cn/tags/tag_pre.asp

奖励园豆:5
artwl | 专家六级 |园豆:16736 | 2010-11-20 22:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册