1
A
回答
2
NullUserException答案是完美的,我做了一點點擴展方法來做到這一點,我在這裏發帖,如果別人需要。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Extenders
{
public static class StringExtender
{
internal static void ParseHtmlDocument(XmlDocument doc, XmlNode root, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
{
XmlNodeList nodes;
if (root == null) root = doc.ChildNodes[0];
nodes = root.ChildNodes;
foreach (XmlNode node in nodes)
{
if (!(allowedTags.Any(x => x.ToLower() == node.Name.ToLower())))
{
var safeNode = doc.CreateTextNode(node.InnerText);
root.ReplaceChild(safeNode, node);
}
else
{
if (node.Attributes != null)
{
var attrList = node.Attributes.OfType<XmlAttribute>().ToList();
foreach (XmlAttribute attr in attrList)
{
if (!(allowedAttributes.Any(x => x.ToLower() == attr.Name)))
{
node.Attributes.Remove(attr);
}
// TODO: if style is allowed, check the allowed keys: values
}
}
}
if (node.ChildNodes.Count > 0)
ParseHtmlDocument(doc, node, allowedTags, allowedAttributes, allowedStyleKeys);
}
}
public static string ParseSafeHtml(this string input, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys)
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<span>" + input + "</span>");
ParseHtmlDocument(xmlDoc, null, allowedTags, allowedAttributes, allowedStyleKeys);
string result;
using (var sw = new StringWriter())
{
using (var xw = new XmlTextWriter(sw))
xmlDoc.WriteTo(xw);
result = sw.ToString();
}
return result.Substring(6, result.Length - 7);
}
}
}
要使用:
var x = "<b>allowed</b><b class='text'>allowed attr</b><b id='5'>not allowed attr</b><i>not all<b>o</b>wed tag</i>".ParseSafeHtml((new string[] { "b", "#text" }), (new string[] { "class" }), (new string[] { }));
,輸出:
<b>allowed</b><b class='text'>allowed attr</b><b>not allowed attr</b>not allowed tag
如果元素是不允許的,將得到的innerText並拉出標籤,清除所有內部標籤。
7
+1
我喜歡這篇文章。我從不厭倦它。 – kbrimington 2010-08-10 18:53:10
+0
完美,那正是我所需要的。 – BrunoLM 2010-08-10 19:20:59
0
您可以使用MS AntiXSS庫消毒潛在的可執行HTML。它到這裏看看:
相關問題
- 1. 剝離HTML標籤在Magento
- 2. 嵌套的HTML表標籤剝離
- 3. 剝離不完整的HTML標籤
- 4. 防止HTML標籤剝離
- 5. PHP DOMDocument剝離HTML標籤
- 6. 正則表達式剝離HTML標籤
- 7. 剝離HTML標籤而不使用HtmlAgilityPack
- 8. 在Rails中剝離標籤
- 9. 使用tinyMCE剝離的HTML標籤
- 10. Codeigniter從輸入中剝離HTML標籤
- 11. 剝離span標籤
- 12. 索引CJK和剝離HTML標籤
- 13. C#Internet Explorer和剝離HTML標籤
- 14. Angular從HTML標籤剝離屬性
- 15. 允許一些html標籤的標籤剝離 - Facebook-ish
- 16. 剝離Python字符串中某些html標籤的最快方法是什麼?
- 17. 如何在Perl中剝離帶有ID的html標籤?
- 18. SimpleXML - 剝離CDATA標籤(或嵌入XML中的HTML標記)
- 19. 使用Feedzilla剝離Rss提要中的Html標籤
- 20. 熊貓:麻煩從DataFrame剝離HTML標籤列
- 21. NSRegularExpression剝離HTML標記
- 22. 剝離CData的標籤XML的Perl
- 23. 正則表達式剝離HTML標籤內容有條件
- 24. 按鈕上的標籤剝離效果
- 25. PHP:將標籤剝離array_values()內的值
- 26. TinyMCE剝離HTML
- 27. XmlNodes剝離HTML
- 28. 來自HTML的JQuery字數和剝離標籤
- 29. 記錄剝離html標籤的計數長度
- 30. IE中的jQuery表單插件響應剝離標籤
[RegEx match open tags not except XHTML self-contained tags]可能重複(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – 2010-08-10 19:04:31
這到底是什麼問題?怎麼可能與我的問題有任何關係?你可以解釋嗎? – BrunoLM 2010-08-10 19:20:26