From 04b1e43780e9a3e5b8fdb1bf3b22ae7e53f0d832 Mon Sep 17 00:00:00 2001 From: Ice3man Date: Mon, 28 Feb 2022 15:37:51 +0530 Subject: [PATCH 1/2] Added duplicate ID detection to validate + misc fixes --- v2/pkg/catalog/loader/loader.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/v2/pkg/catalog/loader/loader.go b/v2/pkg/catalog/loader/loader.go index 6323b832..8979129a 100644 --- a/v2/pkg/catalog/loader/loader.go +++ b/v2/pkg/catalog/loader/loader.go @@ -148,6 +148,12 @@ func (store *Store) Load() { store.workflows = store.LoadWorkflows(store.finalWorkflows) } +var uniqueTemplatesIDValidateMap map[string]string + +func init() { + uniqueTemplatesIDValidateMap = make(map[string]string) +} + // ValidateTemplates takes a list of templates and validates them // erroring out on discovering any faulty templates. func (store *Store) ValidateTemplates(templatesList, workflowsList []string) error { @@ -181,8 +187,10 @@ func areTemplatesValid(store *Store, filteredTemplatePaths map[string]struct{}) func areWorkflowOrTemplatesValid(store *Store, filteredTemplatePaths map[string]struct{}, isWorkflow bool, load func(templatePath string, tagFilter *filter.TagFilter) (bool, error)) bool { areTemplatesValid := true + for templatePath := range filteredTemplatePaths { - if _, err := load(templatePath, store.tagFilter); err != nil { + _, err := load(templatePath, store.tagFilter) + if err != nil { if isParsingError("Error occurred loading template %s: %s\n", templatePath, err) { areTemplatesValid = false continue @@ -195,8 +203,14 @@ func areWorkflowOrTemplatesValid(store *Store, filteredTemplatePaths map[string] areTemplatesValid = false } } else { + if old, ok := uniqueTemplatesIDValidateMap[template.ID]; !ok { + uniqueTemplatesIDValidateMap[template.ID] = templatePath + } else { + areTemplatesValid = false + gologger.Warning().Msgf("Found duplicate template ID during validation %s => %s: %s\n", templatePath, old, template.ID) + } if !isWorkflow && len(template.Workflows) > 0 { - return true + continue } } if isWorkflow { From a1dbbc5e887089a0a86aa1fb62ec7d7429ca5a99 Mon Sep 17 00:00:00 2001 From: Ice3man Date: Thu, 3 Mar 2022 19:01:46 +0530 Subject: [PATCH 2/2] Renamed variables to be more appropriate + changed return message --- v2/cmd/nuclei/main.go | 6 +++++- v2/pkg/catalog/loader/loader.go | 15 +++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/v2/cmd/nuclei/main.go b/v2/cmd/nuclei/main.go index 9cd8a948..ed576a2e 100644 --- a/v2/cmd/nuclei/main.go +++ b/v2/cmd/nuclei/main.go @@ -60,7 +60,11 @@ func main() { }() if err := nucleiRunner.RunEnumeration(); err != nil { - gologger.Fatal().Msgf("Could not run nuclei: %s\n", err) + if options.Validate { + gologger.Fatal().Msgf("Could not validate templates: %s\n", err) + } else { + gologger.Fatal().Msgf("Could not run nuclei: %s\n", err) + } } nucleiRunner.Close() // on successful execution remove the resume file in case it exists diff --git a/v2/pkg/catalog/loader/loader.go b/v2/pkg/catalog/loader/loader.go index 8979129a..2739acb4 100644 --- a/v2/pkg/catalog/loader/loader.go +++ b/v2/pkg/catalog/loader/loader.go @@ -148,10 +148,10 @@ func (store *Store) Load() { store.workflows = store.LoadWorkflows(store.finalWorkflows) } -var uniqueTemplatesIDValidateMap map[string]string +var templateIDPathMap map[string]string func init() { - uniqueTemplatesIDValidateMap = make(map[string]string) + templateIDPathMap = make(map[string]string) } // ValidateTemplates takes a list of templates and validates them @@ -170,7 +170,7 @@ func (store *Store) ValidateTemplates(templatesList, workflowsList []string) err if areTemplatesValid(store, filteredTemplatePaths) && areWorkflowsValid(store, filteredWorkflowPaths) { return nil } - return errors.New("an error occurred during templates validation") + return errors.New("errors occured during template validation") } func areWorkflowsValid(store *Store, filteredWorkflowPaths map[string]struct{}) bool { @@ -189,8 +189,7 @@ func areWorkflowOrTemplatesValid(store *Store, filteredTemplatePaths map[string] areTemplatesValid := true for templatePath := range filteredTemplatePaths { - _, err := load(templatePath, store.tagFilter) - if err != nil { + if _, err := load(templatePath, store.tagFilter); err != nil { if isParsingError("Error occurred loading template %s: %s\n", templatePath, err) { areTemplatesValid = false continue @@ -203,11 +202,11 @@ func areWorkflowOrTemplatesValid(store *Store, filteredTemplatePaths map[string] areTemplatesValid = false } } else { - if old, ok := uniqueTemplatesIDValidateMap[template.ID]; !ok { - uniqueTemplatesIDValidateMap[template.ID] = templatePath + if existingTemplatePath, found := templateIDPathMap[template.ID]; !found { + templateIDPathMap[template.ID] = templatePath } else { areTemplatesValid = false - gologger.Warning().Msgf("Found duplicate template ID during validation %s => %s: %s\n", templatePath, old, template.ID) + gologger.Warning().Msgf("Found duplicate template ID during validation '%s' => '%s': %s\n", templatePath, existingTemplatePath, template.ID) } if !isWorkflow && len(template.Workflows) > 0 { continue