diff --git a/router/router.go b/router/router.go index b7f8003..b773360 100755 --- a/router/router.go +++ b/router/router.go @@ -28,6 +28,8 @@ import ( "github.com/pkg/errors" ) +const maxBodyDrainBytes = 256 * 1024 + // New returns ServeMux with routes using given handler. func New(cfg config.Server, h handler.Handler) *http.ServeMux { @@ -164,12 +166,12 @@ func recoverWrap(h http.Handler) http.Handler { }) } -// flushAndClose helps to flush and close a ReadCloser. Used for request body internal. +// flushAndClose drains at most maxBodyDrainBytes before closing a request body. // Returns if there is any errors. func flushAndClose(rc io.ReadCloser) error { if rc != nil { // flush - _, err := io.Copy(io.Discard, rc) + _, err := io.Copy(io.Discard, io.LimitReader(rc, maxBodyDrainBytes)) if err != nil { return errors.Wrap(err, "request body flush failed") } diff --git a/router/router_test.go b/router/router_test.go index d8c69d0..d8e6739 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -752,6 +752,50 @@ func Test_flushAndClose(t *testing.T) { } } +func TestRoutingUnsupportedMethodBodyLimit(t *testing.T) { + const drainLimit = 256 * 1024 + mux := New(config.Server{Timeout: "1s"}, &dummyHandler{}) + for _, path := range []string{"/authn", "/authz"} { + t.Run(path, func(t *testing.T) { + for _, length := range []int64{drainLimit * 4, -1} { + t.Run(fmt.Sprint(length), func(t *testing.T) { + remaining := drainLimit * 4 + bytesRead := 0 + closed := false + body := &readCloserMock{ + readMock: func(p []byte) (int, error) { + if remaining == 0 { + return 0, io.EOF + } + n := min(len(p), remaining) + remaining -= n + bytesRead += n + return n, nil + }, + closeMock: func() error { + closed = true + return nil + }, + } + req := httptest.NewRequestWithContext(context.Background(), http.MethodPut, path, body) + req.ContentLength = length + w := httptest.NewRecorder() + mux.ServeHTTP(w, req) + if w.Code != http.StatusMethodNotAllowed { + t.Errorf("status = %d, want %d", w.Code, http.StatusMethodNotAllowed) + } + if bytesRead > drainLimit { + t.Errorf("cleanup read %d bytes, want at most %d", bytesRead, drainLimit) + } + if !closed { + t.Error("request body was not closed") + } + }) + } + }) + } +} + func Test_recoverWrap(t *testing.T) { type args struct { h http.Handler diff --git a/third_party/webhook/authn.go b/third_party/webhook/authn.go index 3ee6f23..07eb78e 100644 --- a/third_party/webhook/authn.go +++ b/third_party/webhook/authn.go @@ -30,7 +30,7 @@ func newAuthn(c AuthenticationConfig) *authenticator { func (a *authenticator) getTokenReview(ctx context.Context, req *http.Request) (*authn.TokenReview, error) { b, err := ioutil.ReadAll(req.Body) if err != nil { - return nil, fmt.Errorf("body read error for authentication request, %v", err) + return nil, fmt.Errorf("body read error for authentication request, %w", err) } if len(b) == 0 { return nil, fmt.Errorf("empty body for authentication request") @@ -130,7 +130,7 @@ func (a *authenticator) ServeHTTP(w http.ResponseWriter, r *http.Request) { tr, err := a.getTokenReview(ctx, r) if err != nil { l.Printf("authn request error from %s: %v\n", r.RemoteAddr, err) - http.Error(w, err.Error(), http.StatusBadRequest) + writeRequestError(w, err) return } diff --git a/third_party/webhook/authz.go b/third_party/webhook/authz.go index 25f3b72..443634b 100644 --- a/third_party/webhook/authz.go +++ b/third_party/webhook/authz.go @@ -94,7 +94,7 @@ func (a *authorizer) getSubjectAccessReview(ctx context.Context, req *http.Reque isV1Beta := false // TODO: Remove me! Temporary fix to support both v1 and v1beta1 versions of SubjectAccessReview. b, err := ioutil.ReadAll(req.Body) if err != nil { - return isV1Beta, nil, fmt.Errorf("body read error for authorization request, %v", err) + return isV1Beta, nil, fmt.Errorf("body read error for authorization request, %w", err) } if len(b) == 0 { return isV1Beta, nil, fmt.Errorf("empty body for authorization request") @@ -325,7 +325,7 @@ func (a *authorizer) ServeHTTP(w http.ResponseWriter, r *http.Request) { isV1Beta, sr, err := a.getSubjectAccessReview(ctx, r) if err != nil { getLogger(ctx).Printf("authz request error from %s: %v\n", r.RemoteAddr, err) - http.Error(w, err.Error(), http.StatusBadRequest) + writeRequestError(w, err) return } diff --git a/third_party/webhook/http.go b/third_party/webhook/http.go index e09a058..2642758 100644 --- a/third_party/webhook/http.go +++ b/third_party/webhook/http.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/base32" "encoding/json" + "errors" "log" "net/http" "os" @@ -13,8 +14,20 @@ import ( // this file contains http and context helpers +// Match the default Kubernetes API server request-body limit. +const maxRequestBodyBytes = 3 * 1024 * 1024 + var logKey = struct{}{} +func writeRequestError(w http.ResponseWriter, err error) { + status := http.StatusBadRequest + var maxBytesErr *http.MaxBytesError + if errors.As(err, &maxBytesErr) { + status = http.StatusRequestEntityTooLarge + } + http.Error(w, err.Error(), status) +} + // writeJSON writes the supplied data as JSON to the response writer. func writeJSON(ctx context.Context, w http.ResponseWriter, data interface{}) { b, err := json.Marshal(data) @@ -81,6 +94,7 @@ func wrapHandler(delegate http.Handler, config Config) http.Handler { } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { req := requestWithContext(r, config) + req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes) ctx := req.Context() if isLogEnabled(ctx, LogTraceServer) { dumpRequest(getLogger(ctx), req) diff --git a/third_party/webhook/request_body_test.go b/third_party/webhook/request_body_test.go new file mode 100644 index 0000000..39aa812 --- /dev/null +++ b/third_party/webhook/request_body_test.go @@ -0,0 +1,235 @@ +package webhook + +import ( + "context" + "encoding/json" + "io" + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + authn "k8s.io/api/authentication/v1" + authz "k8s.io/api/authorization/v1" +) + +const testRequestBodyLimit = 3 * 1024 * 1024 + +type bodyLimitUserMapper struct{} + +func (bodyLimitUserMapper) MapUser(context.Context, string, string) (authn.UserInfo, error) { + return authn.UserInfo{}, nil +} + +type bodyLimitResourceMapper struct{} + +func (bodyLimitResourceMapper) MapResource(_ context.Context, spec authz.SubjectAccessReviewSpec) (string, []AthenzAccessCheck, error) { + return spec.User, nil, nil +} + +type bodyLimitEndpoint struct { + name string + path string + apiVersion string + payload string + handler http.Handler +} + +func bodyLimitEndpoints() []bodyLimitEndpoint { + cfg := Config{LogProvider: func(string) Logger { return log.New(io.Discard, "", 0) }} + authorizer := NewAuthorizer(AuthorizationConfig{Config: cfg, Mapper: bodyLimitResourceMapper{}}) + return []bodyLimitEndpoint{ + { + name: "authn", + path: "/authn", + apiVersion: "authentication.k8s.io/v1", + payload: `{"apiVersion":"authentication.k8s.io/v1","kind":"TokenReview","spec":{"token":"invalid-test-input"}}`, + handler: NewAuthenticator(AuthenticationConfig{Config: cfg, Mapper: bodyLimitUserMapper{}}), + }, + { + name: "authz/v1", + path: "/authz", + apiVersion: "authorization.k8s.io/v1", + payload: `{"apiVersion":"authorization.k8s.io/v1","kind":"SubjectAccessReview","spec":{"user":"example-user","resourceAttributes":{"verb":"get","resource":"pods"}}}`, + handler: authorizer, + }, + { + name: "authz/v1beta1", + path: "/authz", + apiVersion: "authorization.k8s.io/v1beta1", + payload: `{"apiVersion":"authorization.k8s.io/v1beta1","kind":"SubjectAccessReview","spec":{"user":"example-user","resourceAttributes":{"verb":"get","resource":"pods"}}}`, + handler: authorizer, + }, + } +} + +type whitespaceReader struct{} + +func (whitespaceReader) Read(p []byte) (int, error) { + for i := range p { + p[i] = ' ' + } + return len(p), nil +} + +type countedRequestBody struct { + io.Reader + bytesRead int64 +} + +func (b *countedRequestBody) Read(p []byte) (int, error) { + n, err := b.Reader.Read(p) + b.bytesRead += int64(n) + return n, err +} + +func (*countedRequestBody) Close() error { return nil } + +func paddedRequestBody(payload string, size int64) *countedRequestBody { + return &countedRequestBody{Reader: io.MultiReader( + strings.NewReader(payload), + io.LimitReader(whitespaceReader{}, size-int64(len(payload))), + )} +} + +func TestWebhookRequestBodyLimit(t *testing.T) { + for _, endpoint := range bodyLimitEndpoints() { + t.Run(endpoint.name, func(t *testing.T) { + for _, size := range []struct { + name string + n int64 + }{ + {"small", int64(len(endpoint.payload))}, + {"below_limit", testRequestBodyLimit - 1}, + {"at_limit", testRequestBodyLimit}, + {"above_limit", testRequestBodyLimit + 1}, + {"larger_body", testRequestBodyLimit * 2}, + } { + t.Run(size.name, func(t *testing.T) { + for _, knownLength := range []bool{true, false} { + name := "known_length" + if !knownLength { + name = "unknown_length" + } + t.Run(name, func(t *testing.T) { + body := paddedRequestBody(endpoint.payload, size.n) + req := httptest.NewRequest(http.MethodPost, endpoint.path, body) + req.ContentLength = -1 + if knownLength { + req.ContentLength = size.n + } + w := httptest.NewRecorder() + endpoint.handler.ServeHTTP(w, req) + + wantStatus := http.StatusOK + if size.n > testRequestBodyLimit { + wantStatus = http.StatusRequestEntityTooLarge + } + if w.Code != wantStatus { + t.Errorf("status = %d, want %d", w.Code, wantStatus) + } + if body.bytesRead > testRequestBodyLimit+1 { + t.Errorf("read %d bytes, want at most %d", body.bytesRead, testRequestBodyLimit+1) + } + if wantStatus != http.StatusOK || w.Code != http.StatusOK { + return + } + var response struct { + APIVersion string + Status struct { + Allowed bool + Authenticated bool + } + } + if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { + t.Fatal(err) + } + if response.APIVersion != endpoint.apiVersion { + t.Errorf("apiVersion = %q, want %q", response.APIVersion, endpoint.apiVersion) + } + if endpoint.path == "/authz" && !response.Status.Allowed { + t.Error("expected the mapper's no-check authorization result") + } + if endpoint.path == "/authn" && response.Status.Authenticated { + t.Error("invalid test input must not authenticate") + } + }) + } + }) + } + }) + } +} + +type failedRequestBody struct{} + +func (failedRequestBody) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF } + +func TestWebhookRequestBodyValidation(t *testing.T) { + for _, endpoint := range bodyLimitEndpoints() { + t.Run(endpoint.name, func(t *testing.T) { + for _, input := range []struct { + name string + body io.Reader + }{ + {"empty", strings.NewReader("")}, + {"malformed", strings.NewReader("{")}, + {"read_error", failedRequestBody{}}, + } { + t.Run(input.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, endpoint.path, input.body) + w := httptest.NewRecorder() + endpoint.handler.ServeHTTP(w, req) + if w.Code != http.StatusBadRequest { + t.Errorf("status = %d, want %d", w.Code, http.StatusBadRequest) + } + }) + } + }) + } +} + +func TestWebhookChunkedRequestBodyLimit(t *testing.T) { + for _, endpoint := range bodyLimitEndpoints() { + t.Run(endpoint.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.ContentLength != -1 || len(r.TransferEncoding) != 1 || r.TransferEncoding[0] != "chunked" { + t.Errorf("expected a chunked request, got length %d and encoding %v", r.ContentLength, r.TransferEncoding) + } + endpoint.handler.ServeHTTP(w, r) + })) + t.Cleanup(server.Close) + client := server.Client() + client.Timeout = 5 * time.Second + for _, test := range []struct { + name string + size int64 + wantStatus int + }{ + {"at_limit", testRequestBodyLimit, http.StatusOK}, + {"above_limit", testRequestBodyLimit + 1, http.StatusRequestEntityTooLarge}, + } { + t.Run(test.name, func(t *testing.T) { + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, server.URL+endpoint.path, + paddedRequestBody(endpoint.payload, test.size)) + if err != nil { + t.Fatal(err) + } + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + if resp.StatusCode != test.wantStatus { + t.Errorf("status = %d, want %d", resp.StatusCode, test.wantStatus) + } + if _, err := io.Copy(io.Discard, resp.Body); err != nil { + t.Fatal(err) + } + }) + } + }) + } +}