Skip to content

Commit db06a1b

Browse files
committedApr 15, 2020
fix serialization of special html chars
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
1 parent 99c45d9 commit db06a1b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
 

‎klogr/klogr.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"runtime"
1010
"sort"
11+
"strings"
1112

1213
"github.com/go-logr/logr"
1314
"k8s.io/klog/v2"
@@ -139,8 +140,11 @@ func pretty(value interface{}) string {
139140
value = err.Error()
140141
}
141142
}
142-
jb, _ := json.Marshal(value)
143-
return string(jb)
143+
buffer := &bytes.Buffer{}
144+
encoder := json.NewEncoder(buffer)
145+
encoder.SetEscapeHTML(false)
146+
encoder.Encode(value)
147+
return strings.TrimSpace(string(buffer.Bytes()))
144148
}
145149

146150
func (l klogger) Info(msg string, kvList ...interface{}) {

‎klogr/klogr_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func TestInfo(t *testing.T) {
6666
text: "test",
6767
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
6868
expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"=null
69+
`,
70+
},
71+
"should correctly html characters": {
72+
text: "test",
73+
keysAndValues: []interface{}{"akey", "<&>"},
74+
expectedOutput: ` "msg"="test" "akey"="<&>"
6975
`,
7076
},
7177
"should correctly handle odd-numbers of KVs in both log values and Info args": {

0 commit comments

Comments
 (0)
Please sign in to comment.