Skip to content

Commit 92d6e4c

Browse files
Merge pull request #5291 from volcano-sh/copilot/cherry-pick-commit-13dde815
[release-1.14] Cherry-pick 13dde81: limit webhook request body size
2 parents 9651e60 + 9017085 commit 92d6e4c

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

pkg/webhooks/router/admission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func RegisterAdmission(service *AdmissionService) error {
4242

4343
// Also register handler to the service.
4444
service.Handler = func(w http.ResponseWriter, r *http.Request) {
45-
Serve(w, r, service.Func)
45+
serve(w, r, service.Func)
4646
}
4747

4848
admissionMap[service.Path] = service

pkg/webhooks/router/server.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ var CONTENTTYPE = "Content-Type"
3535
// APPLICATIONJSON json content.
3636
var APPLICATIONJSON = "application/json"
3737

38-
// Serve the http request.
39-
func Serve(w io.Writer, r *http.Request, admit AdmitFunc) {
38+
// MaxRequestBody caps the admission request body size to avoid OOM from
39+
// oversized requests. 3 MiB matches the kube-apiserver default.
40+
const MaxRequestBody int64 = 3 * 1024 * 1024
41+
42+
// serve the http request.
43+
func serve(w http.ResponseWriter, r *http.Request, admit AdmitFunc) {
4044
var body []byte
4145
if r.Body != nil {
42-
if data, err := io.ReadAll(r.Body); err == nil {
43-
body = data
46+
r.Body = http.MaxBytesReader(w, r.Body, MaxRequestBody)
47+
data, err := io.ReadAll(r.Body)
48+
if err != nil {
49+
klog.Errorf("Failed to read admission request body: %v", err)
50+
http.Error(w, "request body too large", http.StatusRequestEntityTooLarge)
51+
return
4452
}
53+
body = data
4554
}
4655

4756
// verify the content type is accurate

0 commit comments

Comments
 (0)