Skip to content

Commit

Permalink
fix: panic when parsing and extracting JSON key values (#13790)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5ef83a7)
  • Loading branch information
na-- authored and grafana-delivery-bot[bot] committed Aug 7, 2024
1 parent 42742b6 commit c2b52a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit c2b52a3

Please sign in to comment.