ASP.NET潜在危险请求Javascript正则表达式

Mr *_*rok 5 javascript regex asp.net validation

什么是Microsoft .NET Framework用于执行标准验证的正则表达式,该标准验证导致HttpRequestValidationException"当发布HTML或其他可能不安全的内容时,从客户端检测到潜在危险的Request.Form值".

我希望它的精确副本转换为JavaScript,以便提前通知用户.

我当前的正则表达式(/(&#)| <[^ <>] +> /)很接近,但与.NET不同.

我知道这对于不同的.NET版本可能会有所不同,所以我特别想知道:

  • .NET 2的正则表达式
  • .NET 4的正则表达式

Ant*_*ula 5

您可以使用一些反编译工具,亲眼看看根本没有正则表达式。它调用静态方法CrossSiteScriptingValidation.IsDangerousString

但也许您可以使用 Microsoft AntiXSS 库来实现相同的目标。无论如何这里是方法:

internal static bool IsDangerousString(string s, out int matchIndex)
{
    matchIndex = 0;
    int num1 = 0;
    int num2 = s.IndexOfAny(CrossSiteScriptingValidation.startingChars, num1);
    if (num2 < 0)
    {
        return false;
    }
    if (num2 == s.Length - 1)
    {
        return false;
    }
    matchIndex = num2;
    char chars = s.get_Chars(num2);
    if ((chars == 38 || chars == 60) && (CrossSiteScriptingValidation.IsAtoZ(s.get_Chars(num2 + 1)) || s.get_Chars(num2 + 1) == 33 || s.get_Chars(num2 + 1) == 47 || s.get_Chars(num2 + 1) == 63))
    {
        return true;
    }
    else
    {
        if (s.get_Chars(num2 + 1) == 35)
        {
            return true;
        }
    }
    num1 = num2 + 1;
}
Run Code Online (Sandbox Code Playgroud)