Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/operators/matchers/validate.go
2070 views
1
package matchers
2
3
import (
4
"errors"
5
"fmt"
6
"reflect"
7
"strings"
8
9
"github.com/antchfx/xpath"
10
sliceutil "github.com/projectdiscovery/utils/slice"
11
)
12
13
var commonExpectedFields = []string{"Type", "Condition", "Name", "MatchAll", "Negative", "Internal"}
14
15
// Validate perform initial validation on the matcher structure
16
func (matcher *Matcher) Validate() error {
17
// Build a map of YAML‐tag names that are actually set (non-zero) in the matcher.
18
matcherMap := make(map[string]interface{})
19
val := reflect.ValueOf(*matcher)
20
typ := reflect.TypeOf(*matcher)
21
for i := 0; i < typ.NumField(); i++ {
22
field := typ.Field(i)
23
// skip internal / unexported or opt-out fields
24
yamlTag := strings.Split(field.Tag.Get("yaml"), ",")[0]
25
if yamlTag == "" || yamlTag == "-" {
26
continue
27
}
28
if val.Field(i).IsZero() {
29
continue
30
}
31
matcherMap[yamlTag] = struct{}{}
32
}
33
var err error
34
35
var expectedFields []string
36
switch matcher.matcherType {
37
case DSLMatcher:
38
expectedFields = append(commonExpectedFields, "DSL")
39
case StatusMatcher:
40
expectedFields = append(commonExpectedFields, "Status", "Part")
41
case SizeMatcher:
42
expectedFields = append(commonExpectedFields, "Size", "Part")
43
case WordsMatcher:
44
expectedFields = append(commonExpectedFields, "Words", "Part", "Encoding", "CaseInsensitive")
45
case BinaryMatcher:
46
expectedFields = append(commonExpectedFields, "Binary", "Part", "Encoding", "CaseInsensitive")
47
case RegexMatcher:
48
expectedFields = append(commonExpectedFields, "Regex", "Part", "Encoding", "CaseInsensitive")
49
case XPathMatcher:
50
expectedFields = append(commonExpectedFields, "XPath", "Part")
51
}
52
53
if err = checkFields(matcher, matcherMap, expectedFields...); err != nil {
54
return err
55
}
56
57
// validate the XPath query
58
if matcher.matcherType == XPathMatcher {
59
for _, query := range matcher.XPath {
60
if _, err = xpath.Compile(query); err != nil {
61
return err
62
}
63
}
64
}
65
return nil
66
}
67
68
func checkFields(m *Matcher, matcherMap map[string]interface{}, expectedFields ...string) error {
69
var foundUnexpectedFields []string
70
for marshaledFieldName := range matcherMap {
71
// revert back the marshaled name to the original field
72
structFieldName, err := getFieldNameFromYamlTag(marshaledFieldName, *m)
73
if err != nil {
74
return err
75
}
76
if !sliceutil.Contains(expectedFields, structFieldName) {
77
foundUnexpectedFields = append(foundUnexpectedFields, structFieldName)
78
}
79
}
80
if len(foundUnexpectedFields) > 0 {
81
return fmt.Errorf("matcher %s has unexpected fields: %s", m.matcherType, strings.Join(foundUnexpectedFields, ","))
82
}
83
return nil
84
}
85
86
func getFieldNameFromYamlTag(tagName string, object interface{}) (string, error) {
87
reflectType := reflect.TypeOf(object)
88
if reflectType.Kind() != reflect.Struct {
89
return "", errors.New("the object must be a struct")
90
}
91
for idx := 0; idx < reflectType.NumField(); idx++ {
92
field := reflectType.Field(idx)
93
tagParts := strings.Split(field.Tag.Get("yaml"), ",")
94
if len(tagParts) > 0 && tagParts[0] == tagName {
95
return field.Name, nil
96
}
97
}
98
return "", fmt.Errorf("field %s not found", tagName)
99
}
100
101