Path: blob/dev/pkg/operators/matchers/validate.go
2070 views
package matchers12import (3"errors"4"fmt"5"reflect"6"strings"78"github.com/antchfx/xpath"9sliceutil "github.com/projectdiscovery/utils/slice"10)1112var commonExpectedFields = []string{"Type", "Condition", "Name", "MatchAll", "Negative", "Internal"}1314// Validate perform initial validation on the matcher structure15func (matcher *Matcher) Validate() error {16// Build a map of YAML‐tag names that are actually set (non-zero) in the matcher.17matcherMap := make(map[string]interface{})18val := reflect.ValueOf(*matcher)19typ := reflect.TypeOf(*matcher)20for i := 0; i < typ.NumField(); i++ {21field := typ.Field(i)22// skip internal / unexported or opt-out fields23yamlTag := strings.Split(field.Tag.Get("yaml"), ",")[0]24if yamlTag == "" || yamlTag == "-" {25continue26}27if val.Field(i).IsZero() {28continue29}30matcherMap[yamlTag] = struct{}{}31}32var err error3334var expectedFields []string35switch matcher.matcherType {36case DSLMatcher:37expectedFields = append(commonExpectedFields, "DSL")38case StatusMatcher:39expectedFields = append(commonExpectedFields, "Status", "Part")40case SizeMatcher:41expectedFields = append(commonExpectedFields, "Size", "Part")42case WordsMatcher:43expectedFields = append(commonExpectedFields, "Words", "Part", "Encoding", "CaseInsensitive")44case BinaryMatcher:45expectedFields = append(commonExpectedFields, "Binary", "Part", "Encoding", "CaseInsensitive")46case RegexMatcher:47expectedFields = append(commonExpectedFields, "Regex", "Part", "Encoding", "CaseInsensitive")48case XPathMatcher:49expectedFields = append(commonExpectedFields, "XPath", "Part")50}5152if err = checkFields(matcher, matcherMap, expectedFields...); err != nil {53return err54}5556// validate the XPath query57if matcher.matcherType == XPathMatcher {58for _, query := range matcher.XPath {59if _, err = xpath.Compile(query); err != nil {60return err61}62}63}64return nil65}6667func checkFields(m *Matcher, matcherMap map[string]interface{}, expectedFields ...string) error {68var foundUnexpectedFields []string69for marshaledFieldName := range matcherMap {70// revert back the marshaled name to the original field71structFieldName, err := getFieldNameFromYamlTag(marshaledFieldName, *m)72if err != nil {73return err74}75if !sliceutil.Contains(expectedFields, structFieldName) {76foundUnexpectedFields = append(foundUnexpectedFields, structFieldName)77}78}79if len(foundUnexpectedFields) > 0 {80return fmt.Errorf("matcher %s has unexpected fields: %s", m.matcherType, strings.Join(foundUnexpectedFields, ","))81}82return nil83}8485func getFieldNameFromYamlTag(tagName string, object interface{}) (string, error) {86reflectType := reflect.TypeOf(object)87if reflectType.Kind() != reflect.Struct {88return "", errors.New("the object must be a struct")89}90for idx := 0; idx < reflectType.NumField(); idx++ {91field := reflectType.Field(idx)92tagParts := strings.Split(field.Tag.Get("yaml"), ",")93if len(tagParts) > 0 && tagParts[0] == tagName {94return field.Name, nil95}96}97return "", fmt.Errorf("field %s not found", tagName)98}99100101