diff --git a/pkg/templates/compile_test.go b/pkg/templates/compile_test.go index 34c22b0f2..81728d9c2 100644 --- a/pkg/templates/compile_test.go +++ b/pkg/templates/compile_test.go @@ -22,6 +22,7 @@ import ( "github.com/projectdiscovery/nuclei/v3/pkg/progress" "github.com/projectdiscovery/nuclei/v3/pkg/protocols" "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators" + "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/globalmatchers" "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/variables" "github.com/projectdiscovery/nuclei/v3/pkg/protocols/http" "github.com/projectdiscovery/nuclei/v3/pkg/templates" @@ -184,6 +185,26 @@ func Test_ParseWorkflow(t *testing.T) { require.Equal(t, len(expectedTemplate.Workflows), len(got.Workflows)) } +func Test_ParseWorkflowWithGlobalMatchers(t *testing.T) { + setup() + previousGlobalMatchers := executerOpts.Options.EnableGlobalMatchersTemplates + executerOpts.Options.EnableGlobalMatchersTemplates = true + defer func() { + executerOpts.Options.EnableGlobalMatchersTemplates = previousGlobalMatchers + executerOpts.GlobalMatchers = nil + }() + executerOpts.GlobalMatchers = globalmatchers.New() + + filePath := "tests/workflow-global-matchers.yaml" + got, err := templates.Parse(filePath, nil, executerOpts) + require.NoError(t, err, "could not parse workflow template") + require.NotNil(t, got, "workflow template should not be nil") + require.NotNil(t, got.CompiledWorkflow, "compiled workflow should not be nil") + require.Len(t, got.CompiledWorkflow.Workflows, 2) + require.Len(t, got.CompiledWorkflow.Workflows[0].Executers, 1) + require.Len(t, got.CompiledWorkflow.Workflows[1].Executers, 0) +} + func Test_WrongTemplate(t *testing.T) { setup() diff --git a/pkg/templates/tests/global-matcher.yaml b/pkg/templates/tests/global-matcher.yaml new file mode 100644 index 000000000..34c6f30d0 --- /dev/null +++ b/pkg/templates/tests/global-matcher.yaml @@ -0,0 +1,17 @@ +id: global-matcher-test + +info: + name: Global Matcher Test Template + author: pdteam + severity: info + +http: + - method: GET + path: + - "{{BaseURL}}" + global-matchers: true + matchers: + - type: word + part: body + words: + - "test" diff --git a/pkg/templates/tests/workflow-global-matchers.yaml b/pkg/templates/tests/workflow-global-matchers.yaml new file mode 100644 index 000000000..7ac7a0498 --- /dev/null +++ b/pkg/templates/tests/workflow-global-matchers.yaml @@ -0,0 +1,10 @@ +id: workflow-global-matchers + +info: + name: Workflow With Global Matchers + author: pdteam + severity: info + +workflows: + - template: tests/match-1.yaml + - template: tests/global-matcher.yaml diff --git a/pkg/templates/workflows.go b/pkg/templates/workflows.go index 771cdf4d9..d50222a8a 100644 --- a/pkg/templates/workflows.go +++ b/pkg/templates/workflows.go @@ -77,6 +77,9 @@ func parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, preprocessor Pr gologger.Warning().Msgf("Could not parse workflow template %s: %v\n", path, err) continue } + if template == nil { + continue + } if template.Executer == nil { gologger.Warning().Msgf("Could not parse workflow template %s: no executer found\n", path) continue