Path: blob/dev/pkg/tmplexec/flow/flow_executor_test.go
2070 views
package flow_test12import (3"context"4"log"5"testing"6"time"78"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"9"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"10"github.com/projectdiscovery/nuclei/v3/pkg/loader/workflow"11"github.com/projectdiscovery/nuclei/v3/pkg/progress"12"github.com/projectdiscovery/nuclei/v3/pkg/protocols"13"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"14"github.com/projectdiscovery/nuclei/v3/pkg/scan"15"github.com/projectdiscovery/nuclei/v3/pkg/templates"16"github.com/projectdiscovery/nuclei/v3/pkg/testutils"17"github.com/projectdiscovery/ratelimit"18"github.com/stretchr/testify/require"19)2021var executerOpts *protocols.ExecutorOptions2223func setup() {24options := testutils.DefaultOptions25testutils.Init(options)26progressImpl, _ := progress.NewStatsTicker(0, false, false, false, 0)2728executerOpts = &protocols.ExecutorOptions{29Output: testutils.NewMockOutputWriter(options.OmitTemplate),30Options: options,31Progress: progressImpl,32ProjectFile: nil,33IssuesClient: nil,34Browser: nil,35Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory),36RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second),37Parser: templates.NewParser(),38}39workflowLoader, err := workflow.NewLoader(executerOpts)40if err != nil {41log.Fatalf("Could not create workflow loader: %s\n", err)42}43executerOpts.WorkflowLoader = workflowLoader44}4546func TestFlowTemplateWithIndex(t *testing.T) {47// test48setup()49Template, err := templates.Parse("testcases/nuclei-flow-dns.yaml", nil, executerOpts)50require.Nil(t, err, "could not parse template")5152require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not5354err = Template.Executer.Compile()55require.Nil(t, err, "could not compile template")5657input := contextargs.NewWithInput(context.Background(), "hackerone.com")58ctx := scan.NewScanContext(context.Background(), input)59gotresults, err := Template.Executer.Execute(ctx)60require.Nil(t, err, "could not execute template")61require.True(t, gotresults)62}6364func TestFlowTemplateWithID(t *testing.T) {65setup()66// apart from parse->compile->execution this testcase checks support for use custom id for protocol request and invocation of67// the same in js68Template, err := templates.Parse("testcases/nuclei-flow-dns-id.yaml", nil, executerOpts)69require.Nil(t, err, "could not parse template")7071require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not7273err = Template.Executer.Compile()74require.Nil(t, err, "could not compile template")7576target := contextargs.NewWithInput(context.Background(), "hackerone.com")77ctx := scan.NewScanContext(context.Background(), target)78gotresults, err := Template.Executer.Execute(ctx)79require.Nil(t, err, "could not execute template")80require.True(t, gotresults)81}8283func TestFlowWithProtoPrefix(t *testing.T) {84// test85setup()8687// apart from parse->compile->execution this testcase checks88// mix of custom protocol request id and index is supported in js89// and also validates availability of protocol response variables in template context90Template, err := templates.Parse("testcases/nuclei-flow-dns-prefix.yaml", nil, executerOpts)91require.Nil(t, err, "could not parse template")9293require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not9495err = Template.Executer.Compile()96require.Nil(t, err, "could not compile template")9798input := contextargs.NewWithInput(context.Background(), "hackerone.com")99ctx := scan.NewScanContext(context.Background(), input)100gotresults, err := Template.Executer.Execute(ctx)101require.Nil(t, err, "could not execute template")102require.True(t, gotresults)103}104105func TestFlowWithConditionNegative(t *testing.T) {106setup()107108// apart from parse->compile->execution this testcase checks109// if bitwise operator (&&) are properly executed and working110Template, err := templates.Parse("testcases/condition-flow.yaml", nil, executerOpts)111require.Nil(t, err, "could not parse template")112113require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not114115err = Template.Executer.Compile()116require.Nil(t, err, "could not compile template")117118input := contextargs.NewWithInput(context.Background(), "scanme.sh")119ctx := scan.NewScanContext(context.Background(), input)120// expect no results and verify thant dns request is executed and http is not121gotresults, err := Template.Executer.Execute(ctx)122require.Nil(t, err, "could not execute template")123require.False(t, gotresults)124}125126func TestFlowWithConditionPositive(t *testing.T) {127setup()128129// apart from parse->compile->execution this testcase checks130// if bitwise operator (&&) are properly executed and working131Template, err := templates.Parse("testcases/condition-flow.yaml", nil, executerOpts)132require.Nil(t, err, "could not parse template")133134require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not135136err = Template.Executer.Compile()137require.Nil(t, err, "could not compile template")138139input := contextargs.NewWithInput(context.Background(), "cloud.projectdiscovery.io")140ctx := scan.NewScanContext(context.Background(), input)141// positive match . expect results also verify that both dns() and http() were executed142gotresults, err := Template.Executer.Execute(ctx)143require.Nil(t, err, "could not execute template")144require.True(t, gotresults)145}146147func TestFlowWithNoMatchers(t *testing.T) {148setup()149// when using conditional flow with no matchers at all150// we implicitly assume that request was successful and internally changed the result to true (for scope of condition only)151152Template, err := templates.Parse("testcases/condition-flow-no-operators.yaml", nil, executerOpts)153require.Nil(t, err, "could not parse template")154155require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not156157err = Template.Executer.Compile()158require.Nil(t, err, "could not compile template")159160anotherInput := contextargs.NewWithInput(context.Background(), "cloud.projectdiscovery.io")161anotherCtx := scan.NewScanContext(context.Background(), anotherInput)162// positive match . expect results also verify that both dns() and http() were executed163gotresults, err := Template.Executer.Execute(anotherCtx)164require.Nil(t, err, "could not execute template")165require.True(t, gotresults)166167t.Run("Contains Extractor", func(t *testing.T) {168Template, err := templates.Parse("testcases/condition-flow-extractors.yaml", nil, executerOpts)169require.Nil(t, err, "could not parse template")170171require.True(t, Template.Flow != "", "not a flow template") // this is classifer if template is flow or not172173err = Template.Executer.Compile()174require.Nil(t, err, "could not compile template")175176input := contextargs.NewWithInput(context.Background(), "scanme.sh")177ctx := scan.NewScanContext(context.Background(), input)178// positive match . expect results also verify that both dns() and http() were executed179gotresults, err := Template.Executer.Execute(ctx)180require.Nil(t, err, "could not execute template")181require.True(t, gotresults)182})183}184185186