From 9120883a155c961a5d58e752703f3e7a1d8214c6 Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Thu, 25 May 2023 11:54:26 +0200 Subject: [PATCH] Fix no-sec alternative tag (#962) The no-sec alternative tag prepends now automatically the # symbol Signed-off-by: Cosmin Cojocar --- analyzer.go | 4 +++- analyzer_test.go | 4 ++-- config.go | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/analyzer.go b/analyzer.go index 830d338..699366f 100644 --- a/analyzer.go +++ b/analyzer.go @@ -449,10 +449,12 @@ func (gosec *Analyzer) ignore(n ast.Node) map[string]issue.SuppressionInfo { if groups, ok := gosec.context.Comments[n]; ok && !gosec.ignoreNosec { // Checks if an alternative for #nosec is set and, if not, uses the default. - noSecDefaultTag := "#nosec" + noSecDefaultTag := NoSecTag(string(Nosec)) noSecAlternativeTag, err := gosec.config.GetGlobal(NoSecAlternative) if err != nil { noSecAlternativeTag = noSecDefaultTag + } else { + noSecAlternativeTag = NoSecTag(noSecAlternativeTag) } for _, group := range groups { diff --git a/analyzer_test.go b/analyzer_test.go index baa8019..937a2e3 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -407,7 +407,7 @@ var _ = Describe("Analyzer", func() { // overwrite nosec option nosecIgnoreConfig := gosec.NewConfig() - nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive") + nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "falsePositive") customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, false, false, 1, logger) customAnalyzer.LoadRules(rules.Generate(false, rules.NewRuleFilter(false, "G401")).RulesInfo()) @@ -430,7 +430,7 @@ var _ = Describe("Analyzer", func() { // overwrite nosec option nosecIgnoreConfig := gosec.NewConfig() - nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive") + nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "falsePositive") customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, false, false, 1, logger) customAnalyzer.LoadRules(rules.Generate(false, rules.NewRuleFilter(false, "G401")).RulesInfo()) diff --git a/config.go b/config.go index ca4cf21..9cbb7a7 100644 --- a/config.go +++ b/config.go @@ -33,6 +33,11 @@ const ( SSA GlobalOption = "ssa" ) +// NoSecTag returns the tag used to disable gosec for a line of code. +func NoSecTag(tag string) string { + return fmt.Sprintf("%s%s", "#", tag) +} + // Config is used to provide configuration and customization to each of the rules. type Config map[string]interface{}