为了账号安全,请及时绑定邮箱和手机立即绑定

编辑器与html编码问题。

编辑器与html编码问题。

狐的传说 2018-12-07 03:15:35
最近在开发一个项目的时候。需要做留言功能。并且客户要求可以在留言里发表情。就像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> 在页面上。这里还是有的 糊涂。不知道是怎么回事。。高手帮忙解释下。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 286 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信