File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -35,13 +35,22 @@ var CONTENTTYPE = "Content-Type"
3535// APPLICATIONJSON json content.
3636var 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
You can’t perform that action at this time.
0 commit comments