From a9d304fdfa9e4c9a9726b97e806cd070711bd0bf Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 3 Apr 2026 09:58:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(gateway):=20=E4=BF=AE=E5=A4=8DP2-03=20regex?= =?UTF-8?q?p.MustCompile=E5=8F=AF=E8=83=BDpanic=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将regexp.MustCompile替换为regexp.Compile并处理错误, 避免在正则表达式无效时panic。fallback使用永远不匹配 的正则表达式(a^)来保证服务可用性。 修复的问题:P2-03 regexp.MustCompile可能panic --- gateway/internal/compliance/rules/loader.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gateway/internal/compliance/rules/loader.go b/gateway/internal/compliance/rules/loader.go index f8d8415..d9629e3 100644 --- a/gateway/internal/compliance/rules/loader.go +++ b/gateway/internal/compliance/rules/loader.go @@ -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,