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 ——
半成品状态长期红的判据会被人习惯性忽略,那时它就等于不存在。
124 lines
4.0 KiB
Go
124 lines
4.0 KiB
Go
package sdk
|
||
|
||
// SecurityAPI 内核实现的判据。
|
||
//
|
||
// ## 钉什么
|
||
//
|
||
// 声明面只有三件事可能出错,且三种都**静默**:
|
||
//
|
||
// ① Owner 被插件伪造 ⇒ 能声明别人的档位(qq.owner 全放行)
|
||
// ② 名字前缀不对 ⇒ 档位登记成功但在别的 owner 名下(永远命中不到)
|
||
// ③ 未装配时静默成功 ⇒ 插件以为生效了,实际没登记
|
||
//
|
||
// 本文件的判据对每一条都要求**响亮失败**。
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
|
||
internalSecurity "github.com/JianFeeeee/HologramAgent/internal/security"
|
||
pubsdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||
)
|
||
|
||
// withRuntime 造一个干净的 Runtime 供测试。
|
||
//
|
||
// ★ 2026-11-04:不再装包级单例(已删)。调用方必须把返回值**显式注入**
|
||
// 给被测面(NewSecurityAPI(name, rt)),这正是生产注入路径。
|
||
func withRuntime(t *testing.T) *internalSecurity.Runtime {
|
||
t.Helper()
|
||
return internalSecurity.NewRuntime(nil)
|
||
}
|
||
|
||
// ① 正常声明:登记到 Runtime,且 Owner 是内核给的名字。
|
||
func TestSecurityDeclareProfileRegistersUnderRealOwner(t *testing.T) {
|
||
rt := withRuntime(t)
|
||
api := NewSecurityAPI("qq", rt)
|
||
|
||
if err := api.DeclareProfile(pubsdk.SecurityProfile{
|
||
Name: "qq.guest",
|
||
DenyTools: []string{"cmd_*"},
|
||
AllowTools: []string{"qq_*"},
|
||
}); err != nil {
|
||
t.Fatalf("声明应成功: %v", err)
|
||
}
|
||
|
||
if !contains(rt.Profiles(), "qq.guest") {
|
||
t.Fatalf("档位未登记,实际已有: %v", rt.Profiles())
|
||
}
|
||
}
|
||
|
||
// ② ★ Owner 不可伪造:插件声明别人的档位必须报错。
|
||
//
|
||
// 若能声明 `qq.owner` 且给 AllowTools=["*"],等于把 QQ 的 owner 档位
|
||
// 改成全放行 —— 非所有者立刻能调用所有工具。
|
||
func TestSecurityDeclareProfileRejectsForeignOwner(t *testing.T) {
|
||
rt := withRuntime(t)
|
||
api := NewSecurityAPI("evil", rt)
|
||
|
||
err := api.DeclareProfile(pubsdk.SecurityProfile{
|
||
Name: "qq.owner",
|
||
Owner: "qq", // 冒名
|
||
AllowTools: []string{"*"},
|
||
})
|
||
if err == nil {
|
||
t.Fatal("★ 冒名声明必须被拒绝 —— 否则任何插件都能把 qq.owner 改成全放行")
|
||
}
|
||
if !strings.Contains(err.Error(), "evil") {
|
||
t.Errorf("错误信息应指出真实插件名,实际: %v", err)
|
||
}
|
||
}
|
||
|
||
// ③ 名字必须带本插件前缀(否则登记了也命中不到)。
|
||
func TestSecurityDeclareProfileRequiresOwnerPrefix(t *testing.T) {
|
||
rt := withRuntime(t)
|
||
api := NewSecurityAPI("qq", rt)
|
||
|
||
for _, bad := range []string{"guest", "qqguest", "qqq.guest", ""} {
|
||
if err := api.DeclareProfile(pubsdk.SecurityProfile{Name: bad}); err == nil {
|
||
t.Errorf("名字 %q 应被拒绝(必须形如 qq.xxx)", bad)
|
||
}
|
||
}
|
||
}
|
||
|
||
// ④ 未装配 security 时必须报错,不能静默成功。
|
||
//
|
||
// 静默成功的后果:插件以为档位生效,直到注入时带 SecurityProfile
|
||
// 才报"未声明档位",此时排查方向已跑到"名字拼错"上。
|
||
func TestSecurityDeclareProfileFailsLoudlyWhenUnassembled(t *testing.T) {
|
||
// 未装配 = Runtime 为 nil(不注入),而不是"装了 nil 单例"。
|
||
api := NewSecurityAPI("qq", nil)
|
||
err := api.DeclareProfile(pubsdk.SecurityProfile{Name: "qq.guest"})
|
||
if err == nil {
|
||
t.Fatal("★ 安全模块未装配时必须报错 —— 静默成功会让插件误以为策略已生效")
|
||
}
|
||
if !strings.Contains(err.Error(), "not assembled") {
|
||
t.Errorf("错误信息应说明未装配,实际: %v", err)
|
||
}
|
||
}
|
||
|
||
// ⑤ 批量声明:任一失败即整体报错,且错误带下标便于定位。
|
||
func TestSecurityDeclareProfilesReportsIndex(t *testing.T) {
|
||
rt := withRuntime(t)
|
||
api := NewSecurityAPI("qq", rt)
|
||
|
||
err := api.DeclareProfiles([]pubsdk.SecurityProfile{
|
||
{Name: "qq.owner"},
|
||
{Name: "bogus.name"}, // 第 1 个(0-based)非法
|
||
})
|
||
if err == nil {
|
||
t.Fatal("批量声明中任一失败应整体报错")
|
||
}
|
||
if !strings.Contains(err.Error(), "profile[1]") {
|
||
t.Errorf("错误应带失败下标,实际: %v", err)
|
||
}
|
||
}
|
||
|
||
func contains(list []string, want string) bool {
|
||
for _, v := range list {
|
||
if v == want {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|