CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// MaxBodyBytes caps the request body size for state-changing requests. It wraps
|
|
// the body in an http.MaxBytesReader so that any handler reading it (gin's
|
|
// ShouldBind, manual io.ReadAll, etc.) receives an error once the limit is
|
|
// exceeded, which the existing bind-failure path reports as a 400 rather than
|
|
// allocating an unbounded buffer or starting a long DB transaction.
|
|
//
|
|
// Methods without a body (GET/HEAD/OPTIONS/TRACE) and a non-positive limit are
|
|
// passed through untouched. Paths ending in one of skipSuffixes are also passed
|
|
// through uncapped — these are routes that legitimately accept a large upload
|
|
// (e.g. database restore, which streams a multi-MiB SQLite file).
|
|
func MaxBodyBytes(limit int64, skipSuffixes ...string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if limit > 0 {
|
|
switch c.Request.Method {
|
|
case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace:
|
|
default:
|
|
if c.Request.Body != nil && !hasAnySuffix(c.Request.URL.Path, skipSuffixes) {
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, limit)
|
|
}
|
|
}
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func hasAnySuffix(path string, suffixes []string) bool {
|
|
for _, suffix := range suffixes {
|
|
if suffix != "" && strings.HasSuffix(path, suffix) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|