我在這裏做什麼有什麼問題嗎?這是我第一次處理這樣的事情,我只是想確保我理解所有風險等不同的方法。白名單,防止在C#中使用WMD控制XSS
我正在使用WMD獲取用戶輸入,並且我用文字控件顯示它。 因爲它是不可編輯輸入一次,我將存儲HTML而不是降價,
input = Server.HTMLEncode(stringThatComesFromWMDTextArea)
然後運行類似的標籤我希望用戶能夠使用以下。
// Unescape whitelisted tags.
string output = input.Replace("<b>", "<b>").Replace("</b>", "</b>")
.Replace("<i>", "<i>").Replace("</i>", "</i>");
編輯下面是我在做什麼目前:
public static string EncodeAndWhitelist(string html)
{
string[] whiteList = { "b", "i", "strong", "img", "ul", "li" };
string encodedHTML = HttpUtility.HtmlEncode(html);
foreach (string wl in whiteList)
encodedHTML = encodedHTML.Replace("<" + wl + ">", "<" + wl + ">").Replace("</" + wl + ">", "</" + wl + ">");
return encodedHTML;
}
- 請問我在這裏做讓我免受XSS什麼?
- 有沒有其他的考慮 應該做?
- 是否有正常的 標籤到白名單的好名單?
該代碼將不適用於「IMG」標籤,因爲替換「<img>」不允許「src」屬性「 – David 2010-01-20 20:23:56