Skip to content

Comments

fix(lib): scans didn't stop on ctx cancellation#6310

Merged
dwisiswant0 merged 3 commits intodevfrom
dwisiswant0/fix/lib/scans-did-not-stop-immediately-on-context-cancellation
Jul 8, 2025
Merged

fix(lib): scans didn't stop on ctx cancellation#6310
dwisiswant0 merged 3 commits intodevfrom
dwisiswant0/fix/lib/scans-did-not-stop-immediately-on-context-cancellation

Conversation

@dwisiswant0
Copy link
Member

@dwisiswant0 dwisiswant0 commented Jul 8, 2025

Proposed changes

  • add a ctx field to NucleiEngine struct to store the context.
  • use stored context (respect) from NucleiEngine instance in ExecuteWithCallback method.
  • update ExecuteCallbackWithCtx method to make ExecuteScanWithOpts run in a goroutine and wait for either:
    • the scan to complete, or
    • the context to be canceled.

Fixes #6298

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • New Features

    • Added support for context-aware and cancellation-enabled scan execution, allowing scans to be stopped gracefully if needed.
  • Bug Fixes

    • Improved synchronization and control flow during scan execution for more reliable operation.
  • Tests

    • Introduced a new test to verify correct handling of scan cancellation via context.

Signed-off-by: Dwi Siswanto <git@dw1.io>
@dwisiswant0 dwisiswant0 requested a review from Mzack9999 July 8, 2025 10:31
@auto-assign auto-assign bot requested a review from dogancanbakir July 8, 2025 10:31
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 8, 2025

Walkthrough

The changes introduce context-aware cancellation to the NucleiEngine by adding a context field, updating execution methods to respect context cancellation, and synchronizing scan execution with goroutines and wait groups. A new test verifies that scans are properly interrupted when the context is canceled.

Changes

File(s) Change Summary
lib/sdk.go Added context field to NucleiEngine, updated execution methods for context cancellation, refactored execution logic with goroutines and wait groups, added helper function for wait group channel.
lib/sdk_test.go Added test TestContextCancelNucleiEngine to verify scan interruption on context cancellation.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant NucleiEngine
    participant ScanGoroutine
    participant Context

    Caller->>NucleiEngine: ExecuteCallbackWithCtx(ctx, callback)
    NucleiEngine->>ScanGoroutine: Start scan in goroutine
    NucleiEngine->>Context: Wait for ctx.Done()
    alt Context cancelled
        Context-->>NucleiEngine: ctx.Done()
        NucleiEngine->>Caller: Return context.Canceled error
    else Scan completes
        ScanGoroutine-->>NucleiEngine: Scan finished
        NucleiEngine->>Caller: Return scan result
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Immediate stop of scanning when the cancel() function is invoked (#6298)
Context cancellation support in scan execution (#6298)
Test verifying scan interruption on context cancel (#6298)

Suggested reviewers

  • dogancanbakir

Poem

In the warren where the engines run,
A context whisper says, "Be done!"
Now scans will halt, not blindly go—
When cancellation tells them so.
With tests in tow, this rabbit grins,
For context care, the code now wins!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 177b5f7 and 8173a36.

📒 Files selected for processing (1)
  • lib/sdk.go (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/sdk.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Tests (windows-latest)
  • GitHub Check: Tests (ubuntu-latest)
  • GitHub Check: Tests (macOS-latest)
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
lib/sdk_test.go (1)

29-29: Consider using a test server instead of external URL.

The test depends on an external URL http://honey.scanme.sh which could make tests flaky if the service is unavailable. Consider using a local test server or mocking the target.

Would you like me to help create a local test server setup to make the test more reliable and self-contained?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1375495 and e288c8f.

📒 Files selected for processing (2)
  • lib/sdk.go (6 hunks)
  • lib/sdk_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
lib/sdk.go (1)
Learnt from: dogancanbakir
PR: projectdiscovery/nuclei#6059
File: pkg/authprovider/file.go:144-145
Timestamp: 2025-02-19T13:21:35.933Z
Learning: In the Nuclei project, when checking auth strategies returned from LookupAddr, use len() check instead of nil check as the function returns an empty slice for no matches.
🧬 Code Graph Analysis (2)
lib/sdk_test.go (2)
lib/sdk.go (1)
  • NewNucleiEngineCtx (306-322)
lib/config.go (4)
  • WithTemplateFilters (64-95)
  • TemplateFilters (48-60)
  • EnableStatsWithOpts (228-242)
  • StatsOptions (220-224)
lib/sdk.go (2)
pkg/protocols/common/contextargs/contextargs.go (1)
  • Context (22-33)
pkg/output/output.go (1)
  • ResultEvent (146-221)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Tests (windows-latest)
  • GitHub Check: Tests (macOS-latest)
🔇 Additional comments (3)
lib/sdk.go (3)

251-254: Good variable naming to avoid shadowing.

Renaming the loop variable from callback to cb prevents shadowing of the parameter name.


283-288: Good backward compatibility implementation.

The method correctly uses the stored context if available, falling back to context.Background() for compatibility.


329-337: Well-implemented helper function for WaitGroup to channel conversion.

The wait function correctly converts a WaitGroup into a channel that can be used with select statements. The channel is properly closed when the WaitGroup completes.

dwisiswant0 and others added 2 commits July 8, 2025 17:42
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Dwi Siswanto <git@dw1.io>
Copy link
Member

@Mzack9999 Mzack9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! Minor change on return error on scan when SDK is used

@dwisiswant0 dwisiswant0 merged commit 7e2ec68 into dev Jul 8, 2025
22 checks passed
@dwisiswant0 dwisiswant0 deleted the dwisiswant0/fix/lib/scans-did-not-stop-immediately-on-context-cancellation branch July 8, 2025 18:04
Ice3man543 pushed a commit that referenced this pull request Aug 2, 2025
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Scans didn't stop on context cancellation

2 participants