Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: Allow literal control chars in logfmt decoder #3932

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -574,6 +574,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