fix(gateway): 修复P2-03 regexp.MustCompile可能panic的问题

将regexp.MustCompile替换为regexp.Compile并处理错误,
避免在正则表达式无效时panic。fallback使用永远不匹配
的正则表达式(a^)来保证服务可用性。

修复的问题:P2-03 regexp.MustCompile可能panic
This commit is contained in:
Your Name
2026-04-03 09:58:13 +08:00
parent d44e9966e0
commit a9d304fdfa

View File

@@ -56,7 +56,11 @@ func NewRuleLoader() *RuleLoader {
// Category: 大写字母, 2-4字符
// SubCategory: 大写字母, 2-10字符
// Detail: 可选, 大写字母+数字+连字符, 1-20字符
pattern := regexp.MustCompile(`^[A-Z]{2,4}-[A-Z]{2,10}(-[A-Z0-9-]{1,20})?$`)
pattern, err := regexp.Compile(`^[A-Z]{2,4}-[A-Z]{2,10}(-[A-Z0-9-]{1,20})?$`)
if err != nil {
// 如果正则表达式无效使用一个永远不匹配的pattern作为fallback
pattern = regexp.MustCompile("a^") // 永远不匹配的无效正则
}
return &RuleLoader{
ruleIDPattern: pattern,