From 6430274d515963201bd902b73b5c15b14cf0660c Mon Sep 17 00:00:00 2001 From: tbphp Date: Wed, 18 Nov 2026 09:22:21 +0800 Subject: [PATCH 1/2] fix(redaction): recognize signatures only at protocol positions Gemini placeholder signatures are no longer treated as upstream signatures, and signature-like blocks outside the positions responses record are redacted as ordinary upstream-authored content. --- internal/requestredact/redact.go | 13 ++-- internal/requestredact/signature.go | 29 ++++++--- internal/requestredact/signature_test.go | 79 ++++++++++++++++++++---- 3 files changed, 94 insertions(+), 27 deletions(-) diff --git a/internal/requestredact/redact.go b/internal/requestredact/redact.go index d65b562651..ca849bbc24 100644 --- a/internal/requestredact/redact.go +++ b/internal/requestredact/redact.go @@ -465,6 +465,8 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun // inSigned 表示正在按普通上游内容处理客户端改动过的签名内容,内部不再识别签名。 inSigned := false var walk func(gjson.Result, bool, bool, int, bool) error + // visit 另带 v 所在的字段名(数组元素取数组的字段名),用来识别协议签名位置。 + var visit func(gjson.Result, string, bool, bool, int, bool) error var toolResult func(gjson.Result, int) error // 签名内容由上游生成,必须逐字节还给上游:签名块与记录完全一致时按位置换回上游原文和原签名; // 普通签名说明网关没有往里还原过内容,原样发回;有任何改动、新增或签名之后才到的内容时, @@ -533,6 +535,9 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun return walk(value, true, false, depth, false) } walk = func(v gjson.Result, content, data bool, depth int, questions bool) error { + return visit(v, "", content, data, depth, questions) + } + visit = func(v gjson.Result, field string, content, data bool, depth int, questions bool) error { if depth > 64 { return ErrContent } @@ -569,7 +574,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun } } if !data && inModel && !inSigned && v.IsObject() { - if signature, path, found := signaturePosition(v); found { + if signature, path, found := signaturePosition(v, field); found { return signed(v, signature, path, content, depth, questions) } } @@ -580,7 +585,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun var failure error v.ForEach(func(k, child gjson.Result) bool { if v.IsArray() || data { - failure = walk(child, content, data, depth+1, questions) + failure = visit(child, field, content, data, depth+1, questions) return failure == nil } if inModel && ProtocolField(k.Str) { @@ -634,7 +639,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun if jsonToolResults && (v.Get("role").Str == "tool" || v.Get("role").Str == "function" || v.Get("type").Str == "tool_result") { failure = toolResult(child, depth+1) } else { - failure = walk(child, true, false, depth+1, questions) + failure = visit(child, k.Str, true, false, depth+1, questions) } case "prompt": if !child.IsObject() { @@ -654,7 +659,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun case "text", "system", "system_instruction", "systemInstruction", "instructions", "query", "thinking", "reasoning", "reasoning_content", "summary", "code", "refusal", "description", "title", "documents", "texts": failure = walk(child, true, questions && depth == 2 && k.Str == "instructions", depth+1, questions) default: - failure = walk(child, inModel, false, depth+1, questions) + failure = visit(child, k.Str, inModel, false, depth+1, questions) } return failure == nil }) diff --git a/internal/requestredact/signature.go b/internal/requestredact/signature.go index fa9eaef6a7..3814b10425 100644 --- a/internal/requestredact/signature.go +++ b/internal/requestredact/signature.go @@ -329,21 +329,30 @@ func restoreSignedString(value string, entry signatureString) (string, bool) { return restored, utf8.ValidString(restored) } -// signaturePosition 返回协议签名位置上的非空签名及其在对象里的路径:Claude 思考块、 -// Responses 推理文本、reasoning_details 条目、Gemini 兼容接口工具调用的 extra_content,以及 Gemini 片段。 -func signaturePosition(v gjson.Result) (gjson.Result, []any, bool) { +// signaturePosition 返回协议签名位置上的上游签名及其在对象里的路径。只认响应侧会写入还原记录的位置, +// field 是对象所在数组的字段名:content 里的 Claude 思考块与 Responses 推理文本、reasoning_details 条目、 +// tool_calls 里 Gemini 兼容接口的 extra_content,以及 Gemini 的 parts。其他位置上形似签名块的内容 +// 按普通上游内容处理,否则网关还原到那里的明文会在下一轮原样外发。 +func signaturePosition(v gjson.Result, field string) (gjson.Result, []any, bool) { var path []any - kind := v.Get("type").Str - switch { - case kind == "thinking" || kind == "reasoning_text" || strings.HasPrefix(kind, "reasoning."): + switch kind := v.Get("type").Str; { + case field == "content" && (kind == "thinking" || kind == "reasoning_text"), field == "reasoning_details": path = []any{"signature"} - case v.Get("function").IsObject(): + case field == "tool_calls": path = []any{"extra_content", "google", "thought_signature"} - case v.Get("thoughtSignature").Exists(): + case field == "parts" && v.Get("thoughtSignature").Exists(): path = []any{"thoughtSignature"} - default: + case field == "parts": path = []any{"thought_signature"} + default: + return gjson.Result{}, nil, false } signature := PathValue(v, path) - return signature, path, signature.Type == gjson.String && signature.Str != "" + return signature, path, signature.Type == gjson.String && signature.Str != "" && !placeholderSignature(signature.Str) +} + +// placeholderSignature 报告签名是否为 Gemini 文档给出的占位值。客户端转接其他来源的历史时填入, +// 内容不是上游签过的,上游也不校验,按普通上游内容加密不会导致验签失败。 +func placeholderSignature(value string) bool { + return value == "skip_thought_signature_validator" || value == "context_engineering_is_the_way_to_go" } diff --git a/internal/requestredact/signature_test.go b/internal/requestredact/signature_test.go index aac819e236..d2ba978b5d 100644 --- a/internal/requestredact/signature_test.go +++ b/internal/requestredact/signature_test.go @@ -93,24 +93,77 @@ func TestSignatureRecordRoundTrip(t *testing.T) { func TestApplyLeavesUnrecordedSignedContentUnchanged(t *testing.T) { cipher := signedTestCipher(t, 3) - signed := []string{ - `{"type":"thinking","thinking":"test with bob@example.com","signature":"SIG"}`, - `{"functionCall":{"name":"lookup_user","args":{"email":"bob@example.com"}},"thoughtSignature":"SIG"}`, - `{"type":"reasoning.text","text":"bob@example.com","signature":"SIG"}`, - `{"id":"call_1","type":"function","function":{"name":"lookup","arguments":"{\"email\":\"bob@example.com\"}"},"extra_content":{"google":{"thought_signature":"SIG"}}}`, + chatUser := `{"role":"user","content":"alice@example.com"}` + signed := []struct{ body, block, user, signed string }{ + {`{"messages":[` + chatUser + `,{"role":"assistant","content":[%s]}]}`, "messages.1.content.0", "messages.0.content", + `{"type":"thinking","thinking":"test with bob@example.com","signature":"SIG"}`}, + {`{"contents":[{"role":"user","parts":[{"text":"alice@example.com"}]},{"role":"model","parts":[%s]}]}`, "contents.1.parts.0", "contents.0.parts.0.text", + `{"functionCall":{"name":"lookup_user","args":{"email":"bob@example.com"}},"thoughtSignature":"SIG"}`}, + {`{"messages":[` + chatUser + `,{"role":"assistant","content":"ok","reasoning_details":[%s]}]}`, "messages.1.reasoning_details.0", "messages.0.content", + `{"type":"reasoning.text","text":"bob@example.com","signature":"SIG"}`}, + {`{"messages":[` + chatUser + `,{"role":"assistant","tool_calls":[%s]}]}`, "messages.1.tool_calls.0", "messages.0.content", + `{"id":"call_1","type":"function","function":{"name":"lookup","arguments":"{\"email\":\"bob@example.com\"}"},"extra_content":{"google":{"thought_signature":"SIG"}}}`}, + {`{"input":[` + chatUser + `,{"type":"reasoning","summary":[],"content":[%s]}]}`, "input.1.content.0", "input.0.content", + `{"type":"reasoning_text","text":"bob@example.com","signature":"SIG"}`}, } for mode, compiled := range signedTestRules(t) { - for _, block := range signed { - body := `{"messages":[{"role":"user","content":"alice@example.com"},{"role":"assistant","content":[` + block + `]}]}` - got, err := compiled.ApplyWithCipher([]byte(body), cipher) + for _, tc := range signed { + got, err := compiled.ApplyWithCipher([]byte(strings.Replace(tc.body, "%s", tc.signed, 1)), cipher) if err != nil { - t.Fatalf("%s %s: %v", mode, block, err) + t.Fatalf("%s %s: %v", mode, tc.signed, err) + } + if out := gjson.GetBytes(got, tc.block).Raw; out != tc.signed { + t.Errorf("%s: signed content changed:\n got: %s\nwant: %s", mode, out, tc.signed) } - if out := gjson.GetBytes(got, "messages.1.content.0").Raw; out != block { - t.Errorf("%s: signed content changed:\n got: %s\nwant: %s", mode, out, block) + if mode != "none" && gjson.GetBytes(got, tc.user).Str == "alice@example.com" { + t.Errorf("%s: unsigned user content was not protected: %s", mode, got) } - if mode != "none" && gjson.GetBytes(got, "messages.0.content").Str == "alice@example.com" { - t.Errorf("%s: unsigned user content was not protected", mode) + } + } +} + +// Gemini 文档给出的两个占位签名不是上游签过的内容,上游也不校验:按普通上游内容脱敏,签名原样保留。 +func TestApplyRedactsPlaceholderSignedContent(t *testing.T) { + cipher := signedTestCipher(t, 3) + for _, placeholder := range []string{"skip_thought_signature_validator", "context_engineering_is_the_way_to_go"} { + bodies := map[string]string{ + "gemini thoughtSignature": `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"send","args":{"to":"alice@example.com"}},"thoughtSignature":"` + placeholder + `"}]}]}`, + "gemini thought_signature": `{"contents":[{"role":"model","parts":[{"text":"alice@example.com","thought_signature":"` + placeholder + `"}]}]}`, + "chat tool call": `{"messages":[{"role":"assistant","tool_calls":[{"id":"c1","type":"function","function":{"name":"send","arguments":"{\"to\":\"alice@example.com\"}"},"extra_content":{"google":{"thought_signature":"` + placeholder + `"}}}]}]}`, + } + for mode, compiled := range signedTestRules(t) { + if mode == "none" { + continue + } + for name, body := range bodies { + got, err := compiled.ApplyWithCipher([]byte(body), cipher) + if err != nil || strings.Contains(string(got), "alice@example.com") || !strings.Contains(string(got), placeholder) { + t.Errorf("%s/%s/%s: placeholder-signed content = %s / %v", mode, placeholder, name, got, err) + } + } + } + } +} + +// 签名只认响应侧会写入还原记录的协议位置;其他位置上形似签名块的内容按普通上游内容脱敏, +// 否则网关还原到这些位置的明文会在下一轮原样外发。 +func TestApplyRedactsSignatureLookalikesOutsideProtocolPositions(t *testing.T) { + cipher := signedTestCipher(t, 3) + bodies := map[string]string{ + "chat thinking_blocks": `{"messages":[{"role":"assistant","content":"ok","thinking_blocks":[{"type":"thinking","thinking":"alice@example.com","signature":"SIG"}]}]}`, + "text block thought_signature": `{"messages":[{"role":"assistant","content":[{"type":"text","text":"alice@example.com","thought_signature":"SIG"}]}]}`, + "gemini part in content": `{"messages":[{"role":"assistant","content":[{"functionCall":{"name":"send","args":{"to":"alice@example.com"}},"thoughtSignature":"SIG"}]}]}`, + "reasoning detail in content": `{"messages":[{"role":"assistant","content":[{"type":"reasoning.text","text":"alice@example.com","signature":"SIG"}]}]}`, + "tool call in content": `{"messages":[{"role":"assistant","content":[{"id":"c1","type":"function","function":{"name":"send","arguments":"{\"to\":\"alice@example.com\"}"},"extra_content":{"google":{"thought_signature":"SIG"}}}]}]}`, + } + for mode, compiled := range signedTestRules(t) { + if mode == "none" { + continue + } + for name, body := range bodies { + got, err := compiled.ApplyWithCipher([]byte(body), cipher) + if err != nil || strings.Contains(string(got), "alice@example.com") { + t.Errorf("%s/%s: content outside protocol signature positions was not redacted: %s / %v", mode, name, got, err) } } } From 4e8c53150d68080950dcd06b235c30a538fbcb7d Mon Sep 17 00:00:00 2001 From: tbphp Date: Wed, 18 Nov 2026 09:57:51 +0800 Subject: [PATCH 2/2] fix(redaction): limit signatures to direct protocol arrays Recognize signed blocks only in the content, parts, reasoning_details, and tool_calls arrays held directly by a model message or reasoning item, so same-named arrays nested deeper are redacted as ordinary content. --- internal/requestredact/redact.go | 44 ++++++++++++++---------- internal/requestredact/signature.go | 15 ++++++-- internal/requestredact/signature_test.go | 4 +++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/internal/requestredact/redact.go b/internal/requestredact/redact.go index ca849bbc24..435a83eb56 100644 --- a/internal/requestredact/redact.go +++ b/internal/requestredact/redact.go @@ -462,11 +462,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun // 上游生成、随历史回传的内容(助手消息、推理与各类工具调用)按字段整体加密, // 与响应侧整体还原对应,只跳过协议字段。 inModel := false - // inSigned 表示正在按普通上游内容处理客户端改动过的签名内容,内部不再识别签名。 - inSigned := false var walk func(gjson.Result, bool, bool, int, bool) error - // visit 另带 v 所在的字段名(数组元素取数组的字段名),用来识别协议签名位置。 - var visit func(gjson.Result, string, bool, bool, int, bool) error var toolResult func(gjson.Result, int) error // 签名内容由上游生成,必须逐字节还给上游:签名块与记录完全一致时按位置换回上游原文和原签名; // 普通签名说明网关没有往里还原过内容,原样发回;有任何改动、新增或签名之后才到的内容时, @@ -500,10 +496,25 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun } return nil } - inSigned = true - defer func() { inSigned = false }() return walk(v, content, false, depth, questions) } + // signedArray 遍历上游生成内容的对象(消息、推理项)直属的协议数组:只有这些元素可能是签名块, + // 与响应侧写入还原记录的位置一致;更深处同名的数组和对象都按普通上游内容处理。 + signedArray := func(array gjson.Result, field string, depth int, questions bool) error { + if depth > 64 { + return ErrContent + } + var failure error + array.ForEach(func(_, element gjson.Result) bool { + if signature, path, found := signaturePosition(element, field); element.IsObject() && found { + failure = signed(element, signature, path, true, depth+1, questions) + } else { + failure = walk(element, true, false, depth+1, questions) + } + return failure == nil + }) + return failure + } toolResult = func(value gjson.Result, depth int) error { if depth > 64 { return ErrContent @@ -535,9 +546,6 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun return walk(value, true, false, depth, false) } walk = func(v gjson.Result, content, data bool, depth int, questions bool) error { - return visit(v, "", content, data, depth, questions) - } - visit = func(v gjson.Result, field string, content, data bool, depth int, questions bool) error { if depth > 64 { return ErrContent } @@ -573,24 +581,24 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun return nil } } - if !data && inModel && !inSigned && v.IsObject() { - if signature, path, found := signaturePosition(v, field); found { - return signed(v, signature, path, content, depth, questions) - } - } + signedRoot := false if !data && !inModel && v.IsObject() && modelAuthored(v) { - inModel = true + inModel, signedRoot = true, true defer func() { inModel = false }() } var failure error v.ForEach(func(k, child gjson.Result) bool { if v.IsArray() || data { - failure = visit(child, field, content, data, depth+1, questions) + failure = walk(child, content, data, depth+1, questions) return failure == nil } if inModel && ProtocolField(k.Str) { return true } + if signedRoot && child.IsArray() && signatureArray(k.Str) { + failure = signedArray(child, k.Str, depth+1, questions) + return failure == nil + } switch k.Str { case "questions": failure = walk(child, false, false, depth+1, decisions && depth == 0) @@ -639,7 +647,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun if jsonToolResults && (v.Get("role").Str == "tool" || v.Get("role").Str == "function" || v.Get("type").Str == "tool_result") { failure = toolResult(child, depth+1) } else { - failure = visit(child, k.Str, true, false, depth+1, questions) + failure = walk(child, true, false, depth+1, questions) } case "prompt": if !child.IsObject() { @@ -659,7 +667,7 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun case "text", "system", "system_instruction", "systemInstruction", "instructions", "query", "thinking", "reasoning", "reasoning_content", "summary", "code", "refusal", "description", "title", "documents", "texts": failure = walk(child, true, questions && depth == 2 && k.Str == "instructions", depth+1, questions) default: - failure = visit(child, k.Str, inModel, false, depth+1, questions) + failure = walk(child, inModel, false, depth+1, questions) } return failure == nil }) diff --git a/internal/requestredact/signature.go b/internal/requestredact/signature.go index 3814b10425..e2d441a0eb 100644 --- a/internal/requestredact/signature.go +++ b/internal/requestredact/signature.go @@ -329,10 +329,19 @@ func restoreSignedString(value string, entry signatureString) (string, bool) { return restored, utf8.ValidString(restored) } +// signatureArray 报告上游生成内容的对象(消息、推理项)直属的该字段数组是否可能含协议签名块。 +func signatureArray(field string) bool { + switch field { + case "content", "parts", "reasoning_details", "tool_calls": + return true + } + return false +} + // signaturePosition 返回协议签名位置上的上游签名及其在对象里的路径。只认响应侧会写入还原记录的位置, -// field 是对象所在数组的字段名:content 里的 Claude 思考块与 Responses 推理文本、reasoning_details 条目、 -// tool_calls 里 Gemini 兼容接口的 extra_content,以及 Gemini 的 parts。其他位置上形似签名块的内容 -// 按普通上游内容处理,否则网关还原到那里的明文会在下一轮原样外发。 +// v 是上游生成内容的对象直属数组 field 的元素:content 里的 Claude 思考块与 Responses 推理文本、 +// reasoning_details 条目、tool_calls 里 Gemini 兼容接口的 extra_content,以及 Gemini 的 parts。 +// 其他位置上形似签名块的内容按普通上游内容处理,否则网关还原到那里的明文会在下一轮原样外发。 func signaturePosition(v gjson.Result, field string) (gjson.Result, []any, bool) { var path []any switch kind := v.Get("type").Str; { diff --git a/internal/requestredact/signature_test.go b/internal/requestredact/signature_test.go index d2ba978b5d..5dea25efc5 100644 --- a/internal/requestredact/signature_test.go +++ b/internal/requestredact/signature_test.go @@ -155,6 +155,10 @@ func TestApplyRedactsSignatureLookalikesOutsideProtocolPositions(t *testing.T) { "gemini part in content": `{"messages":[{"role":"assistant","content":[{"functionCall":{"name":"send","args":{"to":"alice@example.com"}},"thoughtSignature":"SIG"}]}]}`, "reasoning detail in content": `{"messages":[{"role":"assistant","content":[{"type":"reasoning.text","text":"alice@example.com","signature":"SIG"}]}]}`, "tool call in content": `{"messages":[{"role":"assistant","content":[{"id":"c1","type":"function","function":{"name":"send","arguments":"{\"to\":\"alice@example.com\"}"},"extra_content":{"google":{"thought_signature":"SIG"}}}]}]}`, + // 同名数组不在模型消息的直属字段上,也不是协议位置。 + "nested parts": `{"messages":[{"role":"assistant","content":"ok","extra":{"parts":[{"text":"alice@example.com","thoughtSignature":"SIG"}]}}]}`, + "nested content": `{"messages":[{"role":"assistant","content":"ok","extra":{"content":[{"type":"thinking","thinking":"alice@example.com","signature":"SIG"}]}}]}`, + "array inside content": `{"messages":[{"role":"assistant","content":[[{"type":"thinking","thinking":"alice@example.com","signature":"SIG"}]]}]}`, } for mode, compiled := range signedTestRules(t) { if mode == "none" {