Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/protocols/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicVa
Stderr: buff,
}
}
if err != nil {
gologger.Warning().Msgf("[%s] Could not execute code: %s", request.options.TemplateID, err)
}
Comment on lines +280 to +282
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Propagate execution failures into the event payload, not only logs.

This currently logs the failure but does not set data["error"], so downstream result/event consumers still can’t reliably distinguish execution failure from non-match.

💡 Proposed fix
-	if err != nil {
-		gologger.Warning().Msgf("[%s] Could not execute code: %s", request.options.TemplateID, err)
-	}
+	execError := ""
+	if err != nil {
+		execError = err.Error()
+		gologger.Warning().Msgf("[%s] Could not execute code: %s", request.options.TemplateID, err)
+	}
@@
 	data["template-path"] = request.options.TemplatePath
 	data["template-id"] = request.options.TemplateID
 	data["template-info"] = request.options.TemplateInfo
+	if execError != "" {
+		data["error"] = execError
+	}
 	if gOutput.Stderr.Len() > 0 {
 		data["stderr"] = fmtStdout(gOutput.Stderr.String())
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/protocols/code/code.go` around lines 280 - 282, The handler currently
only logs execution errors but doesn't include them in the event payload; update
the error branch in the execute code path (the block checking if err != nil in
pkg/protocols/code/code.go around request.options.TemplateID) to set the payload
map entry data["error"] (or the existing result/event payload map variable) to
the error information (e.g., err.Error() or the error object) so downstream
consumers can detect failures; keep the existing gologger.Warning() call but
ensure data["error"] is populated whenever err != nil.

gologger.Verbose().Msgf("[%s] Executed code on local machine %v", request.options.TemplateID, input.MetaInput.Input)

if vardump.EnableVarDump {
Expand Down Expand Up @@ -352,6 +355,9 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicVa
if request.options.Options.Debug || request.options.Options.DebugResponse {
gologger.Debug().Msg(msg)
gologger.Print().Msgf("%s\n\n", responsehighlighter.Highlight(event.OperatorsResult, dataOutputString, request.options.Options.NoColor, false))
if gOutput.Stderr.Len() > 0 {
gologger.Debug().Msgf("[%s] Code Stderr:\n%s", request.options.TemplateID, gOutput.Stderr.String())
}
}
if request.options.Options.StoreResponse {
request.options.Output.WriteStoreDebugData(input.MetaInput.Input, request.options.TemplateID, request.Type().String(), fmt.Sprintf("%s\n%s", msg, dataOutputString))
Expand Down
65 changes: 53 additions & 12 deletions pkg/protocols/code/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,68 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
)

func TestCodeProtocol(t *testing.T) {
options := testutils.DefaultOptions
func newTestRequest(t *testing.T, engine []string, source string) (*Request, *testutils.TemplateInfo) {
t.Helper()
info := &testutils.TemplateInfo{
ID: "testing-code",
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
}
return &Request{Engine: engine, Source: source}, info
}

func executeRequest(t *testing.T, request *Request, info *testutils.TemplateInfo) (output.InternalEvent, error) {
t.Helper()
options := testutils.DefaultOptions
testutils.Init(options)
templateID := "testing-code"
request := &Request{
Engine: []string{"sh"},
Source: "echo test",
}
executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
ID: templateID,
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
})

executerOpts := testutils.NewMockExecuterOptions(options, info)
err := request.Compile(executerOpts)
require.Nil(t, err, "could not compile code request")

var gotEvent output.InternalEvent
ctxArgs := contextargs.NewWithInput(context.Background(), "")
err = request.ExecuteWithResults(ctxArgs, nil, nil, func(event *output.InternalWrappedEvent) {
execErr := request.ExecuteWithResults(ctxArgs, nil, nil, func(event *output.InternalWrappedEvent) {
gotEvent = event.InternalEvent
})
return gotEvent, execErr
}

func TestCodeProtocol(t *testing.T) {
request, info := newTestRequest(t, []string{"sh"}, "echo test")
gotEvent, err := executeRequest(t, request, info)
require.Nil(t, err, "could not run code request")
require.NotEmpty(t, gotEvent, "could not get event items")
}

func TestCodeProtocolSuccessResponse(t *testing.T) {
request, info := newTestRequest(t, []string{"sh"}, "echo hello-world")
gotEvent, err := executeRequest(t, request, info)
require.Nil(t, err)
require.Equal(t, "hello-world", gotEvent["response"])
_, hasStderr := gotEvent["stderr"]
require.False(t, hasStderr, "stderr should not be present on success")
}

func TestCodeProtocolStderrCapture(t *testing.T) {
request, info := newTestRequest(t, []string{"sh"}, "echo error-output >&2")
gotEvent, err := executeRequest(t, request, info)
require.Nil(t, err)
require.Contains(t, gotEvent["stderr"], "error-output")
}

func TestCodeProtocolFailingScript(t *testing.T) {
request, info := newTestRequest(t, []string{"sh"}, "echo fail-message >&2; exit 1")
gotEvent, err := executeRequest(t, request, info)
require.Nil(t, err, "ExecuteWithResults should not return an error for non-zero exit codes")
require.NotEmpty(t, gotEvent, "event should still be generated on script failure")
require.Contains(t, gotEvent["stderr"], "fail-message")
require.Empty(t, gotEvent["response"], "stdout should be empty when script only writes to stderr")
}

func TestCodeProtocolMixedOutput(t *testing.T) {
request, info := newTestRequest(t, []string{"sh"}, "echo stdout-data; echo stderr-data >&2")
gotEvent, err := executeRequest(t, request, info)
require.Nil(t, err)
require.Equal(t, "stdout-data", gotEvent["response"])
require.Contains(t, gotEvent["stderr"], "stderr-data")
}
Loading