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.
This commit is contained in:
Dominik Schmidt committed 2026-11-27 15:28:14 +01:00
1 parent 5f49663e4b
commit 35a2cdbc82
2 files changed
+21 -7

No files matched your search

+8 -4
View File
@@ -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:]...)
+13 -3
View File
@@ -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)