从HTML输入中剥离脚本标记 [英] Stripping script tags from HTML input

查看:129
本文介绍了从HTML输入中剥离脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static string MakeWebSafe(this string x){
const string RegexRemove = @(< \s * script [^>]>)| (小于\s * / \s *脚本[^>] *>);
return Regex.Replace(x,RegexRemove,string.Empty,RegexOptions.IgnoreCase);
}

有没有什么理由说这个实现不够好。你能打破它吗?有没有我没有考虑过的事情?如果您使用或使用了不同的东西,它的优点是什么?



我知道这会在文本中留下脚本的正文,但是对于此项目来说没关系。

UPDATE



不要做上面的事情!我最终与此一起去了: HTML敏捷包带标签不在白名单中

您是否考虑过这种情况?

 < scri< script> pt type =text / javascript> 
causehavoc();
< / scr< / script> ipt>

要做的最好的事情是删除所有标记,编码或使用bbcode


public static string MakeWebSafe(this string x) {
    const string RegexRemove = @"(<\s*script[^>]*>)|(<\s*/\s*script[^>]*>)";
    return Regex.Replace(x, RegexRemove, string.Empty, RegexOptions.IgnoreCase);
}

Is there any reason this implementation isn't good enough. Can you break it? Is there anything I haven't considered? If you use or have used something different, what are its advantages?

I'm aware this leaves the body of the script in the text, but that's okay for this project.

UPDATE

Don't do the above! I went with this in the end: HTML Agility Pack strip tags NOT IN whitelist.

解决方案

Have you considered this kind of scenario??

<scri<script>pt type="text/javascript">
    causehavoc();
</scr</script>ipt>

The best thing to do is remove all tags, encode things, or use bbcode

这篇关于从HTML输入中剥离脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆