在.NET中具有特定src属性值的IFrame的Anti XSS Sanitization

时间:2011-06-06 05:06:32

标签: c# .net xss

我希望完成以下操作:使用AntiXSS或AntiSamy库清理WYSIWIG用户输入,但是,允许来自特定域的具有“src”属性的iframe标记。有没有办法实现这个目标?

我正在考虑以某种方式使用正则表达式解析它,并将其与“{iframe-begin}}标记之类的”< iframe“进行交换,之后将其交换为控制器逻辑中的”

谢谢。

1 个答案:

答案 0 :(得分:2)

我还想为我的一个WYSISWIG编辑器进行一些HTML清理。

一个方法是使用Microsoft Anti-Cross Site Scripting Libraryhttp://msdn.microsoft.com/en-us/library/aa973813.aspx

另一种方法是为HTML创建白名单解析器。

以下是我与HTML Agility Pack一起使用的内容,您可以使用标记和允许的属性配置白名单:

public static class HtmlSanitizer     {         private static readonly IDictionary Whitelist;         private static List DeletableNodesXpath = new List();

    static HtmlSanitizer()
    {
        Whitelist = new Dictionary<string, string[]> {
            { "a", new[] { "href" } },
            { "strong", null },
            { "em", null },
            { "blockquote", null },
            { "b", null},
            { "p", null},
            { "ul", null},
            { "ol", null},
            { "li", null},
            { "div", new[] { "align" } },
            { "strike", null},
            { "u", null},                
            { "sub", null},
            { "sup", null},
            { "table", null },
            { "tr", null },
            { "td", null },
            { "th", null }
            };
    }

    public static string Sanitize(string input)
    {
        if (input.Trim().Length < 1)
            return string.Empty;
        var htmlDocument = new HtmlDocument();

        htmlDocument.LoadHtml(input);            
        SanitizeNode(htmlDocument.DocumentNode);
        string xPath = HtmlSanitizer.CreateXPath();

        return StripHtml(htmlDocument.DocumentNode.WriteTo().Trim(), xPath);
    }

    private static void SanitizeChildren(HtmlNode parentNode)
    {
        for (int i = parentNode.ChildNodes.Count - 1; i >= 0; i--)
        {
            SanitizeNode(parentNode.ChildNodes[i]);
        }
    }

    private static void SanitizeNode(HtmlNode node)
    {
        if (node.NodeType == HtmlNodeType.Element)
        {
            if (!Whitelist.ContainsKey(node.Name))
            {
                if (!DeletableNodesXpath.Contains(node.Name))
                {                       
                    //DeletableNodesXpath.Add(node.Name.Replace("?",""));
                    node.Name = "removeableNode";
                    DeletableNodesXpath.Add(node.Name);
                }
                if (node.HasChildNodes)
                {
                    SanitizeChildren(node);
                }                  

                return;
            }

            if (node.HasAttributes)
            {
                for (int i = node.Attributes.Count - 1; i >= 0; i--)
                {
                    HtmlAttribute currentAttribute = node.Attributes[i];
                    string[] allowedAttributes = Whitelist[node.Name];
                    if (allowedAttributes != null)
                    {
                        if (!allowedAttributes.Contains(currentAttribute.Name))
                        {
                            node.Attributes.Remove(currentAttribute);
                        }
                    }
                    else
                    {
                        node.Attributes.Remove(currentAttribute);
                    }
                }
            }
        }

        if (node.HasChildNodes)
        {
            SanitizeChildren(node);
        }
    }

    private static string StripHtml(string html, string xPath)
    {
        HtmlDocument htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
        if (xPath.Length > 0)
        {
            HtmlNodeCollection invalidNodes = htmlDoc.DocumentNode.SelectNodes(@xPath);
            foreach (HtmlNode node in invalidNodes)
            {
                node.ParentNode.RemoveChild(node, true);
            }
        }
        return htmlDoc.DocumentNode.WriteContentTo(); ;
    }

    private static string CreateXPath()
    {
        string _xPath = string.Empty;
        for (int i = 0; i < DeletableNodesXpath.Count; i++)
        {
            if (i != DeletableNodesXpath.Count - 1)
            {
                _xPath += string.Format("//{0}|", DeletableNodesXpath[i].ToString());
            }
            else _xPath += string.Format("//{0}", DeletableNodesXpath[i].ToString());
        }
        return _xPath;
    }
}