我在这里做的事情有什么问题吗?这是我第一次处理这样的事情,我只是想确保我了解不同方法的所有风险等。
我正在使用 WMD 获取用户输入,并使用文字控件显示它。由于一旦输入就无法编辑,我将存储 HTML 而不是 Markdown,
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的侵害吗?
- 是否还有其他需要考虑的因素?
- 是否有一个很好的正常标签列表来列入白名单?