From ea10c7fb153f522ac163b14e47fbdc2cef966e1c Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 9 May 2025 11:00:24 +0200 Subject: [PATCH 1/2] Fix: Replace non-ASCII and non-printable characters with dots in log output --- logger/httplog/body_logger.go | 10 +++++++++- logger/httplog/body_logger_test.go | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/logger/httplog/body_logger.go b/logger/httplog/body_logger.go index 1a59e5c5b..95f897e2f 100644 --- a/logger/httplog/body_logger.go +++ b/logger/httplog/body_logger.go @@ -6,6 +6,7 @@ import ( "fmt" "sort" "strings" + "unicode" ) type bodyLogger struct { @@ -122,7 +123,14 @@ func (b bodyLogger) redactedDump(prefix string, body []byte) string { sb.WriteString("\n") } line = strings.Trim(line, "\r") - sb.WriteString(fmt.Sprintf("%s%s", prefix, line)) + sb.WriteString(prefix) + for _, c := range line { + // Convert non-ASCII and non-printable characters to '.' + if c > unicode.MaxASCII || !unicode.IsPrint(c) { + c = '.' + } + sb.WriteRune(c) + } } return sb.String() } diff --git a/logger/httplog/body_logger_test.go b/logger/httplog/body_logger_test.go index 0eafac055..0e12510ae 100644 --- a/logger/httplog/body_logger_test.go +++ b/logger/httplog/body_logger_test.go @@ -69,6 +69,11 @@ func TestBodyLoggerNotJson(t *testing.T) { assert.Equal(t, dump, "") } +func TestBodyLoggerNotAscii(t *testing.T) { + dump := bodyLogger{debugTruncateBytes: 20}.redactedDump("", []byte("\x01 🚀 🚀")) + assert.Equal(t, dump, ". . .") +} + func TestBodyLoggerNonJSONNewline(t *testing.T) { dump := bodyLogger{debugTruncateBytes: 100}.redactedDump("< ", []byte(` From e23bee8d9901e058c3a30d6edd8012f1de4c158a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 9 May 2025 11:05:31 +0200 Subject: [PATCH 2/2] Fix: Preserve tab characters in log output --- logger/httplog/body_logger.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logger/httplog/body_logger.go b/logger/httplog/body_logger.go index 95f897e2f..653ea3b11 100644 --- a/logger/httplog/body_logger.go +++ b/logger/httplog/body_logger.go @@ -126,7 +126,8 @@ func (b bodyLogger) redactedDump(prefix string, body []byte) string { sb.WriteString(prefix) for _, c := range line { // Convert non-ASCII and non-printable characters to '.' - if c > unicode.MaxASCII || !unicode.IsPrint(c) { + // But preserve tabs and spaces for indentation + if c > unicode.MaxASCII || (!unicode.IsPrint(c) && c != '\t') { c = '.' } sb.WriteRune(c)