Limit the number of operations in a single json patch to be 10,000

Kubernetes-commit: 5e6fc5dce8b12c5ce80e016b208a51c81a8c9ce8
This commit is contained in:
Chao Xu 2019-02-12 23:37:01 -08:00 committed by Kubernetes Publisher
parent 908ce86a0f
commit 3866fe78d2
1 changed files with 10 additions and 0 deletions

View File

@ -49,6 +49,11 @@ import (
utiltrace "k8s.io/utils/trace"
)
const (
// maximum number of operations a single json patch may contain.
maxJSONPatchOperations = 10000
)
// PatchResource returns a function that will handle a resource patch.
func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface, patchTypes []string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
@ -331,6 +336,11 @@ func (p *jsonPatcher) applyJSPatch(versionedJS []byte) (patchedJS []byte, retErr
if err != nil {
return nil, errors.NewBadRequest(err.Error())
}
if len(patchObj) > maxJSONPatchOperations {
return nil, errors.NewRequestEntityTooLargeError(
fmt.Sprintf("The allowed maximum operations in a JSON patch is %d, got %d",
maxJSONPatchOperations, len(patchObj)))
}
patchedJS, err := patchObj.Apply(versionedJS)
if err != nil {
return nil, errors.NewGenericServerResponse(http.StatusUnprocessableEntity, "", schema.GroupResource{}, "", err.Error(), 0, false)