Skip to content

Commit

Permalink
join errors in LogError func
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir committed Nov 17, 2023
1 parent 0d923ec commit be8ad40
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 26 deletions.
1 change: 1 addition & 0 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func (w *StandardWriter) WriteFailure(wrappedEvent *InternalWrappedEvent) error
Timestamp: time.Now(),
//FIXME: this is workaround to encode the template when no results were found
TemplateEncoded: w.encodeTemplate(types.ToString(event["template-path"])),
Error: types.ToString(event["error"]),
}
return w.Write(data)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Timestamp: time.Now(),
MatcherStatus: true,
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/dns/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["raw"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/file/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Response: types.ToString(wrapped.InternalEvent["raw"]),
Timestamp: time.Now(),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
1 change: 1 addition & 0 deletions pkg/protocols/headless/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["data"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
1 change: 1 addition & 0 deletions pkg/protocols/http/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Response: request.truncateResponse(wrapped.InternalEvent["response"]),
CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
31 changes: 15 additions & 16 deletions pkg/protocols/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,26 +655,25 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ
request.options.Output.Request(request.options.TemplatePath, formedURL, request.Type().String(), err)
request.options.Progress.IncrementErrorsBy(1)

// If we have interactsh markers and request times out, still send
// In case of interactsh markers and request times out, still send
// a callback event so in case we receive an interaction, correlation is possible.
if hasInteractMatchers {
outputEvent := request.responseToDSLMap(&http.Response{}, input.MetaInput.Input, formedURL, tostring.UnsafeToString(dumpedRequest), "", "", "", 0, generatedRequest.meta)
if i := strings.LastIndex(hostname, ":"); i != -1 {
hostname = hostname[:i]
}
// Also, to log failed use-cases.
outputEvent := request.responseToDSLMap(&http.Response{}, input.MetaInput.Input, formedURL, tostring.UnsafeToString(dumpedRequest), "", "", "", 0, generatedRequest.meta)
if i := strings.LastIndex(hostname, ":"); i != -1 {
hostname = hostname[:i]
}

if input.MetaInput.CustomIP != "" {
outputEvent["ip"] = input.MetaInput.CustomIP
} else {
outputEvent["ip"] = httpclientpool.Dialer.GetDialedIP(hostname)
}
if input.MetaInput.CustomIP != "" {
outputEvent["ip"] = input.MetaInput.CustomIP
} else {
outputEvent["ip"] = httpclientpool.Dialer.GetDialedIP(hostname)
}

event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
if request.CompiledOperators != nil {
event.InternalEvent = outputEvent
}
callback(event)
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
if request.CompiledOperators != nil {
event.InternalEvent = outputEvent
}
callback(event)
return err
}
defer func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/javascript/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Response: types.ToString(wrapped.InternalEvent["response"]),
IP: types.ToString(wrapped.InternalEvent["ip"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/network/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["data"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
1 change: 1 addition & 0 deletions pkg/protocols/offlinehttp/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["raw"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
1 change: 1 addition & 0 deletions pkg/protocols/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
MatcherStatus: true,
IP: types.ToString(wrapped.InternalEvent["ip"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
1 change: 1 addition & 0 deletions pkg/protocols/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["response"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
1 change: 1 addition & 0 deletions pkg/protocols/whois/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["response"]),
TemplateEncoded: request.options.EncodeTemplate(),
Error: types.ToString(wrapped.InternalEvent["error"]),
}
return data
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/scan/scan_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ func NewScanContext(input *contextargs.Context) *ScanContext {
}

func (s *ScanContext) GenerateResult() []*output.ResultEvent {
errorMessage := joinErrors(s.errors)
results := aggregateResults(s.events)
for _, result := range results {
result.Error = errorMessage
}
return results
return aggregateResults(s.events)
}

func aggregateResults(events []*output.InternalWrappedEvent) []*output.ResultEvent {
Expand Down Expand Up @@ -59,5 +54,15 @@ func (s *ScanContext) LogError(err error) error {
s.OnError(err)
}
s.errors = append(s.errors, err)

errorMessage := joinErrors(s.errors)
results := aggregateResults(s.events)
for _, result := range results {
result.Error = errorMessage
}
for _, e := range s.events {
e.InternalEvent["error"] = errorMessage
}

return err
}
4 changes: 4 additions & 0 deletions pkg/templates/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func (e *ClusterExecuter) ExecuteWithResults(ctx *scan.ScanContext) ([]*output.R
}
}
})
if err != nil {
ctx.LogError(err)
}

if err != nil && e.options.HostErrorsCache != nil {
e.options.HostErrorsCache.MarkFailed(ctx.Input.MetaInput.Input, err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (m *MockOutputWriter) WriteFailure(wrappedEvent *output.InternalWrappedEven
Timestamp: time.Now(),
//FIXME: this is workaround to encode the template when no results were found
TemplateEncoded: m.encodeTemplate(types.ToString(event["template-path"])),
Error: types.ToString(event["error"]),
}
return m.Write(data)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/tmplexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) {

// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
func (e *TemplateExecuter) ExecuteWithResults(ctx *scan.ScanContext) ([]*output.ResultEvent, error) {
// scanCtx := scan.NewScanContext(input)
err := e.engine.ExecuteWithResults(ctx)
if err != nil && !e.options.Options.MatcherStatus {
return nil, err
}
ctx.LogError(err)
return ctx.GenerateResult(), err
}
2 changes: 2 additions & 0 deletions pkg/tmplexec/flow/flow_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ func (f *FlowExecutor) ExecuteWithResults(ctx *scan.ScanContext) error {
// pass flow and execute the js vm and handle errors
value, err := f.jsVM.RunProgram(f.program)
if err != nil {
ctx.LogError(err)

Check failure on line 187 in pkg/tmplexec/flow/flow_executor.go

View workflow job for this annotation

GitHub Actions / Lint Test

Error return value of `ctx.LogError` is not checked (errcheck)
return errorutil.NewWithErr(err).Msgf("failed to execute flow\n%v\n", f.options.Flow)
}
runtimeErr := f.GetRuntimeErrors()
if runtimeErr != nil {
ctx.LogError(runtimeErr)
return errorutil.NewWithErr(runtimeErr).Msgf("got following errors while executing flow")
}
// this is where final result is generated/created
Expand Down
1 change: 1 addition & 0 deletions pkg/tmplexec/generic/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (g *Generic) ExecuteWithResults(ctx *scan.ScanContext) error {
ctx.LogEvent(event)
})
if err != nil {
ctx.LogError(err)

Check failure on line 78 in pkg/tmplexec/generic/exec.go

View workflow job for this annotation

GitHub Actions / Lint Test

Error return value of `ctx.LogError` is not checked (errcheck)
if g.options.HostErrorsCache != nil {
g.options.HostErrorsCache.MarkFailed(ctx.Input.MetaInput.ID(), err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/tmplexec/multiproto/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (m *MultiProtocol) ExecuteWithResults(ctx *scan.ScanContext) error {
err := req.ExecuteWithResults(ctx.Input, output.InternalEvent(values), nil, multiProtoCallback)
// if error skip execution of next protocols
if err != nil {
ctx.LogError(err)

Check failure on line 96 in pkg/tmplexec/multiproto/multi.go

View workflow job for this annotation

GitHub Actions / Lint Test

Error return value of `ctx.LogError` is not checked (errcheck)
return err
}
}
Expand Down

0 comments on commit be8ad40

Please sign in to comment.