Skip to content

Commit

Permalink
Parser: Allow literal control chars in logfmt decoder (#3932)
Browse files Browse the repository at this point in the history
* Allow literal control chars in decoder

* Ensure decoding control chars in logfmt works
  • Loading branch information
Timbus authored Jul 19, 2021
1 parent 58f9d0c commit 267107b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/logql/log/logfmt/jsonstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func unquoteBytes(s []byte) (t []byte, ok bool) {
r := 0
for r < len(s) {
c := s[r]
if c == '\\' || c == '"' || c < ' ' {
if c == '\\' || c == '"' {
break
}
if c < utf8.RuneSelf {
Expand Down Expand Up @@ -118,8 +118,8 @@ func unquoteBytes(s []byte) (t []byte, ok bool) {
w += utf8.EncodeRune(b[w:], rr)
}

// Quote, control characters are invalid.
case c == '"', c < ' ':
// Unescaped quote is invalid.
case c == '"':
return

// ASCII
Expand Down
44 changes: 44 additions & 0 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,50 @@ func Test_logfmtParser_Parse(t *testing.T) {
{Name: "foobar", Value: "foo bar"},
},
},
{
"escaped control chars in logfmt",
[]byte(`foobar="foo\nbar\tbaz"`),
labels.Labels{
{Name: "a", Value: "b"},
},
labels.Labels{
{Name: "a", Value: "b"},
{Name: "foobar", Value: "foo\nbar\tbaz"},
},
},
{
"literal control chars in logfmt",
[]byte("foobar=\"foo\nbar\tbaz\""),
labels.Labels{
{Name: "a", Value: "b"},
},
labels.Labels{
{Name: "a", Value: "b"},
{Name: "foobar", Value: "foo\nbar\tbaz"},
},
},
{
"escaped slash logfmt",
[]byte(`foobar="foo ba\\r baz"`),
labels.Labels{
{Name: "a", Value: "b"},
},
labels.Labels{
{Name: "a", Value: "b"},
{Name: "foobar", Value: `foo ba\r baz`},
},
},
{
"literal newline and escaped slash logfmt",
[]byte("foobar=\"foo bar\nb\\\\az\""),
labels.Labels{
{Name: "a", Value: "b"},
},
labels.Labels{
{Name: "a", Value: "b"},
{Name: "foobar", Value: "foo bar\nb\\az"},
},
},
{
"double property logfmt",
[]byte(`foobar="foo bar" latency=10ms`),
Expand Down

0 comments on commit 267107b

Please sign in to comment.