2022-06-30 13:20:54 +02:00
|
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"reflect"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
2023-08-26 05:11:51 +12:00
|
|
|
|
"github.com/antchfx/xpath"
|
2022-11-06 21:24:23 +01:00
|
|
|
|
sliceutil "github.com/projectdiscovery/utils/slice"
|
2022-06-30 13:20:54 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-01-08 05:12:11 +05:30
|
|
|
|
var commonExpectedFields = []string{"Type", "Condition", "Name", "MatchAll", "Negative", "Internal"}
|
2022-06-30 13:20:54 +02:00
|
|
|
|
|
|
|
|
|
|
// Validate perform initial validation on the matcher structure
|
|
|
|
|
|
func (matcher *Matcher) Validate() error {
|
2025-08-02 15:54:15 +05:30
|
|
|
|
// Build a map of YAML‐tag names that are actually set (non-zero) in the matcher.
|
2022-06-30 13:20:54 +02:00
|
|
|
|
matcherMap := make(map[string]interface{})
|
2025-08-02 15:54:15 +05:30
|
|
|
|
val := reflect.ValueOf(*matcher)
|
|
|
|
|
|
typ := reflect.TypeOf(*matcher)
|
|
|
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
|
|
|
|
field := typ.Field(i)
|
|
|
|
|
|
// skip internal / unexported or opt-out fields
|
|
|
|
|
|
yamlTag := strings.Split(field.Tag.Get("yaml"), ",")[0]
|
|
|
|
|
|
if yamlTag == "" || yamlTag == "-" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if val.Field(i).IsZero() {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
matcherMap[yamlTag] = struct{}{}
|
2022-06-30 13:20:54 +02:00
|
|
|
|
}
|
2025-08-02 15:54:15 +05:30
|
|
|
|
var err error
|
2022-06-30 13:20:54 +02:00
|
|
|
|
|
|
|
|
|
|
var expectedFields []string
|
|
|
|
|
|
switch matcher.matcherType {
|
|
|
|
|
|
case DSLMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "DSL")
|
|
|
|
|
|
case StatusMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "Status", "Part")
|
|
|
|
|
|
case SizeMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "Size", "Part")
|
|
|
|
|
|
case WordsMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "Words", "Part", "Encoding", "CaseInsensitive")
|
|
|
|
|
|
case BinaryMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "Binary", "Part", "Encoding", "CaseInsensitive")
|
|
|
|
|
|
case RegexMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "Regex", "Part", "Encoding", "CaseInsensitive")
|
2023-08-26 05:11:51 +12:00
|
|
|
|
case XPathMatcher:
|
|
|
|
|
|
expectedFields = append(commonExpectedFields, "XPath", "Part")
|
2022-06-30 13:20:54 +02:00
|
|
|
|
}
|
2023-08-26 05:11:51 +12:00
|
|
|
|
|
|
|
|
|
|
if err = checkFields(matcher, matcherMap, expectedFields...); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// validate the XPath query
|
|
|
|
|
|
if matcher.matcherType == XPathMatcher {
|
|
|
|
|
|
for _, query := range matcher.XPath {
|
|
|
|
|
|
if _, err = xpath.Compile(query); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2022-06-30 13:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func checkFields(m *Matcher, matcherMap map[string]interface{}, expectedFields ...string) error {
|
|
|
|
|
|
var foundUnexpectedFields []string
|
|
|
|
|
|
for marshaledFieldName := range matcherMap {
|
|
|
|
|
|
// revert back the marshaled name to the original field
|
|
|
|
|
|
structFieldName, err := getFieldNameFromYamlTag(marshaledFieldName, *m)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !sliceutil.Contains(expectedFields, structFieldName) {
|
|
|
|
|
|
foundUnexpectedFields = append(foundUnexpectedFields, structFieldName)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(foundUnexpectedFields) > 0 {
|
|
|
|
|
|
return fmt.Errorf("matcher %s has unexpected fields: %s", m.matcherType, strings.Join(foundUnexpectedFields, ","))
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getFieldNameFromYamlTag(tagName string, object interface{}) (string, error) {
|
|
|
|
|
|
reflectType := reflect.TypeOf(object)
|
|
|
|
|
|
if reflectType.Kind() != reflect.Struct {
|
|
|
|
|
|
return "", errors.New("the object must be a struct")
|
|
|
|
|
|
}
|
|
|
|
|
|
for idx := 0; idx < reflectType.NumField(); idx++ {
|
|
|
|
|
|
field := reflectType.Field(idx)
|
|
|
|
|
|
tagParts := strings.Split(field.Tag.Get("yaml"), ",")
|
|
|
|
|
|
if len(tagParts) > 0 && tagParts[0] == tagName {
|
|
|
|
|
|
return field.Name, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return "", fmt.Errorf("field %s not found", tagName)
|
|
|
|
|
|
}
|