Skip to content

Commit 529ac8a

Browse files
committed
Limit the read length of ioutil.ReadAll in pkg/credentialprovider
Signed-off-by: Haiyan Meng <[email protected]>
1 parent 9e83e6d commit 529ac8a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

pkg/credentialprovider/azure/azure_acr_helper.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ package azure
4747
import (
4848
"bytes"
4949
"encoding/json"
50+
"errors"
5051
"fmt"
52+
"io"
5153
"io/ioutil"
5254
"net/http"
5355
"net/url"
@@ -178,10 +180,15 @@ func performTokenExchange(
178180
}
179181

180182
var content []byte
181-
if content, err = ioutil.ReadAll(exchange.Body); err != nil {
183+
limitedReader := &io.LimitedReader{R: exchange.Body, N: maxReadLength}
184+
if content, err = ioutil.ReadAll(limitedReader); err != nil {
182185
return "", fmt.Errorf("Www-Authenticate: error reading response from %s", authEndpoint)
183186
}
184187

188+
if limitedReader.N <= 0 {
189+
return "", errors.New("the read limit is reached")
190+
}
191+
185192
var authResp acrAuthResponse
186193
if err = json.Unmarshal(content, &authResp); err != nil {
187194
return "", fmt.Errorf("Www-Authenticate: unable to read response %s", content)

pkg/credentialprovider/azure/azure_credentials.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package azure
1818

1919
import (
2020
"context"
21+
"errors"
2122
"io"
2223
"io/ioutil"
2324
"os"
@@ -38,7 +39,10 @@ import (
3839
var flagConfigFile = pflag.String("azure-container-registry-config", "",
3940
"Path to the file containing Azure container registry configuration information.")
4041

41-
const dummyRegistryEmail = "[email protected]"
42+
const (
43+
dummyRegistryEmail = "[email protected]"
44+
maxReadLength = 10 * 1 << 20 // 10MB
45+
)
4246

4347
var containerRegistryUrls = []string{"*.azurecr.io", "*.azurecr.cn", "*.azurecr.de", "*.azurecr.us"}
4448

@@ -117,10 +121,14 @@ func parseConfig(configReader io.Reader) (*auth.AzureAuthConfig, error) {
117121
return &config, nil
118122
}
119123

120-
configContents, err := ioutil.ReadAll(configReader)
124+
limitedReader := &io.LimitedReader{R: configReader, N: maxReadLength}
125+
configContents, err := ioutil.ReadAll(limitedReader)
121126
if err != nil {
122127
return nil, err
123128
}
129+
if limitedReader.N <= 0 {
130+
return nil, errors.New("the read limit is reached")
131+
}
124132
err = yaml.Unmarshal(configContents, &config)
125133
if err != nil {
126134
return nil, err

pkg/credentialprovider/config.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package credentialprovider
1919
import (
2020
"encoding/base64"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
24+
"io"
2325
"io/ioutil"
2426
"net/http"
2527
"os"
@@ -30,6 +32,10 @@ import (
3032
"k8s.io/klog"
3133
)
3234

35+
const (
36+
maxReadLength = 10 * 1 << 20 // 10MB
37+
)
38+
3339
// DockerConfigJson represents ~/.docker/config.json file info
3440
// see https://github.com/docker/docker/pull/12009
3541
type DockerConfigJson struct {
@@ -195,11 +201,16 @@ func ReadUrl(url string, client *http.Client, header *http.Header) (body []byte,
195201
}
196202
}
197203

198-
contents, err := ioutil.ReadAll(resp.Body)
204+
limitedReader := &io.LimitedReader{R: resp.Body, N: maxReadLength}
205+
contents, err := ioutil.ReadAll(limitedReader)
199206
if err != nil {
200207
return nil, err
201208
}
202209

210+
if limitedReader.N <= 0 {
211+
return nil, errors.New("the read limit is reached")
212+
}
213+
203214
return contents, nil
204215
}
205216

0 commit comments

Comments
 (0)