Skip to content

fix(jsonparser): Fix possible JSON log line corruption caused by json parser on query path (backport release-3.5.x) #18059

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

Merged
merged 1 commit into from
Jun 11, 2025
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
19 changes: 16 additions & 3 deletions pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (j *JSONParser) parseLabelValue(key, value []byte, dataType jsonparser.Valu
sanitized := j.buildSanitizedPrefixFromBuffer()
keyString, ok := j.keys.Get(sanitized, func() (string, bool) {
if j.lbs.BaseHas(string(sanitized)) {
j.prefixBuffer[prefixLen] = append(key, duplicateSuffix...)
j.prefixBuffer[prefixLen] = make([]byte, 0, len(key)+len(duplicateSuffix))
j.prefixBuffer[prefixLen] = append(j.prefixBuffer[prefixLen], key...)
j.prefixBuffer[prefixLen] = append(j.prefixBuffer[prefixLen], duplicateSuffix...)
}

keyPrefix := j.buildSanitizedPrefixFromBuffer()
Expand All @@ -182,8 +184,9 @@ func (j *JSONParser) parseLabelValue(key, value []byte, dataType jsonparser.Valu
})

if j.captureJSONPath {
jsonPath := j.buildJSONPathFromPrefixBuffer()
j.lbs.SetJSONPath(keyString, jsonPath)
if jsonPath := j.buildJSONPathFromPrefixBuffer(); len(jsonPath) > 0 {
j.lbs.SetJSONPath(keyString, jsonPath)
}
}

// reset the prefix position
Expand Down Expand Up @@ -217,10 +220,20 @@ func (j *JSONParser) buildSanitizedPrefixFromBuffer() []byte {
return j.sanitizedPrefixBuffer
}

// buildJSONPathFromPrefixBuffer constructs the JSON path from the accumulated prefix buffer.
// It returns a slice of strings representing each segment of the JSON path.
// If the prefix buffer is empty, it returns nil.
// The function also removes any "_extracted" suffix from "duplicate" fields.
func (j *JSONParser) buildJSONPathFromPrefixBuffer() []string {
if len(j.prefixBuffer) == 0 {
return nil
}

jsonPath := make([]string, 0, len(j.prefixBuffer))
for _, part := range j.prefixBuffer {
partStr := unsafe.String(unsafe.SliceData(part), len(part)) // #nosec G103 -- we know the string is not mutated
// Trim _extracted suffix if the extracted field was a duplicate field
partStr = strings.TrimSuffix(partStr, duplicateSuffix)
jsonPath = append(jsonPath, partStr)
}

Expand Down
27 changes: 26 additions & 1 deletion pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func Test_jsonParser_Parse(t *testing.T) {
},
NoParserHints(),
},
{
"multi depth with duplicates",
[]byte(`{"app":"bar","namespace":"prod","pod":{"uuid":"bar","deployment":{"ref":"foobar"}}}`),
labels.FromStrings("app", "foo",
"pod_uuid", "foo",
),
labels.FromStrings("app", "foo",
"app_extracted", "bar",
"namespace", "prod",
"pod_uuid", "foo",
"pod_uuid_extracted", "bar",
"pod_deployment_ref", "foobar",
),
map[string][]string{
"app_extracted": {"app"},
"namespace": {"namespace"},
"pod_uuid_extracted": {"pod", "uuid"},
"pod_deployment_ref": {"pod", "deployment", "ref"},
},
NoParserHints(),
},
{"numeric",
[]byte(`{"counter":1, "price": {"_net_":5.56909}}`),
labels.EmptyLabels(),
Expand Down Expand Up @@ -194,10 +215,14 @@ func Test_jsonParser_Parse(t *testing.T) {
for _, tt := range tests {
j := NewJSONParser(true)
t.Run(tt.name, func(t *testing.T) {
origLine := string(tt.line)

b := NewBaseLabelsBuilderWithGrouping(nil, tt.hints, false, false).ForLabels(tt.lbs, tt.lbs.Hash())
b.Reset()
_, _ = j.Process(0, tt.line, b)
require.Equal(t, tt.want, b.LabelsResult().Labels())
labels := b.LabelsResult().Labels()
require.Equal(t, origLine, string(tt.line), "original log line was modified")
require.Equal(t, tt.want, labels)

// Check JSON paths if provided
if len(tt.wantJSONPath) > 0 {
Expand Down
Loading