agent: limit HTTP check output to 4k

This commit is contained in:
Ryan Uber 2016-04-14 14:28:07 -07:00
parent 8c8b146f77
commit 8cc16b8238
2 changed files with 16 additions and 6 deletions

View File

@ -2,7 +2,7 @@ package agent
import (
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
@ -423,13 +423,14 @@ func (c *CheckHTTP) check() {
}
defer resp.Body.Close()
// Format the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// Read the response into a circular buffer to limit the size
output, _ := circbuf.NewBuffer(CheckBufSize)
if _, err := io.Copy(output, resp.Body); err != nil {
c.Logger.Printf("[WARN] agent: check '%v': Get error while reading body: %s", c.CheckID, err)
body = []byte{}
}
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, body)
// Format the response body
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, output.String())
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// PASSING (2xx)

View File

@ -1,6 +1,7 @@
package agent
import (
"bytes"
"errors"
"fmt"
"log"
@ -186,7 +187,10 @@ func TestCheckTTL(t *testing.T) {
func mockHTTPServer(responseCode int) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Body larger than 4k limit
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
w.WriteHeader(responseCode)
w.Write(body)
return
})
@ -219,6 +223,11 @@ func expectHTTPStatus(t *testing.T, url string, status string) {
if mock.state["foo"] != status {
t.Fatalf("should be %v %v", status, mock.state)
}
// Allow slightly more data than CheckBufSize, for the header
if n := len(mock.output["foo"]); n > (CheckBufSize + 256) {
t.Fatalf("output too long: %d (%d-byte limit)", n, CheckBufSize)
}
}
func TestCheckHTTPCritical(t *testing.T) {