Files
sub2api/backend/internal/pkg/requestmodel/requestmodel_test.go
T
shaw ce86c1b306 fix(gateway): close bypasses found in the duplicate-field audit
An independent audit of the conflicting-field fix found, and running code
confirmed, the following. Each is fixed here.

- A JSON body labelled multipart/form-data skipped the check: the JSON
  branch was chosen from the Content-Type header, but JSON handlers never
  look at it. ConflictingField and FromBodyCandidates now detect JSON by
  content. The same header-only branch, and a UTF-8 BOM prefix (stripped by
  the handlers' lenient reader), also let a body bypass the group model
  allowlist on main; both are closed.
- Identical duplicates passed and the gateway's own sjson rewrites then
  split them: reasoning-effort downgrade, model mapping and the fast-policy
  service_tier delete only touch the first occurrence. Every repeated member
  is now rejected, whatever its value. Multipart rejects repeated scalar
  fields (model, n, size, ...) except list fields (name[], image,
  image[...], image_url).
- Nested case variants bound case-insensitively by struct-based protocol
  conversions ({"reasoning":{"effort":"low","Effort":"xhigh"}}). Members
  are now folded at every depth, except inside user-defined key spaces
  (JSON Schema properties/$defs, tool arguments, metadata, ...), where id
  and ID are distinct and only exact repeats are rejected.
- The recursive walker cost depth x size (1 MB at depth 9990: 1.6 s) and
  stored every key as a string (~16x the body). It is replaced by one
  linear byte scan with an explicit stack that compares 64-bit hashes
  (random per-process seed) of the decoded, folded names: 0.6 ms for the
  same body, ~3.7 GB/s on a Claude Code-shaped body. Subtrees deeper than
  10000 levels are bracket-counted, so later same-level members are still
  checked.
- text_max_body_size no longer bounded what group middleware buffered:
  the route-level limit ran after them. The limit is now chosen per route
  at group level.
- Body read failures in gateway middleware answered in the OpenAI shape
  with a different message than the handlers. They now use the entry
  protocol's shape and the handler's "limit is N" message. Rejections
  are recorded as ingress reason conflicting_request_fields.
- The middleware strips a leading BOM and restores the body, so composite
  routing and handlers parse the bytes that were checked.

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

285 lines
10 KiB
Go
Raw 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)
}
})
}
}