Merge pull request #2765 from Poor12/fix-io-readall

Add limitReader to io.ReadAll
This commit is contained in:
karmada-bot 2022-11-10 11:23:20 +08:00 committed by GitHub
commit b9612c2e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -19,6 +19,8 @@ const (
// A split symbol that receives multiple values from a command flag
separator = ","
labelSeparator = "="
// MaxRespBodyLength is the max length of http response body
MaxRespBodyLength = 1 << 20 // 1 MiB
)
// IsExist Determine whether the path exists
@ -60,7 +62,7 @@ func InternetIP() (net.IP, error) {
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
content, err := io.ReadAll(io.LimitReader(resp.Body, MaxRespBodyLength))
if err != nil {
return nil, err
}

View File

@ -14,6 +14,11 @@ import (
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
)
const (
// MaxRespBodyLength is the max length of http response body
MaxRespBodyLength = 1 << 20 // 1 MiB
)
var admissionScheme = runtime.NewScheme()
var admissionCodecs = serializer.NewCodecFactory(admissionScheme)
@ -33,7 +38,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
if body, err = io.ReadAll(r.Body); err != nil {
if body, err = io.ReadAll(io.LimitReader(r.Body, MaxRespBodyLength)); err != nil {
klog.Errorf("unable to read the body from the incoming request: %w", err)
res = Errored(http.StatusBadRequest, err)
wh.writeResponse(w, res)