Skip to content

Commit

Permalink
fix logBody to escape unprintable chars. (#2864)
Browse files Browse the repository at this point in the history
* fix logBody to escape unprintable chars.

Signed-off-by: Sepehrdad Sh <[email protected]>

* fix logbody tests

Signed-off-by: Sandor Szücs <[email protected]>

---------

Signed-off-by: Sepehrdad Sh <[email protected]>
Signed-off-by: Sandor Szücs <[email protected]>
Co-authored-by: Sandor Szücs <[email protected]>
  • Loading branch information
sepehrdaddev and szuecs authored Jan 15, 2024
1 parent f2ed1dc commit 25a2292
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions filters/diag/logbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (lb *logBody) Request(ctx filters.FilterContext) {
lb.limit,
func(chunk []byte) {
ctx.Logger().Infof(
`logBody("request") %s: %s`,
`logBody("request") %s: %q`,
req.Header.Get(flowid.HeaderName),
chunk)
},
Expand All @@ -88,7 +88,7 @@ func (lb *logBody) Response(ctx filters.FilterContext) {
lb.limit,
func(chunk []byte) {
ctx.Logger().Infof(
`logBody("response") %s: %s`,
`logBody("response") %s: %q`,
ctx.Request().Header.Get(flowid.HeaderName),
chunk)
},
Expand Down
20 changes: 11 additions & 9 deletions filters/diag/logbody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestLogBody(t *testing.T) {
logbuf := bytes.NewBuffer(nil)
log.SetOutput(logbuf)
buf := bytes.NewBufferString(content)
rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf)
rsp, err := p.Client().Post(p.URL, "text/plain", buf)
log.SetOutput(os.Stderr)
if err != nil {
t.Fatalf("Failed to POST: %v", err)
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestLogBody(t *testing.T) {
logbuf := bytes.NewBuffer(nil)
log.SetOutput(logbuf)
buf := bytes.NewBufferString(content)
rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf)
rsp, err := p.Client().Post(p.URL, "text/plain", buf)
if err != nil {
t.Fatalf("Failed to do post request: %v", err)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestLogBody(t *testing.T) {
logbuf := bytes.NewBuffer(nil)
log.SetOutput(logbuf)
buf := bytes.NewBufferString(requestContent)
rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf)
rsp, err := p.Client().Post(p.URL, "text/plain", buf)
if err != nil {
t.Fatalf("Failed to get respone: %v", err)
}
Expand Down Expand Up @@ -196,16 +196,18 @@ func TestLogBody(t *testing.T) {
logbuf := bytes.NewBuffer(nil)
log.SetOutput(logbuf)
buf := bytes.NewBufferString(content)
rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf)
rsp, err := p.Client().Post(p.URL, "text/plain", buf)
log.SetOutput(os.Stderr)
if err != nil {
t.Fatalf("Failed to POST: %v", err)
}
defer rsp.Body.Close()

want := " " + content[:limit] + `"` + "\n"
if got := logbuf.String(); want != got[len(got)-limit-3:] {
t.Fatalf("Failed want suffix: %q, got: %q\nwant hex: %x\ngot hex : %x", want, got, want, got[len(got)-limit-3:])
want := ` \"` + content[:limit] + "\\\"\"" + "\n"
got := logbuf.String()
from := len(got) - limit - 7
if want != got[from:] {
t.Fatalf("Failed want suffix: %q, got: %q\nwant hex: %x\ngot hex : %x", want, got, want, got[from:])
}
})

Expand All @@ -226,7 +228,7 @@ func TestLogBody(t *testing.T) {
logbuf := bytes.NewBuffer(nil)
log.SetOutput(logbuf)
buf := bytes.NewBufferString(content)
rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf)
rsp, err := p.Client().Post(p.URL, "text/plain", buf)
if err != nil {
t.Fatalf("Failed to do post request: %v", err)
}
Expand All @@ -242,7 +244,7 @@ func TestLogBody(t *testing.T) {
}

// repeatContent("a", 1024) but only 10 bytes
want := " " + strings.Repeat("a", 10) + `"` + "\n"
want := " \\\"" + strings.Repeat("a", 10) + "\\\"\"" + "\n"
if !strings.HasSuffix(got, want) {
t.Fatalf("Failed to find rsp content %q log, got: %q", want, got)
}
Expand Down

0 comments on commit 25a2292

Please sign in to comment.