mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-01 06:47:29 +00:00
三条主线,由 e2e 实测驱动。 ## 一、差分 OTA(internal/update/delta.go) 实测:改一行逻辑,88MB 的二进制逐字节只差 80 字节,而 OTA 一直下整包。 用 rsync 族的滚动哈希块匹配(4KB 粒度),不是 bsdiff —— 后者需 17×N 峰值内存(88MB → 1.5GB),而升级恰可能发生在内存紧张时。 真实二进制实测:88,036,728 字节 → 补丁 77,316 字节(0.088%)。 三道闸:应用前验旧文件 sha256(错基线会静默产出垃圾)、应用后验结果 sha256、COPY/LITERAL 越界防护。任何失败**退回整包**,不向上抛 —— 差分是优化,不是可用性前提。 接入:Source.Resolve 优先试 delta;CheckRelease 按 -from-<commit8>.delta 匹配(不猜基线);CLI 新增 -make-delta;打包脚本 build_delta(需 PREVIOUS_HOMED),delta 进 SHA256SUMS。 ## 二、安装形态分派(internal/update/install.go + channel.go) 内核对「能不能自己升级」此前只有一个答案:直接替换二进制。而 deb/rpm 形态下那样做会让 dpkg/rpm 数据库与实际内容不一致(下次 apt upgrade 覆盖回旧版,或 dpkg 拒绝后续升级)。 新增探测:deb/rpm/tar/container/source 五类形态 + 变体(full/server/ client/toolchain)+ 目录写权限。判据全部来自可观测系统事实(dpkg 的 info 目录、容器标记文件),不做路径推断。 三端统一走 PlanUpgrade 分派;形态闸门放在**下载 880MB 之前**。 ## 三、事件总线统一受安全监控(internal/events + security) 用户指正:「eventbus 上所有事件都要接受安全模块的监控」。 此前 EventPolicy 只被 internal/sdk/events.go 调用,而那是四条路径中的 一条。s.Publish / s.Subscribe / s.EventBus() 拿的是裸 *events.Bus, 任何插件都能绕过策略发任意事件(含伪造内核事件)。 现在闸门在 Bus.Publish/Subscribe 本体上(Guard 接口注入,避免 events ↔ security 成环),四条路径全部覆盖。sdk.EventBus() 改返回带身份的 BusHandle。内核身份免授权,但载荷照样过凭据闸门。 白名单按主体收窄:agent_output/stage/terminal_output 只允许承载者 (webui/cli/agentcli)发布 —— 它们是内核向外的输出面,谁都能发就等于 能伪造 agent 说的话。 ## e2e 暴露并修掉的三个真实回归 [bus] subscribe denied: pluginmgr type=update_request [bus] subscribe denied: skillmgr type=skill_detected 前两个让升级请求静默丢失、职责链交接断掉。内部协作事件必须可订阅 (安全边界在发布侧,订阅只是「听得到」)。 ## 顺带的止血修复 - 回滚判据从 `--version` 改为 digest:旧内核(1.3.x 及以前)没有该 flag,而它恰是最常见的回滚目标 —— 原逻辑让「从旧版升级失败」永久 无法回滚。 - CopyFile 补 fsync(替换 homed 本体时掉电会留半截文件)。 - restore 完整还原才清 marker(原先先清再还原,还原失败就失去第二次 自愈机会)。 - 回滚时移除升级中新增的 depgraph(否则「旧 homed + 新 depgraph」 会因 digest 不匹配崩溃循环)。 - homed-upgrade 不再硬编码 systemctl restart(丢 guard 状态、unit 名 漂移);改用内核已有的 restartCh → exit 42 语义。 - homed-upgrade 不再用 CGO_ENABLED=0 编却依赖 go-sqlite3(stub 下 静默失效,该路径从未工作过);改用 internal/config。 - 服务单元补 User=hologramagent + 三条安装路径补建用户(此前发行物里 完全没有落实降权)。 - rpm 的 fpm 调用补 --after-install(此前 rpm 装完根本不建用户)。 ## 判据 新增 30 条:差分 8(往返/压缩比/错基线/截断/篡改/边界)、通道 8、 执行器 6、事件闸门 8、按主体授权 8。全仓 go test ./... 0 失败。
193 lines
6.0 KiB
Go
193 lines
6.0 KiB
Go
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)
|
||
}
|
||
}
|