diff --git a/pkg/webhook/admission/http.go b/pkg/webhook/admission/http.go index ea7b09992c..2dc5ebbdab 100644 --- a/pkg/webhook/admission/http.go +++ b/pkg/webhook/admission/http.go @@ -50,7 +50,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = wh.WithContextFunc(ctx, r) } - if r.Body == nil { + if r.Body == nil || r.Body == http.NoBody { err := errors.New("request body is empty") wh.getLogger(nil).Error(err, "bad request") wh.writeResponse(w, Errored(http.StatusBadRequest, err)) diff --git a/pkg/webhook/admission/http_test.go b/pkg/webhook/admission/http_test.go index f496d3e71d..b9e0b5f5c1 100644 --- a/pkg/webhook/admission/http_test.go +++ b/pkg/webhook/admission/http_test.go @@ -85,6 +85,19 @@ var _ = Describe("Admission Webhooks", func() { Expect(respRecorder.Body.String()).To(Equal(expected)) }) + It("should error when given a NoBody", func() { + req := &http.Request{ + Header: http.Header{"Content-Type": []string{"application/json"}}, + Method: http.MethodPost, + Body: http.NoBody, + } + + expected := `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}} +` + webhook.ServeHTTP(respRecorder, req) + Expect(respRecorder.Body.String()).To(Equal(expected)) + }) + It("should error when given an infinite body", func() { req := &http.Request{ Header: http.Header{"Content-Type": []string{"application/json"}}, diff --git a/pkg/webhook/authentication/http.go b/pkg/webhook/authentication/http.go index a657ef89db..ea26521e17 100644 --- a/pkg/webhook/authentication/http.go +++ b/pkg/webhook/authentication/http.go @@ -50,7 +50,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = wh.WithContextFunc(ctx, r) } - if r.Body == nil { + if r.Body == nil || r.Body == http.NoBody { err := errors.New("request body is empty") wh.getLogger(nil).Error(err, "bad request") wh.writeResponse(w, Errored(err)) diff --git a/pkg/webhook/authentication/http_test.go b/pkg/webhook/authentication/http_test.go index d8679f99f0..696f7a198f 100644 --- a/pkg/webhook/authentication/http_test.go +++ b/pkg/webhook/authentication/http_test.go @@ -95,6 +95,19 @@ var _ = Describe("Authentication Webhooks", func() { Expect(respRecorder.Body.String()).To(Equal(expected)) }) + It("should error when given a NoBody", func() { + req := &http.Request{ + Header: http.Header{"Content-Type": []string{"application/json"}}, + Method: http.MethodPost, + Body: http.NoBody, + } + + expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"request body is empty"}} +` + webhook.ServeHTTP(respRecorder, req) + Expect(respRecorder.Body.String()).To(Equal(expected)) + }) + It("should error when given an infinite body", func() { req := &http.Request{ Header: http.Header{"Content-Type": []string{"application/json"}},