Просмотр исходного кода

security: GHSA-m8wr-c96h-8r9f (MED) bound the cost of log filter expressions

GetLogs filters were compiled with every expr-lang feature enabled, so a
short expression using ranges and nested builtins could cost about a
million iterations per log entry.

Filters are now checked against an allowlist before compiling: field
comparisons, and/or/not, contains/startsWith/endsWith/in, includes() and
hasTag(). Builtins are disabled and the expression size is capped, so the
cost of evaluating a filter is linear in the expression and the log entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 2 дней назад
Родитель
Сommit
7c8c332bac

+ 107 - 0
service/internal/logfilter/allowlist.go

@@ -0,0 +1,107 @@
+package logfilter
+
+import (
+	"fmt"
+
+	"github.com/expr-lang/expr/ast"
+	"github.com/expr-lang/expr/parser"
+)
+
+// Filters only need field comparisons, boolean logic and the includes/hasTag
+// helpers. Anything else (ranges, builtins such as count or map, closures,
+// arithmetic) can make a short expression do a very large amount of work for
+// every log entry, so it is rejected before compiling.
+
+const maxFilterNodes = 256
+
+var allowedUnaryOperators = map[string]bool{
+	"!":   true,
+	"not": true,
+	"-":   true,
+}
+
+var allowedBinaryOperators = map[string]bool{
+	"==":         true,
+	"!=":         true,
+	"<":          true,
+	">":          true,
+	"<=":         true,
+	">=":         true,
+	"&&":         true,
+	"||":         true,
+	"and":        true,
+	"or":         true,
+	"contains":   true,
+	"startsWith": true,
+	"endsWith":   true,
+	"in":         true,
+}
+
+var allowedFunctions = map[string]bool{
+	"includes": true,
+	"hasTag":   true,
+}
+
+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)
+}
+
+func checkAllowedSyntax(expression string) error {
+	tree, err := parser.Parse(expression)
+	if err != nil {
+		return err
+	}
+
+	visitor := &allowlistVisitor{}
+	ast.Walk(&tree.Node, visitor)
+	return visitor.err
+}
+
+func checkNode(node ast.Node) error {
+	if isLiteralOrIdentifier(node) {
+		return nil
+	}
+
+	switch typed := node.(type) {
+	case *ast.UnaryNode:
+		return checkOperator(typed.Operator, allowedUnaryOperators)
+	case *ast.BinaryNode:
+		return checkOperator(typed.Operator, allowedBinaryOperators)
+	case *ast.CallNode:
+		return checkCall(typed)
+	}
+
+	return errUnsupportedSyntax
+}
+
+func isLiteralOrIdentifier(node ast.Node) bool {
+	switch node.(type) {
+	case *ast.NilNode, *ast.IdentifierNode, *ast.IntegerNode, *ast.FloatNode, *ast.BoolNode, *ast.StringNode, *ast.ArrayNode:
+		return true
+	}
+	return false
+}
+
+func checkOperator(operator string, allowed map[string]bool) error {
+	if !allowed[operator] {
+		return fmt.Errorf("unsupported operator %q in filter expression", operator)
+	}
+	return nil
+}
+
+func checkCall(call *ast.CallNode) error {
+	callee, ok := call.Callee.(*ast.IdentifierNode)
+	if !ok || !allowedFunctions[callee.Value] {
+		return errUnsupportedSyntax
+	}
+	return nil
+}

+ 6 - 0
service/internal/logfilter/filter.go

@@ -42,9 +42,15 @@ func Compile(expression string) (*vm.Program, error) {
 }
 }
 
 
 func compileNormalized(normalized string) (*vm.Program, error) {
 func compileNormalized(normalized string) (*vm.Program, error) {
+	if err := checkAllowedSyntax(normalized); err != nil {
+		return nil, err
+	}
+
 	return expr.Compile(normalized,
 	return expr.Compile(normalized,
 		expr.Env(Record{}),
 		expr.Env(Record{}),
 		expr.AsBool(),
 		expr.AsBool(),
+		expr.DisableAllBuiltins(),
+		expr.MaxNodes(maxFilterNodes),
 		expr.Function("includes", includes),
 		expr.Function("includes", includes),
 		expr.Function("hasTag", hasTag),
 		expr.Function("hasTag", hasTag),
 	)
 	)

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

@@ -104,3 +104,52 @@ func mustMatch(t *testing.T, program *vm.Program, record Record) bool {
 	require.NoError(t, err)
 	require.NoError(t, err)
 	return matched
 	return matched
 }
 }
+
+func TestCompileRejectsExpensiveExpressions(t *testing.T) {
+	expensive := []string{
+		"count(1..999, count(1..999, len(Output)+#>0) == 7) == 7",
+		"len(1..999999) > 0",
+		"(1 in 1..999999)",
+		"all(Tags, {# == \"x\"})",
+		"map(Tags, # + Output) != nil",
+		"Output matches \"(a+)+$\"",
+		"Output + Output == \"\"",
+		"let x = Output; x == \"\"",
+		"Tags[0] == \"webhook\"",
+		"upper(Output) == \"X\"",
+	}
+
+	for _, expression := range expensive {
+		program, err := Compile(expression)
+		require.Error(t, err, "expression must be rejected: %s", expression)
+		assert.Nil(t, program, "expression must not compile: %s", expression)
+	}
+}
+
+func TestCompileAllowsSupportedExpressions(t *testing.T) {
+	record := Record{Status: "Completed", Action: "Backup", User: "admin", Output: "done", Tags: []string{"webhook"}, ExitCode: 0}
+
+	supported := map[string]bool{
+		"Backup":                                     true,
+		"!Backup":                                    false,
+		"Status == \"Completed\"":                    true,
+		"Status != \"Completed\"":                    false,
+		"action contains back and user == admin":     true,
+		"(ExitCode > 0) || !Running":                 true,
+		"hasTag(Tags, \"webhook\")":                  true,
+		"includes(Output, \"done\") && !Blocked":     true,
+		"(Action startsWith \"Back\")":               true,
+		"(\"webhook\" in Tags)":                      true,
+		"(Status in [\"Completed\", \"Timed out\"])": true,
+		"ExitCode == -1":                             false,
+	}
+
+	for expression, expected := range supported {
+		program, err := Compile(expression)
+		require.NoError(t, err, "expression must compile: %s", expression)
+
+		matched, err := Matches(program, record)
+		require.NoError(t, err, expression)
+		assert.Equal(t, expected, matched, expression)
+	}
+}