fix: harden rule and forwarding edge cases

This commit is contained in:
zxr
2026-07-18 09:00:09 +08:00
parent d381360dfb
commit b07661383a
3 changed files with 16 additions and 6 deletions

View File

@@ -72,7 +72,12 @@ func postAlertPayload(cfg *config.AlertForwardConf, path string, payload []byte)
if cfg.InternalKey != "" { if cfg.InternalKey != "" {
req.Header.Set("X-Internal-Key", cfg.InternalKey) req.Header.Set("X-Internal-Key", cfg.InternalKey)
} }
client := &http.Client{Timeout: 10 * time.Second} client := &http.Client{
Timeout: 10 * time.Second,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("发送 Alert 转发请求失败:%w", err) return fmt.Errorf("发送 Alert 转发请求失败:%w", err)

View File

@@ -665,7 +665,7 @@ func trapRuleMatches(rule *models.TrapRule, trapOID, varbindFP string) bool {
if hasOID && !strings.HasPrefix(normOID(trapOID), normOID(rule.OIDPrefix)) { if hasOID && !strings.HasPrefix(normOID(trapOID), normOID(rule.OIDPrefix)) {
return false return false
} }
if rule.VarbindMatchRegex != "" { if hasRE {
re, err := regexp.Compile(rule.VarbindMatchRegex) re, err := regexp.Compile(rule.VarbindMatchRegex)
if err != nil { if err != nil {
return false return false

View File

@@ -25,14 +25,17 @@ func validateSyslogRule(rule *models.SyslogRule) error {
} }
if raw := strings.TrimSpace(rule.SeverityMappingJSON); raw != "" { if raw := strings.TrimSpace(rule.SeverityMappingJSON); raw != "" {
var mapping map[string]string var mapping map[string]any
if err := json.Unmarshal([]byte(raw), &mapping); err != nil { if err := json.Unmarshal([]byte(raw), &mapping); err != nil {
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象:%v请修正 severity_mapping_json 后重试", err) return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象:%v请修正 severity_mapping_json 后重试", err)
} }
if mapping == nil { if mapping == nil {
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象,不能是 null请改为 JSON 对象后重试") return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象,不能是 null请改为 JSON 对象后重试")
} }
for pattern := range mapping { for pattern, severity := range mapping {
if _, ok := severity.(string); !ok {
return fmt.Errorf("severity_mapping_json 的映射键 %q 对应值必须是字符串;请修正该映射值后重试", pattern)
}
if _, err := regexp.Compile(pattern); err != nil { if _, err := regexp.Compile(pattern); err != nil {
return fmt.Errorf("severity_mapping_json 的映射键 %q 不是有效正则表达式:%v请修正该映射键后重试", pattern, err) return fmt.Errorf("severity_mapping_json 的映射键 %q 不是有效正则表达式:%v请修正该映射键后重试", pattern, err)
} }
@@ -49,8 +52,10 @@ func validateSyslogRule(rule *models.SyslogRule) error {
} }
func validateTrapRule(rule *models.TrapRule) error { func validateTrapRule(rule *models.TrapRule) error {
if err := validateOptionalRegex("varbind_match_regex", rule.VarbindMatchRegex); err != nil { if strings.TrimSpace(rule.VarbindMatchRegex) != "" {
return err if _, err := regexp.Compile(rule.VarbindMatchRegex); err != nil {
return fmt.Errorf("varbind_match_regex 不是有效正则表达式:%v请修正 varbind_match_regex 后重试", err)
}
} }
if strings.TrimSpace(rule.OIDPrefix) == "" && strings.TrimSpace(rule.VarbindMatchRegex) == "" { if strings.TrimSpace(rule.OIDPrefix) == "" && strings.TrimSpace(rule.VarbindMatchRegex) == "" {
return fmt.Errorf("Trap 规则的匹配条件全部为空,运行时永远不会命中;请至少填写 oid_prefix、varbind_match_regex 中的一项") return fmt.Errorf("Trap 规则的匹配条件全部为空,运行时永远不会命中;请至少填写 oid_prefix、varbind_match_regex 中的一项")