lib/logstorage: quote logfmt strings only if they contain special chars, which could break logfmt parsing and/or reading

This commit is contained in:
Aliaksandr Valialkin 2024-10-07 14:31:16 +02:00
parent ebd393d8b3
commit 462b7cd597
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 14 additions and 2 deletions

View file

@ -45,7 +45,7 @@ func TestPipePackLogfmt(t *testing.T) {
},
}, [][]Field{
{
{"_msg", `_msg=x foo=abc bar="cde=ab"`},
{"_msg", `_msg=x foo=abc bar=cde=ab`},
{"foo", `abc`},
{"bar", `cde=ab`},
},

View file

@ -91,13 +91,25 @@ func getFieldValue(fields []Field, name string) string {
func needLogfmtQuoting(s string) bool {
for _, c := range s {
if !isTokenRune(c) {
if isLogfmtSpecialChar(c) {
return true
}
}
return false
}
func isLogfmtSpecialChar(c rune) bool {
if c <= 0x20 {
return true
}
switch c {
case '"', '\\':
return true
default:
return false
}
}
// RenameField renames field with the oldName to newName in Fields
func RenameField(fields []Field, oldName, newName string) {
if oldName == "" {