跨站脚本攻击:protected void RowDataBound(object sender, GridViewRowEventArgs e)

Cross Site Scripting attack: protected void RowDataBound(object sender, GridViewRowEventArgs e)

这是我第一次 Post 在这里:

扫描报告中有 2 个问题。请帮我缓解这个问题:

  1. Xss 攻击:protected void gvMSMQ_RowDataBound(object sender, GridViewRowEventArgs e)**

  2. 信息泄露:lblError.Text = "RowBound - " + errorMessage + "......" + ex.Message

感谢您的帮助。

protected void gvMSMQ_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string Path = string.Empty;
    string errorMessage = "";
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Image img = (Image)e.Row.Cells[0].FindControl("img1");
            Literal ltrl = (Literal)e.Row.FindControl("lit1");
            ltrl.Text = ltrl.Text.Replace("trCollapseGrid", "trCollapseGrid" + e.Row.RowIndex.ToString());
            string str = "trCollapseGrid" + e.Row.RowIndex.ToString();
            e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + str + "','" + img.ClientID + "')");
            e.Row.Cells[0].RowSpan = 1;
            errorMessage = "Two";
            //Path = lstMSMQ[e.Row.RowIndex].Path;
            UCEnvironmentViewerQueueGrid ucQueueGrids = (UCEnvironmentViewerQueueGrid)e.Row.FindControl("ucQueueGrids");
            Classes.MSMQprofile msmqObj = new Classes.MSMQprofile();
            var rowItems = e.Row.DataItem;
            msmqObj = rowItems as Classes.MSMQprofile;

            ucQueueGrids.lstNormalMSMQ = msmqObj.NormalQueueList;
            //ucQueueGrids.lstJournalQueue = msmqObj.JournalQueueList;
            ucQueueGrids.BindControl();
        }
    }
    catch (Exception ex)
    {
        //error on this line!
        lblError.Text = "RowBound - " + errorMessage + "......" + ex.Message;
    }
}

跨站点脚本 (XSS) 是一种注入漏洞。此漏洞允许恶意用户通过未经验证的输入插入他们自己的代码(Javascript、HTML 等)。更多关于 XSS 的信息可以在这里找到:OWASP Guide to XSS

扫描器可能会根据此行发出警报:

e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + str + "','" + img.ClientID + "')");

使用这行代码,您要将 onclick 属性添加到 HTML 元素,然后添加对 OpenTable() 的调用并将 str 作为一部分传递的参数。 str的值来自protected void gvMSMQ_RowDataBound(object sender, GridViewRowEventArgs e)中的e,可能是恶意输入。由于 e 在使用前未经过清理,恶意用户可以使用 e 参数在 onclick 属性值中插入恶意代码。

第二个问题是信息泄露。最佳安全做法是清理错误消息,尽可能少地向潜在攻击者提供信息。错误消息可以揭示有关所用技术或系统工作方式的详细信息。此信息可能对有针对性的攻击有用。

问题可能来自以下代码行:

lblError.Text = "RowBound - " + errorMessage + "......" + ex.Message;

当您打印 ex.Message 时,您可能会暴露可用于攻击的错误详细信息。更好的错误消息会指示发生了问题,但不会透露具体细节。请参阅 OWASP's guide to Error Handling, Auditing, and Logging 以获取指导。