Class YmlSafeLoader

java.lang.Object
cloud.opencode.base.yml.security.YmlSafeLoader

public final class YmlSafeLoader extends Object
YAML Safe Loader - Provides secure YAML loading YAML 安全加载器 - 提供安全的 YAML 加载

This class provides utilities for safely loading YAML without arbitrary code execution risks.

此类提供安全加载 YAML 的工具,避免任意代码执行风险。

Features | 主要功能:

  • Configurable allowed types and denied tags - 可配置的允许类型和拒绝标签
  • Maximum depth, size, and alias limits - 最大深度、大小和别名限制
  • Cyclic reference detection - 循环引用检测

Usage Examples | 使用示例:

// Configure allowed types
YmlSafeLoader loader = YmlSafeLoader.builder()
    .allowType(MyClass.class)
    .maxDepth(100)
    .maxSize(1_000_000)
    .build();

// Validate data
loader.validate(data);

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构建后不可变)
  • Null-safe: No (null data skips validation) - 空值安全: 否(空数据跳过验证)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static YmlSafeLoader create()
      Creates a default safe loader. 创建默认的安全加载器。
      Returns:
      a safe loader | 安全加载器
    • builder

      public static YmlSafeLoader.Builder builder()
      Creates a builder for safe loader. 创建安全加载器的构建器。
      Returns:
      a new builder | 新构建器
    • validate

      public void validate(Object data)
      Validates that data is safe. 验证数据是否安全。
      Parameters:
      data - the data to validate | 要验证的数据
      Throws:
      YmlSecurityException - if data is not safe | 如果数据不安全
    • isAllowedType

      public boolean isAllowedType(Class<?> type)
      Checks if a type is allowed. 检查类型是否被允许。
      Parameters:
      type - the type to check | 要检查的类型
      Returns:
      true if allowed | 如果允许则返回 true
    • isDeniedTag

      public boolean isDeniedTag(String tag)
      Checks if a tag is denied. 检查标签是否被拒绝。
      Parameters:
      tag - the tag to check | 要检查的标签
      Returns:
      true if denied | 如果被拒绝则返回 true
    • getMaxDepth

      public int getMaxDepth()
      Gets the maximum depth. 获取最大深度。
      Returns:
      the max depth | 最大深度
    • getMaxSize

      public int getMaxSize()
      Gets the maximum size. 获取最大大小。
      Returns:
      the max size | 最大大小
    • getMaxAliases

      public int getMaxAliases()
      Gets the maximum aliases. 获取最大别名数。
      Returns:
      the max aliases | 最大别名数