我正试图保护我的网站免受跨站脚本攻击(XSS),我正在考虑使用正则表达式来验证用户输入。
这是我的问题:我有一个危险的HTML标签列表...
<applet>
<body>
<embed>
<frame>
<script>
<frameset>
<html>
<iframe>
<img>
<style>
<layer>
<link>
<ilayer>
<meta>
<object>...and我想把它们包含在正则表达式中--这可能吗?如果不是,我应该使用什么?你有任何想法如何实现这样的东西吗?
发布于 2013-03-23 03:27:57
请通读OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet以获取大量信息。黑名单标签并不是一种非常有效的方法,而且会留下空白。您应该过滤输入,在输出到浏览器之前进行清理,对HTML实体进行编码,以及我的链接中讨论的各种其他技术。
发布于 2015-05-06 22:35:58
public static bool ValidateAntiXSS(string inputParameter)
{
if (string.IsNullOrEmpty(inputParameter))
return true;
// Following regex convers all the js events and html tags mentioned in followng links.
//https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
//https://msdn.microsoft.com/en-us/library/ff649310.aspx
var pattren = new StringBuilder();
//Checks any js events i.e. onKeyUp(), onBlur(), alerts and custom js functions etc.
pattren.Append(@"((alert|on\w+|function\s+\w+)\s*\(\s*(['+\d\w](,?\s*['+\d\w]*)*)*\s*\))");
//Checks any html tags i.e. <script, <embed, <object etc.
pattren.Append(@"|(<(script|iframe|embed|frame|frameset|object|img|applet|body|html|style|layer|link|ilayer|meta|bgsound))");
return !Regex.IsMatch(System.Web.HttpUtility.UrlDecode(inputParameter), pattren.ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}发布于 2013-03-23 03:32:18
您应该将字符串编码为HTML。使用dotNET方法
HttpUtils.HtmlEncode(string text)这里有更多详细信息http://msdn.microsoft.com/en-us/library/73z22y6h.aspx
https://stackoverflow.com/questions/15578338
复制相似问题