From 19937c49297700185ce9469c67b20ccf81e74f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20=C3=81ngel=20Jimeno?= Date: Sat, 3 Oct 2020 09:45:07 +0200 Subject: [PATCH] pkg: handle gosimple linter findings (#322) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change is an attempt to handle gosimple linter finfings in order to make the code easier to follow. It includes the following changes: - use strings.Contains instead of strings.Index != -1 - use time.Since which is the standard library helper. See https://github.com/golang/go/blob/go1.15.2/src/time/time.go#L866-L867 - remove unneeded return statements at the end of methods - preallocate maps when their capacity is known - avoid underscoring values when they can be omitted - avoid fmt.Sprintf() calls when the only argument is already a string Signed-off-by: Miguel Ángel Jimeno --- pkg/ffuf/job.go | 17 +++++------------ pkg/ffuf/optionsparser.go | 12 ++++++------ pkg/ffuf/util.go | 2 +- pkg/filter/lines_test.go | 2 +- pkg/filter/regexp_test.go | 2 +- pkg/filter/size_test.go | 2 +- pkg/filter/status_test.go | 2 +- pkg/filter/words_test.go | 2 +- pkg/input/command.go | 5 +---- pkg/input/wordlist.go | 5 +---- pkg/output/file_csv.go | 7 ++----- pkg/output/stdout.go | 17 +++++++---------- pkg/runner/simple.go | 2 +- 13 files changed, 29 insertions(+), 48 deletions(-) diff --git a/pkg/ffuf/job.go b/pkg/ffuf/job.go index 79871d3..07bfc95 100644 --- a/pkg/ffuf/job.go +++ b/pkg/ffuf/job.go @@ -123,10 +123,7 @@ func (j *Job) Start() { } func (j *Job) jobsInQueue() bool { - if j.queuepos < len(j.queuejobs) { - return true - } - return false + return j.queuepos < len(j.queuejobs) } func (j *Job) prepareQueueJob() { @@ -192,14 +189,13 @@ func (j *Job) startExecution() { } wg.Wait() j.updateProgress() - return } func (j *Job) interruptMonitor() { sigChan := make(chan os.Signal, 2) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) go func() { - for _ = range sigChan { + for range sigChan { j.Error = "Caught keyboard interrupt (Ctrl-C)\n" j.Stop() } @@ -321,7 +317,6 @@ func (j *Job) runTask(input map[string][]byte, position int, retried bool) { if j.Config.Recursion && len(resp.GetRedirectLocation(false)) > 0 { j.handleRecursionJob(resp) } - return } //handleRecursionJob adds a new recursion job to the job queue if a new directory is found @@ -356,7 +351,7 @@ func (j *Job) CalibrateResponses() ([]Response, error) { results := make([]Response, 0) for _, input := range cInputs { - inputs := make(map[string][]byte, 0) + inputs := make(map[string][]byte, len(j.Config.InputProviders)) for _, v := range j.Config.InputProviders { inputs[v.Keyword] = []byte(input) } @@ -409,7 +404,7 @@ func (j *Job) CheckStop() { // Check for runtime of entire process if j.Config.MaxTime > 0 { - dur := time.Now().Sub(j.startTime) + dur := time.Since(j.startTime) runningSecs := int(dur / time.Second) if runningSecs >= j.Config.MaxTime { j.Error = "Maximum running time for entire process reached, exiting." @@ -419,7 +414,7 @@ func (j *Job) CheckStop() { // Check for runtime of current job if j.Config.MaxTimeJob > 0 { - dur := time.Now().Sub(j.startTimeJob) + dur := time.Since(j.startTimeJob) runningSecs := int(dur / time.Second) if runningSecs >= j.Config.MaxTimeJob { j.Error = "Maximum running time for this job reached, continuing with next job if one exists." @@ -433,11 +428,9 @@ func (j *Job) CheckStop() { func (j *Job) Stop() { j.Running = false j.Config.Cancel() - return } //Stop current, resume to next func (j *Job) Next() { j.RunningJob = false - return } diff --git a/pkg/ffuf/optionsparser.go b/pkg/ffuf/optionsparser.go index 438f803..1b15e1e 100644 --- a/pkg/ffuf/optionsparser.go +++ b/pkg/ffuf/optionsparser.go @@ -387,7 +387,7 @@ func ConfigFromOptions(parseOpts *ConfigOptions, ctx context.Context, cancel con // Do checks for recursion mode if parseOpts.HTTP.Recursion { if !strings.HasSuffix(conf.Url, "FUZZ") { - errmsg := fmt.Sprintf("When using -recursion the URL (-u) must end with FUZZ keyword.") + errmsg := "When using -recursion the URL (-u) must end with FUZZ keyword." errs.Add(fmt.Errorf(errmsg)) } } @@ -460,20 +460,20 @@ func parseRawRequest(parseOpts *ConfigOptions, conf *Config) error { func keywordPresent(keyword string, conf *Config) bool { //Search for keyword from HTTP method, URL and POST data too - if strings.Index(conf.Method, keyword) != -1 { + if strings.Contains(conf.Method, keyword) { return true } - if strings.Index(conf.Url, keyword) != -1 { + if strings.Contains(conf.Url, keyword) { return true } - if strings.Index(conf.Data, keyword) != -1 { + if strings.Contains(conf.Data, keyword) { return true } for k, v := range conf.Headers { - if strings.Index(k, keyword) != -1 { + if strings.Contains(k, keyword) { return true } - if strings.Index(v, keyword) != -1 { + if strings.Contains(v, keyword) { return true } } diff --git a/pkg/ffuf/util.go b/pkg/ffuf/util.go index b078bec..56c61aa 100644 --- a/pkg/ffuf/util.go +++ b/pkg/ffuf/util.go @@ -25,7 +25,7 @@ func UniqStringSlice(inslice []string) []string { found[v] = true } ret := []string{} - for k, _ := range found { + for k := range found { ret = append(ret, k) } return ret diff --git a/pkg/filter/lines_test.go b/pkg/filter/lines_test.go index 711841e..c6808a4 100644 --- a/pkg/filter/lines_test.go +++ b/pkg/filter/lines_test.go @@ -10,7 +10,7 @@ import ( func TestNewLineFilter(t *testing.T) { f, _ := NewLineFilter("200,301,400-410,500") linesRepr := f.Repr() - if strings.Index(linesRepr, "200,301,400-410,500") == -1 { + if !strings.Contains(linesRepr, "200,301,400-410,500") { t.Errorf("Word filter was expected to have 4 values") } } diff --git a/pkg/filter/regexp_test.go b/pkg/filter/regexp_test.go index 9a06853..f6cd770 100644 --- a/pkg/filter/regexp_test.go +++ b/pkg/filter/regexp_test.go @@ -10,7 +10,7 @@ import ( func TestNewRegexpFilter(t *testing.T) { f, _ := NewRegexpFilter("s([a-z]+)arch") statusRepr := f.Repr() - if strings.Index(statusRepr, "s([a-z]+)arch") == -1 { + if !strings.Contains(statusRepr, "s([a-z]+)arch") { t.Errorf("Status filter was expected to have a regexp value") } } diff --git a/pkg/filter/size_test.go b/pkg/filter/size_test.go index 8c7c88d..b5d7d3d 100644 --- a/pkg/filter/size_test.go +++ b/pkg/filter/size_test.go @@ -10,7 +10,7 @@ import ( func TestNewSizeFilter(t *testing.T) { f, _ := NewSizeFilter("1,2,3,444,5-90") sizeRepr := f.Repr() - if strings.Index(sizeRepr, "1,2,3,444,5-90") == -1 { + if !strings.Contains(sizeRepr, "1,2,3,444,5-90") { t.Errorf("Size filter was expected to have 5 values") } } diff --git a/pkg/filter/status_test.go b/pkg/filter/status_test.go index 64c11d4..9006a5a 100644 --- a/pkg/filter/status_test.go +++ b/pkg/filter/status_test.go @@ -10,7 +10,7 @@ import ( func TestNewStatusFilter(t *testing.T) { f, _ := NewStatusFilter("200,301,400-410,500") statusRepr := f.Repr() - if strings.Index(statusRepr, "200,301,400-410,500") == -1 { + if !strings.Contains(statusRepr, "200,301,400-410,500") { t.Errorf("Status filter was expected to have 4 values") } } diff --git a/pkg/filter/words_test.go b/pkg/filter/words_test.go index c447bff..3c760be 100644 --- a/pkg/filter/words_test.go +++ b/pkg/filter/words_test.go @@ -10,7 +10,7 @@ import ( func TestNewWordFilter(t *testing.T) { f, _ := NewWordFilter("200,301,400-410,500") wordsRepr := f.Repr() - if strings.Index(wordsRepr, "200,301,400-410,500") == -1 { + if !strings.Contains(wordsRepr, "200,301,400-410,500") { t.Errorf("Word filter was expected to have 4 values") } } diff --git a/pkg/input/command.go b/pkg/input/command.go index 2f31020..efcd558 100644 --- a/pkg/input/command.go +++ b/pkg/input/command.go @@ -47,10 +47,7 @@ func (c *CommandInput) IncrementPosition() { //Next will increment the cursor position, and return a boolean telling if there's iterations left func (c *CommandInput) Next() bool { - if c.count >= c.config.InputNum { - return false - } - return true + return c.count < c.config.InputNum } //Value returns the input from command stdoutput diff --git a/pkg/input/wordlist.go b/pkg/input/wordlist.go index 96cf2a9..7fc297e 100644 --- a/pkg/input/wordlist.go +++ b/pkg/input/wordlist.go @@ -57,10 +57,7 @@ func (w *WordlistInput) Keyword() string { //Next will increment the cursor position, and return a boolean telling if there's words left in the list func (w *WordlistInput) Next() bool { - if w.position >= len(w.data) { - return false - } - return true + return w.position < len(w.data) } //IncrementPosition will increment the current position in the inputprovider data slice diff --git a/pkg/output/file_csv.go b/pkg/output/file_csv.go index 3451e68..3394f8a 100644 --- a/pkg/output/file_csv.go +++ b/pkg/output/file_csv.go @@ -25,17 +25,14 @@ func writeCSV(config *ffuf.Config, res []Result, encode bool) error { for _, inputprovider := range config.InputProviders { header = append(header, inputprovider.Keyword) } - - for _, item := range staticheaders { - header = append(header, item) - } + header = append(header, staticheaders...) if err := w.Write(header); err != nil { return err } for _, r := range res { if encode { - inputs := make(map[string][]byte, 0) + inputs := make(map[string][]byte, len(r.Input)) for k, v := range r.Input { inputs[k] = []byte(base64encode(v)) } diff --git a/pkg/output/stdout.go b/pkg/output/stdout.go index a5af305..3e2ab84 100644 --- a/pkg/output/stdout.go +++ b/pkg/output/stdout.go @@ -107,12 +107,10 @@ func (s *Stdoutput) Banner() error { // Proxies if len(s.config.ProxyURL) > 0 { - proxy := fmt.Sprintf("%s", s.config.ProxyURL) - printOption([]byte("Proxy"), []byte(proxy)) + printOption([]byte("Proxy"), []byte(s.config.ProxyURL)) } if len(s.config.ReplayProxyURL) > 0 { - replayproxy := fmt.Sprintf("%s", s.config.ReplayProxyURL) - printOption([]byte("ReplayProxy"), []byte(replayproxy)) + printOption([]byte("ReplayProxy"), []byte(s.config.ReplayProxyURL)) } // Timeout @@ -152,7 +150,7 @@ func (s *Stdoutput) Progress(status ffuf.Progress) { return } - dur := time.Now().Sub(status.StartedAt) + dur := time.Since(status.StartedAt) runningSecs := int(dur / time.Second) var reqRate int64 if runningSecs > 0 { @@ -289,7 +287,7 @@ func (s *Stdoutput) Result(resp ffuf.Response) { // Check if we need the data later if s.config.OutputFile != "" { // No need to store results if we're not going to use them later - inputs := make(map[string][]byte, 0) + inputs := make(map[string][]byte, len(resp.Request.Input)) for k, v := range resp.Request.Input { inputs[k] = v } @@ -404,14 +402,13 @@ func (s *Stdoutput) resultMultiline(resp ffuf.Response) { } func (s *Stdoutput) resultNormal(resp ffuf.Response) { - var res_str string - res_str = fmt.Sprintf("%s%-23s [Status: %s, Size: %d, Words: %d, Lines: %d]", TERMINAL_CLEAR_LINE, s.prepareInputsOneLine(resp), s.colorize(fmt.Sprintf("%d", resp.StatusCode), resp.StatusCode), resp.ContentLength, resp.ContentWords, resp.ContentLines) - fmt.Println(res_str) + res := fmt.Sprintf("%s%-23s [Status: %s, Size: %d, Words: %d, Lines: %d]", TERMINAL_CLEAR_LINE, s.prepareInputsOneLine(resp), s.colorize(fmt.Sprintf("%d", resp.StatusCode), resp.StatusCode), resp.ContentLength, resp.ContentWords, resp.ContentLines) + fmt.Println(res) } func (s *Stdoutput) colorize(input string, status int64) string { if !s.config.Colors { - return fmt.Sprintf("%s", input) + return input } colorCode := ANSI_CLEAR if status >= 200 && status < 300 { diff --git a/pkg/runner/simple.go b/pkg/runner/simple.go index 8af2886..2dca7e0 100644 --- a/pkg/runner/simple.go +++ b/pkg/runner/simple.go @@ -78,7 +78,7 @@ func (r *SimpleRunner) Prepare(input map[string][]byte) (ffuf.Request, error) { for keyword, inputitem := range input { req.Method = strings.ReplaceAll(req.Method, keyword, string(inputitem)) - headers := make(map[string]string, 0) + headers := make(map[string]string, len(req.Headers)) for h, v := range req.Headers { var CanonicalHeader string = textproto.CanonicalMIMEHeaderKey(strings.ReplaceAll(h, keyword, string(inputitem))) headers[CanonicalHeader] = strings.ReplaceAll(v, keyword, string(inputitem))