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
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRequestBodyLimitTooLarge(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
limit := int64(16)
|
|
router := gin.New()
|
|
router.Use(middleware.RequestBodyLimit(limit))
|
|
router.POST("/test", func(c *gin.Context) {
|
|
_, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
status, message := requestBodyReadFailure(nil, c.Request, err)
|
|
c.JSON(status, gin.H{"error": message})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
})
|
|
|
|
payload := bytes.Repeat([]byte("a"), int(limit+1))
|
|
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(payload))
|
|
recorder := httptest.NewRecorder()
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
require.Equal(t, http.StatusRequestEntityTooLarge, recorder.Code)
|
|
require.Contains(t, recorder.Body.String(), pkghttputil.BodyTooLargeMessage(limit))
|
|
}
|
|
|
|
func TestHandleRequestBodyReadErrorCancellation(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
cancelled bool
|
|
committed bool
|
|
readErr error
|
|
handled bool
|
|
}{
|
|
{name: "cancelled", cancelled: true, handled: true},
|
|
{name: "cancelled_after_commit", cancelled: true, committed: true, handled: true},
|
|
{name: "ordinary_read_error"},
|
|
// An HTTP/1 read deadline also cancels the request context; the
|
|
// timeout must reach requestBodyReadFailure (408).
|
|
{name: "read_timeout_with_cancelled_context", cancelled: true, readErr: &net.OpError{Op: "read", Net: "tcp", Err: os.ErrDeadlineExceeded}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
if tc.cancelled {
|
|
cancel()
|
|
}
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil).WithContext(ctx)
|
|
if tc.committed {
|
|
_, err := c.Writer.WriteString("already committed")
|
|
require.NoError(t, err)
|
|
}
|
|
bodyBefore := recorder.Body.String()
|
|
readErr := tc.readErr
|
|
if readErr == nil {
|
|
readErr = io.ErrUnexpectedEOF
|
|
}
|
|
require.Equal(t, tc.handled, handleRequestBodyReadError(c, nil, readErr))
|
|
require.Equal(t, tc.handled, c.IsAborted())
|
|
status := http.StatusOK
|
|
if tc.handled && !tc.committed {
|
|
status = statusClientClosedRequest
|
|
}
|
|
require.Equal(t, status, c.Writer.Status())
|
|
require.Equal(t, bodyBefore, recorder.Body.String(), "cancelled clients must not receive an error body")
|
|
})
|
|
}
|
|
}
|