From db06a1b643e5fcc955f2cf713884a4e5e7f0902b Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Wed, 15 Apr 2020 09:11:57 -0400 Subject: [PATCH] fix serialization of special html chars Signed-off-by: Davanum Srinivas --- klogr/klogr.go | 8 ++++++-- klogr/klogr_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/klogr/klogr.go b/klogr/klogr.go index 944cb70a..9791cd87 100644 --- a/klogr/klogr.go +++ b/klogr/klogr.go @@ -8,6 +8,7 @@ import ( "fmt" "runtime" "sort" + "strings" "github.com/go-logr/logr" "k8s.io/klog/v2" @@ -139,8 +140,11 @@ func pretty(value interface{}) string { value = err.Error() } } - jb, _ := json.Marshal(value) - return string(jb) + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.Encode(value) + return strings.TrimSpace(string(buffer.Bytes())) } func (l klogger) Info(msg string, kvList ...interface{}) { diff --git a/klogr/klogr_test.go b/klogr/klogr_test.go index 795eefd1..712ac9c5 100644 --- a/klogr/klogr_test.go +++ b/klogr/klogr_test.go @@ -66,6 +66,12 @@ func TestInfo(t *testing.T) { text: "test", keysAndValues: []interface{}{"akey", "avalue", "akey2"}, expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"=null +`, + }, + "should correctly html characters": { + text: "test", + keysAndValues: []interface{}{"akey", "<&>"}, + expectedOutput: ` "msg"="test" "akey"="<&>" `, }, "should correctly handle odd-numbers of KVs in both log values and Info args": {