Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/loader/workflow/workflow_loader.go
2070 views
1
package workflow
2
3
import (
4
"github.com/projectdiscovery/gologger"
5
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
6
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/loader/filter"
7
"github.com/projectdiscovery/nuclei/v3/pkg/model"
8
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
9
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
10
)
11
12
type workflowLoader struct {
13
pathFilter *filter.PathFilter
14
tagFilter *templates.TagFilter
15
options *protocols.ExecutorOptions
16
}
17
18
// NewLoader returns a new workflow loader structure
19
func NewLoader(options *protocols.ExecutorOptions) (model.WorkflowLoader, error) {
20
tagFilter, err := templates.NewTagFilter(&templates.TagFilterConfig{
21
Authors: options.Options.Authors,
22
Tags: options.Options.Tags,
23
ExcludeTags: options.Options.ExcludeTags,
24
IncludeTags: options.Options.IncludeTags,
25
IncludeIds: options.Options.IncludeIds,
26
ExcludeIds: options.Options.ExcludeIds,
27
Severities: options.Options.Severities,
28
ExcludeSeverities: options.Options.ExcludeSeverities,
29
Protocols: options.Options.Protocols,
30
ExcludeProtocols: options.Options.ExcludeProtocols,
31
IncludeConditions: options.Options.IncludeConditions,
32
})
33
if err != nil {
34
return nil, err
35
}
36
pathFilter := filter.NewPathFilter(&filter.PathFilterConfig{
37
IncludedTemplates: options.Options.IncludeTemplates,
38
ExcludedTemplates: options.Options.ExcludedTemplates,
39
}, options.Catalog)
40
41
return &workflowLoader{pathFilter: pathFilter, tagFilter: tagFilter, options: options}, nil
42
}
43
44
func (w *workflowLoader) GetTemplatePathsByTags(templateTags []string) []string {
45
includedTemplates, errs := w.options.Catalog.GetTemplatesPath([]string{config.DefaultConfig.TemplatesDirectory})
46
for template, err := range errs {
47
gologger.Error().Msgf("Could not find template '%s': %s", template, err)
48
}
49
50
templatePathMap := w.pathFilter.Match(includedTemplates)
51
52
loadedTemplates := make([]string, 0, len(templatePathMap))
53
for templatePath := range templatePathMap {
54
loaded, _ := w.options.Parser.LoadTemplate(templatePath, w.tagFilter, templateTags, w.options.Catalog)
55
if loaded {
56
loadedTemplates = append(loadedTemplates, templatePath)
57
}
58
}
59
return loadedTemplates
60
}
61
62
func (w *workflowLoader) GetTemplatePaths(templatesList []string, noValidate bool) []string {
63
includedTemplates, errs := w.options.Catalog.GetTemplatesPath(templatesList)
64
for template, err := range errs {
65
gologger.Error().Msgf("Could not find template '%s': %s", template, err)
66
}
67
templatesPathMap := w.pathFilter.Match(includedTemplates)
68
69
loadedTemplates := make([]string, 0, len(templatesPathMap))
70
for templatePath := range templatesPathMap {
71
matched, err := w.options.Parser.LoadTemplate(templatePath, w.tagFilter, nil, w.options.Catalog)
72
if err != nil && !matched {
73
gologger.Warning().Msg(err.Error())
74
} else if matched || noValidate {
75
loadedTemplates = append(loadedTemplates, templatePath)
76
}
77
}
78
return loadedTemplates
79
}
80
81