diff --git a/pkg/webhook/admission/http.go b/pkg/webhook/admission/http.go index 4735800195..8bcbaf45c3 100644 --- a/pkg/webhook/admission/http.go +++ b/pkg/webhook/admission/http.go @@ -47,7 +47,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 be10aea459..ad5e809415 100644 --- a/pkg/webhook/admission/http_test.go +++ b/pkg/webhook/admission/http_test.go @@ -84,6 +84,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 return the response given by the handler with version defaulted to v1", 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 3b0d5c4959..884f572123 100644 --- a/pkg/webhook/authentication/http.go +++ b/pkg/webhook/authentication/http.go @@ -47,7 +47,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 86bd5d0153..0305cefc62 100644 --- a/pkg/webhook/authentication/http_test.go +++ b/pkg/webhook/authentication/http_test.go @@ -94,6 +94,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 return the response given by the handler with version defaulted to v1", func() { req := &http.Request{ Header: http.Header{"Content-Type": []string{"application/json"}},