Skip to content

Commit

Permalink
Merge pull request #91 from adammohammed/fix-sigsegv-for-bad-content-…
Browse files Browse the repository at this point in the history
…type

Check Content-Type before unmarshalling json
  • Loading branch information
phillc authored Sep 26, 2019
2 parents 7adba57 + 308f806 commit be0c109
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package linodego_test

import (
"context"
"log"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -127,3 +129,31 @@ func TestClientAliases(t *testing.T) {
t.Error("Expected alias for Volumes to return a *Resource")
}
}

func TestClient_APIResponseBadGateway(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestClient_APIResponseBadGateway")
defer teardown()

defer func() {
if r := recover(); r != nil {
t.Errorf("Expected Client to handle 502 from API Server")
}
}()

_, err := client.ListImages(context.Background(), nil)

if err == nil {
t.Errorf("Error should be thrown on 502 Response from API")
}

responseError, ok := err.(*Error)

if !ok {
t.Errorf("Error type did not match the expected result")
}

if !strings.Contains(responseError.Message, "Unexpected Content-Type") {
t.Errorf("Error message does not contain: \"Unexpected Content-Type\"")
}

}
12 changes: 12 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func coupleAPIErrors(r *resty.Response, err error) (*resty.Response, error) {
}

if r.Error() != nil {
// Check that response is of the correct content-type before unmarshalling
expectedContentType := r.Request.Header.Get("Accept")
responseContentType := r.Header().Get("Content-Type")
if responseContentType != expectedContentType {
msg := fmt.Sprintf(
"Unexpected Content-Type: Expected: %v, Received: %v",
expectedContentType,
responseContentType,
)
return nil, NewError(msg)
}

apiError, ok := r.Error().(*APIError)
if !ok || (ok && len(apiError.Errors) == 0) {
return r, nil
Expand Down
62 changes: 62 additions & 0 deletions fixtures/TestClient_APIResponseBadGateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Authorization:
- Bearer awesometokenawesometokenawesometoken
Content-Type:
- application/json
User-Agent:
- linodego 0.0.1 https://github.com/linode/linodego
url: https://api.linode.com/v4/images
method: GET
response:
body: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>502 Bad Gateway</title>
<h1>Bad Gateway</h1>
<p>The proxy server received an invalid response from an upstream server.</p>'
headers:
Access-Control-Allow-Headers:
- Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Filter
Access-Control-Allow-Methods:
- HEAD, GET, OPTIONS, POST, PUT, DELETE
Access-Control-Allow-Origin:
- '*'
Cache-Control:
- private, max-age=0, s-maxage=0, no-cache, no-store
Connection:
- keep-alive
Content-Length:
- "37"
Content-Type:
- text/html
Date:
- Tue, 03 Jul 2018 01:38:53 GMT
Retry-After:
- "23"
Server:
- nginx
Vary:
- Authorization, X-Filter
X-Accepted-Oauth-Scopes:
- linodes:read_only
X-Frame-Options:
- DENY
X-Oauth-Scopes:
- '*'
X-Ratelimit-Limit:
- "400"
X-Ratelimit-Remaining:
- "391"
X-Ratelimit-Reset:
- "1530581957"
X-Spec-Version:
- 4.0.2
status: 502 Bad Gateway
code: 502
duration: ""

0 comments on commit be0c109

Please sign in to comment.