Skip to content

Commit c3d156c

Browse files
authored
fix: unbounded read of consuming the http.Body in the validation webhook (#4053)
fix: unbounded read of consuming the http.Body in the validation webhook Reported by @alcls01111 Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
1 parent 639ea7c commit c3d156c

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

dataclients/kubernetes/admission/admission.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
const (
1414
metricNamespace = "routegroup_admission"
1515
metricSubsystem = "admitter"
16+
maxBodySize = 3145728
1617
)
1718

1819
var (
@@ -71,9 +72,9 @@ func Handler(admitter admitter) http.HandlerFunc {
7172
return
7273
}
7374

74-
defer r.Body.Close()
75-
76-
body, err := io.ReadAll(r.Body)
75+
rc := http.MaxBytesReader(w, r.Body, maxBodySize)
76+
defer rc.Close()
77+
body, err := io.ReadAll(rc)
7778
if err != nil {
7879
log.Errorf("Failed to read request: %v", err)
7980
w.WriteHeader(http.StatusInternalServerError)

dataclients/kubernetes/admission/admission_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,37 @@ func TestMalformedRequests(t *testing.T) {
315315
})
316316
}
317317
}
318+
319+
func TestBodySizeLimit(t *testing.T) {
320+
routeGroupHandler := Handler(&RouteGroupAdmitter{RouteGroupValidator: &definitions.RouteGroupValidator{}})
321+
ingressHandler := Handler(&IngressAdmitter{IngressValidator: &definitions.IngressV1Validator{}})
322+
323+
size := maxBodySize + 1
324+
buf := bytes.Buffer{}
325+
buf.WriteString("{")
326+
for range size - 1 {
327+
buf.WriteString(" ")
328+
}
329+
buf.WriteString("}")
330+
331+
makeRequest := func() *http.Request {
332+
method := "POST"
333+
334+
req := httptest.NewRequest(method, "http://example.com/foo", strings.NewReader(buf.String()))
335+
req.Header.Set("Content-Type", "application/json")
336+
return req
337+
}
338+
339+
t.Run("route group", func(t *testing.T) {
340+
w := httptest.NewRecorder()
341+
routeGroupHandler(w, makeRequest())
342+
assert.Equal(t, 500, w.Code)
343+
})
344+
345+
t.Run("ingress", func(t *testing.T) {
346+
w := httptest.NewRecorder()
347+
ingressHandler(w, makeRequest())
348+
assert.Equal(t, 500, w.Code)
349+
})
350+
351+
}

0 commit comments

Comments
 (0)