-
Notifications
You must be signed in to change notification settings - Fork 50
/
parse.go
984 lines (922 loc) · 24.9 KB
/
parse.go
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
package yarx
import (
"bytes"
"fmt"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common"
"github.com/google/cel-go/common/operators"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/parser"
"github.com/kataras/golog"
"github.com/thoas/go-funk"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
)
type YamlPoc struct {
Name string `yaml:"name"`
ID string `yaml:"id"`
Tags []string `yaml:"tags"`
ApplyTo string `yaml:"apply_to"`
Transport string `yaml:"transport"`
Set yaml.Node `yaml:"set"`
Rules map[string]*YamlRule `yaml:"rules"`
Pattern string `yaml:"expression"`
}
type YamlRule struct {
Name string `yaml:"-"`
Request struct {
Method string `yaml:"method"`
Path string `yaml:"path"`
Headers map[string]string `yaml:"headers"`
Body string `yaml:"body"`
FollowRedirects *bool `yaml:"follow_redirects"`
} `yaml:"request"`
Expression string `yaml:"expression"`
Output yaml.Node `yaml:"output"`
}
type MutationRule struct {
Name string // eg: poc-yaml-yapi-rce
Method string
ReplacedURI string
URI *regexp.Regexp
Body *regexp.Regexp
Header map[string]*regexp.Regexp
Status int
MutateFuncs []func(resp http.ResponseWriter, ctx *celContext) error
ExprInfo *expr.SourceInfo
YamlRule *YamlRule
Chain *MutationChain
cel *celContext
level int
}
func NewMutationRule(celCtx *celContext) *MutationRule {
return &MutationRule{
cel: celCtx,
}
}
func (m *MutationRule) String() string {
return m.Chain.Name + "-" + m.Name
}
func (m *MutationRule) Match(req *http.Request, celCtx *celContext) error {
// extract vars from path
sortedURL := SortedURI(req.URL)
for k, v := range m.reSubMatchMap(m.URI, sortedURL, celCtx) {
celCtx.eval[k] = v
}
// extract vars from header
if len(m.Header) != 0 {
for k, re := range m.Header {
value := req.Header.Get(k)
for k, v := range m.reSubMatchMap(re, value, celCtx) {
celCtx.eval[k] = v
}
}
}
// extract vars from body
if req.Method != http.MethodGet && m.Body != nil {
body, err := readClose(req.Body)
if err != nil {
return err
}
for k, v := range m.reSubMatchMap(m.Body, string(body), celCtx) {
celCtx.eval[k] = v
}
}
return nil
}
func (m *MutationRule) reSubMatchMap(r *regexp.Regexp, str string, celCtx *celContext) map[string]interface{} {
match := r.FindStringSubmatch(str)
subMatchMap := make(map[string]string)
for i, name := range r.SubexpNames() {
if i != 0 {
if len(match) <= i {
fmt.Println(match)
fmt.Println(r.SubexpNames())
panic("bug found")
}
subMatchMap[name] = match[i]
}
}
// adjust type according to cel context
result := make(map[string]interface{})
for k, v := range subMatchMap {
valueType, ok := celCtx.vafDefines[k]
if !ok {
result[k] = v
continue
}
switch valueType.GetPrimitive() {
case expr.Type_PRIMITIVE_TYPE_UNSPECIFIED, expr.Type_STRING:
result[k] = v
case expr.Type_BYTES:
result[k] = []byte(v)
case expr.Type_INT64, expr.Type_UINT64:
num, err := strconv.Atoi(v)
if err != nil {
result[k] = v
} else {
result[k] = num
}
default:
result[k] = v
}
}
return result
}
func (m *MutationRule) HTTPHandler() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
err := m.Match(request, m.cel)
if err != nil {
handleError(err, writer)
return
}
for _, fn := range m.MutateFuncs {
err = fn(writer, m.cel)
if err != nil {
golog.Error(err)
}
}
if m.Status != 0 {
writer.WriteHeader(m.Status)
}
}
}
type celContext struct {
option []cel.EnvOption
eval map[string]interface{}
vafDefines map[string]*expr.Type
}
func newCelContext() *celContext {
return &celContext{
option: nil,
eval: make(map[string]interface{}),
vafDefines: make(map[string]*expr.Type),
}
}
type MutationChain struct {
sync.Mutex
Name string
pattern []string
rules []*MutationRule
currentLevel int32
lock chan struct{}
}
func (g *MutationChain) IsLast(rule *MutationRule) bool {
return rule.level == len(g.rules)-1
}
func (g *MutationChain) IsFirst(rule *MutationRule) bool {
return rule.level == 0
}
//func (g *MutationChain) IsNextRun(uri string, rule *MutationRule) bool {
// subData := rule.reSubMatchMap(rule.URI, uri, rule.cel)
// for k, v := range subData {
// existValue := rule.cel.eval[k]
// if existValue != nil && existValue != v {
// return false
// }
// }
// return true
//}
type Yarx struct {
mu sync.RWMutex
chains []*MutationChain
}
func (y *Yarx) ParseFile(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return y.Parse(data)
}
func (y *Yarx) Parse(pocData []byte) error {
var poc YamlPoc
if bytes.Contains(pocData, []byte("newReverse")) {
return ErrReverseNotSupported
}
err := yaml.Unmarshal(pocData, &poc)
if err != nil {
return err
}
chain := &MutationChain{
currentLevel: -1,
Name: poc.Name,
lock: make(chan struct{}, 1),
}
// parse global vars
env := NewCELEnv()
varDefines := make(map[string]*expr.Type)
for i := range poc.Set.Content {
if i%2 != 0 {
continue
}
key := poc.Set.Content[i].Value
value := poc.Set.Content[i+1].Value
ast, issues := env.Compile(value)
if issues != nil && issues.Err() != nil {
if strings.Contains(issues.Err().Error(), "undeclared reference to 'request'") {
return ErrRequestNotSupported
} else {
return issues.Err()
}
}
varDefines[key] = ast.ResultType()
}
evalMap := make(map[string]interface{})
executePath, err := y.getShortestPath(&poc)
golog.Debugf("shortest pattern: %v", executePath)
chain.pattern = executePath
for level, ruleName := range executePath {
rule, ok := poc.Rules[ruleName]
if !ok {
return fmt.Errorf("incomplete poc: %s not found", ruleName)
}
if err := y.canMock(rule); err != nil {
return err
}
rule.Name = ruleName
mutateRule := NewMutationRule(&celContext{
vafDefines: varDefines,
eval: evalMap,
})
mutateRule.YamlRule = rule
mutateRule.Chain = chain
mutateRule.level = level
mutateRule.Name = ruleName
mutateRule.cel.vafDefines = varDefines
if err := y.genRequestMatch(rule, mutateRule); err != nil {
return fmt.Errorf("generate request matches error: %w", err)
}
if err := y.genMutation(rule, mutateRule); err != nil {
return fmt.Errorf("generate mutation rules error: %w", err)
}
varDefines = mutateRule.cel.vafDefines
chain.rules = append(chain.rules, mutateRule)
}
y.mu.Lock()
defer y.mu.Unlock()
y.chains = append(y.chains, chain)
return nil
}
func (y *Yarx) parseExpr(expression string, rule *MutationRule) error {
pr, _ := parser.NewParser()
parsed, commonErr := pr.Parse(common.NewTextSource(expression))
if len(commonErr.GetErrors()) != 0 {
return fmt.Errorf(commonErr.ToDisplayString())
}
rule.ExprInfo = parsed.GetSourceInfo()
//Rule.ExpectedVars = y.GetInternalIdentifier(parsed.GetExpr())
return y.walkExpr(parsed.GetExpr(), rule)
}
func (y *Yarx) Chains() []*MutationChain {
y.mu.RLock()
defer y.mu.RUnlock()
return append(y.chains[0:0], y.chains...)
}
func (y *Yarx) Rules() []*MutationRule {
y.mu.RLock()
defer y.mu.RUnlock()
ret := make([]*MutationRule, 0, len(y.chains))
for _, chain := range y.chains {
for _, rule := range chain.rules {
ret = append(ret, rule)
}
}
return ret
}
func (y *Yarx) canMock(yamlRule *YamlRule) error {
req := yamlRule.Request
meaningLessKey := []string{"host", "content-length", "content-type"}
hasBodyOrBody := func() bool {
if len(yamlRule.Request.Headers) != 0 {
var gotOtherKey bool
for k := range yamlRule.Request.Headers {
if funk.ContainsString(meaningLessKey, strings.ToLower(k)) {
continue
}
gotOtherKey = true
}
if gotOtherKey {
return true
}
}
if yamlRule.Request.Method != http.MethodGet && strings.TrimSpace(yamlRule.Request.Body) != "" {
return true
}
return false
}()
if strings.Count(req.Path, "/") < 2 {
u, err := url.ParseRequestURI(req.Path)
if err != nil {
return err
}
if strings.Contains(u.RawPath, "{{") && !hasBodyOrBody {
return fmt.Errorf("path is too flexible to build the route, [%s]", req.Path)
}
}
if (req.Path == "" || req.Path == "/") && !hasBodyOrBody {
return fmt.Errorf("path is too flexible to build the route, [%s]", req.Path)
}
return nil
}
//func (y *Yarx) GetInternalIdentifier(expr *expr.Expr) []string {
// var result []string
// result = y.getAllIdentifier(expr, result)
// result = funk.UniqString(result)
// var ret []string
// for _, val := range result {
// if val == "response" {
// continue
// }
// ret = append(ret, val)
// }
// return ret
//}
//
//func (y *Yarx) getAllIdentifier(curExpr *expr.Expr, cur []string) []string {
// if curExpr.Id == -1 {
// return cur
// }
// switch typedExpr := curExpr.ExprKind.(type) {
// case *expr.Expr_ConstExpr:
// case *expr.Expr_IdentExpr:
// curExpr.Id = -1
// return append(cur, typedExpr.IdentExpr.Name)
// case *expr.Expr_SelectExpr:
// curExpr.Id = -1
// return y.getAllIdentifier(typedExpr.SelectExpr.GetOperand(), cur)
// case *expr.Expr_CallExpr:
// curExpr.Id = -1
// if typedExpr.CallExpr.Target != nil {
// cur = append(cur, y.getAllIdentifier(typedExpr.CallExpr.Target, cur)...)
// }
// for _, arg := range typedExpr.CallExpr.GetArgs() {
// cur = append(cur, y.getAllIdentifier(arg, cur)...)
// }
// case *expr.Expr_ListExpr:
// case *expr.Expr_StructExpr:
// case *expr.Expr_ComprehensionExpr:
// }
// return cur
//}
func (y *Yarx) getShortestPath(poc *YamlPoc) ([]string, error) {
pr, _ := parser.NewParser()
parsed, commonErr := pr.Parse(common.NewTextSource(poc.Pattern))
if len(commonErr.GetErrors()) != 0 {
return nil, fmt.Errorf(commonErr.ToDisplayString())
}
return y.getShortestExprPath(parsed.GetExpr(), nil), nil
}
func (y *Yarx) getShortestExprPath(curExpr *expr.Expr, path []string) []string {
switch curExpr.GetCallExpr().Function {
case operators.LogicalOr:
var leftPath, rightPath []string
args := curExpr.GetCallExpr().GetArgs()
leftPath = y.getShortestExprPath(args[0], leftPath)
rightPath = y.getShortestExprPath(args[1], rightPath)
if len(leftPath) < len(rightPath) {
path = append(path, leftPath...)
} else {
path = append(path, rightPath...)
}
case operators.LogicalAnd:
for _, arg := range curExpr.GetCallExpr().GetArgs() {
path = y.getShortestExprPath(arg, path)
}
default:
path = append(path, curExpr.GetCallExpr().Function)
}
return path
}
func (y *Yarx) genMutation(yamlRule *YamlRule, mutateRule *MutationRule) error {
if err := y.parseExpr(yamlRule.Expression, mutateRule); err != nil {
return fmt.Errorf("parsing expression of %s, %s", yamlRule.Name, err)
}
if len(yamlRule.Output.Content) != 0 {
// 这个 Rule 要单独做一些工作,然后合并到主 Rule
outputRule := NewMutationRule(&celContext{
eval: make(map[string]interface{}),
vafDefines: make(map[string]*expr.Type),
})
for i := range yamlRule.Output.Content {
if i%2 == 0 {
continue
}
value := yamlRule.Output.Content[i].Value
if err := y.parseExpr(value, outputRule); err != nil {
return fmt.Errorf("parsing expression of %s, %s", yamlRule.Name, err)
}
}
var metrics RespMetrics
for _, fn := range outputRule.MutateFuncs {
if err := fn(&metrics, mutateRule.cel); err != nil {
return fmt.Errorf("executing output expression of %s, %s", yamlRule.Name, err)
}
}
vars, varTypes, err := executeOutput(yamlRule.Output.Content, &metrics)
if err != nil {
return err
}
mutateRule.MutateFuncs = append(mutateRule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
for k, v := range metrics.HeaderMap() {
oldValue := resp.Header().Get(k)
resp.Header().Set(k, oldValue+v)
}
if len(metrics.status) != 0 {
// 同一个 Rule 内不可能存在 response.status == 200 && response.status == 300
resp.WriteHeader(metrics.status[0])
}
if len(metrics.body) != 0 {
resp.Write([]byte{'\n'})
resp.Write(metrics.body)
}
return nil
})
for k, v := range vars {
mutateRule.cel.eval[k] = v
}
for k, t := range varTypes {
mutateRule.cel.vafDefines[k] = t
}
}
return nil
}
func (y *Yarx) genRequestMatch(yamlRule *YamlRule, mutateRule *MutationRule) error {
req := yamlRule.Request
var uriRegexp, bodyRegexp *regexp.Regexp
var err error
// method
var method = http.MethodGet
if req.Method != "" {
method = req.Method
}
mutateRule.Method = method
var replacedStr string
if req.Path != "" {
uriInfo, err := url.ParseRequestURI(req.Path)
if err != nil {
return err
}
uriRegexp, replacedStr, err = variableToRegexp(SortedURI(uriInfo), mutateRule.cel.eval, true, false)
if err != nil {
return err
}
} else {
uriRegexp = regexp.MustCompile(``)
}
mutateRule.URI = uriRegexp
mutateRule.ReplacedURI = replacedStr
// headers
headerRegexps := make(map[string]*regexp.Regexp)
for k, v := range req.Headers {
v = strings.TrimSpace(v)
var withWrapper = true
//poc-yaml-confluence-cve-2019-3396-lfi
if strings.EqualFold(k, "host") || strings.EqualFold(k, "content-length") {
continue
}
// poc-yaml-discuz-wooyun-2010-080723
if strings.EqualFold(k, "cookie") {
v = strings.TrimRight(v, ";")
withWrapper = false
}
var headerRe *regexp.Regexp
if strings.Contains(v, "{{") {
headerRe, _, err = variableToRegexp(v, mutateRule.cel.eval, withWrapper, false)
} else {
headerRe, err = regexp.Compile(regexp.QuoteMeta(v))
}
if err != nil {
return err
}
headerRegexps[k] = headerRe
}
mutateRule.Header = headerRegexps
// prepare body
req.Body = strings.TrimSpace(req.Body)
if req.Body != "" {
bodyRegexp, _, err = variableToRegexp(req.Body, mutateRule.cel.eval, false, true)
if err != nil {
return err
}
} else {
bodyRegexp = regexp.MustCompile(``)
}
mutateRule.Body = bodyRegexp
return nil
}
func readClose(rc io.ReadCloser) ([]byte, error) {
defer rc.Close()
return ioutil.ReadAll(rc)
}
func executeOutput(output []*yaml.Node, resp *RespMetrics) (map[string]interface{}, map[string]*expr.Type, error) {
result := map[string]interface{}{
"response.body": resp.body,
"response.status": resp.status,
"response.headers": resp.HeaderMap(),
}
typeMap := make(map[string]*expr.Type)
env := NewCELEnv()
for i := range output {
if i%2 != 0 {
continue
}
var newEnv *cel.Env = env
key := output[i].Value
value := output[i+1].Value
var err error
for key, t := range typeMap {
newEnv, err = newEnv.Extend(cel.Declarations(decls.NewVar(key, t)))
if err != nil {
return nil, nil, err
}
}
ast, errs := newEnv.Compile(value)
if errs != nil && errs.Err() != nil {
return nil, nil, errs.Err()
}
typeMap[key] = ast.ResultType()
prg, err := newEnv.Program(ast)
if err != nil {
return nil, nil, err
}
out, _, err := prg.Eval(result)
if err != nil {
return nil, nil, err
}
result[key] = out.Value()
}
for k := range result {
if strings.HasPrefix(k, "response.") {
delete(result, k)
}
}
return result, typeMap, nil
}
func (y *Yarx) walkExpr(curExpr *expr.Expr, rule *MutationRule) error {
switch typedExpr := curExpr.ExprKind.(type) {
case *expr.Expr_ConstExpr:
case *expr.Expr_IdentExpr:
case *expr.Expr_SelectExpr:
case *expr.Expr_CallExpr:
target := typedExpr.CallExpr.GetTarget()
if target != nil {
if err := y.walkExpr(target, rule); err != nil {
return err
}
}
args := typedExpr.CallExpr.GetArgs()
function := typedExpr.CallExpr.GetFunction()
switch function {
case "bcontains", "contains", "icontains":
if err := y.onContains(curExpr, rule); err != nil {
return err
}
case "matches", "bmatches":
if err := y.onMatches(curExpr, rule); err != nil {
return err
}
case "submatch", "bsubmatch":
if err := y.onSubMatch(curExpr, rule); err != nil {
return err
}
case operators.Equals, operators.NotEquals:
if err := y.onEqualLike(curExpr, rule); err != nil {
return err
}
default:
}
for _, arg := range args {
if err := y.walkExpr(arg, rule); err != nil {
return err
}
}
case *expr.Expr_ListExpr:
case *expr.Expr_StructExpr:
case *expr.Expr_ComprehensionExpr:
}
return nil
}
func (y *Yarx) onEqualLike(curExpr *expr.Expr, rule *MutationRule) error {
typedExpr := curExpr.ExprKind.(*expr.Expr_CallExpr)
info := &positionInfo{}
getCallingPostion(typedExpr, info)
arg := typedExpr.CallExpr.Args[1]
// 200 == response.status
if info.argIndex != 0 {
arg = typedExpr.CallExpr.Args[0]
}
prg, err := y.exprToNewProgram(arg, rule)
if err != nil {
return fmt.Errorf("contains function: %w", err)
}
switch info.position {
case PositionBody:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
body := []byte(mustString(out))
if typedExpr.CallExpr.Function == operators.Equals {
_, _ = resp.Write(body)
} else {
_, _ = resp.Write(append(body, 'k', 'o', 'a', 'l', 'r'))
}
return nil
})
case PositionStatus:
statusStr, err := y.exprToString(arg, rule)
if err != nil {
return err
}
status, err := strconv.Atoi(statusStr)
if err != nil {
return err
}
if rule.Status != 0 && rule.Status != status {
golog.Warnf("status may be conflict %d and %d in %s", rule.Status, status, rule.Chain.Name)
}
rule.Status = status
return nil
case PositionHeader:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
value := mustString(out)
oldValue := resp.Header().Get(info.headerKey)
if typedExpr.CallExpr.Function == operators.Equals {
resp.Header().Set(info.headerKey, oldValue+value)
} else {
resp.Header().Set(info.headerKey, oldValue+value+"k")
}
return nil
})
}
return nil
}
func (y *Yarx) onMatches(curExpr *expr.Expr, rule *MutationRule) error {
typedExpr := curExpr.ExprKind.(*expr.Expr_CallExpr)
info := &positionInfo{}
getCallingPostion(typedExpr, info)
prg, err := y.exprToNewProgram(typedExpr.CallExpr.Target, rule)
if err != nil {
return fmt.Errorf("contains function: %w", err)
}
switch info.position {
case PositionBody:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
fakeData, err := Generate(mustString(out), 10)
if err != nil {
return err
}
_, _ = resp.Write([]byte(fakeData))
return nil
})
case PositionHeader:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
fakeData, err := Generate(mustString(out), 10)
if err != nil {
return err
}
oldValue := resp.Header().Get(info.headerKey)
resp.Header().Set(info.headerKey, oldValue+fakeData)
return nil
})
default:
return fmt.Errorf("unknow position in contains function")
}
return nil
}
func (y *Yarx) onSubMatch(curExpr *expr.Expr, rule *MutationRule) error {
typedExpr := curExpr.ExprKind.(*expr.Expr_CallExpr)
info := &positionInfo{}
getCallingPostion(typedExpr, info)
prg, err := y.exprToNewProgram(typedExpr.CallExpr.Target, rule)
if err != nil {
return fmt.Errorf("contains function: %w", err)
}
switch info.position {
case PositionBody:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
fakeData, err := Generate(mustString(out), 10)
if err != nil {
return err
}
_, _ = resp.Write([]byte(fakeData))
return nil
})
case PositionHeader:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
fakeData, err := Generate(mustString(out), 10)
if err != nil {
return err
}
oldValue := resp.Header().Get(info.headerKey)
resp.Header().Set(info.headerKey, oldValue+fakeData)
return nil
})
default:
return fmt.Errorf("unknow position in submatch function")
}
return nil
}
func (y *Yarx) onContains(curExpr *expr.Expr, rule *MutationRule) error {
typedExpr := curExpr.ExprKind.(*expr.Expr_CallExpr)
info := &positionInfo{}
getCallingPostion(typedExpr, info)
prg, err := y.exprToNewProgram(typedExpr.CallExpr.Args[0], rule)
if err != nil {
return fmt.Errorf("contains function: %w", err)
}
switch info.position {
case PositionBody:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
_, _ = resp.Write([]byte(mustString(out)))
return nil
})
case PositionHeader:
rule.MutateFuncs = append(rule.MutateFuncs, func(resp http.ResponseWriter, ctx *celContext) error {
out, _, err := prg.Eval(ctx.eval)
if err != nil {
return err
}
oldValue := resp.Header().Get(info.headerKey)
resp.Header().Set(info.headerKey, oldValue+mustString(out))
return nil
})
default:
return fmt.Errorf("unknow position in contains function")
}
return nil
}
func (y *Yarx) exprToString(pr *expr.Expr, mutateRule *MutationRule) (string, error) {
ast := cel.ParsedExprToAst(&expr.ParsedExpr{
Expr: pr,
SourceInfo: mutateRule.ExprInfo,
})
return cel.AstToString(ast)
}
func (y *Yarx) exprToNewProgram(pr *expr.Expr, mutateRule *MutationRule) (cel.Program, error) {
ast := cel.ParsedExprToAst(&expr.ParsedExpr{
Expr: pr,
SourceInfo: mutateRule.ExprInfo,
})
expression, err := cel.AstToString(ast)
if err != nil {
return nil, fmt.Errorf("Rule body: ast to string error: %w", err)
}
//fmt.Printf("partial expr: %s\n", expression)
env := NewCELEnv()
var exprDecls []*expr.Decl
for n, t := range mutateRule.cel.vafDefines {
exprDecls = append(exprDecls, decls.NewVar(n, t))
}
env, err = env.Extend(cel.Declarations(exprDecls...))
if err != nil {
return nil, err
}
ast, iss := env.Compile(expression)
if iss.Err() != nil {
return nil, iss.Err()
}
return env.Program(ast)
}
func mustString(val ref.Val) string {
switch v := val.(type) {
case types.String:
return v.Value().(string)
case types.Bytes:
return string(v.Value().([]byte))
case types.Int:
return fmt.Sprintf("%d", v.Value().(int64))
default:
panic("unsupported value type")
}
}
const (
PositionBody = "body"
PositionHeader = "header"
PositionStatus = "status"
)
type positionInfo struct {
position string
headerKey string
argIndex int
}
func getCallingPostion(curExpr *expr.Expr_CallExpr, info *positionInfo) bool {
if curExpr.CallExpr.GetTarget() == nil && len(curExpr.CallExpr.GetArgs()) == 2 {
arg0 := curExpr.CallExpr.Args[0]
arg1 := curExpr.CallExpr.Args[1]
switch curExpr.CallExpr.Function {
case operators.Index:
if arg0.GetSelectExpr() != nil && arg0.GetSelectExpr().Field == "headers" {
info.headerKey = arg1.GetConstExpr().GetStringValue()
info.position = PositionHeader
return true
}
}
} else {
switch targetExpr := curExpr.CallExpr.Target.GetExprKind().(type) {
case *expr.Expr_SelectExpr:
if getSelectPosition(targetExpr, info) {
return true
}
case *expr.Expr_CallExpr:
if getCallingPostion(targetExpr, info) {
return true
}
}
}
for i, arg := range curExpr.CallExpr.Args {
switch typedArg := arg.ExprKind.(type) {
case *expr.Expr_CallExpr:
if getCallingPostion(typedArg, info) {
info.argIndex = i
return true
}
case *expr.Expr_SelectExpr:
if getSelectPosition(typedArg, info) {
info.argIndex = i
return true
}
}
}
return false
}
func getSelectPosition(curExpr *expr.Expr_SelectExpr, info *positionInfo) bool {
field := curExpr.SelectExpr.Field
if field == "body" {
info.position = PositionBody
return true
} else if field == "status" {
info.position = PositionStatus
return true
} else if field == "content_type" {
info.position = PositionHeader
info.headerKey = "Content-Type"
return true
}
return false
}
type RespMetrics struct {
body []byte
header http.Header
status []int
}
func (f *RespMetrics) Header() http.Header {
if f.header == nil {
f.header = make(http.Header)
}
return f.header
}
func (f *RespMetrics) HeaderMap() map[string]string {
header := f.Header()
ret := make(map[string]string)
for k, vv := range header {
if len(vv) != 0 {
ret[k] = vv[0]
}
}
return ret
}
func (f *RespMetrics) Write(bytes []byte) (int, error) {
f.body = append(f.body, bytes...)
//f.body = append(f.body, '\n')
return len(f.body), nil
}
func (f *RespMetrics) WriteHeader(statusCode int) {
f.status = append(f.status, statusCode)
return
}