mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-05 16:57:32 +00:00
## 用户裁定
> secutiry 的实现也是错误的,所有共享数据统一挂载一个 kernel 提供的
> 挂载点上暴露。
> store也应该调用security的加密能力!
## 缺陷
security 包原有**三份**独立包级单例,各自被 InitXxx/SetXxx 装入、
各自有访问器,三份都绕开了内核统一挂载点 kstate.State:
global *Store 变量库(凭据别名) Global()
runtime *Runtime 安全防火墙 RuntimeGlobal()
globalCodec *Codec 配置落盘加密 ConfigCodec()
## 为什么单例是错的(不是"多一个全局变量")
本 agent 的 a.security 与 security.Global() 是**两个独立变量**。
正常装配下它们恰好指向同一份数据,但"恰好"不是保证:
· 测试替身:测试装单例、被测代码用 agent 字段 ⇒ 测的与跑的不是同一份
· 多 Runtime:两个内核实例/热重载时单例只有一份,另一方被覆盖
· 装配顺序:单例装晚了,先注册的 provider 就没被装饰
第三条**曾真实存在**:ProviderManager.Register 在注册时读单例套装饰器,
而 llm 模块原本不声明 FacSecurity 依赖 —— 顺序正确纯属巧合。
改动过程中判据如实报红 640 次泄漏。
## 本次做的(T3.1 + T3.3)
### 删 global *Store → 注入
变量库的唯一来源是 security 模块产出的 Runtime,经注入面分发:
AgentConfig.Security → a.security.Store() → a.secretStore()
ProviderManager.SetSecretStore
plugin.Registry.SetSecretStore → sdk.NewSecretsAPI(store)
core 的 4 处 security.Global()(output/toolcall/tooldefs/secret_tools)
改为 a.secretStore();context 与 stageHost 改为注入字段;
provider 装饰器改为读注入的 Store。
### 删 runtime *Runtime → 注入
plugin.Registry.SetSecurityRuntime → sdk.NewSecurityAPI(name, rt)
→ sdk.newEventsAPI(name, bus, rt)
Prepare 的幂等缓存改为 kernelmod/security 私有变量 ——
装配器私有,不再是任何包都能取的公开访问器。
### 顺带删死代码
security.FillToolArgs / FillPayload 生产调用者为 0(StageHost 已自己
用注入的 Store 做同一件事),连同只测它们的判据一起删除。
### llm 模块补 FacSecurity 依赖
这是**真实存在**的依赖:注册 provider 时要套出向装饰器,而装饰器需要
变量库。此前靠"单例先装了"掩盖了顺序错误。
## 判据(均经变异验证)
TestNoPackageLevelSingletons 源码扫描;加回 var global ⇒ 红(实测)
TestMustStoreDoesNotRegisterGlobally 两次 MustStore 必须得到不同实例
TestStoreEncryptsWithOwnKey 落盘无明文 + 重载可解
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l 干净
go test ./... -count=1 63 包全过,0 FAIL
干净检出(git worktree) build + 3 包测试全绿
## 未做(见 kernelplan.md T3.2)
globalCodec 收敛 + Store 复用 Codec
还牵涉 config 的 6 处调用点与**存量密文惰性迁移**,单独一步做。
TestNoPackageLevelSingletons 的禁表暂不含 globalCodec ——
半成品状态长期红的判据会被人习惯性忽略,那时它就等于不存在。
154 lines
4.9 KiB
Go
154 lines
4.9 KiB
Go
package proc
|
||
|
||
// 端到端:插件的**安全策略声明**能力(sdk `Security()`)。
|
||
//
|
||
// ## 这条链路为什么要真机验证
|
||
//
|
||
// 与 variable store(e2e_secrets)同型:新增一个 proc method 要改
|
||
// **4 个地方**(协议常量 / 外层分派 / 内层分发 / 能力表),少一处就报
|
||
// "未知 method"。单测只能证明各段自洽,证明不了接上了。
|
||
//
|
||
// 而且它跨了**四层**:
|
||
//
|
||
// ① 插件调 sdk.Security().DeclareProfile("qq.guest", …)
|
||
// ② 模板 → RPC "security.declareProfile"
|
||
// ③ 内核 coreHandler.handleSecurity → sdk.SecurityAPI(security_impl)
|
||
// ④ → security.Runtime.DeclareProfile(进程级单例,真正被 CheckTool 读)
|
||
//
|
||
// ## 最关键的一点:Owner 由内核填
|
||
//
|
||
// 插件在参数里可以写任意 Owner(甚至写别的插件名),但宿主用
|
||
// `PluginName()` 覆盖它 —— 本判据正面验证「插件声明自己的档位成功」,
|
||
// 负面验证(冒名被拒)在 internal/sdk/security_impl_test.go。
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
|
||
internalConfig "github.com/JianFeeeee/HologramAgent/internal/config"
|
||
internalSDK "github.com/JianFeeeee/HologramAgent/internal/sdk"
|
||
internalSecurity "github.com/JianFeeeee/HologramAgent/internal/security"
|
||
)
|
||
|
||
// securityE2EPlugin 用真实模板编译:只做一次档位声明。
|
||
const securityE2EPlugin = `package main
|
||
|
||
import (
|
||
sdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||
)
|
||
|
||
type secPlugin struct{ name string }
|
||
|
||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||
return &secPlugin{name: name}, nil
|
||
}
|
||
|
||
func (p *secPlugin) Name() string { return p.name }
|
||
func (p *secPlugin) Stop() error { return nil }
|
||
|
||
func (p *secPlugin) Start(s *sdk.PluginSDK) error {
|
||
sec := s.Security()
|
||
s.RegisterTool("e2e_declare", sdk.ToolDef{
|
||
Description: "声明安全档位",
|
||
Parameters: map[string]interface{}{"type": "object"},
|
||
}, func(args map[string]interface{}) (interface{}, error) {
|
||
if sec == nil {
|
||
return "UNAVAILABLE", nil
|
||
}
|
||
err := sec.DeclareProfile(sdk.SecurityProfile{
|
||
Name: "e2eproc.guest",
|
||
DenyTools: []string{"cmd_*"},
|
||
})
|
||
if err != nil {
|
||
return "DECLARE_ERR: " + err.Error(), nil
|
||
}
|
||
return "OK", nil
|
||
})
|
||
return nil
|
||
}
|
||
`
|
||
|
||
// ★★ 判据:真实模板插件的档位声明能穿透四层到达 Runtime。
|
||
func TestE2E_RealTemplateSecurityDeclare(t *testing.T) {
|
||
bin := buildPluginWithRealTemplate(t, securityE2EPlugin)
|
||
|
||
dir := t.TempDir()
|
||
setTestCodec(t)
|
||
|
||
// ★ 2026-11-04:包级单例已删。Runtime 显式构造并**注入**给插件 SDK
|
||
// (生产路径同款:kstate.State.Security → plugin.Registry.SetSecurityRuntime)。
|
||
rt := internalSecurity.NewRuntime(nil)
|
||
_ = internalConfig.NewConfigRegistry
|
||
|
||
host, err := NewHost()
|
||
if err != nil {
|
||
t.Fatalf("NewHost: %v", err)
|
||
}
|
||
defer host.Close()
|
||
|
||
core := newFakeCore()
|
||
// ★ 用内核侧真实插件名注入 —— 这就是 Owner 不可伪造的来源。
|
||
core.securityAPI = internalSDK.NewSecurityAPI("e2eproc", rt)
|
||
p := New("e2e", bin, dir, nil, host, nil)
|
||
if err := p.Start(core); err != nil {
|
||
t.Fatalf("Start: %v", err)
|
||
}
|
||
defer p.Close()
|
||
|
||
core.mu.Lock()
|
||
handler, ok := core.tools["e2e_declare"]
|
||
core.mu.Unlock()
|
||
if !ok {
|
||
t.Fatal("插件工具未注册")
|
||
}
|
||
|
||
out, err := handler(nil)
|
||
if err != nil {
|
||
t.Fatalf("工具调用失败: %v", err)
|
||
}
|
||
got, _ := out.(string)
|
||
if got != "OK" {
|
||
t.Fatalf("声明应成功,实际: %q", got)
|
||
}
|
||
|
||
// ★ 核心断言:档位真的进了 Runtime(而不是只"没报错")
|
||
profiles := rt.Profiles()
|
||
found := false
|
||
for _, n := range profiles {
|
||
if n == "e2eproc.guest" {
|
||
found = true
|
||
}
|
||
}
|
||
if !found {
|
||
t.Fatalf("★ 档位未登记到 Runtime,已有: %v —— RPC 链路某一段没接上", profiles)
|
||
}
|
||
|
||
// 再验证它真的能约束工具(否则"登记了但没用"
|
||
//
|
||
// ★ 这里我曾把断言写反(`if d.Allowed { Fail("应放行") }`)——
|
||
// 把“期望放行”写成了“若放行则失败”。现在明确:
|
||
// 未激活档位 ⇒ cmd_run 必须**放行**(档位是按需激活的)
|
||
if d := rt.CheckTool("cmd_run"); !d.Allowed {
|
||
t.Fatalf("未激活档位时 cmd_run 应放行(档位按需激活),实际被拒: %s", d.Reason)
|
||
}
|
||
if err := rt.ActivateProfile("e2eproc.guest", 0); err != nil {
|
||
t.Fatalf("激活档位失败: %v", err)
|
||
}
|
||
if d := rt.CheckTool("cmd_run"); d.Allowed {
|
||
t.Fatal("激活 e2eproc.guest 后 cmd_run 必须被拒 —— 档位声明了却不生效")
|
||
}
|
||
}
|
||
|
||
// 未装配声明面时,插件应拿到明确错误而不是静默成功。
|
||
func TestE2E_SecurityDeclareWithoutAPI(t *testing.T) {
|
||
core := newFakeCore()
|
||
core.securityAPI = nil // 模拟宿主未接声明面
|
||
|
||
h := &coreHandler{sdk: core}
|
||
if _, err := h.Handle(MethodSecurityDeclareProfile, []byte(`{}`)); err == nil {
|
||
t.Fatal("未装配声明面时必须报错(静默成功会让插件误以为已登记)")
|
||
} else if !strings.Contains(err.Error(), "not wired") {
|
||
t.Fatalf("错误信息应说明未装配,实际: %v", err)
|
||
}
|
||
}
|