Skip to content

Commit

Permalink
Review feedback: limit number of bytes read under error.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Jun 1, 2017
1 parent 46abe8c commit 24a113b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 4 additions & 1 deletion storage/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
Expand All @@ -32,6 +33,8 @@ import (
"github.com/prometheus/prometheus/util/httputil"
)

const maxErrMsgLen = 256

// Client allows reading and writing from/to a remote HTTP endpoint.
type Client struct {
index int // Used to differentiate metrics.
Expand Down Expand Up @@ -118,7 +121,7 @@ func (c *Client) Store(samples model.Samples) error {
defer httpResp.Body.Close()

if httpResp.StatusCode/100 != 2 {
scanner := bufio.NewScanner(httpResp.Body)
scanner := bufio.NewScanner(io.LimitReader(httpResp.Body, maxErrMsgLen))
line := ""
if scanner.Scan() {
line = scanner.Text()
Expand Down
11 changes: 7 additions & 4 deletions storage/remote/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import (
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
)

var longErrMessage = strings.Repeat("error message", maxErrMsgLen)

func TestStoreHTTPErrorHandling(t *testing.T) {
tests := []struct {
code int
Expand All @@ -37,22 +40,22 @@ func TestStoreHTTPErrorHandling(t *testing.T) {
},
{
code: 300,
err: fmt.Errorf("server returned HTTP status 300 Multiple Choices: test error"),
err: fmt.Errorf("server returned HTTP status 300 Multiple Choices: " + longErrMessage[:maxErrMsgLen]),
},
{
code: 404,
err: fmt.Errorf("server returned HTTP status 404 Not Found: test error"),
err: fmt.Errorf("server returned HTTP status 404 Not Found: " + longErrMessage[:maxErrMsgLen]),
},
{
code: 500,
err: recoverableError{fmt.Errorf("server returned HTTP status 500 Internal Server Error: test error")},
err: recoverableError{fmt.Errorf("server returned HTTP status 500 Internal Server Error: " + longErrMessage[:maxErrMsgLen])},
},
}

for i, test := range tests {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "test error", test.code)
http.Error(w, longErrMessage, test.code)
}),
)

Expand Down

0 comments on commit 24a113b

Please sign in to comment.