Files
sub2api/backend/internal/handler/openai_gateway_ws_model_conflict_test.go
T
shaw 263a13f160 fix(gateway): close second-round audit findings on the duplicate scanner
A re-audit of ce86c1b30 found, and running code confirmed:

- scanJSONString re-scanned to the closing quote after every escape, so
  scanning was quadratic in escapes (200k "\n": 0.56 s; a 10 MB code-like
  body: 24 s). It now visits each quote once and counts the backslashes
  before it: 6 us and 0.7 ms for the same bodies.
- A multipart body with a JSON preamble (RFC 2046; skipped by Go's
  multipart reader) took the JSON branch only, bypassing both the group
  allowlist and the multipart duplicate check. A multipart Content-Type
  now always runs the multipart check, FromBodyCandidates merges JSON and
  multipart candidates, and a body that is both JSON and a form with parts
  is rejected as ambiguous. The JSON scan stops after the first top-level
  value, matching gjson, so binary file parts are never tokenised.
- LooksLikeJSON now skips every byte <= 0x20 before the first value, as
  gjson does.
- Freeform key spaces gained variables, headers, dependentRequired,
  dependencies and labels, matched on the decoded name. metadata is no
  longer freeform: Antigravity binds metadata.user_id case-insensitively
  while sticky sessions read it with gjson.
- Multipart also rejects file parts named model/session, parts without a
  parsable name, and parts carrying two Content-Disposition headers, and
  counts file and text parts with the same name together.
- WebSocket: passthrough forwards session.update and every other client
  event verbatim, and dedicated parsing reads fields from the raw frame
  before any re-encode. Both now reject a frame with repeated fields
  before reading it.
- The linear-compare limit rises to 32 to cut map churn for mid-sized
  objects.

Tests that relied on duplicates passing now assert rejection: a dedicated
WS recovery test that sent two previous_response_id keys, and WS
allowlist cases whose close reason comes from the earlier duplicate check.

Refs: GHSA-p3r4-jcm3-2hph
2026-11-20 11:32:05 +08:00

129 lines
6.4 KiB
Go

package handler
import (
"testing"
"github.com/Wei-Shaw/sub2api/internal/service"
)
const wsModelConflictCloseReason = "more than once"
// 没有开启白名单的分组:首帧里重复 model 键取值不一致(计费按第一个、上游可能
// 按最后一个绑定)必须以 1008 关闭,帧不到达上游。
func TestOpenAIResponsesWebSocket_FirstFrameConflictingModelKeysRejectedWithoutAllowlist(t *testing.T) {
for _, mode := range []string{service.OpenAIWSIngressModePassthrough, service.OpenAIWSIngressModeDedicated} {
t.Run(mode, func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4-nano","model":"gpt-5.4","stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
firstFrameCloseExpected: true,
closeReason: wsModelConflictCloseReason,
})
})
}
}
// 大小写变体键同样会被 encoding/json 绑定到同一个字段。
func TestOpenAIResponsesWebSocket_FirstFrameConflictingCaseVariantModelKeyRejected(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4-nano","Model":"gpt-5.4","stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: service.OpenAIWSIngressModePassthrough,
firstFrameCloseExpected: true,
closeReason: wsModelConflictCloseReason,
})
}
// 白名单开启且两个模型都在名单内:白名单放行,模型歧义检查仍要拒绝。
func TestOpenAIResponsesWebSocket_FirstFrameConflictingAllowlistedModelKeysRejected(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4-nano","model":"gpt-5.4","stream":false}`,
group: wsAllowlistGroup(true, "gpt-5.4-nano", "gpt-5.4"),
ingressMode: service.OpenAIWSIngressModePassthrough,
firstFrameCloseExpected: true,
closeReason: wsModelConflictCloseReason,
})
}
// 后续 turn 的帧同样检查。
func TestOpenAIResponsesWebSocket_SubsequentTurnConflictingModelKeysRejectedWithoutAllowlist(t *testing.T) {
for _, mode := range []string{service.OpenAIWSIngressModePassthrough, service.OpenAIWSIngressModeDedicated} {
t.Run(mode, func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","stream":false}`,
secondPayload: `{"type":"response.create","model":"gpt-5.4-nano","model":"gpt-5.4","stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
secondTurnCloseExpected: true,
closeReason: wsModelConflictCloseReason,
})
})
}
}
// 取值相同的重复键同样拒绝:网关的 sjson 改写(模型映射、推理强度降级)只改
// 首个,改写后两者就会分叉。首帧与后续 turn 都要关闭连接。
func TestOpenAIResponsesWebSocket_IdenticalDuplicateKeysRejectedWithoutAllowlist(t *testing.T) {
for _, mode := range []string{service.OpenAIWSIngressModePassthrough, service.OpenAIWSIngressModeDedicated} {
t.Run(mode+"/first_frame", func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","model":"gpt-5.4","stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
firstFrameCloseExpected: true,
closeReason: `declares field "model" more than once`,
})
})
t.Run(mode+"/subsequent_turn", func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","stream":false}`,
secondPayload: `{"type":"response.create","model":"gpt-5.4","reasoning":{"effort":"xhigh","effort":"xhigh"},"stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
secondTurnCloseExpected: true,
closeReason: `declares field "reasoning.effort" more than once`,
})
})
}
}
// 非 model 字段同样参与计费与策略:首帧 service_tier、后续 turn 嵌套的
// reasoning.effort 取值不一致都必须关闭连接。
func TestOpenAIResponsesWebSocket_ConflictingBillingFieldsRejected(t *testing.T) {
for _, mode := range []string{service.OpenAIWSIngressModePassthrough, service.OpenAIWSIngressModeDedicated} {
t.Run(mode+"/first_frame_service_tier", func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","service_tier":"flex","stream":false,"service_tier":"priority"}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
firstFrameCloseExpected: true,
closeReason: `declares field "service_tier" more than once`,
})
})
t.Run(mode+"/subsequent_turn_reasoning_effort", func(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","stream":false}`,
secondPayload: `{"type":"response.create","model":"gpt-5.4","reasoning":{"effort":"low","effort":"xhigh"},"stream":false}`,
group: wsAllowlistGroup(false),
ingressMode: mode,
secondTurnCloseExpected: true,
closeReason: `declares field "reasoning.effort" more than once`,
})
})
}
}
// passthrough 原样转发 session.update 等非 response.create 事件:帧内重复字段
// 同样必须关闭连接,不能到达上游。
func TestOpenAIResponsesWebSocket_PassthroughSessionUpdateDuplicateRejected(t *testing.T) {
runOpenAIResponsesWebSocketUsageLogCase(t, openAIResponsesWSUsageLogCase{
firstPayload: `{"type":"response.create","model":"gpt-5.4","stream":false}`,
midPayload: `{"type":"session.update","session":{"model":"gpt-5.4","model":"gpt-4.1"}}`,
group: wsAllowlistGroup(false),
ingressMode: service.OpenAIWSIngressModePassthrough,
midFrameCloseExpected: true,
closeReason: `declares field "session.model" more than once`,
})
}