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: sanatize structured metadata at query time #13983

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
account for noop pipeline
  • Loading branch information
MasslessParticle committed Aug 27, 2024
commit de98c7552ca9657dfddea9bf219df65a75a36db3
20 changes: 12 additions & 8 deletions pkg/logql/log/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (n *noopPipeline) ForStream(labels labels.Labels) StreamPipeline {
}
n.mu.RUnlock()

sp := &noopStreamPipeline{n.baseBuilder.ForLabels(labels, h)}
sp := &noopStreamPipeline{n.baseBuilder.ForLabels(labels, h), make([]int, 0, 10)}

n.mu.Lock()
defer n.mu.Unlock()
Expand All @@ -92,7 +92,8 @@ func IsNoopPipeline(p Pipeline) bool {
}

type noopStreamPipeline struct {
builder *LabelsBuilder
builder *LabelsBuilder
offsetsBuf []int
}

func (n noopStreamPipeline) ReferencedStructuredMetadata() bool {
Expand All @@ -101,6 +102,9 @@ func (n noopStreamPipeline) ReferencedStructuredMetadata() bool {

func (n noopStreamPipeline) Process(_ int64, line []byte, structuredMetadata ...labels.Label) ([]byte, LabelsResult, bool) {
n.builder.Reset()
for i, lb := range structuredMetadata {
structuredMetadata[i].Name = replaceChars(lb.Name, n.offsetsBuf)
}
n.builder.Add(StructuredMetadataLabel, structuredMetadata...)
return line, n.builder.LabelsResult(), true
}
Expand Down Expand Up @@ -223,7 +227,7 @@ func (p *streamPipeline) Process(ts int64, line []byte, structuredMetadata ...la
p.builder.Reset()

for i, lb := range structuredMetadata {
structuredMetadata[i].Name = p.replaceChars(lb.Name)
structuredMetadata[i].Name = replaceChars(lb.Name, p.offsetsBuf)
}

p.builder.Add(StructuredMetadataLabel, structuredMetadata...)
Expand Down Expand Up @@ -388,17 +392,17 @@ func unsafeGetString(buf []byte) string {
return *((*string)(unsafe.Pointer(&buf)))
}

func (p *streamPipeline) replaceChars(str string) string {
p.offsetsBuf = p.offsetsBuf[:0]
func replaceChars(str string, offsets []int) string {
offsets = offsets[:0]
for i, r := range str {
Copy link
Contributor

@ashwanthgoli ashwanthgoli Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at prometheus NormalizeLabel, we also need to handle labels starting with a number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great find. I didn't realize NormalizeLabel was a thing!

if !isDigit(r) && !isAlpha(r) {
p.offsetsBuf = append(p.offsetsBuf, i)
offsets = append(offsets, i)
}
}

if len(p.offsetsBuf) > 0 {
if len(offsets) > 0 {
runes := []rune(str)
for _, offset := range p.offsetsBuf {
for _, offset := range offsets {
runes[offset] = '_'
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/logql/log/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
require.Equal(t, true, matches)

// test structured metadata with disallowed label names
structuredMetadata = append(labels.FromStrings("y", "1", "z", "2"), labels.Label{Name: "something-bad", Value: "foo"})
expectedStructuredMetadata := append(labels.FromStrings("y", "1", "z", "2"), labels.Label{Name: "something_bad", Value: "foo"})
structuredMetadata = append(labels.FromStrings("y", "1", "z", "2"), labels.Label{Name: "zsomething-bad", Value: "foo"})
expectedStructuredMetadata := append(labels.FromStrings("y", "1", "z", "2"), labels.Label{Name: "zsomething_bad", Value: "foo"})
expectedLabelsResults = append(lbs, expectedStructuredMetadata...)

l, lbr, matches = pipeline.ForStream(lbs).Process(0, []byte(""), structuredMetadata...)
Expand Down Expand Up @@ -188,7 +188,7 @@
expectedStructuredMetadata := append(structuredMetadata, labels.Label{Name: "zsomething_bad", Value: "foo"})
expectedLabelsResults = append(lbs, expectedStructuredMetadata...)

l, lbr, matches = p.ForStream(lbs).Process(0, []byte(""), withBadLabel...)

Check failure on line 191 in pkg/logql/log/pipeline_test.go

View workflow job for this annotation

GitHub Actions / check / golangciLint

ineffectual assignment to l (ineffassign)
require.Equal(t, NewLabelsResult(expectedLabelsResults.String(), expectedLabelsResults.Hash(), lbs, expectedStructuredMetadata, labels.EmptyLabels()), lbr)
require.Equal(t, expectedLabelsResults.Hash(), lbr.Hash())
require.Equal(t, expectedLabelsResults.String(), lbr.String())
Expand Down
Loading