-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
254 lines (207 loc) · 5.72 KB
/
parser.go
File metadata and controls
254 lines (207 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package where
import (
"fmt"
"strings"
"github.com/alecthomas/participle/v2"
"github.com/pkg/errors"
)
type (
// Parser represents a configured filter expression parser with validation options.
Parser struct {
parser *participle.Parser[Filter]
opts *parserOptions
}
// parserOptions holds configuration options for the parser.
parserOptions struct {
maxDepth int
maxINItems int
allowedFuncs map[string]bool
}
// ParserOption is a function type for configuring parser options.
ParserOption func(*parserOptions)
)
// WithMaxDepth returns a ParserOption that sets the maximum nesting depth for expressions.
func WithMaxDepth(depth int) ParserOption {
return func(o *parserOptions) {
o.maxDepth = depth
}
}
// WithMaxINItems returns a ParserOption that sets the maximum number of items allowed in IN expressions.
func WithMaxINItems(max int) ParserOption {
return func(o *parserOptions) {
o.maxINItems = max
}
}
// WithFunctions returns a ParserOption that restricts which functions are allowed in expressions.
// This provides parse-time validation - note that all functions are supported at the driver level.
// Use the Validator for runtime validation instead for more comprehensive security.
func WithFunctions(names ...string) ParserOption {
return func(o *parserOptions) {
if o.allowedFuncs == nil {
o.allowedFuncs = make(map[string]bool)
}
for _, name := range names {
o.allowedFuncs[strings.ToUpper(name)] = true
}
}
}
// NewParser creates a new parser with the specified options.
func NewParser(opts ...ParserOption) (*Parser, error) {
options := &parserOptions{
maxDepth: 10,
maxINItems: 1000,
}
for _, opt := range opts {
opt(options)
}
lex, err := NewLexer()
if err != nil {
return nil, fmt.Errorf("failed to create lexer: %w", err)
}
parser, err := participle.Build[Filter](
participle.Lexer(lex),
participle.Elide("Whitespace"),
participle.UseLookahead(5),
)
if err != nil {
return nil, fmt.Errorf("failed to build parser: %w", err)
}
return &Parser{
parser: parser,
opts: options,
}, nil
}
// Parse parses a filter expression string and returns the parsed Filter AST.
// The input is validated according to the parser's configured options.
func (p *Parser) Parse(input string) (*Filter, error) {
if input == "" {
return nil, errors.New("empty filter expression")
}
filter, err := p.parser.ParseString("", input)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse filter expression")
}
if err := p.validate(filter); err != nil {
return nil, errors.Wrapf(err, "filter validation failed")
}
return filter, nil
}
func (p *Parser) validate(filter *Filter) error {
return p.validateExpression(filter.Expression, 0)
}
func (p *Parser) validateExpression(expr *Expression, depth int) error {
if depth > p.opts.maxDepth {
return fmt.Errorf("expression depth exceeds maximum of %d", p.opts.maxDepth)
}
if expr == nil || len(expr.Or) == 0 {
return nil
}
for _, term := range expr.Or {
if err := p.validateTerm(term, depth); err != nil {
return err
}
}
return nil
}
func (p *Parser) validateTerm(term *Term, depth int) error {
if term == nil || len(term.And) == 0 {
return nil
}
for _, factor := range term.And {
if err := p.validateFactor(factor, depth); err != nil {
return err
}
}
return nil
}
func (p *Parser) validateFactor(factor *Factor, depth int) error {
if factor == nil {
return errors.New("empty factor")
}
if factor.SubExpr != nil {
return p.validateExpression(factor.SubExpr, depth+1)
}
if factor.Predicate != nil {
return p.validatePredicate(factor.Predicate)
}
return errors.New("empty factor content")
}
func (p *Parser) validatePredicate(pred *Predicate) error {
if pred == nil {
return errors.New("empty predicate")
}
// Validate the left side (field/function/literal)
if err := p.validateValue(pred.Left); err != nil {
return err
}
if pred.Operation == nil {
return errors.New("predicate missing operation")
}
return p.validateOperation(pred.Operation)
}
func (p *Parser) validateOperation(op *Operation) error {
if op == nil {
return errors.New("empty operation")
}
if op.Compare != nil {
return p.validateValue(op.Compare.Right)
}
if op.Like != nil {
return p.validateValue(op.Like.Pattern)
}
if op.Between != nil {
if err := p.validateValue(op.Between.Lower); err != nil {
return err
}
return p.validateValue(op.Between.Upper)
}
if op.In != nil {
if len(op.In.Values) == 0 {
return errors.New("IN expression requires at least one value")
}
if len(op.In.Values) > p.opts.maxINItems {
return fmt.Errorf("IN expression exceeds maximum of %d items", p.opts.maxINItems)
}
for _, value := range op.In.Values {
if err := p.validateValue(value); err != nil {
return err
}
}
return nil
}
if op.IsNull != nil {
// IS NULL doesn't require additional validation
return nil
}
return errors.New("operation has no valid type")
}
func (p *Parser) validateValue(val *Value) error {
if val == nil {
return nil
}
if val.Function != nil {
if p.opts.allowedFuncs != nil {
if !p.opts.allowedFuncs[strings.ToUpper(val.Function.Name)] {
return fmt.Errorf("function %q is not allowed", val.Function.Name)
}
}
for _, arg := range val.Function.Args {
if err := p.validateValue(arg); err != nil {
return err
}
}
}
if val.SubExpr != nil {
return p.validateExpression(val.SubExpr, 0)
}
return nil
}
// Parse is a convenience function that creates a default parser and parses the input.
// For more control over parsing options, create a parser with NewParser.
func Parse(input string) (*Filter, error) {
parser, err := NewParser()
if err != nil {
return nil, errors.Wrap(err, "failed to create parser")
}
return parser.Parse(input)
}