From 25a2292e7975506c33362ca1e24433498931b0e8 Mon Sep 17 00:00:00 2001 From: Sepehrdad <26747519+sepehrdaddev@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:05:33 +0100 Subject: [PATCH] fix logBody to escape unprintable chars. (#2864) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix logBody to escape unprintable chars. Signed-off-by: Sepehrdad Sh <26747519+sepehrdaddev@users.noreply.github.com> * fix logbody tests Signed-off-by: Sandor Szücs --------- Signed-off-by: Sepehrdad Sh <26747519+sepehrdaddev@users.noreply.github.com> Signed-off-by: Sandor Szücs Co-authored-by: Sandor Szücs --- filters/diag/logbody.go | 4 ++-- filters/diag/logbody_test.go | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/filters/diag/logbody.go b/filters/diag/logbody.go index d057fc1636..28607730ff 100644 --- a/filters/diag/logbody.go +++ b/filters/diag/logbody.go @@ -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) }, @@ -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) }, diff --git a/filters/diag/logbody_test.go b/filters/diag/logbody_test.go index c921becd7f..6fbd1cf6c5 100644 --- a/filters/diag/logbody_test.go +++ b/filters/diag/logbody_test.go @@ -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) @@ -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) } @@ -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) } @@ -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:]) } }) @@ -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) } @@ -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) }