这是对“Rubens Farias”回答的回应,其中包含我提出的代码示例。我像这样使用了一个while循环......
while (Regex.IsMatch(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)", RegexOptions.Compiled | RegexOptions.IgnoreCase))
{
returnVal = Regex.Replace(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)",
delegate(Match match)
{
return String.Concat(match.Groups[1].Value, match.Groups[3].Value);
}, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
对于那些感兴趣的人,这是我用来帮助防止 XSS 的整个方法...
/// <summary>
/// 'Helps' protect against XSS (Cross Site Scripting attacks) by stripping out known evil HTML elements
/// such as script and style. Used for outputing text generated by a Rich Text Editor. Doesn't HTML encode!
/// </summary>
/// <param name="input">Input string to strip bad HTML elements from</param>
public static string XSSProtect(string input)
{
string returnVal = input ?? "";
returnVal = Regex.Replace(returnVal, @"\<script(.*?)\>(.*?)\<\/script(.*?)\>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
returnVal = Regex.Replace(returnVal, @"\<style(.*?)\>(.*?)\<\/style(.*?)\>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
while (Regex.IsMatch(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)", RegexOptions.Compiled | RegexOptions.IgnoreCase))
{
returnVal = Regex.Replace(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)",
delegate(Match match)
{
return String.Concat(match.Groups[1].Value, match.Groups[3].Value);
}, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
return returnVal;
}