Skip to content

Commit 15f586a

Browse files
authored
Merge pull request kubernetes#84963 from liggitt/feature-json-codes
Fix json patch limit check
2 parents ed3cc6a + 3d5f11b commit 15f586a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/patch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (p *jsonPatcher) applyJSPatch(versionedJS []byte) (patchedJS []byte, retErr
341341
// TODO(liggitt): drop this once golang json parser limits stack depth (https://github.com/golang/go/issues/31789)
342342
if len(p.patchBytes) > 1024*1024 {
343343
v := []interface{}{}
344-
if err := json.Unmarshal(p.patchBytes, v); err != nil {
344+
if err := json.Unmarshal(p.patchBytes, &v); err != nil {
345345
return nil, errors.NewBadRequest(fmt.Sprintf("error decoding patch: %v", err))
346346
}
347347
}
@@ -365,7 +365,7 @@ func (p *jsonPatcher) applyJSPatch(versionedJS []byte) (patchedJS []byte, retErr
365365
// TODO(liggitt): drop this once golang json parser limits stack depth (https://github.com/golang/go/issues/31789)
366366
if len(p.patchBytes) > 1024*1024 {
367367
v := map[string]interface{}{}
368-
if err := json.Unmarshal(p.patchBytes, v); err != nil {
368+
if err := json.Unmarshal(p.patchBytes, &v); err != nil {
369369
return nil, errors.NewBadRequest(fmt.Sprintf("error decoding patch: %v", err))
370370
}
371371
}

test/integration/apiserver/max_request_body_bytes_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ func TestMaxResourceSize(t *testing.T) {
9393
t.Errorf("expected success or bad request err, got %v", err)
9494
}
9595
})
96+
t.Run("JSONPatchType should handle a valid patch just under the max limit", func(t *testing.T) {
97+
patchBody := []byte(`[{"op":"add","path":"/foo","value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}]`)
98+
err = rest.Patch(types.JSONPatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
99+
Body(patchBody).Do().Error()
100+
if err != nil {
101+
t.Errorf("unexpected error: %v", err)
102+
}
103+
})
96104
t.Run("MergePatchType should handle a patch just under the max limit", func(t *testing.T) {
97105
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
98106
err = rest.Patch(types.MergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
@@ -101,6 +109,14 @@ func TestMaxResourceSize(t *testing.T) {
101109
t.Errorf("expected success or bad request err, got %v", err)
102110
}
103111
})
112+
t.Run("MergePatchType should handle a valid patch just under the max limit", func(t *testing.T) {
113+
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
114+
err = rest.Patch(types.MergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
115+
Body(patchBody).Do().Error()
116+
if err != nil {
117+
t.Errorf("unexpected error: %v", err)
118+
}
119+
})
104120
t.Run("StrategicMergePatchType should handle a patch just under the max limit", func(t *testing.T) {
105121
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
106122
err = rest.Patch(types.StrategicMergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
@@ -109,6 +125,14 @@ func TestMaxResourceSize(t *testing.T) {
109125
t.Errorf("expected success or bad request err, got %v", err)
110126
}
111127
})
128+
t.Run("StrategicMergePatchType should handle a valid patch just under the max limit", func(t *testing.T) {
129+
patchBody := []byte(`{"value":0` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
130+
err = rest.Patch(types.StrategicMergePatchType).AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
131+
Body(patchBody).Do().Error()
132+
if err != nil {
133+
t.Errorf("unexpected error: %v", err)
134+
}
135+
})
112136
t.Run("ApplyPatchType should handle a patch just under the max limit", func(t *testing.T) {
113137
patchBody := []byte(`{"value":` + strings.Repeat("[", 3*1024*1024/2-100) + strings.Repeat("]", 3*1024*1024/2-100) + `}`)
114138
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
@@ -117,6 +141,14 @@ func TestMaxResourceSize(t *testing.T) {
117141
t.Errorf("expected success or bad request err, got %#v", err)
118142
}
119143
})
144+
t.Run("ApplyPatchType should handle a valid patch just under the max limit", func(t *testing.T) {
145+
patchBody := []byte(`{"apiVersion":"v1","kind":"Secret"` + strings.Repeat(" ", 3*1024*1024-100) + `}`)
146+
err = rest.Patch(types.ApplyPatchType).Param("fieldManager", "test").AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
147+
Body(patchBody).Do().Error()
148+
if err != nil {
149+
t.Errorf("unexpected error: %v", err)
150+
}
151+
})
120152
t.Run("Delete should limit the request body size", func(t *testing.T) {
121153
err = c.Delete().AbsPath(fmt.Sprintf("/api/v1/namespaces/default/secrets/test")).
122154
Body(hugeData).Do().Error()

0 commit comments

Comments
 (0)