Skip to content

Commit

Permalink
fix(logql): Fix panic in json parsing when using invalid array index
Browse files Browse the repository at this point in the history
This PR fixes a panic that occurs with the following json parser
expression:

```
| json keys[""]
```

when the log line is the following:

```json
{"keys": ["a", "b", "c"]}
```

Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
  • Loading branch information
chaudum committed Oct 4, 2024
1 parent 8438d41 commit a116b50
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ require (
github.com/efficientgo/core v1.0.0-rc.3
github.com/fsnotify/fsnotify v1.7.0
github.com/gogo/googleapis v1.4.1
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32
github.com/grafana/jsonparser v0.0.0-20241004153430-023329977675
github.com/grafana/loki/pkg/push v0.0.0-20240924133635-758364c7775f
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hashicorp/raft v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,8 @@ github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85 h1:xLuzPoOzdfNb/RF/I
github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85/go.mod h1:crI9WX6p0IhrqB+DqIUHulRW853PaNFf7o4UprV//3I=
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 h1:X8IKQ0wu40wpvYcKfBcc5T4QnhdQjUhtUtB/1CY89lE=
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56/go.mod h1:PGk3RjYHpxMM8HFPhKKo+vve3DdlPUELZLSDEFehPuU=
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32 h1:NznuPwItog+rwdVg8hAuGKP29ndRSzJAwhxKldkP8oQ=
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32/go.mod h1:796sq+UcONnSlzA3RtlBZ+b/hrerkZXiEmO8oMjyRwY=
github.com/grafana/jsonparser v0.0.0-20241004153430-023329977675 h1:U94jQ2TQr1m3HNyE8efSdyaBbDrdPaWImXyenuKZ/nw=
github.com/grafana/jsonparser v0.0.0-20241004153430-023329977675/go.mod h1:796sq+UcONnSlzA3RtlBZ+b/hrerkZXiEmO8oMjyRwY=
github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe h1:yIXAAbLswn7VNWBIvM71O2QsgfgW9fRXZNR0DXe6pDU=
github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=
Expand Down
20 changes: 20 additions & 0 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ func TestJSONExpressionParser(t *testing.T) {
labels.FromStrings("param", "1"),
NoParserHints(),
},
{
"object element not present",
testLine,
[]LabelExtractionExpr{
NewLabelExtractionExpr("undefined", `pod[""]`),
},
labels.EmptyLabels(),
labels.FromStrings("undefined", ""),
NoParserHints(),
},
{
"accessing invalid array index",
testLine,
[]LabelExtractionExpr{
NewLabelExtractionExpr("param", `pod.deployment.params[""]`),
},
labels.EmptyLabels(),
labels.FromStrings("param", ""),
NoParserHints(),
},
{
"array string element",
testLine,
Expand Down
8 changes: 2 additions & 6 deletions vendor/github.com/grafana/jsonparser/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ github.com/grafana/go-gelf/v2/gelf
# github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56
## explicit; go 1.18
github.com/grafana/gomemcache/memcache
# github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32
# github.com/grafana/jsonparser v0.0.0-20241004153430-023329977675
## explicit; go 1.13
github.com/grafana/jsonparser
# github.com/grafana/loki/pkg/push v0.0.0-20240924133635-758364c7775f => ./pkg/push
Expand Down

0 comments on commit a116b50

Please sign in to comment.