Path: blob/dev/pkg/protocols/common/utils/excludematchers/excludematchers.go
2073 views
package excludematchers12import (3"strings"4)56// ExcludeMatchers is an instance for excluding matchers with template IDs7type ExcludeMatchers struct {8values map[string]struct{}9templateIDs map[string]struct{}10matcherNames map[string]struct{}11}1213// New returns a new exclude matchers instance14//15// Wildcard and non-wildcard values are supported.16// <template-id>:<matcher-name> is the syntax. Wildcards can be specified17// using * character for either value.18//19// Ex- http-missing-security-headers:* skips all http-missing-security-header templates20func New(values []string) *ExcludeMatchers {21excludeMatchers := &ExcludeMatchers{22values: make(map[string]struct{}),23templateIDs: make(map[string]struct{}),24matcherNames: make(map[string]struct{}),25}26for _, value := range values {27partValues := strings.SplitN(value, ":", 2)28if len(partValues) < 2 {29// If there is no matcher name, consider it as template ID30if _, ok := excludeMatchers.templateIDs[value]; !ok {31excludeMatchers.templateIDs[value] = struct{}{}32}33continue34}35templateID, matcherName := partValues[0], partValues[1]3637// Handle wildcards38if templateID == "*" {39if _, ok := excludeMatchers.matcherNames[matcherName]; !ok {40excludeMatchers.matcherNames[matcherName] = struct{}{}41}42} else if matcherName == "*" {43if _, ok := excludeMatchers.templateIDs[templateID]; !ok {44excludeMatchers.templateIDs[templateID] = struct{}{}45}46} else {47if _, ok := excludeMatchers.values[value]; !ok {48excludeMatchers.values[value] = struct{}{}49}50}51}52return excludeMatchers53}5455// Match returns true if templateID and matcherName matches the blocklist56func (e *ExcludeMatchers) Match(templateID, matcherName string) bool {57if _, ok := e.templateIDs[templateID]; ok {58return true59}60if _, ok := e.matcherNames[matcherName]; ok {61return true62}63matchName := strings.Join([]string{templateID, matcherName}, ":")64_, found := e.values[matchName]65return found66}676869