From 9d900da43b0acac7770ead060f938708cc0e4071 Mon Sep 17 00:00:00 2001 From: simple-minded <191973953+hakarrr@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:44:51 +0530 Subject: [PATCH 1/3] fix(sdk): scope ExecuteCallbackWithCtx callbacks to the call Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/sdk.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/sdk.go b/lib/sdk.go index 4ed71cbd93..036a5c5be3 100644 --- a/lib/sdk.go +++ b/lib/sdk.go @@ -321,7 +321,12 @@ func (e *NucleiEngine) ExecuteCallbackWithCtx(ctx context.Context, callback ...f filtered = append(filtered, cb) } } - e.resultCallbacks = append(e.resultCallbacks, filtered...) + // The callbacks passed here belong to this execution only. Keep the ones + // registered on the engine (WithResultCallback) and drop these once the scan + // has finished, otherwise a later call replays its results to them as well. + engineCallbacks := e.resultCallbacks + e.resultCallbacks = append(engineCallbacks[:len(engineCallbacks):len(engineCallbacks)], filtered...) + defer func() { e.resultCallbacks = engineCallbacks }() templatesAndWorkflows := append(e.store.Templates(), e.store.Workflows()...) if len(templatesAndWorkflows) == 0 { From 001412566f0aaaaab9453376517771d90a1aaa19 Mon Sep 17 00:00:00 2001 From: simple-minded <191973953+hakarrr@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:45:11 +0530 Subject: [PATCH 2/3] test(sdk): cover per-call callbacks in ExecuteCallbackWithCtx Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/callbacks_reuse_test.go | 103 ++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lib/callbacks_reuse_test.go diff --git a/lib/callbacks_reuse_test.go b/lib/callbacks_reuse_test.go new file mode 100644 index 0000000000..31572107bd --- /dev/null +++ b/lib/callbacks_reuse_test.go @@ -0,0 +1,103 @@ +package nuclei_test + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "sync/atomic" + "testing" + + nuclei "github.com/projectdiscovery/nuclei/v3/lib" + "github.com/projectdiscovery/nuclei/v3/pkg/output" + "github.com/stretchr/testify/require" +) + +// Each ExecuteCallbackWithCtx call must deliver results only to the callbacks +// passed to that call. +func TestExecuteCallbackWithCtxDoesNotReplayEarlierCallbacks(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "ok") + })) + defer srv.Close() + + tplDir := t.TempDir() + tpl := `id: callback-reuse +info: + name: Callback reuse + author: test + severity: info +http: + - method: GET + path: + - "{{BaseURL}}/" + matchers: + - type: word + words: + - "ok" +` + require.NoError(t, os.WriteFile(filepath.Join(tplDir, "tpl.yaml"), []byte(tpl), 0o600)) + + ne, err := nuclei.NewNucleiEngineCtx( + context.Background(), + nuclei.DisableUpdateCheck(), + nuclei.WithTemplatesOrWorkflows(nuclei.TemplateSources{Templates: []string{tplDir}}), + ) + require.NoError(t, err) + defer ne.Close() + ne.Options().RestrictLocalNetworkAccess = false + ne.LoadTargets([]string{srv.URL}, false) + + var first, second atomic.Int32 + require.NoError(t, ne.ExecuteCallbackWithCtx(context.Background(), func(*output.ResultEvent) { first.Add(1) })) + require.Equal(t, int32(1), first.Load(), "first run result") + require.NoError(t, ne.ExecuteCallbackWithCtx(context.Background(), func(*output.ResultEvent) { second.Add(1) })) + require.Equal(t, int32(1), second.Load(), "second run result") + require.Equal(t, int32(1), first.Load(), "the first run's callback must not receive the second run's results") +} + +// A callback registered on the engine with WithResultCallback keeps receiving +// the results of every execution. +func TestExecuteCallbackWithCtxKeepsEngineCallback(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "ok") + })) + defer srv.Close() + + tplDir := t.TempDir() + tpl := `id: callback-engine +info: + name: Callback engine + author: test + severity: info +http: + - method: GET + path: + - "{{BaseURL}}/" + matchers: + - type: word + words: + - "ok" +` + require.NoError(t, os.WriteFile(filepath.Join(tplDir, "tpl.yaml"), []byte(tpl), 0o600)) + + var engineCalls, runCalls atomic.Int32 + ne, err := nuclei.NewNucleiEngineCtx( + context.Background(), + nuclei.DisableUpdateCheck(), + nuclei.WithTemplatesOrWorkflows(nuclei.TemplateSources{Templates: []string{tplDir}}), + nuclei.WithResultCallback(func(*output.ResultEvent) { engineCalls.Add(1) }), + ) + require.NoError(t, err) + defer ne.Close() + ne.Options().RestrictLocalNetworkAccess = false + ne.LoadTargets([]string{srv.URL}, false) + + for i := 1; i <= 2; i++ { + require.NoError(t, ne.ExecuteCallbackWithCtx(context.Background(), func(*output.ResultEvent) { runCalls.Add(1) })) + require.Equal(t, int32(i), engineCalls.Load(), "engine callback after run %d", i) + require.Equal(t, int32(i), runCalls.Load(), "per-run callback after run %d", i) + } +} From 46d909636d1f2599f722b4b4a64830db87797b29 Mon Sep 17 00:00:00 2001 From: simple-minded <191973953+hakarrr@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:07:08 +0530 Subject: [PATCH 3/3] test(sdk): check the fmt.Fprint error return in the callback tests Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/callbacks_reuse_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/callbacks_reuse_test.go b/lib/callbacks_reuse_test.go index 31572107bd..3500c92b49 100644 --- a/lib/callbacks_reuse_test.go +++ b/lib/callbacks_reuse_test.go @@ -19,7 +19,7 @@ import ( // passed to that call. func TestExecuteCallbackWithCtxDoesNotReplayEarlierCallbacks(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "ok") + _, _ = fmt.Fprint(w, "ok") })) defer srv.Close() @@ -62,7 +62,7 @@ http: // the results of every execution. func TestExecuteCallbackWithCtxKeepsEngineCallback(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "ok") + _, _ = fmt.Fprint(w, "ok") })) defer srv.Close()