package events // 总线闸门的判据(用户 2026-10-28:「eventbus 上所有事件都要接受安全模块的监控」)。 // // ## 这组判据要钉住什么 // // 在此之前,授权只存在于 internal/sdk/events.go 那**一条**路径。 // 其余三条(s.Publish / s.Subscribe / s.EventBus())拿的是裸 *events.Bus, // 任何插件都能绕过策略发任意事件 —— 包括伪造 kernel 事件。 // // 现在闸门在 Bus.Publish / Bus.Subscribe 本体上,绕不过去。 // 所以判据的重点是「**绕过尝试必须失败**」,而不是「正常路径能跑」。 import ( "errors" "testing" ) // fakeGuard 是一个只放行白名单的闸门。 type fakeGuard struct { allowPublish map[string]bool allowSubscribe map[string]bool scrubCalls int } func (g *fakeGuard) AllowPublish(p Publisher, t EventType) (bool, string) { if p.IsKernel() { return true, "" } if g.allowPublish[string(t)] { return true, "" } return false, "not allowed" } func (g *fakeGuard) AllowSubscribe(p Publisher, t EventType) (bool, string) { if p.IsKernel() { return true, "" } if g.allowSubscribe[string(t)] { return true, "" } return false, "not allowed" } func (g *fakeGuard) ScrubPayload(_ EventType, payload map[string]interface{}) map[string]interface{} { g.scrubCalls++ if _, ok := payload["secret"]; ok { out := map[string]interface{}{} for k, v := range payload { if k == "secret" { out[k] = "@{redacted}" continue } out[k] = v } return out } return payload } // ① 插件发布未授权事件必须被**拒绝**,且不送达订阅者。 // // 这是最核心的一条:能发 raw_input / agent_output 等于能伪造内核事件。 func TestGuardRejectsUnauthorizedPublish(t *testing.T) { bus := NewBus() bus.SetGuard(&fakeGuard{allowPublish: map[string]bool{"skill_detected": true}}) got := 0 bus.Subscribe(EventRawInput, func(*Event) { got++ }) err := bus.PublishAs(Publisher{Name: "evil"}, &Event{Type: EventRawInput}) if err == nil { t.Fatal("★ 未授权发布必须返回错误(静默丢弃会让插件以为发了)") } var de *DeniedError if !errors.As(err, &de) { t.Fatalf("应返回 DeniedError,实际 %T", err) } if got != 0 { t.Errorf("★ 被拒事件不应送达订阅者,实际送达 %d 次", got) } } // ② 授权事件正常送达,且来源是插件名。 func TestGuardAllowsAuthorizedPublish(t *testing.T) { bus := NewBus() bus.SetGuard(&fakeGuard{allowPublish: map[string]bool{"skill_detected": true}}) var got *Event bus.Subscribe(EventSkillDetected, func(e *Event) { got = e }) if err := bus.PublishAs(Publisher{Name: "clawhubadapter"}, &Event{Type: EventSkillDetected, Payload: map[string]interface{}{"path": "/x"}}); err != nil { t.Fatalf("白名单事件应放行: %v", err) } if got == nil { t.Fatal("订阅者应收到事件") } } // ③ 内核身份不受授权限制(内核模块发布系统事件不需要策略)。 func TestGuardAllowsKernelPublish(t *testing.T) { bus := NewBus() bus.SetGuard(&fakeGuard{allowPublish: map[string]bool{}}) if err := bus.PublishAs(Publisher{}, &Event{Type: EventSystem}); err != nil { t.Fatalf("内核身份应放行: %v", err) } } // ④ 内核身份**也要过载荷脱敏**。 // // 凭据闸门与身份无关:内核把 api_key 拼进事件,和插件拼进去一样危险。 func TestGuardScrubsKernelPayload(t *testing.T) { bus := NewBus() g := &fakeGuard{allowPublish: map[string]bool{}} bus.SetGuard(g) var got *Event bus.Subscribe(EventSystem, func(e *Event) { got = e }) bus.Publish(&Event{Type: EventSystem, Payload: map[string]interface{}{"secret": "sk-123"}}) if got == nil { t.Fatal("应收到事件") } if got.Payload["secret"] != "@{redacted}" { t.Errorf("★ 内核载荷也必须脱敏,实际 %v", got.Payload["secret"]) } if g.scrubCalls == 0 { t.Error("ScrubPayload 应被调用") } } // ⑤ 未授权订阅必须被拒。 // // 若能订阅 raw_input,插件就能读到用户的全部输入。 func TestGuardRejectsUnauthorizedSubscribe(t *testing.T) { bus := NewBus() bus.SetGuard(&fakeGuard{allowSubscribe: map[string]bool{"tools_ready": true}}) if _, err := bus.SubscribeAs(Publisher{Name: "evil"}, EventRawInput, func(*Event) {}); err == nil { t.Fatal("★ 未授权订阅必须报错") } // 白名单内的可订阅 if _, err := bus.SubscribeAs(Publisher{Name: "p1"}, EventToolsReady, func(*Event) {}); err != nil { t.Errorf("白名单订阅应放行: %v", err) } } // ⑥ 没有闸门时行为不变(开发/测试场景,且保证既有部署不被意外破坏)。 func TestGuardAbsentBehavesAsBefore(t *testing.T) { bus := NewBus() got := 0 bus.Subscribe(EventRawInput, func(*Event) { got++ }) if err := bus.PublishAs(Publisher{Name: "any"}, &Event{Type: EventRawInput}); err != nil { t.Fatalf("无闸门时不应报错: %v", err) } if got != 1 { t.Errorf("无闸门时应正常送达,实际 %d", got) } } // ⑦ 闸门可以被后装(启动顺序:总线先建、security 后装配)。 // // 这条钉住真实的装配顺序:kernelapi 创建 Bus,security 模块在更晚的层 // 才注入 Guard。先发的 security_ready 事件不受影响,之后全部受控。 func TestGuardCanBeInstalledLater(t *testing.T) { bus := NewBus() // 先无闸门发一条 if err := bus.PublishAs(Publisher{Name: "p"}, &Event{Type: EventSystem}); err != nil { t.Fatal(err) } // 装闸门后必须生效 bus.SetGuard(&fakeGuard{allowPublish: map[string]bool{}}) if err := bus.PublishAs(Publisher{Name: "p"}, &Event{Type: EventSystem}); err == nil { t.Fatal("★ 后装闸门必须立即生效") } } // ⑧ 未授权发布不得污染 EventAll 通配订阅。 func TestGuardDeniesBeforeWildcardHandlers(t *testing.T) { bus := NewBus() bus.SetGuard(&fakeGuard{allowPublish: map[string]bool{}}) got := 0 bus.Subscribe(EventAll, func(*Event) { got++ }) _ = bus.PublishAs(Publisher{Name: "evil"}, &Event{Type: EventAgentOutput}) if got != 0 { t.Errorf("★ 被拒事件不得送到 EventAll 订阅者,实际 %d", got) } }