mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 17:25:28 +00:00
* fix(lib): scans didn't stop on ctx cancellation Signed-off-by: Dwi Siswanto <git@dw1.io> * Update lib/sdk_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix(lib): wait resources to be released b4 return Signed-off-by: Dwi Siswanto <git@dw1.io> --------- Signed-off-by: Dwi Siswanto <git@dw1.io> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package nuclei_test
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"testing"
|
|
"time"
|
|
|
|
nuclei "github.com/projectdiscovery/nuclei/v3/lib"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestContextCancelNucleiEngine(t *testing.T) {
|
|
// create nuclei engine with options
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ne, err := nuclei.NewNucleiEngineCtx(ctx,
|
|
nuclei.WithTemplateFilters(nuclei.TemplateFilters{Tags: []string{"oast"}}),
|
|
nuclei.EnableStatsWithOpts(nuclei.StatsOptions{MetricServerPort: 0}),
|
|
)
|
|
require.NoError(t, err, "could not create nuclei engine")
|
|
|
|
go func() {
|
|
time.Sleep(time.Second * 2)
|
|
cancel()
|
|
log.Println("Test: context cancelled")
|
|
}()
|
|
|
|
// load targets and optionally probe non http/https targets
|
|
ne.LoadTargets([]string{"http://honey.scanme.sh"}, false)
|
|
// when callback is nil it nuclei will print JSON output to stdout
|
|
err = ne.ExecuteWithCallback(nil)
|
|
if err != nil {
|
|
// we expect a context cancellation error
|
|
require.ErrorIs(t, err, context.Canceled, "was expecting context cancellation error")
|
|
}
|
|
defer ne.Close()
|
|
}
|