Skip to content

Conversation

@yaron12n
Copy link

@yaron12n yaron12n commented Jan 23, 2026

Proposed changes

Adding API for getting mapping of cluster id template ids.
logs are published with templateID (cluster ID)
i'd like to know which templates refer to the exectued request

Proof

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
    • SDK adds public methods to fetch template IDs for a specific cluster and to retrieve all cluster-to-template mappings.
    • Clustering now records and returns mappings of cluster IDs to original template IDs.
    • Introduces a thread-safe cluster-mappings type with safe read, copy, and retrieval operations for consumers.

✏️ Tip: You can customize this high-level summary in your review settings.

@auto-assign auto-assign bot requested a review from dogancanbakir January 23, 2026 17:07
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 23, 2026

Walkthrough

Adds generation and safe storage of cluster→template-ID mappings during template clustering, exposes those mappings on ExecutorOptions, and provides SDK methods to read a cluster's template IDs or retrieve all mappings.

Changes

Cohort / File(s) Summary
SDK API Additions
lib/sdk.go
Adds GetClusterTemplateIDs(clusterID string) []string and GetAllClusterMappings() map[string][]string on NucleiEngine to expose cluster mappings.
Cluster Mapping Type & Helpers
pkg/templates/types/cluster_mappings.go
New ClusterMappingsMap type with NewClusterMappingsMap, Get, GetAll, and Copy to provide safe read access and deep-copy semantics.
Cluster Mapping Generation
pkg/templates/cluster.go
ClusterTemplates signature changed to return a third value map[string][]string; builds and returns mappings (clusterID → template IDs) alongside templates and count.
ExecutorOptions Storage
pkg/protocols/protocols.go, pkg/core/execute_options.go
Adds ClusterMappings *templateTypes.ClusterMappingsMap to ExecutorOptions; execution path wraps returned mappings with NewClusterMappingsMap and assigns to executerOpts.ClusterMappings.
Call Site Adjustments
pkg/protocols/common/automaticscan/util.go, pkg/templates/workflows.go
Updated calls to ClusterTemplates to accept the new third return value; some call sites discard the extra return where unused.

Sequence Diagram

sequenceDiagram
    participant SDK as SDK User
    participant Engine as NucleiEngine
    participant Core as Executor/Core
    participant Templates as Template Clustering
    participant Storage as ClusterMappingsMap

    SDK->>Engine: GetClusterTemplateIDs(clusterID)
    Engine->>Storage: Get(clusterID)
    Storage-->>Engine: []string
    Engine-->>SDK: []string

    Core->>Templates: ClusterTemplates(templatesList, options)
    Templates->>Templates: build clusters and mappings
    Templates-->>Core: templates, count, mappings
    Core->>Storage: NewClusterMappingsMap(mappings)
    Core->>Core: executerOpts.ClusterMappings = ...
    
    SDK->>Engine: GetAllClusterMappings()
    Engine->>Storage: GetAll()
    Storage-->>Engine: map[clusterID][]string
    Engine-->>SDK: map[clusterID][]string
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop through code with nimble feet,

Clusters mapped and records neat,
IDs tucked safe in a cozy map,
SDK calls — a cheerful snap,
A rabbit's nibble on every feat 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: exposing cluster IDs mapping to template IDs through new SDK methods and supporting infrastructure.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/protocols/protocols.go (1)

274-316: Copy() method does not copy the ClusterMappings field.

The ClusterMappings field (which stores cluster ID to template IDs mapping during execution) is not included in the Copy() method. All other fields are copied, but this field is missing. This could cause issues if copied ExecutorOptions are used in contexts expecting cluster mappings information, as the field will be nil. Add the field to the copy assignment (either share the reference with copy.ClusterMappings = e.ClusterMappings or copy the map data appropriately).

🤖 Fix all issues with AI agents
In `@pkg/protocols/protocols.go`:
- Around line 150-151: Change the ClusterMappings field type to
*templateTypes.ClusterMappingsMap and ensure the templates types package is
imported with the templateTypes alias; then update the Copy() method on the same
struct to deep-copy or clone ClusterMappings into the returned object (preserve
nil handling) and update ApplyNewEngineOptions() to copy/assign the
ClusterMappings from the source options to the target when applying new engine
options so cluster mappings are preserved; reference the ClusterMappings field,
Copy() method, and ApplyNewEngineOptions() method when making these edits.
🧹 Nitpick comments (5)
pkg/templates/types/cluster_mappings.go (3)

4-6: Consider making the Map field unexported for encapsulation.

The Map field is exported, which allows direct mutation and bypasses the accessor methods (Get, GetAll). This could lead to unintended modifications. If external read access is intentional, consider providing a read-only interface instead.

Suggested encapsulation improvement
 // ClusterMappingsMap wraps cluster ID to template IDs mapping
 type ClusterMappingsMap struct {
-	Map map[string][]string
+	m map[string][]string
 }

Then update the constructor and methods to use c.m instead of c.Map.


14-16: Get returns the internal slice directly, allowing external mutation.

Unlike GetAll which deep-copies slices, Get returns the internal slice reference. Callers can mutate the returned slice, affecting the internal state.

Return a copy for consistency with GetAll
 // Get returns the template IDs for a given cluster ID
 func (c *ClusterMappingsMap) Get(clusterID string) ([]string, bool) {
 	v, ok := c.Map[clusterID]
+	if !ok {
+		return nil, false
+	}
-	return v, ok
+	return append([]string{}, v...), true
 }

14-17: Add nil receiver/map guards to prevent panics.

Both Get and GetAll will panic if called on a nil receiver or if the internal map is nil. Consider adding defensive checks.

Add nil guards
 // Get returns the template IDs for a given cluster ID
 func (c *ClusterMappingsMap) Get(clusterID string) ([]string, bool) {
+	if c == nil || c.Map == nil {
+		return nil, false
+	}
 	v, ok := c.Map[clusterID]
 	return v, ok
 }

 // GetAll returns a copy of the entire map
 func (c *ClusterMappingsMap) GetAll() map[string][]string {
+	if c == nil || c.Map == nil {
+		return nil
+	}
 	result := make(map[string][]string, len(c.Map))
 	for k, v := range c.Map {
 		result[k] = append([]string{}, v...)
 	}
 	return result
 }

Also applies to: 20-25

lib/sdk.go (1)

364-373: Consider returning a defensive copy to prevent external mutation.

If ClusterMappings.Get() returns the internal slice directly (as implemented in cluster_mappings.go), callers of this SDK method could mutate the internal state. For a public SDK API, returning a copy would be safer.

Return a defensive copy
 func (e *NucleiEngine) GetClusterTemplateIDs(clusterID string) []string {
 	if e.executerOpts == nil || e.executerOpts.ClusterMappings == nil {
 		return nil
 	}
 	templateIDs, ok := e.executerOpts.ClusterMappings.Get(clusterID)
 	if !ok {
 		return nil
 	}
-	return templateIDs
+	return append([]string{}, templateIDs...)
 }

Alternatively, fix this at the ClusterMappingsMap.Get() level to ensure consistency.

pkg/core/execute_options.go (1)

44-49: Comment mentions "thread-safe" but ClusterMappingsMap lacks synchronization.

The comment on line 46 states "thread-safe" but ClusterMappingsMap doesn't include any synchronization primitives (mutex, RWMutex, sync.Map). If the mappings are only written once during initialization and then read-only, this is fine for concurrent reads, but the comment could be clarified.

Clarify the comment
-		// Store cluster mappings in executerOpts for SDK access (thread-safe)
+		// Store cluster mappings in executerOpts for SDK access (read-only after initialization)

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

🤖 Fix all issues with AI agents
In `@pkg/templates/types/cluster_mappings.go`:
- Around line 14-17: The Get method on ClusterMappingsMap can panic when called
on a nil receiver or when c.Map is nil; update ClusterMappingsMap.Get to first
check if c == nil || c.Map == nil and return nil, false in that case so callers
get a safe, consistent nil-safe behavior (matching how Copy() handles nil
receivers); modify the function body of ClusterMappingsMap.Get to perform these
nil checks before accessing c.Map[clusterID].
- Around line 20-26: GetAll currently panics when called on a nil receiver or
when c.Map is nil; add a nil receiver check at the start of
ClusterMappingsMap.GetAll (e.g., if c == nil || c.Map == nil) and return an
empty map[string][]string to match the behavior of Copy(), then proceed to
deep-copy entries as before; reference the GetAll method on the
ClusterMappingsMap type and mirror the nil-safe behavior used in Copy().
🧹 Nitpick comments (1)
pkg/templates/types/cluster_mappings.go (1)

8-11: Constructor performs shallow copy of the input map.

NewClusterMappingsMap assigns the map directly without deep copying. If the caller mutates the original map after construction, it will affect the internal state of ClusterMappingsMap. Consider whether this is intentional or if a deep copy is needed for encapsulation.

♻️ Proposed fix for defensive deep copy
 func NewClusterMappingsMap(m map[string][]string) *ClusterMappingsMap {
-	return &ClusterMappingsMap{Map: m}
+	if m == nil {
+		return &ClusterMappingsMap{Map: nil}
+	}
+	copied := make(map[string][]string, len(m))
+	for k, v := range m {
+		copied[k] = append([]string{}, v...)
+	}
+	return &ClusterMappingsMap{Map: copied}
 }

@yaron12n yaron12n closed this Jan 23, 2026
@yaron12n yaron12n reopened this Jan 23, 2026
@dogancanbakir
Copy link
Member

@yaron12n Curious, is there a use case for this?

@yaron12n
Copy link
Author

@dogancanbakir
in WriteStoreDebugData i get sometimes cluster-%s template id
and i want to wire it up after execution for logging purposes to see which templates ids are related to this request/response

@yaron12n
Copy link
Author

@Mzack9999 @dogancanbakir Can you CR this? 🙏

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.

2 participants