Repository navigation
fix(sdk): scope ExecuteCallbackWithCtx callbacks to the call #8421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hakarrr
wants to merge
3
commits into
projectdiscovery:dev
Choose a base branch
from
hakarrr:hakarrr-patch-4
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late Interactsh results can miss the per-call callback of the execution that produced them.
ExecuteCallbackWithCtxremoves its per-call callbacks when it returns. The scan does not wait for Interactsh delivery, and the single-engine path has no request scope to hold the callbacks. The Interactsh poller stays active until the engine closes its client. An interaction that matches one of this execution's registrations during that window is written through the captured writer. That writer reads only the engine callbacks. The per-call callback does not receive the result. Without engine callbacks, the result goes to the default output.The base behavior delivered these late results to the per-call callback. This change removes that delivery.
Keep the per-call callbacks available for late interactions from the same execution until its Interactsh registrations are finished. Do not deliver results from later executions to them. The restore step must still prevent the replay that this change fixes. The lifetime rule is a design choice: a per-execution request scope can enforce it, but creating one for a single-engine call also makes the call wait for the cooldown when it registers interactions.