Files
Fanjun Kong 72f2e1a897 just for testing
Signed-off-by: Fanjun Kong <kongfanjun@iscas.ac.cn>
2026-01-30 16:04:18 +08:00

56 lines
1.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 公共函数库 - 输入验证和安全工具函数
# 验证包名格式(防止路径遍历和命令注入)
# 只允许字母、数字、点、下划线、加号、减号、波浪号
# 波浪号用于 RPM 预发布版本(如 2.14~rc1
# 参数: $1 - 包名
# 返回: 0=合法, 1=非法
validate_package_name() {
local name="$1"
if [[ ! "$name" =~ ^[a-zA-Z0-9._~+-]+$ ]]; then
return 1
fi
return 0
}
# 验证文件路径(防止路径遍历)
# 参数: $1 - 文件路径
# 返回: 0=安全, 1=包含路径遍历
validate_path() {
local path="$1"
if [[ "$path" == *".."* ]] || [[ "$path" == *"/"* ]]; then
return 1
fi
return 0
}
# 安全的文件追加函数(带文件锁)
# 参数: $1 - 目标文件, $2 - 内容
safe_append() {
local file="$1"
local content="$2"
(
flock -x 200
echo "$content" >> "$file"
) 200>"${file}.lock"
}
# 记录失败信息(带文件锁)
# 参数: $1 - 包名, $2 - 失败原因
log_failure() {
local pkg_name="$1"
local reason="$2"
local failed_file="${RESULTS_DIR}/failed.txt"
(
flock -x 201
echo "${pkg_name}:${reason}" >> "$failed_file"
) 201>"${failed_file}.lock"
}
# 导出函数供其他脚本使用
export -f validate_package_name
export -f validate_path
export -f safe_append
export -f log_failure