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 ——
半成品状态长期红的判据会被人习惯性忽略,那时它就等于不存在。
118 lines
4.8 KiB
Go
118 lines
4.8 KiB
Go
package sdk
|
||
|
||
// SecurityAPI 的内核实现:把插件声明的安全档位登记到进程级 security.Runtime。
|
||
//
|
||
// ## 分工(用户裁定 2026-10-26 / 2026-10-29)
|
||
//
|
||
// 插件:DeclareProfile / DeclareProfiles (只声明,不判定)
|
||
// 内核:Runtime.CheckTool / ActivateProfile (只执行,不信任插件)
|
||
//
|
||
// 本文件是**声明面的转发层**,只做三件事:
|
||
//
|
||
// ① 用**内核侧真实插件名**填 Owner(插件无法伪造自己的身份)
|
||
// ② 校验名字前缀是 `<插件名>.`(防插件 A 声明 `qq.owner` 抢插件 B 的语义)
|
||
// ③ 转发到 security.Runtime.DeclareProfile
|
||
//
|
||
// ## 为什么 Owner 必须由内核填,而不是信任插件传的值
|
||
//
|
||
// 档位名是**全局唯一**的(`qq.owner` / `qq.guest`),而 Runtime 拒绝跨 owner
|
||
// 覆盖。若允许插件自报 owner,那么任何拿到声明面的插件都能声明
|
||
// `qq.owner` 并给它 `AllowTools: ["*"]` —— 直接把 QQ 的 owner 档位改成全放行。
|
||
//
|
||
// 所以 `SecurityProfile.Owner` 字段在**内核侧一律被覆盖**为真实插件名,
|
||
// 插件传什么都无效(包括传空、传错)。这不是"校验后拒绝",而是
|
||
// **根本不采信** —— 后者没有可绕过的边界。
|
||
//
|
||
// ## 未启用时的行为
|
||
//
|
||
// security 未注入 Runtime 时返回明确错误,而不是静默成功。
|
||
// 理由:插件会以为自己的档位生效了,而实际什么都没登记 ——
|
||
// 等到注入时带 `SecurityProfile: "qq.guest"` 才会响亮报"未声明档位",
|
||
// 那时排查方向已经跑偏到"档位名拼错"。早失败比晚失败好。
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
internalSecurity "github.com/JianFeeeee/HologramAgent/internal/security"
|
||
pubsdk "github.com/JianFeeeee/hologramagentsdk/sdk"
|
||
)
|
||
|
||
// securityImpl 实现 pubsdk.SecurityAPI。
|
||
type securityImpl struct {
|
||
// pluginName 是内核注入的**真实**插件名,不来自插件自述。
|
||
pluginName string
|
||
// rt 是内核注入的安全运行时(可 nil)。
|
||
//
|
||
// ★ 2026-11-04:改用注入而不是 `RuntimeGlobal()` 包级单例 ——
|
||
// 那个单例绕开了内核统一挂载点(kstate.State.Security)。
|
||
rt *internalSecurity.Runtime
|
||
}
|
||
|
||
// NewSecurityAPI 构造策略声明面(内核装配时注入给每个插件)。
|
||
//
|
||
// pluginName 必须是内核侧的插件标识(Registry 里的 name),
|
||
// **不能**取自插件自己的 Name() 返回值。
|
||
func NewSecurityAPI(pluginName string, rt *internalSecurity.Runtime) pubsdk.SecurityAPI {
|
||
return &securityImpl{pluginName: pluginName, rt: rt}
|
||
}
|
||
|
||
func (s *securityImpl) DeclareProfile(p pubsdk.SecurityProfile) error {
|
||
rt := s.rt
|
||
if rt == nil {
|
||
return fmt.Errorf("security: declare profile %q failed - security module is not assembled "+
|
||
"(the profile would be silently unregistered)", p.Name)
|
||
}
|
||
name := strings.TrimSpace(p.Name)
|
||
if name == "" {
|
||
return fmt.Errorf("security: declare profile failed - empty profile name")
|
||
}
|
||
|
||
// ★ Owner 用内核侧真实插件名覆盖,不采信 p.Owner。
|
||
owner := s.pluginName
|
||
if p.Owner != "" && p.Owner != owner {
|
||
// 不静默改:显式告知插件作者"你填的 owner 被忽略了",
|
||
// 否则他照着错误认知继续写(如以为能给别的插件声明档位)。
|
||
return fmt.Errorf("security: declare profile %q failed - Owner is %q but this plugin is %q "+
|
||
"(a plugin may only declare profiles under its own name)", name, p.Owner, owner)
|
||
}
|
||
|
||
// 名字必须是 `<本插件名>.` 开头。前缀约束在 Runtime 里也有一份,
|
||
// 这里提前判是为了给出**能指向插件作者**的错误文案。
|
||
if !strings.HasPrefix(name, owner+".") {
|
||
return fmt.Errorf("security: declare profile %q failed - name must be prefixed with %q "+
|
||
"(e.g. %q)", name, owner+".", owner+".guest")
|
||
}
|
||
|
||
if err := rt.DeclareProfile(internalSecurity.Profile{
|
||
Name: name,
|
||
Owner: owner,
|
||
Policy: internalSecurity.PrincipalPolicy{
|
||
AllowTools: p.AllowTools,
|
||
DenyTools: p.DenyTools,
|
||
},
|
||
SensitiveWords: p.SensitiveWords,
|
||
ToolSensitiveWords: p.ToolSensitiveWords,
|
||
DetectInjection: p.DetectInjection,
|
||
}); err != nil {
|
||
return fmt.Errorf("security: declare profile %q: %w", name, err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *securityImpl) DeclareProfiles(ps []pubsdk.SecurityProfile) error {
|
||
// 先全量校验再逐个登记:部分成功比全不成功更难排查
|
||
//("为什么 qq.owner 生效了 qq.guest 没生效"这类问题很难定位)。
|
||
// 但 Runtime 的 DeclareProfile 是幂等覆盖的,所以即使中途失败,
|
||
// 已登记的那些也只是"多登记了",不会留下半个档位。
|
||
for i, p := range ps {
|
||
if err := s.DeclareProfile(p); err != nil {
|
||
return fmt.Errorf("profile[%d]: %w", i, err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// 编译期确认实现了公开接口。
|
||
var _ pubsdk.SecurityAPI = (*securityImpl)(nil)
|