Skip to content

Commit

Permalink
(fix) dont write obviously failed requests, --log-scan-errors fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonjza committed Sep 17, 2024
1 parent 744ab91 commit 5780809
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pkg/runner/drivers/chromedp.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (run *Chromedp) Witness(target string, runner *runner.Runner) (*models.Resu
// run any javascript we have
if run.options.Scan.JavaScript != "" {
if err := chromedp.Run(navigationCtx, chromedp.Evaluate(run.options.Scan.JavaScript, nil)); err != nil {
logger.Error("failed to evaluate user-provided javascript", "err", err)
return nil, fmt.Errorf("failed to evaluate user-provided javascript: %w", err)
}
}

Expand All @@ -350,7 +350,9 @@ func (run *Chromedp) Witness(target string, runner *runner.Runner) (*models.Resu
cookies, err = storage.GetCookies().Do(ctx)
return err
})); err != nil {
logger.Error("could not get cookies", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get cookies", "err", err)
}
} else {
for _, cookie := range cookies {
result.Cookies = append(result.Cookies, models.Cookie{
Expand All @@ -372,13 +374,17 @@ func (run *Chromedp) Witness(target string, runner *runner.Runner) (*models.Resu

// grab the title
if err := chromedp.Run(navigationCtx, chromedp.Title(&result.Title)); err != nil {
logger.Error("could not get page title", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get page title", "err", err)
}
}

// get html
if !run.options.Scan.SkipHTML {
if err := chromedp.Run(navigationCtx, chromedp.OuterHTML(":root", &result.HTML, chromedp.ByQueryAll)); err != nil {
logger.Error("could not get page html", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get page html", "err", err)
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions pkg/runner/drivers/go-rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ func (run *Gorod) Witness(target string, runner *runner.Runner) (*models.Result,
body, err := proto.NetworkGetResponseBody{RequestID: e.RequestID}.Call(page)
if err != nil {
if run.options.Logging.LogScanErrors {
run.log.Error("could not get network request response body", "url", e.Response.URL, "err", err)
if run.options.Logging.LogScanErrors {
run.log.Error("could not get network request response body", "url", e.Response.URL, "err", err)
}
return
}
}
Expand Down Expand Up @@ -345,7 +347,9 @@ func (run *Gorod) Witness(target string, runner *runner.Runner) (*models.Result,
// get cookies
cookies, err := page.Cookies([]string{})
if err != nil {
logger.Error("could not get cookies", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get cookies", "err", err)
}
} else {
for _, cookie := range cookies {
result.Cookies = append(result.Cookies, models.Cookie{
Expand All @@ -368,15 +372,19 @@ func (run *Gorod) Witness(target string, runner *runner.Runner) (*models.Result,
// get and set the last results info before triggering the
info, err := page.Info()
if err != nil {
logger.Error("could not get page info", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get page info", "err", err)
}
} else {
result.Title = info.Title
}

if !run.options.Scan.SkipHTML {
html, err := page.HTML()
if err != nil {
logger.Error("could not get page html", "err", err)
if run.options.Logging.LogScanErrors {
logger.Error("could not get page html", "err", err)
}
} else {
result.HTML = html
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ func (run *Runner) Run() {
run.log.Error("failed to witness target", "target", target, "err", err)
}
} else {

// assume that status code 0 means there was no information, so
// don't send anything to writers.
if result.ResponseCode == 0 {
if run.options.Logging.LogScanErrors {
run.log.Error("failed to witness target, status code was 0", "target", target)
continue
}
}

if err := run.runWriters(result); err != nil {
run.log.Error("failed to write result for target", "target", target, "err", err)
}
Expand Down

0 comments on commit 5780809

Please sign in to comment.