From 1c76398aeac2b35be6decb197f6303055e9e06a5 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar <45962551+tarunKoyalwar@users.noreply.github.com> Date: Fri, 16 Aug 2024 08:01:23 -0700 Subject: [PATCH] lint error fixes (#5531) * lint error fixes * chore: satisfy non-constant format str in call lint (govet) Signed-off-by: Dwi Siswanto --------- Signed-off-by: Dwi Siswanto Co-authored-by: Dwi Siswanto --- cmd/tmc/main.go | 8 ++++---- lib/example_test.go | 2 +- pkg/input/formats/openapi/examples.go | 2 +- pkg/input/formats/openapi/generator.go | 4 ++-- pkg/js/devtools/bindgen/generator.go | 2 +- pkg/js/devtools/tsgen/parser.go | 2 +- pkg/js/utils/nucleijs.go | 10 +++++++--- pkg/output/output.go | 4 ++-- pkg/projectfile/httputil.go | 2 +- .../common/helpers/responsehighlighter/hexdump.go | 3 ++- pkg/protocols/dns/request.go | 2 +- pkg/protocols/headless/request.go | 2 +- pkg/reporting/reporting.go | 2 +- pkg/tmplexec/exec.go | 4 +++- 14 files changed, 28 insertions(+), 21 deletions(-) diff --git a/cmd/tmc/main.go b/cmd/tmc/main.go index eae38a1ca7..5cb38afbee 100644 --- a/cmd/tmc/main.go +++ b/cmd/tmc/main.go @@ -162,7 +162,7 @@ func process(opts options) error { var updated bool // if max-requests is updated dataString, updated, err = parseAndAddMaxRequests(templateCatalog, path, dataString) if err != nil { - gologger.Info().Label("max-request").Msgf(logErrMsg(path, err, opts.debug, errFile)) + gologger.Info().Label("max-request").Msg(logErrMsg(path, err, opts.debug, errFile)) } else { if updated { gologger.Info().Label("max-request").Msgf("✅ updated template: %s\n", path) @@ -255,7 +255,7 @@ func enhanceTemplate(data string) (string, bool, error) { return data, false, errorutil.New("validation failed").WithTag("validate") } if templateResp.Error.Name != "" { - return data, false, errorutil.New(templateResp.Error.Name) + return data, false, errorutil.New("%s", templateResp.Error.Name) } if strings.TrimSpace(templateResp.Enhanced) == "" && !templateResp.Lint { if templateResp.LintError.Reason != "" { @@ -289,7 +289,7 @@ func formatTemplate(data string) (string, bool, error) { return data, false, errorutil.New("validation failed").WithTag("validate") } if templateResp.Error.Name != "" { - return data, false, errorutil.New(templateResp.Error.Name) + return data, false, errorutil.New("%s", templateResp.Error.Name) } if strings.TrimSpace(templateResp.Updated) == "" && !templateResp.Lint { if templateResp.LintError.Reason != "" { @@ -345,7 +345,7 @@ func validateTemplate(data string) (bool, error) { return false, errorutil.New("validation failed").WithTag("validate") } if validateResp.Error.Name != "" { - return false, errorutil.New(validateResp.Error.Name) + return false, errorutil.New("%s", validateResp.Error.Name) } return false, errorutil.New("template validation failed") } diff --git a/lib/example_test.go b/lib/example_test.go index 1bbc20e584..1c705677a2 100644 --- a/lib/example_test.go +++ b/lib/example_test.go @@ -82,5 +82,5 @@ func TestMain(m *testing.M) { // no need to run this test on github actions return } - m.Run() + os.Exit(m.Run()) } diff --git a/pkg/input/formats/openapi/examples.go b/pkg/input/formats/openapi/examples.go index 3b788ca42b..9e7224ab73 100644 --- a/pkg/input/formats/openapi/examples.go +++ b/pkg/input/formats/openapi/examples.go @@ -18,7 +18,7 @@ func getSchemaExample(schema *openapi3.Schema) (interface{}, bool) { return schema.Default, true } - if schema.Enum != nil && len(schema.Enum) > 0 { + if len(schema.Enum) > 0 { return schema.Enum[0], true } return nil, false diff --git a/pkg/input/formats/openapi/generator.go b/pkg/input/formats/openapi/generator.go index adcb26b2fa..4027d76fc7 100644 --- a/pkg/input/formats/openapi/generator.go +++ b/pkg/input/formats/openapi/generator.go @@ -72,7 +72,7 @@ func GenerateRequestsFromSchema(schema *openapi3.T, opts formats.InputFormatOpti } missingVarMap[param.Name] = struct{}{} } - + for _, serverURL := range schema.Servers { pathURL := serverURL.URL // Split the server URL into baseURL and serverPath @@ -203,7 +203,7 @@ func generateRequestsFromOp(opts *generateReqOptions) error { paramValue = value.Schema.Value.Default } else if value.Schema.Value.Example != nil { paramValue = value.Schema.Value.Example - } else if value.Schema.Value.Enum != nil && len(value.Schema.Value.Enum) > 0 { + } else if len(value.Schema.Value.Enum) > 0 { paramValue = value.Schema.Value.Enum[0] } else { if !opts.opts.SkipFormatValidation { diff --git a/pkg/js/devtools/bindgen/generator.go b/pkg/js/devtools/bindgen/generator.go index 313c6d41ca..2a58ca7f0d 100644 --- a/pkg/js/devtools/bindgen/generator.go +++ b/pkg/js/devtools/bindgen/generator.go @@ -249,7 +249,7 @@ func identifyGenDecl(pkg *ast.Package, decl *ast.GenDecl, data *TemplateData) { if !spec.Names[0].IsExported() { continue } - if spec.Values == nil || len(spec.Values) == 0 { + if len(spec.Values) == 0 { continue } data.PackageVars[spec.Names[0].Name] = spec.Names[0].Name diff --git a/pkg/js/devtools/tsgen/parser.go b/pkg/js/devtools/tsgen/parser.go index e285b5f007..9f7d81ed7b 100644 --- a/pkg/js/devtools/tsgen/parser.go +++ b/pkg/js/devtools/tsgen/parser.go @@ -489,7 +489,7 @@ func (p *EntityParser) extractVarsNConstants() { if !spec.Names[0].IsExported() { continue } - if spec.Values == nil || len(spec.Values) == 0 { + if len(spec.Values) == 0 { continue } // get comments or description diff --git a/pkg/js/utils/nucleijs.go b/pkg/js/utils/nucleijs.go index d3d4560813..9d9e3f4ece 100644 --- a/pkg/js/utils/nucleijs.go +++ b/pkg/js/utils/nucleijs.go @@ -60,12 +60,16 @@ func (j *NucleiJS) HandleError(err error, msg ...string) { if len(msg) == 0 { j.ThrowError(err) } - j.Throw(fmt.Sprintf("%s: %s", strings.Join(msg, ":"), err.Error())) + j.Throw("%s: %s", strings.Join(msg, ":"), err.Error()) } // Throw throws an error in goja runtime func (j *NucleiJS) Throw(format string, args ...interface{}) { - panic(j.runtime().ToValue(fmt.Sprintf(format, args...))) + if len(args) > 0 { + panic(j.runtime().ToValue(fmt.Sprintf(format, args...))) + } + + panic(j.runtime().ToValue(format)) } // GetArg returns argument at index from goja runtime if not found throws error @@ -95,7 +99,7 @@ func (j *NucleiJS) GetArgSafe(args []goja.Value, index int, defaultValue any) an // Require throws an error if expression is false func (j *NucleiJS) Require(expr bool, msg string) { if !expr { - j.Throw(msg) + j.Throw("%s", msg) } } diff --git a/pkg/output/output.go b/pkg/output/output.go index 9f68122b0f..4f02d71c38 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -525,12 +525,12 @@ func tryParseCause(err error) error { if strings.HasPrefix(msg, "ReadStatusLine:") { // last index is actual error (from rawhttp) parts := strings.Split(msg, ":") - return errkit.New(strings.TrimSpace(parts[len(parts)-1])) + return errkit.New("%s", strings.TrimSpace(parts[len(parts)-1])) } if strings.Contains(msg, "read ") { // same here parts := strings.Split(msg, ":") - return errkit.New(strings.TrimSpace(parts[len(parts)-1])) + return errkit.New("%s", strings.TrimSpace(parts[len(parts)-1])) } return err } diff --git a/pkg/projectfile/httputil.go b/pkg/projectfile/httputil.go index 3f00b8e811..dafeff3fd8 100644 --- a/pkg/projectfile/httputil.go +++ b/pkg/projectfile/httputil.go @@ -17,7 +17,7 @@ func hash(v interface{}) (string, error) { sh := sha256.New() - if _, err = io.WriteString(sh, string(data)); err != nil { + if _, err = sh.Write(data); err != nil { return "", err } return hex.EncodeToString(sh.Sum(nil)), nil diff --git a/pkg/protocols/common/helpers/responsehighlighter/hexdump.go b/pkg/protocols/common/helpers/responsehighlighter/hexdump.go index decc985c72..36d60c34da 100644 --- a/pkg/protocols/common/helpers/responsehighlighter/hexdump.go +++ b/pkg/protocols/common/helpers/responsehighlighter/hexdump.go @@ -44,7 +44,8 @@ func toHighLightedHexDump(hexDump, snippetToHighlight string) (HighlightableHexD hexDumpRowValues := hexDumpParsePattern.FindAllStringSubmatch(hexDump, -1) if hexDumpRowValues == nil || len(hexDumpRowValues) != strings.Count(hexDump, "\n") { message := "could not parse hexdump" - gologger.Warning().Msgf(message) + gologger.Warning().Msg(message) + return HighlightableHexDump{}, errors.New(message) } diff --git a/pkg/protocols/dns/request.go b/pkg/protocols/dns/request.go index 83501c15b1..a16c2af88b 100644 --- a/pkg/protocols/dns/request.go +++ b/pkg/protocols/dns/request.go @@ -142,7 +142,7 @@ func (request *Request) execute(input *contextargs.Context, domain string, metad if request.options.Options.Debug || request.options.Options.DebugRequests || request.options.Options.StoreResponse { msg := fmt.Sprintf("[%s] Dumped DNS request for %s", request.options.TemplateID, question) if request.options.Options.Debug || request.options.Options.DebugRequests { - gologger.Info().Str("domain", domain).Msgf(msg) + gologger.Info().Str("domain", domain).Msg(msg) gologger.Print().Msgf("%s", requestString) } if request.options.Options.StoreResponse { diff --git a/pkg/protocols/headless/request.go b/pkg/protocols/headless/request.go index 973dfa642a..c6fd28a5de 100644 --- a/pkg/protocols/headless/request.go +++ b/pkg/protocols/headless/request.go @@ -174,7 +174,7 @@ func (request *Request) executeRequestWithPayloads(input *contextargs.Context, p reqBuilder.WriteString("\t" + actStepStr + "\n") } } - gologger.Debug().Msgf(reqBuilder.String()) + gologger.Debug().Msg(reqBuilder.String()) } var responseBody string diff --git a/pkg/reporting/reporting.go b/pkg/reporting/reporting.go index 172c31105e..889f92f3f7 100644 --- a/pkg/reporting/reporting.go +++ b/pkg/reporting/reporting.go @@ -240,7 +240,7 @@ func (c *ReportingClient) Close() { if failed > 0 { msgBuilder.WriteString(fmt.Sprintf(", %d failed", failed)) } - gologger.Info().Msgf(msgBuilder.String()) + gologger.Info().Msgf("%v", msgBuilder.String()) } } } diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index c510131da2..3d09f5e7a0 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -68,8 +68,10 @@ func (e *TemplateExecuter) Compile() error { if cliOptions.Verbose { rawErrorMessage := dslCompilationError.Error() formattedErrorMessage := strings.ToUpper(rawErrorMessage[:1]) + rawErrorMessage[1:] + "." - gologger.Warning().Msgf(formattedErrorMessage) + + gologger.Warning().Msg(formattedErrorMessage) gologger.Info().Msgf("The available custom DSL functions are:") + fmt.Println(dsl.GetPrintableDslFunctionSignatures(cliOptions.NoColor)) } }