Cleanup some loader parts + moved workflow loading separate

dev
Ice3man543 2021-08-19 01:58:54 +05:30
parent 297ce0fc83
commit 01b902f119
4 changed files with 51 additions and 57 deletions

View File

@ -100,53 +100,38 @@ func (s *Store) ValidateTemplates(templatesList, workflowsList []string) bool {
workflowsMap := s.pathFilter.Match(includedWorkflows)
notErrored := true
errorValidationFunc := func(message string, template string, err error) {
if strings.Contains(err.Error(), "cannot create template executer") {
return
}
if err == filter.ErrExcluded {
return
}
notErrored = false
gologger.Error().Msgf(message, template, err)
}
for k := range templatesMap {
_, err := s.loadTemplate(k, false)
_, err := parsers.LoadTemplate(k, s.tagFilter)
if err != nil {
if strings.Contains(err.Error(), "cannot create template executer") {
continue
}
if err == filter.ErrExcluded {
continue
}
notErrored = false
gologger.Error().Msgf("Error occurred loading template %s: %s\n", k, err)
errorValidationFunc("Error occurred loading template %s: %s\n", k, err)
continue
}
_, err = templates.Parse(k, s.preprocessor, s.config.ExecutorOptions)
if err != nil {
if strings.Contains(err.Error(), "cannot create template executer") {
continue
}
if err == filter.ErrExcluded {
continue
}
notErrored = false
gologger.Error().Msgf("Error occurred parsing template %s: %s\n", k, err)
errorValidationFunc("Error occurred parsing template %s: %s\n", k, err)
continue
}
}
for k := range workflowsMap {
_, err := s.loadTemplate(k, true)
_, err := parsers.LoadWorkflow(k, true, s.tagFilter, nil)
if err != nil {
if strings.Contains(err.Error(), "cannot create template executer") {
continue
}
if err == filter.ErrExcluded {
continue
}
notErrored = false
gologger.Error().Msgf("Error occurred loading workflow %s: %s\n", k, err)
errorValidationFunc("Error occurred loading workflow %s: %s\n", k, err)
continue
}
_, err = templates.Parse(k, s.preprocessor, s.config.ExecutorOptions)
if err != nil {
if strings.Contains(err.Error(), "cannot create template executer") {
continue
}
if err == filter.ErrExcluded {
continue
}
notErrored = false
gologger.Error().Msgf("Error occurred parsing workflow %s: %s\n", k, err)
errorValidationFunc("Error occurred parsing workflow %s: %s\n", k, err)
continue
}
}
return notErrored
@ -159,7 +144,7 @@ func (s *Store) LoadTemplates(templatesList []string) []*templates.Template {
loadedTemplates := make([]*templates.Template, 0, len(templatesMap))
for k := range templatesMap {
loaded, err := s.loadTemplate(k, false)
loaded, err := parsers.LoadTemplate(k, s.tagFilter)
if err != nil {
gologger.Warning().Msgf("Could not load template %s: %s\n", k, err)
}
@ -182,7 +167,7 @@ func (s *Store) LoadWorkflows(workflowsList []string) []*templates.Template {
loadedWorkflows := make([]*templates.Template, 0, len(workflowsMap))
for k := range workflowsMap {
loaded, err := s.loadTemplate(k, true)
loaded, err := parsers.LoadWorkflow(k, true, s.tagFilter, nil)
if err != nil {
gologger.Warning().Msgf("Could not load workflow %s: %s\n", k, err)
}
@ -197,7 +182,3 @@ func (s *Store) LoadWorkflows(workflowsList []string) []*templates.Template {
}
return loadedWorkflows
}
func (s *Store) loadTemplate(templatePath string, isWorkflow bool) (bool, error) {
return parsers.Load(templatePath, isWorkflow, nil, s.tagFilter) // TODO consider separating template and workflow loading logic
}

View File

@ -3,9 +3,10 @@ package model
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"strings"
"gopkg.in/yaml.v2"
"github.com/projectdiscovery/nuclei/v2/internal/severity"
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
)

View File

@ -17,29 +17,42 @@ import (
const mandatoryFieldMissingTemplate = "mandatory '%s' field is missing"
// Load loads a template by parsing metadata and running all tag and path based filters on the template.
func Load(templatePath string, isWorkflow bool, workflowTags []string, tagFilter *filter.TagFilter) (bool, error) {
template, templateParseError := parseTemplate(templatePath)
// LoadTemplate loads a template by parsing metadata and running all tag
// and path based filters on the template.
func LoadTemplate(templatePath string, tagFilter *filter.TagFilter) (bool, error) {
return load(templatePath, false, nil, tagFilter)
}
// LoadTemplate loads a template by parsing metadata and running all tag
// based filters on the template.
//
// isWorkflow when false, means that the template itself is not a workflow
// however options tags may have been passed in workflowTags slice.
func LoadWorkflow(templatePath string, isWorkflow bool, tagFilter *filter.TagFilter, workflowTags []string) (bool, error) {
return load(templatePath, isWorkflow, workflowTags, tagFilter)
}
func load(path string, isWorkflow bool, workflowTags []string, tagFilter *filter.TagFilter) (bool, error) {
template, templateParseError := parseTemplate(path)
if templateParseError != nil {
return false, templateParseError
}
// If this is called for a workflow and we don't have a workflow, return
if isWorkflow && len(template.Workflows) == 0 {
return false, nil
}
templateInfo := template.Info
if validationError := validateMandatoryInfoFields(&templateInfo); validationError != nil {
return false, validationError
}
if len(template.Workflows) > 0 {
if isWorkflow {
return true, nil // if a workflow is declared and this template is a workflow, then load
} else { //nolint:indent-error-flow,revive // preferred: readability and extensibility
return false, nil // if a workflow is declared and this template is not a workflow then do not load
}
} else if isWorkflow {
return false, nil // if no workflows are declared and this template is a workflow then do not load
} else { // if workflows are not declared and the template is not a workflow then parse it
return isInfoMetadataMatch(tagFilter, &templateInfo, workflowTags)
// Validation of the metadata match happens in all scenarios.
templateMatch, matchErr := isInfoMetadataMatch(tagFilter, &templateInfo, []string{})
if matchErr != nil || !templateMatch {
return false, matchErr
}
return true, nil
}
func isInfoMetadataMatch(tagFilter *filter.TagFilter, templateInfo *model.Info, workflowTags []string) (bool, error) {
@ -54,7 +67,6 @@ func isInfoMetadataMatch(tagFilter *filter.TagFilter, templateInfo *model.Info,
} else {
match, err = tagFilter.MatchWithWorkflowTags(templateTags, templateAuthors, templateSeverity, workflowTags)
}
if err == filter.ErrExcluded {
return false, filter.ErrExcluded
}

View File

@ -36,7 +36,7 @@ func (w *workflowLoader) ListTags(workflowTags []string) []string {
loadedTemplates := make([]string, 0, len(templatesMap))
for k := range templatesMap {
loaded, err := Load(k, false, workflowTags, w.tagFilter)
loaded, err := LoadWorkflow(k, false, w.tagFilter, workflowTags)
if err != nil {
gologger.Warning().Msgf("Could not load template %s: %s\n", k, err)
} else if loaded {
@ -53,7 +53,7 @@ func (w *workflowLoader) ListTemplates(templatesList []string, noValidate bool)
loadedTemplates := make([]string, 0, len(templatesMap))
for k := range templatesMap {
matched, err := Load(k, false, nil, w.tagFilter)
matched, err := LoadTemplate(k, w.tagFilter)
if err != nil {
gologger.Warning().Msgf("Could not load template %s: %s\n", k, err)
} else if matched || noValidate {