refactor: 4项架构优化 — CEL缓存/POC隔离/服务缓存/结果统一

1. CEL 表达式编译缓存
   - 新增 CelProgCache,同一 POC 的所有规则/参数组合共享编译后的 Program
   - clusterpoc 热路径上消除重复的 Compile+Program 调用

2. POC 全局状态消除
   - allPocs/pocLoaded 全局变量改为 pocStore 按 PocPath 缓存
   - 不同 PocPath 的扫描独立加载,Web API 并发场景不再互相覆盖

3. serviceCache 下沉到 per-session State
   - 服务识别缓存从包级全局 map 迁移到 State.serviceCache (sync.Map)
   - BaseScanStrategy 通过 SetState 注入 session state
   - 消除多个并发扫描之间的服务识别缓存串台

4. POC 结果输出路径统一
   - 提取 buildVulnDetails/buildVulnLogMsg/saveVulnResult 三个公共函数
   - CheckMultiPoc 和 recordVulnerabilityResult 共用统一的结果构造逻辑
   - 消除 details 字段名不一致和日志格式差异
This commit is contained in:
ZacharyZcR
2026-06-13 19:24:57 +08:00
parent 6a1636112f
commit 02ad8f5334
10 changed files with 261 additions and 230 deletions
+8 -19
View File
@@ -338,11 +338,7 @@ func TestFilterPocs(t *testing.T) {
{Name: "Nginx-Path-Traversal"},
}
// 保存原始 allPocs 并在测试后恢复
origAllPocs := allPocs
defer func() { allPocs = origAllPocs }()
allPocs = testPocs
// 直接使用 testPocs 作为输入
tests := []struct {
name string
@@ -408,7 +404,7 @@ func TestFilterPocs(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := filterPocs(tt.pocName)
result := filterPocs(tt.pocName, testPocs)
if len(result) != tt.expectedCount {
t.Errorf("filterPocs(%q) returned %d pocs, want %d", tt.pocName, len(result), tt.expectedCount)
@@ -449,19 +445,14 @@ func TestFilterPocs(t *testing.T) {
}
func TestFilterPocsNilSafety(t *testing.T) {
// 测试全是 nil 的情况
origAllPocs := allPocs
defer func() { allPocs = origAllPocs }()
nilPocs := []*lib.Poc{nil, nil, nil}
allPocs = []*lib.Poc{nil, nil, nil}
result := filterPocs("test")
result := filterPocs("test", nilPocs)
if len(result) != 0 {
t.Errorf("filterPocs with all nil should return empty slice, got %d items", len(result))
}
// 空 pocName 返回所有 POCs(包括 nil
result = filterPocs("")
result = filterPocs("", nilPocs)
if len(result) != 3 {
t.Errorf("filterPocs with empty name should return all pocs (including nil), got %d items, want 3", len(result))
}
@@ -497,12 +488,10 @@ func TestCreateBaseRequestHeaders(t *testing.T) {
func TestExecutePOCsEarlyReturns(t *testing.T) {
cfg := common.NewConfig()
session := common.NewScanSession(cfg, common.NewState(), &common.FlagVars{})
previous := allPocs
allPocs = nil
t.Cleanup(func() { allPocs = previous })
executePOCs(context.Background(), config.PocInfo{}, cfg, session)
executePOCs(context.Background(), config.PocInfo{Target: "http://example.com", PocName: "missing"}, cfg, session)
var emptyPocs []*lib.Poc
executePOCs(context.Background(), config.PocInfo{}, cfg, session, emptyPocs)
executePOCs(context.Background(), config.PocInfo{Target: "http://example.com", PocName: "missing"}, cfg, session, emptyPocs)
}
func TestDirectoryExists(t *testing.T) {