fix(P2): 修复4个P2轻微问题

P2-01: 通配符scope安全风险 (scope_auth.go)
- 添加hasWildcardScope()函数检测通配符scope
- 添加logWildcardScopeAccess()函数记录审计日志
- 在RequireScope/RequireAllScopes/RequireAnyScope中间件中调用审计日志

P2-02: isSamePayload比较字段不完整 (audit_service.go)
- 添加ActionDetail字段比较
- 添加ResultMessage字段比较
- 添加Extensions字段比较
- 添加compareExtensions()辅助函数

P2-03: regexp.MustCompile可能panic (sanitizer.go)
- 添加compileRegex()安全编译函数替代MustCompile
- 处理编译错误,避免panic

P2-04: StrategyRoundRobin未实现 (router.go)
- 添加selectByRoundRobin()方法
- 添加roundRobinCounter原子计数器
- 使用atomic.AddUint64实现线程安全的轮询

P2-05: 错误信息泄露内部细节 - 已在MED-09中处理,跳过
This commit is contained in:
Your Name
2026-04-03 09:39:32 +08:00
parent 732c97f85b
commit b2d32be14f
8 changed files with 289 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package sanitizer
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
@@ -287,4 +288,44 @@ func TestSanitizer_MultipleViolations(t *testing.T) {
assert.True(t, result.HasViolation())
assert.GreaterOrEqual(t, len(result.Violations), 3)
}
}
// P2-03: regexp.MustCompile可能panic应该使用regexp.Compile并处理错误
func TestP2_03_NewCredentialScanner_InvalidRegex(t *testing.T) {
// 测试一个无效的正则表达式
// 由于NewCredentialScanner内部使用MustCompile这里我们测试在初始化时是否会panic
// 创建一个会panic的场景无效正则应该被Compile检测而不是MustCompile
// 通过检查NewCredentialScanner是否能正常创建不panic来验证
defer func() {
if r := recover(); r != nil {
t.Errorf("P2-03 BUG: NewCredentialScanner panicked with invalid regex: %v", r)
}
}()
// 这里如果正则都是有效的应该不会panic
scanner := NewCredentialScanner()
if scanner == nil {
t.Error("scanner should not be nil")
}
// 但我们无法在测试中模拟无效正则因为MustCompile在编译时就panic了
// 所以这个测试更多是文档性质的
t.Logf("P2-03: NewCredentialScanner uses MustCompile which panics on invalid regex - should use Compile with error handling")
}
// P2-03: 验证MustCompile在无效正则时会panic
// 这个测试演示了问题使用无效正则会导致panic
func TestP2_03_MustCompile_PanicsOnInvalidRegex(t *testing.T) {
invalidRegex := "[invalid" // 无效的正则,缺少结束括号
defer func() {
if r := recover(); r != nil {
t.Logf("P2-03 CONFIRMED: MustCompile panics on invalid regex: %v", r)
}
}()
// 这行会panic
_ = regexp.MustCompile(invalidRegex)
t.Error("Should have panicked")
}