Skip to content

Commit 5ef83a7

Browse files
authored
fix: panic when parsing and extracting JSON key values (#13790)
1 parent bb257f5 commit 5ef83a7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

‎pkg/logql/log/parser_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ func Test_jsonParser_Parse(t *testing.T) {
3939
),
4040
NoParserHints(),
4141
},
42+
{
43+
"whitespace key value",
44+
[]byte(`{" ": {"foo":"bar"}}`),
45+
labels.EmptyLabels(),
46+
labels.FromStrings("foo", "bar"),
47+
NoParserHints(),
48+
},
49+
{
50+
"whitespace key and whitespace subkey values",
51+
[]byte(`{" ": {" ":"bar"}}`),
52+
labels.EmptyLabels(),
53+
labels.FromStrings("", "bar"),
54+
NoParserHints(),
55+
},
56+
{
57+
"whitespace key and empty subkey values",
58+
[]byte(`{" ": {"":"bar"}}`),
59+
labels.EmptyLabels(),
60+
labels.FromStrings("", "bar"),
61+
NoParserHints(),
62+
},
63+
{
64+
"empty key and empty subkey values",
65+
[]byte(`{"": {"":"bar"}}`),
66+
labels.EmptyLabels(),
67+
labels.FromStrings("", "bar"),
68+
NoParserHints(),
69+
},
4270
{
4371
"escaped",
4472
[]byte(`{"counter":1,"foo":"foo\\\"bar", "price": {"_net_":5.56909}}`),

‎pkg/logql/log/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ func sanitizeLabelKey(key string, isPrefix bool) string {
4040

4141
// appendSanitize appends the sanitized key to the slice.
4242
func appendSanitized(to, key []byte) []byte {
43+
key = bytes.TrimSpace(key)
44+
4345
if len(key) == 0 {
4446
return to
4547
}
46-
key = bytes.TrimSpace(key)
4748

4849
if len(to) == 0 && key[0] >= '0' && key[0] <= '9' {
4950
to = append(to, '_')

0 commit comments

Comments
 (0)