Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/utils/excludematchers/excludematchers.go
2073 views
1
package excludematchers
2
3
import (
4
"strings"
5
)
6
7
// ExcludeMatchers is an instance for excluding matchers with template IDs
8
type ExcludeMatchers struct {
9
values map[string]struct{}
10
templateIDs map[string]struct{}
11
matcherNames map[string]struct{}
12
}
13
14
// New returns a new exclude matchers instance
15
//
16
// Wildcard and non-wildcard values are supported.
17
// <template-id>:<matcher-name> is the syntax. Wildcards can be specified
18
// using * character for either value.
19
//
20
// Ex- http-missing-security-headers:* skips all http-missing-security-header templates
21
func New(values []string) *ExcludeMatchers {
22
excludeMatchers := &ExcludeMatchers{
23
values: make(map[string]struct{}),
24
templateIDs: make(map[string]struct{}),
25
matcherNames: make(map[string]struct{}),
26
}
27
for _, value := range values {
28
partValues := strings.SplitN(value, ":", 2)
29
if len(partValues) < 2 {
30
// If there is no matcher name, consider it as template ID
31
if _, ok := excludeMatchers.templateIDs[value]; !ok {
32
excludeMatchers.templateIDs[value] = struct{}{}
33
}
34
continue
35
}
36
templateID, matcherName := partValues[0], partValues[1]
37
38
// Handle wildcards
39
if templateID == "*" {
40
if _, ok := excludeMatchers.matcherNames[matcherName]; !ok {
41
excludeMatchers.matcherNames[matcherName] = struct{}{}
42
}
43
} else if matcherName == "*" {
44
if _, ok := excludeMatchers.templateIDs[templateID]; !ok {
45
excludeMatchers.templateIDs[templateID] = struct{}{}
46
}
47
} else {
48
if _, ok := excludeMatchers.values[value]; !ok {
49
excludeMatchers.values[value] = struct{}{}
50
}
51
}
52
}
53
return excludeMatchers
54
}
55
56
// Match returns true if templateID and matcherName matches the blocklist
57
func (e *ExcludeMatchers) Match(templateID, matcherName string) bool {
58
if _, ok := e.templateIDs[templateID]; ok {
59
return true
60
}
61
if _, ok := e.matcherNames[matcherName]; ok {
62
return true
63
}
64
matchName := strings.Join([]string{templateID, matcherName}, ":")
65
_, found := e.values[matchName]
66
return found
67
}
68
69