下面是我得到checkmarx报告的代码,说明它容易存储XSS.it,说数据层从dt数据库中获取数据。然后,此元素的值将流经代码,而不会被正确过滤或编码为 ,最终会在aspx页面中显示给用户。防止asp.net中的跨站点脚本攻击C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_OnRowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" Width ="1000px" class="grid">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:TextBox> //this is the line vulnerable to XSS
</EditItemTemplate>
</asp:TemplateField>
</Columns>
代码背后
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter("Select Uid,Uname,Utype,Uemail,ClientName,ProjectName,Ulog from usrtable where [email protected] and [email protected], con);
adapt.SelectCommand.Parameters.AddWithValue("@clientname", clientname);
adapt.SelectCommand.Parameters.AddWithValue("@Normal", "Normal");
adapt.Fill(dt);
con.Close();
}
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
我应该编码所有这些正在传递到项目模板或者是它的代码容易受到任何其他行的列值。如果它的html编码,我该如何实现它。请通过这个问题指导我。
不要字符串concat你的sql查询! –
@ DanielA.White您忘记了这个https://xkcd.com/327/ ;-) – Orangesandlemons
在您的文本框中输入以下内容:'Liam OR 1 = 1' .... O – Liam