Repository navigation
fix(rules): 修复规则引擎三个 High 问题(畸形条件恒命中 / 重载失败丢快照 / 表达式超时 fail-open) - #4
Merged
Merged
Conversation
This was referenced Oct 8, 2026
Owner
Author
|
已 rebase 到 合并顺序:#1 → #6 → #3 → #2 → #4 → #5(请按此顺序依次合并;每个 PR 合并后,下一个 PR 的 base 会由 GitHub 自动改指向其上一级的目标分支)。 验证:每一步 rebase 后均 |
This was referenced Oct 8, 2026
added 3 commits
October 8, 2026 07:25
- store.RuleConditions.UnmarshalJSON 不再吞错:非数组/对象、元素形状不对、 树里的废节点一律返回包着 ErrMalformedConditions 的错误,API 回 400 (旧实现返回空条件组 = 恒命中,PATCH 还因 IsZero 被静默忽略并回 200) - Scan 读到脏行不拖垮加载,但记下 decodeErr:求值侧判不可编译(永不命中), Value() 拒绝写回(ErrInvalidConditions),MarshalJSON 原样回显而非 "[]" - UpdateRule 不把解析失败的现值条件洗成 "[]";CreateRule 拒绝写入 - 扁平形态与树形态一致:任一叶子不可求值即整条规则不可编译; 值级同样如此(ip 列表混非法 CIDR、正则列表混编译不过的值),纯空白值仍跳过 - 新增 rules.ValidateConditions,与加载期共用 compileConditions; resolveRule 在写入 conditions 或切回 visual 时用它校验,丢任何东西都 400 - 有意更新 TestBadConditionDroppedAtLoad / TestNestedTreeBadLeafDropsWholeRule / TestRuleConditionsJSONGarbageDegrades(改名 ...Rejected):旧断言钉住的正是 fail-open
- cacheEntry 保留最近一份加载成功的快照:TTL 过期与 Invalidate 只让它不再"新鲜", 不再丢弃;重新加载失败(error 或 panic)时继续用它,而不是返回空快照 - 失败记 failedAt/failedGen,退避 3s 内同租户请求不再打库(旧实现排队的请求 拿到锁后各自重试,串行等待);Invalidate 自增代数后立即重试,不被退避挡住 - 已有旧快照时 tryLock,别人在加载就直接用旧的,不在租户锁上排队 - 只有从没加载成功过的租户才按无规则放行(fail-open) - Len() 只统计有效快照;新增 6 个缓存回归测试
- runProgram 直接同步调用 vm.Run:goroutine+timer 的 1ms 超时在 CPU 饱和/GC 停顿时 让正常表达式"超时 → 未命中",负载越高越 fail-open。求值步数由编译期封顶: 已对照 expr v1.17.8 compiler.go 核实 OpJumpBackward 只由 15 个谓词内置函数的 emitLoop/emitLoopBackwards 生成,exprAudit 全部拒掉;MemoryBudget 与 recover 保留兜底 - auditVisitor 拒绝右值不是字符串常量的 matches(含 not matches / 需常量折叠的写法), 避免每次求值对访客可控字符串现编译正则 - 有意更新 TestExprEvaluationHasWallClockBudget → TestExprEvaluationIsSynchronousAndDoesNotFailOpenUnderLoad;新增 TestExprDynamicMatchesRejected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复代码审查中规则引擎的三个 High 问题。基于
fix/critical-review。1. 畸形 conditions 退化成匹配所有访客的空条件组
store.RuleConditions.UnmarshalJSON不再吞掉错误。以下情况现在都会报错(错误包着ErrMalformedConditions,API 回 400,并给出具体原因):不是数组或对象、元素形状不对、树里有废节点。旧实现会返回空条件组(即恒命中);PATCH 时又被IsZero静默忽略,接口照样回 200。Scan读到脏行时不报错,不拖垮整租户加载,而是记下decodeErr:Value()拒绝写回(ErrInvalidConditions→ 400);MarshalJSON原样回显库里的字节,不再显示成[];UpdateRule不会把它洗成[](只改 enabled/name 的 PATCH 不碰这一列)。rules.ValidateConditions,和加载期共用compileConditions。resolveRule在写入 conditions 或从 expression 切回 visual 时用它校验,丢掉任何东西都回 400,报错带 JSON 路径。2. 快照重新加载失败时丢掉了上一份好快照
Invalidate只是让它不再新鲜,不会丢弃。重新加载失败(error 或 panic)时继续用旧快照。failedAt/failedGen),3 秒退避内同租户的请求不再访问数据库。旧实现里,排队的请求拿到锁后会各自重试一次,只能串行等待。Invalidate自增代数后会立即重试,不受退避影响。tryLock:别人正在加载就直接返回旧快照,不在租户锁上排队。3. 表达式规则的 1ms 墙钟超时在负载下 fail-open
runProgram去掉了每次求值的 goroutine + timer,直接同步调用vm.Run。compiler.go核实:OpJumpBackward只由 15 个谓词内置函数(all/any/map/filter/reduce…)的emitLoop/emitLoopBackwards生成,而exprAudit已全部拒掉。所以字节码没有回跳,求值步数由字节码长度封顶。MemoryBudget(256KB)和 recover 兜底。auditVisitor拒绝右值不是字符串常量的matches,包括not matches和需要常量折叠才变成常量的写法,避免每次求值都对访客可控的字符串现编译正则。测试
go build ./...、go vet ./...、gofmt通过go test ./internal/rules/... ./internal/domain ./internal/geo ./internal/jwt ./internal/mailer ./internal/rbac ./internal/config ./internal/httpapi/templates ./pkg/event ./pkg/plugin全部通过internal/store中不依赖数据库的条件 JSON 测试通过(依赖数据库的用例因本地无测试库被 skip)internal/httpapi用例(POST/PATCH 畸形条件回 400)需要测试库,本地未运行,请在 CI 确认有意更新的旧测试(旧断言钉住的正是 fail-open 行为,注释里已写明原因):
TestBadConditionDroppedAtLoadTestNestedTreeBadLeafDropsWholeRule(扁平形态对照部分)TestRuleConditionsJSONGarbageDegrades→TestRuleConditionsJSONGarbageRejectedTestExprEvaluationHasWallClockBudget→TestExprEvaluationIsSynchronousAndDoesNotFailOpenUnderLoad行为变化提醒
Invalidate后重新加载的那几毫秒内,并发请求可能还会拿到改动前的快照(stale-while-revalidate),不再阻塞等待。Invalidate的那次调用仍然返回空快照,保持原有行为未改。