stderr 的 JSONL 是无主人在场的落盘面,却在此前原样落工具结果(result)、
子代理回复(text/summary)与上游报文,等于一次 tail 就能拷走整段对话。
本切片交付两件地基:
1) 写侧脱敏(trace.JSONRecorder)
- 新增 trace/span_props.go:safePropKeys 白名单与 projectSpanProps 投影,
读侧(safeSpanProps)与写侧共用同一张表、同一个投影函数;
- WriteSpan 落盘前经 trace.SanitizeRecord 投影:白名单键清洗,表外键只留
Digest(bytes/runes/sha256 前缀,收进 omitted),事件先 RedactEvent 再把
正文键折成 Digest。落到磁盘的只有元数据,正文一个字节都不留;
- 新增 trace/digest.go:Digest/DigestOf/DigestOfValue,给「脱敏后仍要能诊断」
一个构造上不含正文的取值形态(体积 + 指纹),两条投影都按类型放行它。
2) 共享上游错误体收敛 helper(给 M41/M42,禁止三家各写一份)
- trace.UpstreamError(upstream, status, contentType, body):只保留 status /
contentType / bodyLen / body sha256 前缀 / 上游 JSON 的 error.code /
error.type / 截断 128 rune 的 error.message,其余一律丢弃,响应体原文绝不
进 error 或日志;*UpstreamErrorInfo 提供 Error()/Attrs()/Unwrap()(errors.Is
可判 ErrUpstream 走降级链);
- trace.HostOf / trace.MaskQuery:日志里的 URL 只回来源,凭据类 query 与
fragment 值打码为 ***,user:pass@ 整段打码。
3) 剥掉三处正文上 span
- loop:tool span 不再放 result,改放 result_summary(与 tool_execution.end
帧同源、已脱敏截断)与 result_digest;
- orchestration:subagent span 不再放 text,subagent.done 事件不再放 summary,
改记 status/steps/chars/sha256;
- automations:AddRuleByName 不再 %v 整个 payload,改记 subject/type/bytes/sha256。
测试:以哨兵正文在落盘字节上断言(JSONL / slog 输出),并分别对「编排层收到的
原始 span」与「写侧投影」两层各钉一遍;UpstreamError 用回显 input 的 422 报文
与 HTML 错误页各钉一条;解析出/未解析出 JSON 都不回原文。以上均做过变异验证
(去掉保护即变红)。
验收:go build ./... + go vet ./... + go test -count=1 ./... 全绿;
trace/loop/orchestration 另跑 -race 全绿。
144 lines
5.3 KiB
Go
144 lines
5.3 KiB
Go
package trace
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestRedactTextScrubsCredentials asserts every credential shape the dashboard
|
|
// could be handed is replaced, while ordinary text passes through unchanged.
|
|
func TestRedactTextScrubsCredentials(t *testing.T) {
|
|
t.Parallel()
|
|
secrets := []string{
|
|
"api_key=sk-abcdef0123456789",
|
|
`{"token":"abcdef1234567890"}`,
|
|
"password=hunter2hunter2",
|
|
"Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.abc.def",
|
|
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4",
|
|
"access_token: ghp_0123456789abcdefghij",
|
|
}
|
|
for _, in := range secrets {
|
|
got := RedactText(in)
|
|
if !strings.Contains(got, redactedMarker) {
|
|
t.Fatalf("RedactText(%q) = %q, want the redaction marker", in, got)
|
|
}
|
|
for _, leak := range []string{"sk-abcdef0123456789", "hunter2hunter2", "abcdef1234567890",
|
|
"eyJhbGciOiJIUzI1NiJ9", "ghp_0123456789abcdefghij"} {
|
|
if strings.Contains(got, leak) {
|
|
t.Fatalf("RedactText(%q) = %q, still carries %q", in, got, leak)
|
|
}
|
|
}
|
|
}
|
|
|
|
benign := "the tool returned 3 rows; stop_reason=tool_calls"
|
|
if got := RedactText(benign); got != benign {
|
|
t.Fatalf("RedactText(%q) = %q, want it unchanged", benign, got)
|
|
}
|
|
if got := truncate("", maxSummaryRunes); got != "" {
|
|
t.Fatalf("empty text = %q, want empty", got)
|
|
}
|
|
long := strings.Repeat("x", maxSummaryRunes+10)
|
|
if got := RedactText(long); !strings.HasSuffix(got, "…") || len([]rune(got)) != maxSummaryRunes+1 {
|
|
t.Fatalf("long text was not truncated to the summary bound: %d runes", len([]rune(got)))
|
|
}
|
|
}
|
|
|
|
// TestRedactEventScrubsNestedValues asserts a secret nested in a map or a slice is
|
|
// scrubbed too — the shapes a tool result or error payload actually has.
|
|
func TestRedactEventScrubsNestedValues(t *testing.T) {
|
|
t.Parallel()
|
|
ev := Event{Type: "tool.done", Props: map[string]any{
|
|
"tool": "http",
|
|
"headers": map[string]any{
|
|
"Authorization": "Bearer sk-live-0123456789abcdef",
|
|
},
|
|
"args_keys": []string{"api_key", "url"},
|
|
"nested": []any{map[string]any{"password": "hunter2hunter2"}},
|
|
"count": 2,
|
|
}}
|
|
|
|
got := RedactEvent(ev)
|
|
if got.Type != ev.Type || got.Time != ev.Time {
|
|
t.Fatalf("event envelope changed: %+v", got)
|
|
}
|
|
blob, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
for _, leak := range []string{"sk-live-0123456789abcdef", "hunter2hunter2"} {
|
|
if strings.Contains(string(blob), leak) {
|
|
t.Fatalf("redacted event still carries %q: %s", leak, blob)
|
|
}
|
|
}
|
|
if count, ok := got.Props["count"].(int); !ok || count != 2 {
|
|
t.Fatalf("non-string prop = %v, want it passed through", got.Props["count"])
|
|
}
|
|
// The original event must not be mutated (a caller may still need it).
|
|
if orig, _ := ev.Props["headers"].(map[string]any)["Authorization"].(string); !strings.Contains(orig, "sk-live-") {
|
|
t.Fatalf("RedactEvent mutated the caller's event: %v", orig)
|
|
}
|
|
}
|
|
|
|
// TestNodePropsAndLogAreRedacted asserts the published graph — both a tool node's
|
|
// result_summary prop and its log events — never carries a credential. This is the
|
|
// design/19 §3 red line at the last point before publication.
|
|
//
|
|
// M40 之后 result_summary 是**生产侧**写入的已脱敏摘要(loop/recordToolResult),
|
|
// 读侧只做白名单投影,不再从结果原文反推。因此这里同时钉住两件事:白名单键
|
|
// 的值仍被逐层清洗;而任何「结果原文」形态的 prop(表外的 result)根本不出图,
|
|
// 哪怕某个生产者又把它塞了回来。
|
|
func TestNodePropsAndLogAreRedacted(t *testing.T) {
|
|
t.Parallel()
|
|
rec := spanRec("t1", "step1", "tool.http", SpanKindTool, SpanStatusOK,
|
|
map[string]any{
|
|
"result_summary": `{"api_key":"sk-abcdef0123456789"}`,
|
|
"result": map[string]any{"body": `{"api_key":"sk-abcdef0123456789"}`},
|
|
})
|
|
rec.Events = []Event{{
|
|
Type: "tool.done",
|
|
Props: map[string]any{
|
|
"tool": "http",
|
|
"args_keys": []string{"api_key"},
|
|
"result_summary": `{"api_key":"sk-abcdef0123456789"}`,
|
|
},
|
|
}}
|
|
|
|
g := graphFor([]SpanRecord{rec}, "token=abcdef1234567890", "reply")
|
|
node := nodeByID(g, "t1")
|
|
if len(node.Log) != 1 || node.Log[0].Type != "tool.done" {
|
|
t.Fatalf("node log = %+v, want the recorded event", node.Log)
|
|
}
|
|
blob, err := json.Marshal(g)
|
|
if err != nil {
|
|
t.Fatalf("marshal graph: %v", err)
|
|
}
|
|
if strings.Contains(string(blob), "sk-abcdef0123456789") {
|
|
t.Fatalf("published graph leaked the tool result: %s", blob)
|
|
}
|
|
if !strings.Contains(string(blob), redactedMarker) {
|
|
t.Fatalf("published graph carries no redaction marker: %s", blob)
|
|
}
|
|
if got, ok := node.Props["result_summary"].(string); !ok || !strings.Contains(got, redactedMarker) {
|
|
t.Fatalf("result_summary prop = %v, want it scrubbed", node.Props["result_summary"])
|
|
}
|
|
if _, ok := node.Props["result"]; ok {
|
|
t.Fatalf("表外 prop result 出图了:%+v(读侧只投影白名单)", node.Props)
|
|
}
|
|
// The round's own input/output labels are scrubbed as well.
|
|
if in := nodeByID(g, "input").Label; strings.Contains(in, "abcdef1234567890") {
|
|
t.Fatalf("input label leaked a credential: %q", in)
|
|
}
|
|
}
|
|
|
|
// TestRedactEventKeepsEmptyPropsNil guards the wire shape: an event without props
|
|
// must not gain an empty map.
|
|
func TestRedactEventKeepsEmptyPropsNil(t *testing.T) {
|
|
t.Parallel()
|
|
ev := Event{Type: "llm.done", Time: time.Now()}
|
|
if got := RedactEvent(ev); got.Props != nil {
|
|
t.Fatalf("props = %v, want nil", got.Props)
|
|
}
|
|
}
|