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

--screenshot-filter option #266

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func init() {
scanCmd.PersistentFlags().BoolVar(&opts.Scan.SaveContent, "save-content", false, "Save content from network requests to the configured writers. WARNING: This flag has the potential to make your storage explode in size")
scanCmd.PersistentFlags().BoolVar(&opts.Scan.SkipHTML, "skip-html", false, "Don't include the first request's HTML response when writing results")
scanCmd.PersistentFlags().BoolVar(&opts.Scan.ScreenshotToWriter, "write-screenshots", false, "Store screenshots with writers in addition to filesystem storage")
scanCmd.PersistentFlags().IntSliceVar(&opts.Scan.HttpCodeFilter, "http-code-filter", []int{}, "Http response codes to screenshot. this is a filter (by default all codes are screenshotted)")

// Chrome options
scanCmd.PersistentFlags().StringVar(&opts.Chrome.Path, "chrome-path", "", "The path to a Google Chrome binary to use (downloads a platform-appropriate binary by default)")
Expand Down
7 changes: 7 additions & 0 deletions pkg/runner/drivers/chromedp.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ func (run *Chromedp) Witness(target string, thisRunner *runner.Runner) (*models.
time.Sleep(time.Duration(run.options.Scan.Delay) * time.Second)
}

// check if the preflight returned a code to process.
// an empty slice implies no filtering
if (len(run.options.Scan.HttpCodeFilter) > 0) &&
!islazy.SliceHasInt(run.options.Scan.HttpCodeFilter, result.ResponseCode) {
return nil, fmt.Errorf("received HTTP status code (%d), which is not among the allowed screenshot response codes.", result.ResponseCode)
}

// run any javascript we have
if run.options.Scan.JavaScript != "" {
if err := chromedp.Run(navigationCtx, chromedp.Evaluate(run.options.Scan.JavaScript, nil)); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/runner/drivers/go-rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ func (run *Gorod) Witness(target string, runner *runner.Runner) (*models.Result,
time.Sleep(time.Duration(run.options.Scan.Delay) * time.Second)
}

// check if the preflight returned a code to process.
// an empty slice implies no filtering
if (len(run.options.Scan.HttpCodeFilter) > 0) &&
!islazy.SliceHasInt(run.options.Scan.HttpCodeFilter, result.ResponseCode) {
return nil, fmt.Errorf("received HTTP status code (%d), which is not among the allowed screenshot response codes.", result.ResponseCode)
}

// run any javascript we have
if run.options.Scan.JavaScript != "" {
_, err := page.Eval(run.options.Scan.JavaScript)
Expand Down
4 changes: 4 additions & 0 deletions pkg/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ type Scan struct {
// Save content stores content from network requests (warning) this
// could make written artefacts huge
SaveContent bool
// HttpCodeFilter are http response codes to screenshot. this is a filter.
// by default all codes are screenshotted
HttpCodeFilter []int
}

// NewDefaultOptions returns Options with some default values
Expand All @@ -105,6 +108,7 @@ func NewDefaultOptions() *Options {
Timeout: 60,
UriFilter: []string{"http", "https"},
ScreenshotFormat: "jpeg",
HttpCodeFilter: []int{},
},
Logging: Logging{
Debug: true,
Expand Down