Fix bug where a nil reader could crash the redis middleware logger (#49571)

This commit is contained in:
Petri-Johan Last 2023-03-17 09:53:10 +02:00 committed by GitHub
parent 13e8742972
commit 95e411ad09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -53,7 +53,7 @@ func redisLoggerMiddleware() Middleware {
// Read body
var requestBody []byte
if req != nil && req.Body != nil {
if req != nil && req.GetBody != nil {
body, _ := req.GetBody()
if body != nil {
var readErr error

View File

@ -1,8 +1,10 @@
package httpcli
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
@ -24,6 +26,7 @@ func TestRedisLoggerMiddleware(t *testing.T) {
normalReq, _ := http.NewRequest("GET", "http://dev/null", strings.NewReader("horse"))
complexReq, _ := http.NewRequest("PATCH", "http://test.aa?a=2", strings.NewReader("graph"))
complexReq.Header.Set("Cache-Control", "no-cache")
postReqEmptyBody, _ := http.NewRequest("POST", "http://dev/null", io.NopCloser(bytes.NewBuffer([]byte{})))
testCases := []struct {
req *http.Request
@ -68,6 +71,20 @@ func TestRedisLoggerMiddleware(t *testing.T) {
}),
err: "oh no",
},
{
req: postReqEmptyBody,
name: "post request with empty body",
cli: newFakeClientWithHeaders(map[string][]string{"X-Test-Header": {"value1", "value2"}}, http.StatusOK, []byte(`{"permission":false}`), nil),
err: "<nil>",
want: &types.OutboundRequestLogItem{
Method: postReqEmptyBody.Method,
URL: postReqEmptyBody.URL.String(),
RequestHeaders: map[string][]string{},
RequestBody: "",
StatusCode: http.StatusOK,
ResponseHeaders: map[string][]string{"Content-Type": {"text/plain; charset=utf-8"}, "X-Test-Header": {"value1", "value2"}},
},
},
}
// Enable feature