Skip to content

Commit

Permalink
Fix parsing of traceql queries longer than 1024 chars (#3571)
Browse files Browse the repository at this point in the history
* fix really long static vals

Signed-off-by: Joe Elliott <number101010@gmail.com>

* protect against long attr names as well

Signed-off-by: Joe Elliott <number101010@gmail.com>

---------

Signed-off-by: Joe Elliott <number101010@gmail.com>
  • Loading branch information
joe-elliott committed Apr 15, 2024
1 parent f9b37a8 commit 0dd31db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/traceql/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,24 @@ func parseQuotedAtrribute(s *scanner.Scanner) (string, error) {
}

func tryScopeAttribute(l *scanner.Scanner, currentScope int) (int, bool) {
const longestScope = 9 // "resource." is the longest scope

// copy the scanner to avoid advancing if it's not a scope.
s := *l
str := ""
for s.Peek() != scanner.EOF {
if s.Peek() == '.' {
r := s.Peek()
if r == '.' { // we've found a scope attribute
str += string(s.Next())
break
}
if !isAttributeRune(r) { // we can't have a scope with invalid characters, so just bail
break
}
if len(str) > longestScope { // we can't have a scope longer than the longest scope, so just bail
break
}

str += string(s.Next())
}
tok := tokens[str]
Expand Down
25 changes: 25 additions & 0 deletions pkg/traceql/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package traceql

import (
"fmt"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1166,3 +1167,27 @@ func TestHints(t *testing.T) {
})
}
}

func TestReallyLongQuery(t *testing.T) {
for i := 1000; i < 1050; i++ {
longVal := strings.Repeat("a", i)

// static value
query := fmt.Sprintf("{ .a = `%s` }", longVal)
expected := newBinaryOperation(OpEqual, NewAttribute("a"), NewStaticString(longVal))

actual, err := Parse(query)

require.NoError(t, err, "i=%d", i)
require.Equal(t, newRootExpr(newPipeline(newSpansetFilter(expected))), actual, "i=%d", i)

// attr name
query = fmt.Sprintf("{ .%s = `foo` }", longVal)
expected = newBinaryOperation(OpEqual, NewAttribute(longVal), NewStaticString("foo"))

actual, err = Parse(query)

require.NoError(t, err, "i=%d", i)
require.Equal(t, newRootExpr(newPipeline(newSpansetFilter(expected))), actual, "i=%d", i)
}
}

0 comments on commit 0dd31db

Please sign in to comment.