using Ganss.Xss;

namespace Dpz.Core.Web.Models;

public class CrossSiteRule
{
    public Allowed? Tags { get; set; }

    public Allowed? Attributes { get; set; }

    public Allowed? CssProperties { get; set; }

    public IHtmlSanitizer CustomHtmlSanitizer()
    {
        var sanitizer = new HtmlSanitizer();
        if (Attributes?.Add?.Length > 0)
        {
            Attributes.Add.ForEach(x => sanitizer.AllowedAttributes.Add(x));
        }

        if (Attributes?.Remove?.Length > 0)
        {
            Attributes.Remove.ForEach(x => sanitizer.AllowedAttributes.Remove(x));
        }

        if (Tags?.Add?.Length > 0)
        {
            Tags.Add.ForEach(x => sanitizer.AllowedTags.Add(x));
        }

        if (Tags?.Remove?.Length > 0)
        {
            Tags.Remove.ForEach(x => sanitizer.AllowedTags.Remove(x));
        }

        if (CssProperties?.Add?.Length > 0)
        {
            CssProperties.Add.ForEach(x => sanitizer.AllowedCssProperties.Add(x));
        }

        if (CssProperties?.Remove?.Length > 0)
        {
            CssProperties.Remove.ForEach(x => sanitizer.AllowedAttributes.Remove(x));
        }
        return sanitizer;
    }
}

public class Allowed
{
    public string[]? Add { get; set; }

    public string[]? Remove { get; set; }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

这是一个用于配置HTML内容安全性(防止XSS跨站脚本攻击)的类库代码。让我详细解释一下:

主要功能

这个代码定义了一个灵活的HTML清理器配置系统,用于控制哪些HTML标签、属性和CSS属性是被允许的。

类结构分析

CrossSiteRule 类

这是主要的配置类,包含三个可选属性:

  • Tags: 控制允许/禁用的HTML标签
  • Attributes: 控制允许/禁用的HTML属性
  • CssProperties: 控制允许/禁用的CSS属性

Allowed 类

这是一个通用的配置类,包含两个字符串数组:

  • Add: 要添加到允许列表的项目
  • Remove: 要从允许列表中移除的项目

CustomHtmlSanitizer() 方法详解

这个方法创建并配置一个自定义的HTML清理器:

public IHtmlSanitizer CustomHtmlSanitizer()
{
    var sanitizer = new HtmlSanitizer();
    
    // 添加/移除允许的HTML属性
    if (Attributes?.Add?.Length > 0)
        Attributes.Add.ForEach(x => sanitizer.AllowedAttributes.Add(x));
    if (Attributes?.Remove?.Length > 0)
        Attributes.Remove.ForEach(x => sanitizer.AllowedAttributes.Remove(x));
    
    // 添加/移除允许的HTML标签
    if (Tags?.Add?.Length > 0)
        Tags.Add.ForEach(x => sanitizer.AllowedTags.Add(x));
    if (Tags?.Remove?.Length > 0)
        Tags.Remove.ForEach(x => sanitizer.AllowedTags.Remove(x));
    
    // 添加/移除允许的CSS属性
    if (CssProperties?.Add?.Length > 0)
        CssProperties.Add.ForEach(x => sanitizer.AllowedCssProperties.Add(x));
    if (CssProperties?.Remove?.Length > 0)
        CssProperties.Remove.ForEach(x => sanitizer.AllowedAttributes.Remove(x)); // ⚠️ 这里有bug
    
    return sanitizer;
}

发现的问题

在最后一个条件块中存在一个错误:

CssProperties.Remove.ForEach(x => sanitizer.AllowedAttributes.Remove(x));

应该改为:

CssProperties.Remove.ForEach(x => sanitizer.AllowedCssProperties.Remove(x));

使用场景

这个类通常用于:

  1. 内容管理系统 - 允许用户输入富文本但需要过滤危险内容
  2. 博客/论坛系统 - 清理用户发布的HTML内容
  3. 邮件系统 - 安全地显示HTML邮件内容
  4. 任何需要处理不可信HTML输入的场景

使用示例

var rule = new CrossSiteRule
{
    Tags = new Allowed 
    { 
        Add = new[] { "video", "audio" },
        Remove = new[] { "script", "iframe" }
    },
    Attributes = new Allowed
    {
        Add = new[] { "data-custom" },
        Remove = new[] { "onclick", "onload" }
    }
};

var sanitizer = rule.CustomHtmlSanitizer();
string cleanHtml = sanitizer.Sanitize(userInputHtml);

这样的设计提供了很好的灵活性,允许开发者根据具体需求精确控制HTML内容的安全策略。

评论加载中...