Browse Source

fix: bound the log filter syntax check for nested chained comparisons

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 day ago
parent
commit
175bf148dd
2 changed files with 70 additions and 12 deletions
  1. 46 12
      service/internal/logfilter/allowlist.go
  2. 24 0
      service/internal/logfilter/filter_test.go

+ 46 - 12
service/internal/logfilter/allowlist.go

@@ -44,15 +44,14 @@ var allowedFunctions = map[string]bool{
 
 var errUnsupportedSyntax = fmt.Errorf("unsupported filter expression; use field comparisons, and/or/not, contains, includes() and hasTag()")
 
-type allowlistVisitor struct {
-	err error
-}
-
-func (v *allowlistVisitor) Visit(node *ast.Node) {
-	if v.err != nil {
-		return
-	}
-	v.err = checkNode(*node)
+var errFilterTooComplex = fmt.Errorf("filter expression is too complex")
+
+// The parser shares the middle operand of a chained comparison between two
+// branches, so nesting them makes the tree grow exponentially while the text
+// stays short. Each node is checked before its children, and the walk stops
+// after maxFilterNodes visits, so the check itself stays cheap.
+type allowlistWalker struct {
+	visited int
 }
 
 func checkAllowedSyntax(expression string) error {
@@ -61,9 +60,44 @@ func checkAllowedSyntax(expression string) error {
 		return err
 	}
 
-	visitor := &allowlistVisitor{}
-	ast.Walk(&tree.Node, visitor)
-	return visitor.err
+	walker := &allowlistWalker{}
+	return walker.check(tree.Node)
+}
+
+func (walker *allowlistWalker) check(node ast.Node) error {
+	walker.visited++
+	if walker.visited > maxFilterNodes {
+		return errFilterTooComplex
+	}
+
+	if err := checkNode(node); err != nil {
+		return err
+	}
+
+	return walker.checkAll(childNodes(node))
+}
+
+func (walker *allowlistWalker) checkAll(nodes []ast.Node) error {
+	for _, child := range nodes {
+		if err := walker.check(child); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func childNodes(node ast.Node) []ast.Node {
+	switch typed := node.(type) {
+	case *ast.UnaryNode:
+		return []ast.Node{typed.Node}
+	case *ast.BinaryNode:
+		return []ast.Node{typed.Left, typed.Right}
+	case *ast.CallNode:
+		return typed.Arguments
+	case *ast.ArrayNode:
+		return typed.Nodes
+	}
+	return nil
 }
 
 func checkNode(node ast.Node) error {

+ 24 - 0
service/internal/logfilter/filter_test.go

@@ -2,6 +2,7 @@ package logfilter
 
 import (
 	"testing"
+	"time"
 
 	"github.com/expr-lang/expr/vm"
 	"github.com/stretchr/testify/assert"
@@ -153,3 +154,26 @@ func TestCompileAllowsSupportedExpressions(t *testing.T) {
 		assert.Equal(t, expected, matched, expression)
 	}
 }
+
+func nestedChainedComparison(depth int) string {
+	term := "0"
+	for range depth {
+		term = "0<(" + term + ")<0"
+	}
+	return term
+}
+
+func TestCompileRejectsNestedChainedComparisonsQuickly(t *testing.T) {
+	expressions := []string{
+		"unknown(" + nestedChainedComparison(40) + ")",
+		nestedChainedComparison(40),
+	}
+
+	for _, expression := range expressions {
+		start := time.Now()
+		program, err := Compile(expression)
+		require.Error(t, err)
+		assert.Nil(t, program)
+		assert.Less(t, time.Since(start), time.Second)
+	}
+}