fix(alert): prevent divide-by-zero panic and unbounded growth on zero-duration rule

A general alert rule with Duration:0 (accepted by the API, no minimum
validation) drove fail*100/total with total==0, panicking with an integer
divide-by-zero inside checkStatus, which has no recover and would take down the
whole alert goroutine — a config-reachable DoS by any user able to create alert
rules. Skip duration<=0 rules in Check via continue (not boundCheck, which would
pollute hasPassedRule and skip sibling valid rules). Also trim the sample slice
when max<=0 in alertsentinel, otherwise it appended every tick without ever
trimming. Adds regression tests for zero-only and mixed rule sets.
This commit is contained in:
naiba
2026-06-05 02:42:49 +00:00
parent 36240f5888
commit 0759f5c801
3 changed files with 61 additions and 4 deletions
+7
View File
@@ -166,6 +166,13 @@ func (r *AlertRule) Check(points [][]bool) (int, bool) {
continue
} else {
// 常规报警
// duration<=0 是无意义的规则(持续 0 秒):直接跳过该规则,
// 既不污染 hasPassedRule(否则会连带跳过同一 alert 里其它有效
// 规则),也避免下方 fail*100/total 在 total=0 时整数除零 panic
// —— checkStatus 无 recover,一次 panic 会拖垮整个告警 goroutine。
if duration <= 0 {
continue
}
if hasPassedRule = boundCheck(len(points), duration, hasPassedRule); hasPassedRule {
continue
}