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
30 lines
1.1 KiB
Go
30 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// requestBodyReadFailure maps a request body read error to the response status
|
|
// and message. Read timeouts (408) are no longer reported as malformed
|
|
// requests; other read failures keep the stable generic 400 message. Failures
|
|
// other than the configured size limit are logged with bounded, payload-free
|
|
// diagnostics (kind, declared length, bytes received and read duration) so
|
|
// operators can tell a stalled or slow upload from a broken client without
|
|
// logging request content.
|
|
func requestBodyReadFailure(reqLog *zap.Logger, req *http.Request, err error) (int, string) {
|
|
failure := pkghttputil.DescribeRequestBodyReadFailure(req, err)
|
|
if failure.Kind != pkghttputil.RequestBodyReadErrorMaxBytes {
|
|
logRequestBodyReadFailure(reqLog, failure)
|
|
}
|
|
return failure.Status, failure.ClientMessage()
|
|
}
|
|
|
|
func logRequestBodyReadFailure(reqLog *zap.Logger, failure pkghttputil.RequestBodyReadFailure) {
|
|
if reqLog != nil {
|
|
reqLog.Warn("read request body failed", failure.LogFields()...)
|
|
}
|
|
}
|