1Password/connect-sdk-go

connect.readResponseBody does not properly report HTTP errors without a JSON body

nicmr opened this issue · 0 comments

nicmr commented

Your environment

SDK Version: 1.3.0

Connect Server Version: Will update once I find out (client issue, probably doesn't matter)

OS: MacOS 12.4

Go Version: 1.18.3

What happened?

There was a HTTP 502 Bad Gateway error on http requests to the machine running the Connect server.
Instead of checking the http response code and returning an appropriate error, I received the following error:

import (
	onePasswordConnect "github.com/1Password/connect-sdk-go/connect"
)

func reproduceError(url string, token string, vault string, item string) {
	onePasswordClient := onePasswordConnect.NewClient(url, token)
	onePasswordItem, err := onePasswordClient.GetItem(item, vault) // <-- returns decoding error
}
coding error response: invalid character '<' looking for beginning of value

goroutine 1 [running]:
/somepackage/onepassword.getFile({PARAMS OMITTED FOR PRIVACY})
...

This is caused by the following function, which expects all HTTP responses with a non-ok code to contain a json body, and returns a decoding error otherwise:

func readResponseBody(resp *http.Response, expectedStatusCode int) ([]byte, error) {

What did you expect to happen?

Unexpected HTTP status codes return a descriptive error. Gracefully handle the http error response body not containing a JSON body.

Steps to reproduce

  1. Make a request to a server (that may or may not be running 1password Connect successfully) that will return a non-ok http response code, but does not contain a JSON response body
  2. Use any function that makes a REST request, e.g. connect.GetItem
  3. Inspect the returned error

Notes & Logs

If you'd like a PR to fix this, I can work on it.