Files
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

295 lines
11 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package requestmodel
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
)
func TestFromBodyJSONModel(t *testing.T) {
if got := FromBody("application/json", []byte(`{"model":"gpt-5.4"}`)); got != "gpt-5.4" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyJSONSessionModel(t *testing.T) {
if got := FromBody("application/json", []byte(`{"session":{"model":"gpt-live"},"sdp":"v=0"}`)); got != "gpt-live" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyJSONModelWinsOverSession(t *testing.T) {
body := []byte(`{"model":"top-level","session":{"model":"nested"}}`)
if got := FromBody("application/json", body); got != "top-level" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyJSONBlankModelFallsThrough(t *testing.T) {
if got := FromBody("application/json", []byte(`{"model":" "}`)); got != "" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyMultipartModelField(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
_ = writer.WriteField("prompt", "draw")
_ = writer.WriteField("model", "gpt-image-1")
_ = writer.Close()
if got := FromBody(writer.FormDataContentType(), body.Bytes()); got != "gpt-image-1" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyMultipartSessionField(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
_ = writer.WriteField("sdp", "v=0")
_ = writer.WriteField("session", `{"model":"live-alias"}`)
_ = writer.WriteField("media", "file-part") // 字段名不匹配,应被跳过
_ = writer.Close()
if got := FromBody(writer.FormDataContentType(), body.Bytes()); got != "live-alias" {
t.Fatalf("got %q", got)
}
}
func TestFromBodySkipsFilePartsBeforeModelField(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", "input.png")
if err != nil {
t.Fatalf("create form file: %v", err)
}
_, _ = part.Write(bytes.Repeat([]byte{0}, 4096))
_ = writer.WriteField("model", "gpt-image-1")
_ = writer.Close()
if got := FromBody(writer.FormDataContentType(), body.Bytes()); got != "gpt-image-1" {
t.Fatalf("got %q", got)
}
}
func TestFromBodyNoModelReturnsEmpty(t *testing.T) {
if got := FromBody("application/json", []byte(`{"input":"hi"}`)); got != "" {
t.Fatalf("got %q", got)
}
if got := FromBody("text/plain", []byte("plain")); got != "" {
t.Fatalf("got %q", got)
}
}
func TestResetRequestBodyAllowsRereadWithoutCopy(t *testing.T) {
body := []byte(`{"model":"gpt-5.4","input":"hi"}`)
req := httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewReader(body))
ResetRequestBody(req, body)
got, err := httputil.ReadRequestBodyWithPrealloc(req)
if err != nil {
t.Fatalf("reread: %v", err)
}
if &got[0] != &body[0] {
t.Fatalf("expected zero-copy reread of the same slice")
}
if req.ContentLength != int64(len(body)) {
t.Fatalf("content length mismatch: %d", req.ContentLength)
}
if req.Header.Get("Content-Length") != strconv.Itoa(len(body)) {
t.Fatalf("content-length header mismatch: %q", req.Header.Get("Content-Length"))
}
}
func TestResetRequestBodyNilRequestIsNoop(t *testing.T) {
ResetRequestBody(nil, []byte("x"))
}
func TestFromBodyForRouteLivePrefersSessionModel(t *testing.T) {
body := []byte(`{"model":"top-level","session":{"model":"session-model"},"sdp":"v=0"}`)
if got := FromBodyForRoute("/v1/live", "application/json", body); got != "session-model" {
t.Fatalf("live route should prefer session.model, got %q", got)
}
if got := FromBodyForRoute("/backend-api/codex/realtime/calls", "application/json", body); got != "session-model" {
t.Fatalf("codex realtime route should prefer session.model, got %q", got)
}
if got := FromBodyForRoute("/live", "application/json", body); got != "session-model" {
t.Fatalf("root live alias should prefer session.model, got %q", got)
}
}
func TestFromBodyForRouteNonLivePrefersTopLevelModel(t *testing.T) {
body := []byte(`{"model":"top-level","session":{"model":"session-model"}}`)
if got := FromBodyForRoute("/v1/messages", "application/json", body); got != "top-level" {
t.Fatalf("non-live route should prefer top-level model, got %q", got)
}
if got := FromBodyForRoute("/v1/responses", "application/json", body); got != "top-level" {
t.Fatalf("responses route should prefer top-level model, got %q", got)
}
}
func TestFromBodyForRouteFallsBackWhenPreferredFieldMissing(t *testing.T) {
if got := FromBodyForRoute("/v1/live", "application/json", []byte(`{"model":"top-level"}`)); got != "top-level" {
t.Fatalf("live without session.model should fall back to model, got %q", got)
}
if got := FromBodyForRoute("/v1/messages", "application/json", []byte(`{"session":{"model":"session-model"}}`)); got != "session-model" {
t.Fatalf("non-live without model should fall back to session.model, got %q", got)
}
}
func TestFromBodyForRouteMultipartSessionFirstForLive(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
_ = writer.WriteField("model", "top-level")
_ = writer.WriteField("session", `{"model":"session-model"}`)
_ = writer.Close()
contentType := writer.FormDataContentType()
if got := FromBodyForRoute("/v1/live", contentType, body.Bytes()); got != "session-model" {
t.Fatalf("live multipart should prefer session field, got %q", got)
}
if got := FromBodyForRoute("/v1/messages", contentType, body.Bytes()); got != "top-level" {
t.Fatalf("non-live multipart should prefer model field, got %q", got)
}
}
func TestIsLiveRequestRoute(t *testing.T) {
for _, path := range []string{"/v1/live", "/live", "/backend-api/codex/realtime/calls"} {
if !IsLiveRequestRoute(path) {
t.Fatalf("%s should be a live route", path)
}
}
for _, path := range []string{"/v1/messages", "/v1/responses", "/realtime", ""} {
if IsLiveRequestRoute(path) {
t.Fatalf("%s should not be a live route", path)
}
}
}
// gjson 的部分扫描会在 multipart 字节流里误匹配 session JSON 内的 model;
// 声明为 multipart 的请求体必须走 multipart 解析。
func TestFromBodyMultipartContentTypeSkipsJSONScan(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
_ = writer.WriteField("model", "top-level")
_ = writer.WriteField("session", `{"model":"session-model"}`)
_ = writer.Close()
if got := FromBody(writer.FormDataContentType(), body.Bytes()); got != "top-level" {
t.Fatalf("multipart body should use the model field, got %q", got)
}
if got := FromBodyForRoute("/v1/live", writer.FormDataContentType(), body.Bytes()); got != "session-model" {
t.Fatalf("live multipart should use the session field, got %q", got)
}
}
func TestFromBodyCandidatesIncludesCaseVariantsAndDuplicates(t *testing.T) {
// encoding/json 绑定对键名大小写不敏感、重复键取末值;gjson 取首个。
// 候选集必须包含全部出现。
got := FromBodyCandidates("", "application/json", []byte(`{"Model":"caps-model"}`))
if len(got) != 1 || got[0] != "caps-model" {
t.Fatalf("case-variant key must be a candidate, got %#v", got)
}
got = FromBodyCandidates("", "application/json", []byte(`{"model":"first","model":"last"}`))
if len(got) != 2 || got[0] != "first" || got[1] != "last" {
t.Fatalf("duplicate keys must both be candidates, got %#v", got)
}
}
func TestFromBodyCandidatesSessionVariants(t *testing.T) {
body := []byte(`{"Session":{"Model":"s-caps"},"session":{"model":"s-lower"},"model":"top"}`)
// 非 Live 入口:顶层候选。
got := FromBodyCandidates("", "application/json", body)
if len(got) != 1 || got[0] != "top" {
t.Fatalf("non-live should use top-level candidates, got %#v", got)
}
// Live 入口:session 候选(含大小写变体与重复)。
got = FromBodyCandidates("/v1/live", "application/json", body)
if len(got) != 2 || got[0] != "s-caps" || got[1] != "s-lower" {
t.Fatalf("live should collect all session candidates, got %#v", got)
}
}
func TestFromBodyCandidatesMultipartDuplicateFields(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
_ = writer.WriteField("model", "gpt-image-1")
_ = writer.WriteField("model", "gpt-image-1.5")
_ = writer.WriteField("session", `{"model":"live-a"}`)
_ = writer.WriteField("session", `{"model":"live-b"}`)
_ = writer.Close()
models, sessions := multipartModelCandidates(writer.FormDataContentType(), body.Bytes())
if len(models) != 2 || models[0] != "gpt-image-1" || models[1] != "gpt-image-1.5" {
t.Fatalf("duplicate model fields must all be candidates, got %#v", models)
}
if len(sessions) != 2 {
t.Fatalf("duplicate session fields must all be captured, got %#v", sessions)
}
got := FromBodyCandidates("", writer.FormDataContentType(), body.Bytes())
if len(got) != 2 || got[0] != "gpt-image-1" || got[1] != "gpt-image-1.5" {
t.Fatalf("non-live multipart candidates mismatch, got %#v", got)
}
got = FromBodyCandidates("/v1/live", writer.FormDataContentType(), body.Bytes())
if len(got) != 2 || got[0] != "live-a" || got[1] != "live-b" {
t.Fatalf("live multipart session candidates mismatch, got %#v", got)
}
}
func TestFromBodyForRouteReturnsFirstCandidate(t *testing.T) {
got := FromBodyForRoute("", "application/json", []byte(`{"model":"first","model":"last"}`))
if got != "first" {
t.Fatalf("dispatch helper keeps gjson-first semantics, got %q", got)
}
}
// 回归:JSON handler 不看 Content-Type、宽容读取会剥掉 BOM,准入候选必须与之
// 一致,否则把请求体标成 multipart 或加 BOM 就能让白名单拿不到任何模型而放行。
func TestFromBodyCandidatesFollowsWhatJSONHandlersParse(t *testing.T) {
body := []byte(`{"model":"claude-opus-4-8","messages":[]}`)
cases := []struct {
name string
contentType string
body []byte
}{
{name: "multipart 标签", contentType: "multipart/form-data; boundary=zzz", body: body},
{name: "无 boundary 的 multipart 标签", contentType: "multipart/form-data", body: body},
{name: "UTF-8 BOM", contentType: "application/json", body: append([]byte{0xEF, 0xBB, 0xBF}, body...)},
{name: "BOM 加 multipart 标签", contentType: "multipart/form-data", body: append([]byte{0xEF, 0xBB, 0xBF}, body...)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := FromBodyCandidates("/v1/messages", tc.contentType, tc.body)
if len(got) != 1 || got[0] != "claude-opus-4-8" {
t.Fatalf("candidates = %v, want [claude-opus-4-8]", got)
}
if route := FromBodyForRoute("/v1/messages", tc.contentType, tc.body); route != "claude-opus-4-8" {
t.Fatalf("route model = %q", route)
}
})
}
}
// 回归:multipart handler 跳过首个 boundary 之前的前导内容,前导内容是 JSON 时
// 白名单必须同时看到表单里的 model,不能只取 JSON 解读。
func TestFromBodyCandidatesMultipartWithJSONPreamble(t *testing.T) {
body := []byte("{}\r\n--B\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\ngpt-image-2\r\n--B--\r\n")
got := FromBodyCandidates("/v1/images/edits", "multipart/form-data; boundary=B", body)
if len(got) != 1 || got[0] != "gpt-image-2" {
t.Fatalf("candidates = %v, want [gpt-image-2]", got)
}
}