c# - .NET 中具有特定 src 属性值的 IFrame 的反 XSS 清理

标签 c# .net xss

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

我正在考虑以某种方式用 Regex 对其进行解析,并将其换出“< iframe”以替换为 {{iframe-begin}} 标记之类的东西,然后在 Controller 逻辑中将其换出“

谢谢。

最佳答案

我还想为我的一位 WYSISWIG 编辑器做一些 HTML 清理。

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

另一个是为 HTML 创建一个白名单解析器。

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

公共(public)静态类 HtmlSanitizer { 私有(private)静态只读 IDictionary 白名单; 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;
    }
}

关于c# - .NET 中具有特定 src 属性值的 IFrame 的反 XSS 清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247969/

相关文章:

c# - 任何人都知道 IL/CLR 如何准确生成局部函数 C#7

c# - 有没有办法指定自定义依赖属性的默认 ValidatesOnDataErrors?

c# - 如果它是空的 ('' ) 字符串,则忽略属性来自 Json/Api Response C#

javascript - 阻止对另一个域的请求?

URL 中使用的 Javascript

c# - 使用 src-test 目录布局容器化 ASP.NET Core API

c# - Microsoft JScript 运行时错误 : 'Sys' is undefined (in mvc3 c# razor)

c# - 在 C# 中调试 DLLImport

c# - 避免外部代码中的属性歧义

javascript - 如何使用 easyXDM 发起跨站 POST 请求?