diff --git a/internal/requestredact/redact.go b/internal/requestredact/redact.go index d65b562651..435a83eb56 100644 --- a/internal/requestredact/redact.go +++ b/internal/requestredact/redact.go @@ -462,8 +462,6 @@ 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 var toolResult func(gjson.Result, int) error // 签名内容由上游生成,必须逐字节还给上游:签名块与记录完全一致时按位置换回上游原文和原签名; @@ -498,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 @@ -568,13 +581,9 @@ 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); 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 @@ -586,6 +595,10 @@ func (c *Compiled) apply(body []byte, data, decisions bool, depth int, patchCoun 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) diff --git a/internal/requestredact/signature.go b/internal/requestredact/signature.go index fa9eaef6a7..e2d441a0eb 100644 --- a/internal/requestredact/signature.go +++ b/internal/requestredact/signature.go @@ -329,21 +329,39 @@ 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) { +// signatureArray 报告上游生成内容的对象(消息、推理项)直属的该字段数组是否可能含协议签名块。 +func signatureArray(field string) bool { + switch field { + case "content", "parts", "reasoning_details", "tool_calls": + return true + } + return false +} + +// signaturePosition 返回协议签名位置上的上游签名及其在对象里的路径。只认响应侧会写入还原记录的位置, +// 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 - 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..5dea25efc5 100644 --- a/internal/requestredact/signature_test.go +++ b/internal/requestredact/signature_test.go @@ -93,24 +93,81 @@ 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"}}}]}]}`, + // 同名数组不在模型消息的直属字段上,也不是协议位置。 + "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" { + 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) } } }