Skip to content
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

feat: add -enable-global-matchers flag #5857

Merged
merged 3 commits into from
Nov 27, 2024

Conversation

dwisiswant0
Copy link
Member

@dwisiswant0 dwisiswant0 commented Nov 26, 2024

Proposed changes

Close #5856

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

    • Introduced a new option, EnableGlobalMatchersTemplates, allowing users to enable loading of global matchers templates for enhanced configurability.
  • Bug Fixes

    • Updated comments for clarity regarding the EnableSelfContainedTemplates option.
  • Documentation

    • Improved comments in code to enhance understanding of template processing options.

Copy link

coderabbitai bot commented Nov 26, 2024

Walkthrough

The changes introduced in this pull request add a new command-line flag, EnableGlobalMatchersTemplates, to enhance the configurability of the Nuclei tool. This flag allows users to control the execution of global matchers templates, which are disabled by default. The changes also involve updates to method logic and the Options struct to accommodate this new functionality, ensuring that template processing aligns with the user's explicit preferences.

Changes

File Change Summary
cmd/nuclei/main.go Added new boolean option EnableGlobalMatchersTemplates to command-line flags; updated comments.
pkg/templates/compile.go Modified isGlobalMatchersEnabled method to check for EnableGlobalMatchersTemplates; updated comments.
pkg/types/types.go Updated comment for EnableSelfContainedTemplates; added new field EnableGlobalMatchersTemplates.
lib/config.go Introduced new function EnableGlobalMatchersTemplates to modify NucleiEngine options.

Assessment against linked issues

Objective Addressed Explanation
Add -enable-global-matchers flag (#[5856])
Control execution of global matchers templates (#[5856])
Ensure default behavior is disabled for global matchers (#[5856])

Poem

🐇 In the garden of code, a new flag takes flight,
To guide global matchers, from morning till night.
With options now clearer, we hop with delight,
For templates will dance, when the user says "right!"
So let’s celebrate changes, with joy and with cheer,
A configurable Nuclei, for all to revere! 🌼


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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

@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: 1

🧹 Outside diff range and nitpick comments (4)
pkg/templates/compile.go (2)

Line range hint 85-96: Document the early return behavior and add error handling

The early return with nil values when global matchers are enabled could be confusing to callers. Consider:

  1. Adding documentation to explain this behavior
  2. Adding error handling for AddOperator

Apply this diff to improve the code:

 if template.isGlobalMatchersEnabled(options) {
     item := &globalmatchers.Item{
         TemplateID:   template.ID,
         TemplatePath: filePath,
         TemplateInfo: template.Info,
     }
     for _, request := range template.RequestsHTTP {
         item.Operators = append(item.Operators, request.CompiledOperators)
     }
-    options.GlobalMatchers.AddOperator(item)
-    return nil, nil
+    if err := options.GlobalMatchers.AddOperator(item); err != nil {
+        return nil, fmt.Errorf("failed to add global matcher: %w", err)
+    }
+    // Return nil template when global matchers are enabled as they are handled separately
+    return nil, nil
 }

122-126: Extend support for global matchers to all protocols

The current implementation only checks HTTP requests for global matchers, potentially missing templates that use other protocols. This limitation is noted in the TODO comment.

Consider:

  1. Adding support for other protocols like DNS, Network, etc.
  2. Documenting the current HTTP-only limitation in the method's documentation.
  3. Creating a tracking issue for implementing global matchers support in other protocols.

Would you like me to help create a GitHub issue to track this enhancement?

pkg/types/types.go (1)

388-389: Add documentation for the new flag.

The new EnableGlobalMatchersTemplates field is well-placed and follows the existing pattern. However, it would benefit from a documentation comment explaining its purpose and default behavior.

Add a descriptive comment:

+	// EnableGlobalMatchersTemplates enables processing of global-matchers templates.
+	// When disabled (default), global matchers templates will not be executed.
 	EnableGlobalMatchersTemplates bool
cmd/nuclei/main.go (1)

267-267: Consider adding usage examples in the help text.

The flag would benefit from having an example in the help text section to demonstrate its usage, similar to other flags.

Add an example like this to the help text:

Run nuclei with global matchers disabled:
+	$ nuclei -target example.com -no-global-matchers
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b2d4efe and db26ade.

📒 Files selected for processing (3)
  • cmd/nuclei/main.go (1 hunks)
  • pkg/templates/compile.go (2 hunks)
  • pkg/types/types.go (1 hunks)
🔇 Additional comments (3)
pkg/templates/compile.go (1)

122-126: Verify impact on existing templates

Let's verify how this change affects existing templates and identify potential breaking changes.

✅ Verification successful

Let me gather more information about this change to understand its full impact.


Based on the gathered information, I can now provide a final response:

No impact on existing templates - feature is opt-in

The EnableGlobalMatchersTemplates flag is a new opt-in feature that is disabled by default. The code change is safe because:

  1. No existing templates use global matchers (verified by searching the codebase)
  2. The feature must be explicitly enabled via the EnableGlobalMatchersTemplates flag
  3. The implementation follows the same pattern as other similar opt-in features like EnableSelfContainedTemplates and EnableFileTemplates
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for templates using global matchers and verify their protocol types

# Find templates with global matchers
echo "Templates with global matchers:"
rg -l "globalMatchers:\s*true" || echo "No templates found with global matchers"

# Check for non-HTTP protocols using global matchers
echo -e "\nNon-HTTP protocols using global matchers:"
rg -B5 -A5 "globalMatchers:\s*true" | rg "(dns|network|headless|ssl|websocket|whois|code|javascript):" -B10 || echo "No non-HTTP protocols found with global matchers"

Length of output: 510


Script:

#!/bin/bash
# Check for the usage of EnableGlobalMatchersTemplates flag
echo "Checking EnableGlobalMatchersTemplates usage:"
rg "EnableGlobalMatchersTemplates" -A 2 -B 2

# Check for any other global matcher related code
echo -e "\nChecking other global matcher related code:"
rg "globalMatchers" -A 2 -B 2

# Check the Options struct definition
echo -e "\nChecking Options struct definition:"
ast-grep --pattern 'type Options struct {
  $$$
  EnableGlobalMatchersTemplates $_
  $$$
}'

Length of output: 28706

pkg/types/types.go (1)

386-386: LGTM! Clear comment update.

The comment update for EnableSelfContainedTemplates improves clarity by correctly stating that it "enables" rather than "disables" processing.

cmd/nuclei/main.go (1)

267-267: LGTM! The flag implementation looks good.

The new -enable-global-matchers flag is properly integrated into the templates flag group with appropriate naming and description. The implementation follows the existing patterns in the codebase.

Let's verify the flag integration:

✅ Verification successful

The flag implementation is properly integrated and functional

The verification shows that:

  • The flag is properly defined in the types.go with appropriate documentation
  • It's correctly implemented in pkg/templates/compile.go where it controls the execution of global matchers through the isGlobalMatchersEnabled function
  • The implementation follows a clean pattern: if the flag is disabled, it returns false early, otherwise proceeds with checking global matchers in HTTP requests
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the flag integration and related code

# Check if the flag is properly integrated with the Options struct
rg -A 2 "EnableGlobalMatchersTemplates" 

# Check for any related template processing code that uses this flag
ast-grep --pattern 'func $_($$) {
  $$$
  EnableGlobalMatchersTemplates
  $$$
}'

Length of output: 543


Script:

#!/bin/bash
# Let's check the implementation in compile.go to understand how the flag is used
rg -B 5 -A 5 "EnableGlobalMatchersTemplates" pkg/templates/compile.go

# Also check if there are any global matchers related files/templates
fd "global-matchers" 

Length of output: 459

pkg/types/types.go Show resolved Hide resolved
Copy link
Member

@dogancanbakir dogancanbakir left a comment

Choose a reason for hiding this comment

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

LGTM -left a comment for minor suggestion + failing tests

pkg/templates/compile.go Outdated Show resolved Hide resolved
Copy link

@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: 1

🧹 Outside diff range and nitpick comments (2)
pkg/templates/compile.go (2)

115-116: Track TODO: Support for all protocols in global matchers

The documentation indicates that global matchers currently only support HTTP protocol. Let's ensure this limitation is tracked for future implementation.

Would you like me to create a GitHub issue to track the implementation of global matchers support for other protocols?


124-127: Add unit tests for the new flag

Please add unit tests to verify:

  1. Global matchers are disabled by default
  2. Global matchers are enabled only when the flag is set
  3. Behavior with nil Options

Would you like me to help generate the unit test cases?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between db26ade and 9c25086.

📒 Files selected for processing (1)
  • pkg/templates/compile.go (1 hunks)

pkg/templates/compile.go Show resolved Hide resolved
Copy link

@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: 0

🧹 Outside diff range and nitpick comments (1)
lib/config.go (1)

396-402: Enhance function documentation while implementation LGTM!

The implementation follows the established pattern for SDK options and correctly sets the flag. However, the documentation could be more comprehensive to help users make informed decisions.

Consider expanding the documentation to include:

-// EnableGlobalMatchersTemplates allows loading/executing global-matchers templates
+// EnableGlobalMatchersTemplates allows loading/executing global-matchers templates.
+// Global matchers templates are special templates that run against all targets and can be used
+// for tasks like passive scanning or gathering additional information. Enable this option only
+// when you specifically need these templates, as they may increase scan time and resource usage.
+//
+// Example usage:
+//   engine := nuclei.New(nuclei.EnableGlobalMatchersTemplates())
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9c25086 and f913314.

📒 Files selected for processing (1)
  • lib/config.go (1 hunks)

@ehsandeep ehsandeep merged commit 3a07fa9 into dev Nov 27, 2024
19 checks passed
@ehsandeep ehsandeep deleted the dwisiswant0/feat/add-enable-global-matchers-flag branch November 27, 2024 09:08
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.

[FEATURE] Add -enable-global-matchers flag
3 participants