Files
HomeAgent/internal/security/gate_test.go
JianFeeeee 47afd67ca1 refactor(security): 删除三份包级单例,共享数据统一挂 kstate
## 用户裁定

> 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 ——
  半成品状态长期红的判据会被人习惯性忽略,那时它就等于不存在。
2026-11-04 11:57:48 +08:00

146 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package security
// 安全边界(Gate / Runtime)的判据。
//
// 这些判据守的是**用户裁定的架构不变量**(2026-10-26):
//
// > 安全模块就是横在其他所有模块与 core 之间的一堵墙:
// > core 发出的请求和外部返回的请求都会经过它审查,通过才能进行下一步。
//
// 所以每一条都在问同一个问题:**这条数据流有没有可能绕过审查**。
// 判据都必须能红(变异实现即失败),否则等于没写。
import (
"strings"
"testing"
)
// testStore 造一个已装好主密钥的变量库(不落盘到真实数据目录)。
func testStore(t *testing.T) *Store {
t.Helper()
dir := t.TempDir()
s, err := MustStore(Options{
Path: dir + "/vars.json",
KeyFn: func() ([]byte, error) { return []byte("0123456789abcdef0123456789abcdef"), nil },
Enabled: true,
})
if err != nil {
t.Fatalf("InitGlobal: %v", err)
}
// 全局单例是进程级的:测试之间要复位,否则互相污染。
return s
}
// ① 出向:明文必须被换成 @{别名},不能原样发给外部。
func TestGateOutboundBlocksPlaintext(t *testing.T) {
s := testStore(t)
g := NewGate(s)
const secret = "ghp_0123456789abcdefghijklmnopqrstuvwxyz"
out, n := g.PrepareOutbound(Inspection{Direction: DirectionOutbound, Stage: "tool_args", Tool: "cmd_run"},
"curl -H 'Authorization: Bearer "+secret+"' https://api.example.com")
if n == 0 {
t.Fatal("出向没有拦截明文 —— 凭据会直接发给外部(这堵墙没起作用)")
}
if strings.Contains(out, secret) {
t.Fatalf("出向结果仍含明文: %q", out)
}
if !strings.Contains(out, "@{sec_") {
t.Fatalf("出向结果应含变量别名,实际: %q", out)
}
}
// ② 入向:外部返回的明文同样必须被换掉。
//
// ★ 为什么入向是必须的:入向内容直接进模型上下文(role=tool)。
// 只在"写记忆"时清洗是最坏组合 —— 记忆侧干净、当轮已泄漏、事后查不到。
func TestGateInboundScrubsPlaintext(t *testing.T) {
s := testStore(t)
g := NewGate(s)
const secret = "ghp_abcdefghijklmnopqrstuvwxyz0123456789"
out, n := g.ScrubInbound(Inspection{Direction: DirectionInbound, Stage: "tool_result", Tool: "cmd_run"},
"server said: token="+secret)
if n == 0 {
t.Fatal("入向没有清洗明文 —— 明文会原样进入模型上下文")
}
if strings.Contains(out, secret) {
t.Fatalf("入向结果仍含明文: %q", out)
}
}
// ③ 执行前回填:外部必须拿到真实值("插件无感知"的既有语义)。
func TestGateFillsAliasBeforeExecution(t *testing.T) {
s := testStore(t)
g := NewGate(s)
const secret = "ghp_FILLME0123456789abcdefghijklmnopqrst"
alias, err := s.Submit(secret, "test")
if err != nil {
t.Fatal(err)
}
out, n := g.FillForExecution(Inspection{Direction: DirectionOutbound, Stage: "plugin_call"},
"token=@{"+alias+"}")
if n == 0 {
t.Fatal("回填没有发生 —— 插件会收到字面量 @{别名} 而不是真实值")
}
if !strings.Contains(out, secret) {
t.Fatalf("回填结果不含真实值: %q", out)
}
}
// ④ 未启用时零成本透传(且**不是**"装配失败")。
//
// 判据同时钉住两件事:Disabled 不是 nil(调用点只有一条路径),
// 且它真的不改写内容(不能假装审查过)。
func TestDisabledRuntimePassesThrough(t *testing.T) {
r := NewDisabledRuntime()
if r == nil {
t.Fatal("Disabled Runtime 必须是有效对象,不能让调用点各自判 nil")
}
if r.Enabled() {
t.Fatal("Disabled Runtime 不应报告 enabled")
}
const text = "ghp_SHOULDNOTBECHANGED0123456789abcdefgh"
if out, n := r.PrepareOutbound(Inspection{Stage: "x"}, text); n != 0 || out != text {
t.Errorf("disabled 出向应原样返回: n=%d out=%q", n, out)
}
if out, n := r.ScrubInbound(Inspection{Stage: "x"}, text); n != 0 || out != text {
t.Errorf("disabled 入向应原样返回: n=%d out=%q", n, out)
}
if out, n := r.FillForExecution(Inspection{Stage: "x"}, text); n != 0 || out != text {
t.Errorf("disabled 回填应原样返回: n=%d out=%q", n, out)
}
}
// ⑤ 启用后 Runtime 必须报告 enabled —— 状态面据此证明"审查真的在生效",
// 而不是靠读配置猜(配置开了但装配失败是另一件事)。
func TestEnabledRuntimeReportsEnabled(t *testing.T) {
s := testStore(t)
r := NewRuntime(s)
if !r.Enabled() {
t.Fatal("装好变量库的 Runtime 必须报告 enabled")
}
}
// ⑥ nil Runtime 不得 panic(装配期错误不该变成崩溃)。
func TestNilRuntimeIsSafe(t *testing.T) {
var r *Runtime
if out, n := r.PrepareOutbound(Inspection{}, "x"); n != 0 || out != "x" {
t.Errorf("nil runtime 出向应透传")
}
if out, n := r.ScrubInbound(Inspection{}, "x"); n != 0 || out != "x" {
t.Errorf("nil runtime 入向应透传")
}
if r.Enabled() {
t.Error("nil runtime 不应报告 enabled")
}
if r.Store() != nil {
t.Error("nil runtime 不应给出 Store")
}
}