Files
sub2api/backend/internal/server/middleware/request_field_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

283 lines
12 KiB
Go

package middleware
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
)
// newRequestFieldConflictTestRouter 按网关链的顺序挂载:白名单准入在前,
// 字段歧义拒绝在后。handlerBodies 记录 handler 实际读到的请求体。
func newRequestFieldConflictTestRouter(apiKey *service.APIKey) (*gin.Engine, *[]string) {
gin.SetMode(gin.TestMode)
router := gin.New()
var handlerBodies []string
router.Use(func(c *gin.Context) {
if apiKey != nil {
c.Set(string(ContextKeyAPIKey), apiKey)
}
c.Next()
})
router.Use(GroupModelAllowlist())
router.Use(RejectConflictingRequestFields())
handler := func(c *gin.Context) {
body, _ := httputil.ReadRequestBodyWithPrealloc(c.Request)
handlerBodies = append(handlerBodies, string(body))
c.Status(http.StatusOK)
}
for _, path := range []string{
"/v1/messages", "/v1/responses", "/v1/chat/completions", "/v1/images/edits", "/v1/live",
"/v1beta/models/*modelAction",
} {
router.POST(path, handler)
}
router.GET("/v1/models", handler)
return router, &handlerBodies
}
// 复现报告的请求体:第一个 model 是便宜模型,最后一个是贵模型。分组没有开启
// 白名单时旧行为是放行并按第一个计费,现在必须在进入 handler 之前拒绝。
func TestRejectConflictingRequestFieldsBlocksDisagreeingModelWithoutAllowlist(t *testing.T) {
for _, apiKey := range []*service.APIKey{nil, {}, allowlistAPIKey(false)} {
router, handlerBodies := newRequestFieldConflictTestRouter(apiKey)
w := doJSON(t, router, http.MethodPost, "/v1/responses",
`{"model":"gpt-5.4-nano","stream":false,"input":"hi","model":"gpt-5.4"}`)
if w.Code != http.StatusBadRequest {
t.Fatalf("conflicting model keys must be rejected, got %d: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `\"model\"`) || !strings.Contains(w.Body.String(), "invalid_request_error") {
t.Fatalf("expected the field name and invalid_request_error, got %s", w.Body.String())
}
if len(*handlerBodies) != 0 {
t.Fatalf("expected handler not to run, got %v", *handlerBodies)
}
}
}
// 白名单开启且两个模型都在名单内时,白名单会放行;字段歧义检查仍要拒绝,
// 否则计费模型和转发模型照样分叉。
func TestRejectConflictingRequestFieldsBlocksEvenWhenAllCandidatesAllowlisted(t *testing.T) {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(true, "claude-3-5-haiku-20241022", "claude-opus-4-8"))
w := doJSON(t, router, http.MethodPost, "/v1/messages",
`{"model":"claude-3-5-haiku-20241022","max_tokens":64,"messages":[{"role":"user","content":"hi"}],"model":"claude-opus-4-8"}`)
if w.Code != http.StatusBadRequest {
t.Fatalf("conflicting model keys must be rejected, got %d: %s", w.Code, w.Body.String())
}
if len(*handlerBodies) != 0 {
t.Fatalf("expected handler not to run, got %v", *handlerBodies)
}
}
func TestRejectConflictingRequestFieldsCoversOtherFields(t *testing.T) {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
for _, body := range []string{
`{"model":"gpt-5.4","service_tier":"default","input":"hi","service_tier":"priority"}`,
`{"model":"gpt-5.4","Model":"gpt-5.4-nano"}`,
`{"model":"gpt-5.4","messages":[],"messages":[{"role":"user","content":"hi"}]}`,
} {
w := doJSON(t, router, http.MethodPost, "/v1/chat/completions", body)
if w.Code != http.StatusBadRequest {
t.Fatalf("body %s must be rejected, got %d: %s", body, w.Code, w.Body.String())
}
}
if len(*handlerBodies) != 0 {
t.Fatalf("expected handler not to run, got %v", *handlerBodies)
}
}
func TestRejectConflictingRequestFieldsErrorFormats(t *testing.T) {
router, _ := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
body := `{"model":"a","contents":[],"model":"b"}`
openai := doJSON(t, router, http.MethodPost, "/v1/responses", body)
if !strings.Contains(openai.Body.String(), `"error":{"message"`) || strings.Contains(openai.Body.String(), "model_not_found") {
t.Fatalf("unexpected OpenAI error format: %s", openai.Body.String())
}
anthropic := doJSON(t, router, http.MethodPost, "/v1/messages", body)
if !strings.Contains(anthropic.Body.String(), `"type":"error"`) || !strings.Contains(anthropic.Body.String(), `"type":"invalid_request_error"`) {
t.Fatalf("unexpected Anthropic error format: %s", anthropic.Body.String())
}
google := doJSON(t, router, http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", body)
if google.Code != http.StatusBadRequest || !strings.Contains(google.Body.String(), `"status":"INVALID_ARGUMENT"`) {
t.Fatalf("unexpected Google error format: %d %s", google.Code, google.Body.String())
}
}
// 普通请求体和非对象请求体都放行,handler 读到的请求体与客户端发送的完全一致。
func TestRejectConflictingRequestFieldsPassesUnambiguousBodiesUnchanged(t *testing.T) {
for _, body := range []string{
`{"model":"gpt-5.4","input":"hi"}`,
`{"model":"gpt-5.4","prompt":{"id":"p","variables":{"k":"1","K":"1"}}}`,
`not json`,
``,
} {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
w := doJSON(t, router, http.MethodPost, "/v1/responses", body)
if w.Code != http.StatusOK {
t.Fatalf("body %q should pass, got %d: %s", body, w.Code, w.Body.String())
}
if len(*handlerBodies) != 1 || (*handlerBodies)[0] != body {
t.Fatalf("handler must read the original body %q, got %v", body, *handlerBodies)
}
}
}
func TestRejectConflictingRequestFieldsMultipart(t *testing.T) {
send := func(models ...string) *httptest.ResponseRecorder {
router, _ := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
var body bytes.Buffer
writer := multipart.NewWriter(&body)
for _, model := range models {
_ = writer.WriteField("model", model)
}
_ = writer.WriteField("prompt", "cat")
_ = writer.Close()
req := httptest.NewRequest(http.MethodPost, "/v1/images/edits", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
return w
}
if w := send("gpt-image-1", "gpt-image-1.5"); w.Code != http.StatusBadRequest {
t.Fatalf("conflicting multipart model fields must be rejected, got %d: %s", w.Code, w.Body.String())
}
if w := send("gpt-image-1", "gpt-image-1"); w.Code != http.StatusBadRequest {
t.Fatalf("repeated multipart model fields must be rejected even when identical, got %d: %s", w.Code, w.Body.String())
}
if w := send("gpt-image-1"); w.Code != http.StatusOK {
t.Fatalf("single multipart model field should pass, got %d: %s", w.Code, w.Body.String())
}
}
func TestRejectConflictingRequestFieldsLiveSessionModel(t *testing.T) {
router, _ := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
w := doJSON(t, router, http.MethodPost, "/v1/live", `{"sdp":"v=0","session":{"model":"gpt-live-a","model":"gpt-live-b"}}`)
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "session.model") {
t.Fatalf("conflicting session.model must be rejected, got %d: %s", w.Code, w.Body.String())
}
}
// GET/DELETE 不带需要检查的请求体:中间件不读取,直接放行。
func TestRejectConflictingRequestFieldsDoesNotReadBodylessMethods(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(RejectConflictingRequestFields())
called := false
router.GET("/v1/models", func(c *gin.Context) {
called = true
c.Status(http.StatusOK)
})
body := &readTrackingBody{Reader: strings.NewReader(`{"model":"a","model":"b"}`)}
req := httptest.NewRequest(http.MethodGet, "/v1/models", body)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK || !called {
t.Fatalf("GET must reach the handler, got %d", w.Code)
}
if body.read {
t.Fatal("middleware must not read the body of a GET request")
}
}
// 413 传递:请求体超过上限时保持现有的 413 响应,不被改写成 400。
func TestRejectConflictingRequestFieldsRequestBodyTooLargePassesThrough413(t *testing.T) {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
bigBody := `{"model":"a","padding":"` + strings.Repeat("x", 512) + `","model":"b"}`
req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(bigBody))
req.Header.Set("Content-Type", "application/json")
req.Body = http.MaxBytesReader(httptest.NewRecorder(), req.Body, 64)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("expected 413, got %d: %s", w.Code, w.Body.String())
}
// 与 handler 读取失败时一致:Messages 入口用 Anthropic 格式,文案带上限。
if !strings.Contains(w.Body.String(), `"type":"error"`) || !strings.Contains(w.Body.String(), "Request body too large, limit is 64B") {
t.Fatalf("expected an Anthropic-shaped 413 naming the limit, got %s", w.Body.String())
}
if len(*handlerBodies) != 0 {
t.Fatalf("expected handler not to run, got %v", *handlerBodies)
}
}
// 取值相同的重复字段同样拒绝:网关的 sjson 改写(推理强度降级、模型映射、
// fast policy 删除 service_tier)只改首个,改写后两者就会分叉。
func TestRejectConflictingRequestFieldsRejectsIdenticalDuplicates(t *testing.T) {
for _, body := range []string{
`{"model":"gpt-5.4","input":"hi","model":"gpt-5.4"}`,
`{"model":"gpt-5.4","input":"hi","reasoning":{"effort":"xhigh","effort":"xhigh"}}`,
`{"model":"gpt-5.4","service_tier":"priority","input":"hi","service_tier":"priority"}`,
} {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
w := doJSON(t, router, http.MethodPost, "/v1/responses", body)
if w.Code != http.StatusBadRequest || len(*handlerBodies) != 0 {
t.Fatalf("body %q must be rejected before the handler, got %d: %s", body, w.Code, w.Body.String())
}
}
}
// JSON handler 不看 Content-Type:把 JSON 请求体标成 multipart 不能绕过检查。
func TestRejectConflictingRequestFieldsIgnoresMultipartLabelOnJSONBody(t *testing.T) {
for _, contentType := range []string{"multipart/form-data", "multipart/form-data; boundary=zzz", "text/plain"} {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(`{"model":"haiku","messages":[],"model":"opus"}`))
req.Header.Set("Content-Type", contentType)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest || len(*handlerBodies) != 0 {
t.Fatalf("Content-Type %q must not bypass the check, got %d: %s", contentType, w.Code, w.Body.String())
}
}
}
// BOM 在这里剥掉后回填:合成路由与 handler 读到的是本层检查过的同一份字节。
func TestRejectConflictingRequestFieldsStripsBOMForDownstream(t *testing.T) {
router, handlerBodies := newRequestFieldConflictTestRouter(allowlistAPIKey(false))
w := doJSON(t, router, http.MethodPost, "/v1/responses", "\xef\xbb\xbf"+`{"model":"gpt-5.4","input":"hi"}`)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if len(*handlerBodies) != 1 || (*handlerBodies)[0] != `{"model":"gpt-5.4","input":"hi"}` {
t.Fatalf("handler must read the BOM-free body, got %q", *handlerBodies)
}
w = doJSON(t, router, http.MethodPost, "/v1/responses", "\xef\xbb\xbf"+`{"model":"a","input":"hi","model":"b"}`)
if w.Code != http.StatusBadRequest {
t.Fatalf("BOM-prefixed duplicate must be rejected, got %d: %s", w.Code, w.Body.String())
}
}
func TestRejectConflictingRequestFieldsMarksIngressReject(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
var reason any
router.Use(func(c *gin.Context) {
c.Next()
reason, _ = c.Get(ingressRejectReasonContextKey)
})
router.Use(RejectConflictingRequestFields())
router.POST("/v1/responses", func(c *gin.Context) { c.Status(http.StatusOK) })
w := doJSON(t, router, http.MethodPost, "/v1/responses", `{"model":"a","model":"b"}`)
if w.Code != http.StatusBadRequest || reason != IngressRejectConflictingRequestFields {
t.Fatalf("expected 400 with ingress reason %q, got %d reason=%v", IngressRejectConflictingRequestFields, w.Code, reason)
}
}