Files
sub2api/backend/internal/service/openai_cyber_transcript.go
T
shaw 7e3b6c72fa fix(openai): 按已屏蔽前缀长度精确匹配 cyber transcript,修复长会话误拦 (#8519)
同一来源触发 cyber 屏蔽后,scope 激活期间任何历史超过 256 个 item 的
会话都会因 lookup 截断被 fail-closed 判为 transcript_lookup_limit_exceeded,
即使与被屏蔽会话毫无关系、且带有不同的显式 session_id。

改为在命中时记录 transcript 屏蔽 key 覆盖的历史长度(按 API Key 建 Redis
SET 索引,随 scope 同 TTL),查询时仅在这些长度处重算前缀哈希:
- 无关长会话不再被误拦,历史长度本身不再决定屏蔽
- 追加任意数量 item 仍能命中被屏蔽前缀,防逃逸能力不降
- 查询 MGET 由最多 256 key 降为记录长度数,遍历在最长记录长度处提前终止
- scope 门控、API Key 命名空间与 cyber-transcript:v3 哈希格式均不变
2026-11-05 21:51:26 +08:00

177 lines
5.5 KiB
Go

package service
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"strconv"
"strings"
"github.com/tidwall/gjson"
)
// CyberSessionTranscriptBlock is a cumulative history-prefix key together with
// the number of history items it covers. Recording the length lets lookups
// recompute exactly that prefix, so long or padded continuations stay exact.
type CyberSessionTranscriptBlock struct {
Key string
Length int
}
// CyberSessionTranscriptBlocks returns the exact full-history block followed
// by an optional rewrite-tolerant block ending before the latest user turn.
// The latter requires model-generated history so shared first-turn templates
// cannot block unrelated conversations.
func CyberSessionTranscriptBlocks(apiKeyID int64, body []byte) []CyberSessionTranscriptBlock {
var full, preLatestUser CyberSessionTranscriptBlock
// This is an entropy heuristic, not provenance proof: authenticated
// server-side history would be required to distinguish fixed few-shot
// assistant items perfectly.
hasModelGeneratedItem := false
walkOpenAICyberTranscript(apiKeyID, body, func(item gjson.Result, length int, prefixKey func() string) bool {
if openAICyberTranscriptItemStartsUserTurn(item) && hasModelGeneratedItem && full.Length > 0 {
preLatestUser = full
}
full = CyberSessionTranscriptBlock{Key: prefixKey(), Length: length}
if openAICyberTranscriptItemIsModelGenerated(item) {
hasModelGeneratedItem = true
}
return true
})
if full.Length == 0 {
return nil
}
blocks := []CyberSessionTranscriptBlock{full}
if preLatestUser.Length > 0 {
blocks = append(blocks, preLatestUser)
}
return blocks
}
// cyberSessionTranscriptKeysAt recomputes the transcript prefix keys at the
// recorded block lengths, in ascending length order. Lengths beyond the
// request history cannot match and are skipped.
func cyberSessionTranscriptKeysAt(apiKeyID int64, body []byte, lengths []int) []string {
wanted := make(map[int]struct{}, len(lengths))
maxLength := 0
for _, length := range lengths {
if length > 0 {
wanted[length] = struct{}{}
maxLength = max(maxLength, length)
}
}
if len(wanted) == 0 {
return nil
}
keys := make([]string, 0, len(wanted))
walkOpenAICyberTranscript(apiKeyID, body, func(_ gjson.Result, length int, prefixKey func() string) bool {
if _, ok := wanted[length]; ok {
keys = append(keys, prefixKey())
}
return length < maxLength
})
return keys
}
// walkOpenAICyberTranscript hashes model-visible history cumulatively and calls
// visit after each item with the 1-based prefix length. prefixKey returns the
// key of the prefix ending at that item and is only valid during the call.
// Returning false from visit stops the walk.
func walkOpenAICyberTranscript(apiKeyID int64, body []byte, visit func(item gjson.Result, length int, prefixKey func() string) bool) {
if len(body) == 0 {
return
}
root := openAIRequestPayloadView(body)
if !root.Exists() || !root.IsObject() {
return
}
h := sha256.New()
_, _ = h.Write([]byte("cyber-transcript:v3|api_key="))
_, _ = h.Write([]byte(strconv.FormatInt(apiKeyID, 10)))
// Model and tool definitions are request configuration rather than history.
// Root instructions remain model-visible context and participate in identity.
for _, field := range []string{"instructions"} {
v := root.Get(field)
if !v.Exists() || (v.Type == gjson.String && strings.TrimSpace(v.String()) == "") {
continue
}
canonical := normalizeCompatSeedJSON(json.RawMessage(v.Raw))
if v.Type == gjson.String {
canonical = v.String()
}
_, _ = h.Write([]byte("|"))
_, _ = h.Write([]byte(field))
_, _ = h.Write([]byte("="))
_, _ = h.Write([]byte(canonical))
}
prefixKey := func() string { return hex.EncodeToString(h.Sum(nil)) }
length := 0
feed := func(_, item gjson.Result) bool {
canonical := item.Raw
switch item.Type {
case gjson.String:
encoded, _ := json.Marshal(item.String())
canonical = string(encoded)
case gjson.JSON:
canonical = normalizeCompatSeedJSON(json.RawMessage(item.Raw))
}
if strings.TrimSpace(canonical) == "" {
return true
}
_, _ = h.Write([]byte("|item="))
_, _ = h.Write([]byte(canonical))
length++
return visit(item, length, prefixKey)
}
if messages := root.Get("messages"); messages.Exists() {
if messages.IsArray() {
messages.ForEach(feed)
}
return
}
input := root.Get("input")
if input.IsArray() {
input.ForEach(feed)
return
}
if input.Type == gjson.String && strings.TrimSpace(input.String()) != "" {
feed(gjson.Result{}, input)
}
}
func openAICyberTranscriptItemStartsUserTurn(item gjson.Result) bool {
if strings.EqualFold(strings.TrimSpace(item.Get("role").String()), "user") {
content := item.Get("content")
if content.IsArray() {
hasUserContent := false
content.ForEach(func(_, block gjson.Result) bool {
switch strings.ToLower(strings.TrimSpace(block.Get("type").String())) {
case "tool_result", "function_call_output", "custom_tool_call_output", "computer_call_output":
default:
hasUserContent = true
}
return !hasUserContent
})
return hasUserContent
}
return content.Exists()
}
return strings.EqualFold(strings.TrimSpace(item.Get("type").String()), "input_text")
}
func openAICyberTranscriptItemIsModelGenerated(item gjson.Result) bool {
switch strings.ToLower(strings.TrimSpace(item.Get("role").String())) {
case "assistant", "model":
return true
}
switch strings.ToLower(strings.TrimSpace(item.Get("type").String())) {
case "output_text", "function_call", "tool_call", "custom_tool_call", "computer_call":
return true
default:
return false
}
}