Class SafePathUtil
java.lang.Object
cloud.opencode.base.image.security.SafePathUtil
Safe Path Util
安全路径工具类
Utilities for validating and sanitizing file paths for image operations.
用于验证和清理图片操作文件路径的工具类。
Usage Examples | 使用示例:
// Validate path
SafePathUtil.validatePath(path);
// Check for path traversal
boolean safe = SafePathUtil.isSafePath(path, baseDir);
// Sanitize filename
String safe = SafePathUtil.sanitizeFilename("../evil.jpg");
Features | 主要功能:
- Path traversal attack prevention - 路径遍历攻击防护
- Filename sanitization - 文件名清理
- Image file extension validation - 图片文件扩展名验证
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
- Null-safe: No (throws on null path) - 空值安全: 否(null 路径抛异常)
Performance | 性能特性:
- Time complexity: O(m) where m is the filename length - regex matching and string replacement scan the filename once - 时间复杂度: O(m),m 为文件名长度 - 正则匹配和字符串替换对文件名各扫描一次
- Space complexity: O(m) - sanitized string proportional to filename length - 空间复杂度: O(m) - 清理后的字符串与文件名长度成正比
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanensureParentExists(Path path) Ensure parent directory exists 确保父目录存在static PathgenerateOutputPath(Path inputPath, ImageFormat format) Generate safe output path with new format 生成带新格式的安全输出路径static PathgenerateOutputPath(Path inputPath, String suffix) Generate safe output path 生成安全的输出路径static StringgetExtension(String filename) Get file extension 获取文件扩展名static ImageFormatGet format from path 从路径获取格式static booleanisAllowedExtension(String extension) Check if extension is allowed 检查扩展名是否允许static booleanisSafePath(Path path, Path baseDir) Check if path is safe (within base directory) 检查路径是否安全(在基础目录内)static StringsanitizeFilename(String filename) Sanitize filename for safe storage 清理文件名以安全存储static voidvalidatePath(Path path) Validate path for image operations 验证图片操作的路径static voidvalidatePath(Path path, Path baseDir) Validate path within base directory 在基础目录内验证路径
-
Method Details
-
validatePath
Validate path for image operations 验证图片操作的路径- Parameters:
path- the path to validate | 要验证的路径- Throws:
ImageValidationException- if path is invalid | 如果路径无效
-
validatePath
Validate path within base directory 在基础目录内验证路径- Parameters:
path- the path to validate | 要验证的路径baseDir- the base directory | 基础目录- Throws:
ImageValidationException- if path is outside base dir | 如果路径在基础目录之外
-
isSafePath
-
sanitizeFilename
-
getExtension
-
isAllowedExtension
Check if extension is allowed 检查扩展名是否允许- Parameters:
extension- the extension to check | 要检查的扩展名- Returns:
- true if allowed | 如果允许返回true
-
getFormat
Get format from path 从路径获取格式- Parameters:
path- the file path | 文件路径- Returns:
- the image format or null | 图片格式或null
-
generateOutputPath
-
generateOutputPath
Generate safe output path with new format 生成带新格式的安全输出路径- Parameters:
inputPath- the input path | 输入路径format- the new format | 新格式- Returns:
- the output path | 输出路径
-
ensureParentExists
Ensure parent directory exists 确保父目录存在- Parameters:
path- the file path | 文件路径- Returns:
- true if directory exists or was created | 如果目录存在或已创建返回true
-