Skip to content

Commit

Permalink
fix: potential panics on error (#1389)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* In the [`net/http` Do method](https://pkg.go.dev/net/http#Client.Do),
if an error is returned, the http response should be ignored. Currently,
we defer closing the response body which leads to a nil pointer error
  • Loading branch information
kangmingtay authored Jan 29, 2024
1 parent 0100a80 commit 5ad703b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
1 change: 0 additions & 1 deletion internal/api/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func makeRequest(ctx context.Context, tok *oauth2.Token, g *oauth2.Config, url s
defer utilities.SafeClose(res.Body)

bodyBytes, _ := io.ReadAll(res.Body)
defer utilities.SafeClose(res.Body)
res.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/sms_provider/twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func (t *TwilioProvider) SendSms(phone, message, channel, otp string) (string, e
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.SetBasicAuth(t.Config.AccountSid, t.Config.AuthToken)
res, err := client.Do(r)
defer utilities.SafeClose(res.Body)
if err != nil {
return "", err
}
defer utilities.SafeClose(res.Body)
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
resp := &twilioErrResponse{}
if err := json.NewDecoder(res.Body).Decode(resp); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/sms_provider/twilio_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (t *TwilioVerifyProvider) SendSms(phone, message, channel string) (string,
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.SetBasicAuth(t.Config.AccountSid, t.Config.AuthToken)
res, err := client.Do(r)
defer utilities.SafeClose(res.Body)
if err != nil {
return "", err
}
defer utilities.SafeClose(res.Body)
if !(res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated) {
resp := &twilioErrResponse{}
if err := json.NewDecoder(res.Body).Decode(resp); err != nil {
Expand Down Expand Up @@ -114,10 +114,10 @@ func (t *TwilioVerifyProvider) VerifyOTP(phone, code string) error {
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.SetBasicAuth(t.Config.AccountSid, t.Config.AuthToken)
res, err := client.Do(r)
defer utilities.SafeClose(res.Body)
if err != nil {
return err
}
defer utilities.SafeClose(res.Body)
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
resp := &twilioErrResponse{}
if err := json.NewDecoder(res.Body).Decode(resp); err != nil {
Expand Down

0 comments on commit 5ad703b

Please sign in to comment.