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
28 lines
1.2 KiB
Go
28 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// AbortWithRequestBodyReadFailure writes the shared response for a failed body
|
|
// read in middleware that inspects the body before the handler. It applies the
|
|
// same classification, client message, protocol-shaped error body and
|
|
// payload-free diagnostics as the gateway handlers, so a body read in
|
|
// middleware fails exactly as it would in the handler.
|
|
func AbortWithRequestBodyReadFailure(c *gin.Context, component string, err error) {
|
|
failure := httputil.DescribeRequestBodyReadFailure(c.Request, err)
|
|
if failure.Kind != httputil.RequestBodyReadErrorMaxBytes {
|
|
logRequestBodyReadFailure(c, component, failure)
|
|
}
|
|
writeGatewayInvalidRequest(c, failure.Status, failure.ClientMessage())
|
|
c.Abort()
|
|
}
|
|
|
|
func logRequestBodyReadFailure(c *gin.Context, component string, failure httputil.RequestBodyReadFailure) {
|
|
fields := append([]zap.Field{zap.String("component", component)}, failure.LogFields()...)
|
|
logger.FromContext(c.Request.Context()).Warn("read request body failed", fields...)
|
|
}
|