Skip to content

Commit

Permalink
fix: http connection leak in NewBearer()
Browse files Browse the repository at this point in the history
The `response.Body` for the first `http.Get()` call in `NewBearer()`
is not closed. This leaks connections to the `registry` server and
can result in substantial memory usage in the `registry`, as the
registry allocates a 4MB buffer for each connection and neither end
enforces an idle timeout.

Fixes #378
  • Loading branch information
kppullin authored and paullaffitte committed Aug 13, 2024
1 parent 5fe3ef2 commit 3d7560d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/proxy/bearer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (b *Bearer) GetToken() string {

func NewBearer(endpoint string, path string) (*Bearer, error) {
response, err := http.Get(endpoint + path)
if response != nil && response.Body != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
Expand All @@ -49,6 +52,9 @@ func NewBearer(endpoint string, path string) (*Bearer, error) {
url := fmt.Sprintf("%s?service=%s&scope=%s", wwwAuthenticate["realm"], wwwAuthenticate["service"], wwwAuthenticate["scope"])

response, err := http.Get(url)
if response != nil && response.Body != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
Expand All @@ -57,8 +63,6 @@ func NewBearer(endpoint string, path string) (*Bearer, error) {
if err != nil {
return nil, err
}

response.Body.Close()
}

return &bearer, nil
Expand Down

0 comments on commit 3d7560d

Please sign in to comment.