3

我在<WebMethod>某些页面上定义了一些 PageMethods (页面中的静态方法标记为 ),并使用 ajax 调用来调用它们。如果发送的数据被认为可能是 XSS,那么这个到服务器的 POST 显然不会触发会引发 HttpRequestValidationException 的 ASP.NET 代码,所以我想复制该检查代码以在我的页面方法中运行它。

任何人都知道该代码的详细信息或我在哪里可以找到它?我查看了 MS AntiXss 库,但它只进行编码,而不是实际检查输入,AFAIK。

编辑:或者指出我的代码方向或进行类似检查的库。

4

1 回答 1

3

在引发 System.Web.HttpRequestValidationException 时分析堆栈跟踪,我们可以找出抛出它的代码。

System.Web.HttpRequestValidationException (0x80004005):从客户端检测到潜在危险的 Request.Form 值(IdentifierTextBox="

在 System.Web.HttpRequest.ValidateString(字符串值,字符串 collectionKey,RequestValidationSource requestCollection)

使用 Reflector 我们发现 ValidateString 正在调用:RequestValidator.Current.IsValidRequestString,而后者又调用 CrossSiteScriptingValidation.IsDangerousString,即:

internal static bool IsDangerousString(string s, out int matchIndex)
{
matchIndex = 0;
int startIndex = 0;
while (true)
{
    int num2 = s.IndexOfAny(startingChars, startIndex);
    if (num2 < 0)
    {
        return false;
    }
    if (num2 == (s.Length - 1))
    {
        return false;
    }
    matchIndex = num2;
    char ch = s[num2];
    if (ch != '&')
    {
        if ((ch == '<') && ((IsAtoZ(s[num2 + 1]) || (s[num2 + 1] == '!')) || ((s[num2 + 1] == '/') || (s[num2 + 1] == '?'))))
        {
            return true;
        }
    }
    else if (s[num2 + 1] == '#')
    {
        return true;
    }
    startIndex = num2 + 1;
}
}
于 2011-03-24T16:56:28.867 回答