mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
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
141 lines
5.0 KiB
Go
141 lines
5.0 KiB
Go
//go:build unit
|
|
|
|
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"testing/iotest"
|
|
|
|
pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRequestBodyReadFailureLogsWithoutPayload(t *testing.T) {
|
|
log, logs := newObservedLogger(t)
|
|
req, err := http.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader("secret-payload-marker"))
|
|
require.NoError(t, err)
|
|
req.Header.Set("Content-Encoding", "gzip")
|
|
req.ContentLength = 21
|
|
|
|
status, message := requestBodyReadFailure(log, req, errors.New(`decode Content-Encoding "gzip": unexpected EOF secret-payload-marker`))
|
|
|
|
require.Equal(t, http.StatusBadRequest, status)
|
|
require.Equal(t, "Failed to read request body", message)
|
|
entries := logs.All()
|
|
require.Len(t, entries, 1)
|
|
require.Equal(t, "read request body failed", entries[0].Message)
|
|
fields := entries[0].ContextMap()
|
|
require.Equal(t, "decode_content_encoding", fields["error_kind"])
|
|
require.Equal(t, "gzip", fields["content_encoding"])
|
|
require.EqualValues(t, 21, fields["content_length"])
|
|
require.NotContains(t, entries[0].Message+fmt.Sprint(fields), "secret-payload-marker")
|
|
}
|
|
|
|
func TestRequestBodyReadFailureKeepsBodyLimitMessageWithoutLogging(t *testing.T) {
|
|
log, logs := newObservedLogger(t)
|
|
status, message := requestBodyReadFailure(log, nil, &http.MaxBytesError{Limit: 2 << 20})
|
|
require.Equal(t, http.StatusRequestEntityTooLarge, status)
|
|
require.Equal(t, pkghttputil.BodyTooLargeMessage(2<<20), message)
|
|
require.Empty(t, logs.All())
|
|
}
|
|
|
|
// cancelOnReadErr models net/http on HTTP/1: a failed body read cancels the
|
|
// request context before the error reaches the handler.
|
|
type cancelOnReadErr struct {
|
|
err error
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func (r cancelOnReadErr) Read([]byte) (int, error) {
|
|
r.cancel()
|
|
return 0, r.err
|
|
}
|
|
|
|
// The Anthropic /v1/messages handler reads the full body before it knows the
|
|
// model or account. A failed read must report why instead of a generic 400.
|
|
func TestGatewayMessagesClassifiesRequestBodyReadFailures(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
deadlineErr := &net.OpError{Op: "read", Net: "tcp", Err: os.ErrDeadlineExceeded}
|
|
|
|
tests := []struct {
|
|
name string
|
|
readErr error
|
|
cancelCtx bool
|
|
wantStatus int
|
|
wantMessage string
|
|
}{
|
|
{"h2 read timeout", deadlineErr, false, http.StatusRequestTimeout, "Timed out reading request body"},
|
|
// net/http cancels the request context when an HTTP/1 read deadline
|
|
// fires; this is still a timeout, not a client that went away.
|
|
{"http1 read timeout", deadlineErr, true, http.StatusRequestTimeout, "Timed out reading request body"},
|
|
// A client that went away gets no body, matching the failover path.
|
|
{"client disconnect", io.ErrUnexpectedEOF, true, 499, ""},
|
|
{"truncated body", io.ErrUnexpectedEOF, false, http.StatusBadRequest, "Failed to read request body"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
failing := iotest.ErrReader(tc.readErr)
|
|
if tc.cancelCtx {
|
|
failing = cancelOnReadErr{err: tc.readErr, cancel: cancel}
|
|
}
|
|
body := io.MultiReader(strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[`), failing)
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/messages", body).WithContext(ctx)
|
|
req.ContentLength = 1 << 20
|
|
c.Request = req
|
|
groupID := int64(1)
|
|
c.Set(string(middleware.ContextKeyAPIKey), &service.APIKey{ID: 1, UserID: 1, GroupID: &groupID})
|
|
c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{UserID: 1, Concurrency: 1})
|
|
|
|
(&GatewayHandler{}).Messages(c)
|
|
|
|
require.Equal(t, tc.wantStatus, c.Writer.Status())
|
|
if tc.wantMessage == "" {
|
|
require.Empty(t, rec.Body.String())
|
|
return
|
|
}
|
|
var resp struct {
|
|
Type string `json:"type"`
|
|
Error struct {
|
|
Type string `json:"type"`
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
|
|
require.Equal(t, "error", resp.Type)
|
|
require.Equal(t, "invalid_request_error", resp.Error.Type)
|
|
require.Equal(t, tc.wantMessage, resp.Error.Message)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Google-format errors carry no type field; body read failures must still be
|
|
// attributed to the client request rather than the platform.
|
|
func TestGeminiRequestBodyReadTimeoutClassifiedAsClient(t *testing.T) {
|
|
entry := recordGoogleFormatOpsError(t, func(c *gin.Context) {
|
|
c.Request.Body = io.NopCloser(iotest.ErrReader(&net.OpError{Op: "read", Net: "tcp", Err: os.ErrDeadlineExceeded}))
|
|
readGeminiCachedContentBody(c)
|
|
})
|
|
require.Equal(t, http.StatusRequestTimeout, entry.StatusCode)
|
|
require.Equal(t, "invalid_request_error", entry.ErrorType)
|
|
require.Equal(t, "request", entry.ErrorPhase)
|
|
require.Equal(t, "client", entry.ErrorOwner)
|
|
require.Equal(t, "P3", entry.Severity)
|
|
}
|