Merge pull request #83261 from liggitt/yaml-limits
limit yaml/json decode size
This commit is contained in:
@@ -21,11 +21,11 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
|
||||
"k8s.io/kubernetes/test/integration/framework"
|
||||
)
|
||||
|
||||
@@ -33,13 +33,11 @@ import (
|
||||
func TestMaxResourceSize(t *testing.T) {
|
||||
stopCh := make(chan struct{})
|
||||
defer close(stopCh)
|
||||
clientSet, _ := framework.StartTestServer(t, stopCh, framework.TestServerSetup{
|
||||
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
|
||||
opts.GenericServerRunOptions.MaxRequestBodyBytes = 1024 * 1024
|
||||
},
|
||||
})
|
||||
clientSet, _ := framework.StartTestServer(t, stopCh, framework.TestServerSetup{})
|
||||
|
||||
hugeData := []byte(strings.Repeat("x", 1024*1024+1))
|
||||
hugeData := []byte(strings.Repeat("x", 3*1024*1024+1))
|
||||
|
||||
rest := clientSet.Discovery().RESTClient()
|
||||
|
||||
c := clientSet.CoreV1().RESTClient()
|
||||
t.Run("Create should limit the request body size", func(t *testing.T) {
|
||||
@@ -87,6 +85,38 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
|
||||
}
|
||||
})
|
||||
t.Run("JSONPatchType should handle a patch just under the max limit", func(t *testing.T) {
|
||||
patchBody := []byte(`[{"op":"add","path":"/foo","value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}]`)
|
||||
err = rest.Patch(types.JSONPatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
|
||||
Body(patchBody).Do().Error()
|
||||
if err != nil && !errors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("MergePatchType should handle a patch just under the max limit", func(t *testing.T) {
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.MergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
|
||||
Body(patchBody).Do().Error()
|
||||
if err != nil && !errors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("StrategicMergePatchType should handle a patch just under the max limit", func(t *testing.T) {
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.StrategicMergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
|
||||
Body(patchBody).Do().Error()
|
||||
if err != nil && !errors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("ApplyPatchType should handle a patch just under the max limit", func(t *testing.T) {
|
||||
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
|
||||
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
|
||||
Body(patchBody).Do().Error()
|
||||
if err != nil && !errors.IsBadRequest(err) {
|
||||
t.Errorf("expected success or bad request err, got %#v", err)
|
||||
}
|
||||
})
|
||||
t.Run("Delete should limit the request body size", func(t *testing.T) {
|
||||
err = c.Delete().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
|
||||
Body(hugeData).Do().Error()
|
||||
@@ -98,4 +128,128 @@ func TestMaxResourceSize(t *testing.T) {
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
// Create YAML over 3MB limit
|
||||
t.Run("create should limit yaml parsing", func(t *testing.T) {
|
||||
yamlBody := []byte(`
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mytest
|
||||
values: ` + strings.Repeat("[", 3*1024*1024))
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/yaml").
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw()
|
||||
if !apierrors.IsRequestEntityTooLargeError(err) {
|
||||
t.Errorf("expected too large error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Create YAML just under 3MB limit, nested
|
||||
t.Run("create should handle a yaml document just under the maximum size with correct nesting", func(t *testing.T) {
|
||||
yamlBody := []byte(`
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mytest
|
||||
values: ` + strings.Repeat("[", 3*1024*1024/2-500) + strings.Repeat("]", 3*1024*1024/2-500))
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/yaml").
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw()
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Create YAML just under 3MB limit, not nested
|
||||
t.Run("create should handle a yaml document just under the maximum size with unbalanced nesting", func(t *testing.T) {
|
||||
yamlBody := []byte(`
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mytest
|
||||
values: ` + strings.Repeat("[", 3*1024*1024-1000))
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/yaml").
|
||||
SetHeader("Content-Type", "application/yaml").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(yamlBody).
|
||||
DoRaw()
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Create JSON over 3MB limit
|
||||
t.Run("create should limit json parsing", func(t *testing.T) {
|
||||
jsonBody := []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": {
|
||||
"name": "mytest"
|
||||
},
|
||||
"values": ` + strings.Repeat("[", 3*1024*1024/2) + strings.Repeat("]", 3*1024*1024/2) + "}")
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw()
|
||||
if !apierrors.IsRequestEntityTooLargeError(err) {
|
||||
t.Errorf("expected too large error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Create JSON just under 3MB limit, nested
|
||||
t.Run("create should handle a json document just under the maximum size with correct nesting", func(t *testing.T) {
|
||||
jsonBody := []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": {
|
||||
"name": "mytest"
|
||||
},
|
||||
"values": ` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + "}")
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw()
|
||||
// TODO(liggitt): expect bad request on deep nesting, rather than success on dropped unknown field data
|
||||
if err != nil && !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Create JSON just under 3MB limit, not nested
|
||||
t.Run("create should handle a json document just under the maximum size with unbalanced nesting", func(t *testing.T) {
|
||||
jsonBody := []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": {
|
||||
"name": "mytest"
|
||||
},
|
||||
"values": ` + strings.Repeat("[", 3*1024*1024-1000) + "}")
|
||||
|
||||
_, err := rest.Post().
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
AbsPath("/api/v1/namespaces/default/configmaps").
|
||||
Body(jsonBody).
|
||||
DoRaw()
|
||||
if !apierrors.IsBadRequest(err) {
|
||||
t.Errorf("expected bad request, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@@ -307,30 +307,36 @@ func TestObjectSizeResponses(t *testing.T) {
|
||||
client := clientset.NewForConfigOrDie(&restclient.Config{Host: s.URL})
|
||||
|
||||
const DeploymentMegabyteSize = 100000
|
||||
const DeploymentTwoMegabyteSize = 1000000
|
||||
const DeploymentTwoMegabyteSize = 175000
|
||||
const DeploymentThreeMegabyteSize = 250000
|
||||
|
||||
expectedMsgFor1MB := `etcdserver: request is too large`
|
||||
expectedMsgFor2MB := `rpc error: code = ResourceExhausted desc = trying to send message larger than max`
|
||||
expectedMsgFor3MB := `Request entity too large: limit is 3145728`
|
||||
expectedMsgForLargeAnnotation := `metadata.annotations: Too long: must have at most 262144 characters`
|
||||
|
||||
deployment1 := constructBody("a", DeploymentMegabyteSize, "labels", t) // >1 MB file
|
||||
deployment2 := constructBody("a", DeploymentTwoMegabyteSize, "labels", t) // >2 MB file
|
||||
deployment1 := constructBody("a", DeploymentMegabyteSize, "labels", t) // >1 MB file
|
||||
deployment2 := constructBody("a", DeploymentTwoMegabyteSize, "labels", t) // >2 MB file
|
||||
deployment3 := constructBody("a", DeploymentThreeMegabyteSize, "labels", t) // >3 MB file
|
||||
|
||||
deployment3 := constructBody("a", DeploymentMegabyteSize, "annotations", t)
|
||||
deployment4 := constructBody("a", DeploymentMegabyteSize, "annotations", t)
|
||||
|
||||
deployment4 := constructBody("sample/sample", DeploymentMegabyteSize, "finalizers", t) // >1 MB file
|
||||
deployment5 := constructBody("sample/sample", DeploymentTwoMegabyteSize, "finalizers", t) // >2 MB file
|
||||
deployment5 := constructBody("sample/sample", DeploymentMegabyteSize, "finalizers", t) // >1 MB file
|
||||
deployment6 := constructBody("sample/sample", DeploymentTwoMegabyteSize, "finalizers", t) // >2 MB file
|
||||
deployment7 := constructBody("sample/sample", DeploymentThreeMegabyteSize, "finalizers", t) // >3 MB file
|
||||
|
||||
requests := []struct {
|
||||
size string
|
||||
deploymentObject *appsv1.Deployment
|
||||
expectedMessage string
|
||||
}{
|
||||
{"1 MB", deployment1, expectedMsgFor1MB},
|
||||
{"2 MB", deployment2, expectedMsgFor2MB},
|
||||
{"1 MB", deployment3, expectedMsgForLargeAnnotation},
|
||||
{"1 MB", deployment4, expectedMsgFor1MB},
|
||||
{"2 MB", deployment5, expectedMsgFor2MB},
|
||||
{"1 MB labels", deployment1, expectedMsgFor1MB},
|
||||
{"2 MB labels", deployment2, expectedMsgFor2MB},
|
||||
{"3 MB labels", deployment3, expectedMsgFor3MB},
|
||||
{"1 MB annotations", deployment4, expectedMsgForLargeAnnotation},
|
||||
{"1 MB finalizers", deployment5, expectedMsgFor1MB},
|
||||
{"2 MB finalizers", deployment6, expectedMsgFor2MB},
|
||||
{"3 MB finalizers", deployment7, expectedMsgFor3MB},
|
||||
}
|
||||
|
||||
for _, r := range requests {
|
||||
|
Reference in New Issue
Block a user