下面是我得到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