Skip to content

Commit

Permalink
Fix some memory leaks caused by not closing HTTP request body (evcc-i…
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Dec 10, 2020
1 parent 2df30f6 commit 1bc1c45
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 9 deletions.
10 changes: 7 additions & 3 deletions cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ func runHealth(cmd *cobra.Command, args []string) {
var ok bool
resp, err := client.Get(fmt.Sprintf("http+unix://%s/health", serviceName))

if err == nil && resp.StatusCode == http.StatusOK {
log.INFO.Printf("health check ok")
ok = true
if err == nil {
resp.Body.Close()

if resp.StatusCode == http.StatusOK {
log.INFO.Printf("health check ok")
ok = true
}
}

if !ok {
Expand Down
2 changes: 1 addition & 1 deletion util/request/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (e StatusError) HasStatus(codes ...int) bool {
return false
}

// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx. It closes the request body after reading.
func ReadBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()

Expand Down
11 changes: 9 additions & 2 deletions vehicle/bluelink/bluelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ func (v *API) getCookies() (cookieClient *request.Helper, err error) {
v.config.CCSPServiceID,
v.config.URI,
)
_, err = cookieClient.Get(uri)

var resp *http.Response
if resp, err = cookieClient.Get(uri); err == nil {
resp.Body.Close()
}
}

return cookieClient, err
Expand All @@ -159,7 +163,10 @@ func (v *API) setLanguage(cookieClient *request.Helper) error {

req, err := request.New(http.MethodPost, v.config.URI+v.config.Lang, request.MarshalJSON(data), request.JSONEncoding)
if err == nil {
_, err = cookieClient.Do(req)
var resp *http.Response
if resp, err = cookieClient.Do(req); err == nil {
resp.Body.Close()
}
}

return err
Expand Down
1 change: 1 addition & 0 deletions vehicle/bmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (v *BMW) login(user, password string) error {
if err != nil {
return err
}
defer resp.Body.Close()

query, err := url.ParseQuery(resp.Header.Get("Location"))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions vehicle/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (v *ID) authFlow() error {
var vars vw.FormVars
if err == nil {
vars, err = vw.FormValues(resp.Body, "form#emailPasswordForm")
resp.Body.Close()
}

// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/identifier
Expand Down
2 changes: 2 additions & 0 deletions vehicle/nissan.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func (v *Nissan) authFlow() error {
v.Client.CheckRedirect = nil

if err == nil {
resp.Body.Close()

var location *url.URL
if location, err = url.Parse(resp.Header.Get("Location")); err == nil {
if code = location.Query().Get("code"); code == "" {
Expand Down
5 changes: 4 additions & 1 deletion vehicle/porsche.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (v *Porsche) authFlow() error {
if err != nil {
return err
}
resp.Body.Close()

query, err := url.ParseQuery(resp.Request.URL.RawQuery)
if err != nil {
Expand Down Expand Up @@ -161,9 +162,10 @@ func (v *Porsche) authFlow() error {
}

// process the auth so the session is authenticated
if _, err = client.Do(req); err != nil {
if resp, err = client.Do(req); err != nil {
return err
}
resp.Body.Close()

var CodeVerifier, _ = cv.CreateCodeVerifier()
codeChallenge := CodeVerifier.CodeChallengeS256()
Expand All @@ -188,6 +190,7 @@ func (v *Porsche) authFlow() error {
if err != nil {
return err
}
resp.Body.Close()

query, err = url.ParseQuery(resp.Request.URL.RawQuery)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion vehicle/vw.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ func (v *VW) Close() error {
})

if err == nil {
_, err = v.Do(req)
var resp *http.Response
if resp, err = v.Do(req); err == nil {
resp.Body.Close()
}
}

return err
Expand Down
10 changes: 9 additions & 1 deletion vehicle/vw/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ type Identity struct {
*http.Client
}

// redirect follows HTTP redirect header if error is nil. Request body is closed.
func (v *Identity) redirect(resp *http.Response, err error) (*http.Response, error) {
if err == nil {
uri := resp.Header.Get("Location")
if uri == "" {
return nil, errors.New("could not find expected HTTP redirect header\ngo to https://www.portal.volkswagen-we.com/ check account status")
}

resp, err = v.Get(uri)
if resp, err = v.Get(uri); err == nil {
resp.Body.Close()
}
}

return resp, err
Expand All @@ -44,6 +47,9 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error
// GET identity.vwgroup.io/oidc/v1/authorize?ui_locales=de&scope=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&response_type=code&state=gmiJOaB4&redirect_uri=https%3A%2F%2Fwww.portal.volkswagen-we.com%2Fportal%2Fweb%2Fguest%2Fcomplete-login&nonce=38042ee3-b7a7-43cf-a9c1-63d2f3f2d9f3&prompt=login&client_id=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com
uri := "https://identity.vwgroup.io/oidc/v1/authorize?" + query.Encode()
resp, err := v.Get(uri)
if err == nil {
resp.Body.Close()
}

// GET identity.vwgroup.io/signin-service/v1/signin/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa
if err == nil {
Expand All @@ -52,6 +58,7 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error

if err == nil {
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
resp.Body.Close()
}
}

Expand Down Expand Up @@ -79,6 +86,7 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error

if err == nil {
vars, err = FormValues(resp.Body, "form#credentialsForm")
resp.Body.Close()
}
}

Expand Down

0 comments on commit 1bc1c45

Please sign in to comment.