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

fix: panic when parsing and extracting JSON key values #13790

Merged
merged 1 commit into from
Aug 7, 2024
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
28 changes: 28 additions & 0 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ func Test_jsonParser_Parse(t *testing.T) {
),
NoParserHints(),
},
{
"whitespace key value",
[]byte(`{" ": {"foo":"bar"}}`),
labels.EmptyLabels(),
labels.FromStrings("foo", "bar"),
NoParserHints(),
},
{
"whitespace key and whitespace subkey values",
[]byte(`{" ": {" ":"bar"}}`),
labels.EmptyLabels(),
labels.FromStrings("", "bar"),
NoParserHints(),
},
{
"whitespace key and empty subkey values",
[]byte(`{" ": {"":"bar"}}`),
labels.EmptyLabels(),
labels.FromStrings("", "bar"),
NoParserHints(),
},
{
"empty key and empty subkey values",
[]byte(`{"": {"":"bar"}}`),
labels.EmptyLabels(),
labels.FromStrings("", "bar"),
NoParserHints(),
},
{
"escaped",
[]byte(`{"counter":1,"foo":"foo\\\"bar", "price": {"_net_":5.56909}}`),
Expand Down
3 changes: 2 additions & 1 deletion pkg/logql/log/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ func sanitizeLabelKey(key string, isPrefix bool) string {

// appendSanitize appends the sanitized key to the slice.
func appendSanitized(to, key []byte) []byte {
key = bytes.TrimSpace(key)

if len(key) == 0 {
return to
}
key = bytes.TrimSpace(key)

if len(to) == 0 && key[0] >= '0' && key[0] <= '9' {
to = append(to, '_')
Expand Down
Loading