From 35a2cdbc82b08e5ceaf1f92939e3a47f6bb457e3 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 27 Nov 2026 15:24:08 +0100 Subject: [PATCH] fix(search): reject a scope that is negated or joined with OR A scope narrows the whole search to one folder. NOT scope: searched inside the folder instead of outside it, and OR scope: was treated as AND. Both are a 400 now. --- services/search/pkg/search/scope.go | 12 ++++++++---- services/search/pkg/search/scope_test.go | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/services/search/pkg/search/scope.go b/services/search/pkg/search/scope.go index 643a02f1d6..c4aa7b4bec 100644 --- a/services/search/pkg/search/scope.go +++ b/services/search/pkg/search/scope.go @@ -13,6 +13,7 @@ import ( var ( ErrNestedScope = errors.New("scope: must be a top-level restriction") ErrRepeatedScope = errors.New("scope: must be given once") + ErrScopeOperator = errors.New("scope: cannot be negated or joined with OR") ) // ExtractScope takes the scope restriction out of the query, together with @@ -41,11 +42,14 @@ func ExtractScope(nodes []ast.Node) ([]ast.Node, string, error) { continue } - rest := append(append([]ast.Node{}, nodes[:i]...), nodes[i+1:]...) - if i > 0 && isOperator(rest[i-1], kql.BoolNOT) { - rest = append(rest[:i-1], rest[i:]...) - i-- + if i > 0 && (isOperator(nodes[i-1], kql.BoolNOT) || isOperator(nodes[i-1], kql.BoolOR)) { + return nil, "", ErrScopeOperator } + if i+1 < len(nodes) && isOperator(nodes[i+1], kql.BoolOR) { + return nil, "", ErrScopeOperator + } + + rest := append(append([]ast.Node{}, nodes[:i]...), nodes[i+1:]...) switch { case i < len(rest) && isBooleanOperator(rest[i]): rest = append(rest[:i], rest[i+1:]...) diff --git a/services/search/pkg/search/scope_test.go b/services/search/pkg/search/scope_test.go index eac8bb36f0..686ba6923a 100644 --- a/services/search/pkg/search/scope_test.go +++ b/services/search/pkg/search/scope_test.go @@ -24,9 +24,6 @@ var _ = DescribeTable("ExtractScope", Entry("joined implicitly", `name:x scope:1$2!3`, `name:x`, `1$2!3`), Entry("joined implicitly at the start", `scope:1$2!3 name:x`, `name:x`, `1$2!3`), Entry("between free text", `foo scope:1$2!3 bar`, `foo bar`, `1$2!3`), - Entry("joined with OR", `name:x OR scope:1$2!3`, `name:x`, `1$2!3`), - Entry("negated", `name:x AND NOT scope:1$2!3`, `name:x`, `1$2!3`), - Entry("negated at the start", `NOT scope:1$2!3 AND name:x`, `name:x`, `1$2!3`), Entry("alone", `scope:1$2!3`, ``, `1$2!3`), Entry("no scope", `name:x AND tag:y`, `name:x AND tag:y`, ``), Entry("a quoted value is not a scope", `name:"scope:1$2!3"`, `name:"scope:1$2!3"`, ``), @@ -44,6 +41,19 @@ var _ = DescribeTable("ExtractScope rejects a nested scope", Entry("as a group value inside a group", `name:x AND (tag:y OR scope:(1$2!3))`), ) +var _ = DescribeTable("ExtractScope rejects a scope that is negated or joined with OR", + func(query string) { + _, _, err := search.ExtractScope(parse(query).Nodes) + Expect(err).To(MatchError(search.ErrScopeOperator)) + }, + Entry("negated", `name:x AND NOT scope:1$2!3`), + Entry("negated at the start", `NOT scope:1$2!3 AND name:x`), + Entry("negated alone", `NOT scope:1$2!3`), + Entry("joined with OR", `name:x OR scope:1$2!3`), + Entry("joined with OR at the start", `scope:1$2!3 OR name:x`), + Entry("joined with OR in the middle", `name:x AND scope:1$2!3 OR tag:y`), +) + var _ = Describe("ExtractScope", func() { It("rejects a second scope", func() { _, _, err := search.ExtractScope(parse(`scope:1$2!3 AND name:x AND scope:1$2!4`).Nodes)