Skip to content

Commit 8932f70

Browse files
Krlierccojocar
authored andcommitted
Add flag to handle '#nosec' alternative (#346)
* Add logic to check for a #nosec alternative * Add NoSecAlternative as a new global variable * Add nosec-tag flag
1 parent 4b59c94 commit 8932f70

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

analyzer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,15 @@ func (gosec *Analyzer) AppendError(file string, err error) {
251251
// ignore a node (and sub-tree) if it is tagged with a "#nosec" comment
252252
func (gosec *Analyzer) ignore(n ast.Node) ([]string, bool) {
253253
if groups, ok := gosec.context.Comments[n]; ok && !gosec.ignoreNosec {
254+
255+
// Checks if an alternative for #nosec is set and, if not, uses the default.
256+
noSecAlternative, err := gosec.config.GetGlobal(NoSecAlternative)
257+
if err != nil {
258+
noSecAlternative = "#nosec"
259+
}
260+
254261
for _, group := range groups {
255-
if strings.Contains(group.Text(), "#nosec") {
262+
if strings.Contains(group.Text(), noSecAlternative) {
256263
gosec.stats.NumNosec++
257264

258265
// Pull out the specific rules that are listed to be ignored.

analyzer_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,54 @@ var _ = Describe("Analyzer", func() {
265265

266266
})
267267

268+
It("should be possible to change the default #nosec directive to another one", func() {
269+
// Rule for MD5 weak crypto usage
270+
sample := testutils.SampleCodeG401[0]
271+
source := sample.Code[0]
272+
273+
// overwrite nosec option
274+
nosecIgnoreConfig := gosec.NewConfig()
275+
nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive")
276+
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, logger)
277+
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
278+
279+
nosecPackage := testutils.NewTestPackage()
280+
defer nosecPackage.Close()
281+
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #falsePositive", 1)
282+
nosecPackage.AddFile("md5.go", nosecSource)
283+
err := nosecPackage.Build()
284+
Expect(err).ShouldNot(HaveOccurred())
285+
err = customAnalyzer.Process(buildTags, nosecPackage.Path)
286+
Expect(err).ShouldNot(HaveOccurred())
287+
nosecIssues, _, _ := customAnalyzer.Report()
288+
Expect(nosecIssues).Should(HaveLen(0))
289+
290+
})
291+
292+
It("should not ignore vulnerabilities", func() {
293+
// Rule for MD5 weak crypto usage
294+
sample := testutils.SampleCodeG401[0]
295+
source := sample.Code[0]
296+
297+
// overwrite nosec option
298+
nosecIgnoreConfig := gosec.NewConfig()
299+
nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive")
300+
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, logger)
301+
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
302+
303+
nosecPackage := testutils.NewTestPackage()
304+
defer nosecPackage.Close()
305+
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec", 1)
306+
nosecPackage.AddFile("md5.go", nosecSource)
307+
err := nosecPackage.Build()
308+
Expect(err).ShouldNot(HaveOccurred())
309+
err = customAnalyzer.Process(buildTags, nosecPackage.Path)
310+
Expect(err).ShouldNot(HaveOccurred())
311+
nosecIssues, _, _ := customAnalyzer.Report()
312+
Expect(nosecIssues).Should(HaveLen(sample.Errors))
313+
314+
})
315+
268316
It("should be able to analyze Go test package", func() {
269317
customAnalyzer := gosec.NewAnalyzer(nil, true, logger)
270318
customAnalyzer.LoadRules(rules.Generate().Builders())

cmd/gosec/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var (
6565
// format output
6666
flagFormat = flag.String("fmt", "text", "Set output format. Valid options are: json, yaml, csv, junit-xml, html, sonarqube, or text")
6767

68+
// #nosec alternative tag
69+
flagAlternativeNoSec = flag.String("nosec-tag", "", "Set an alternative string for #nosec. Some examples: #dontanalyze, #falsepositive")
70+
6871
// output file
6972
flagOutput = flag.String("out", "", "Set output file for results")
7073

@@ -148,6 +151,9 @@ func loadConfig(configFile string) (gosec.Config, error) {
148151
if *flagIgnoreNoSec {
149152
config.SetGlobal(gosec.Nosec, "true")
150153
}
154+
if *flagAlternativeNoSec != "" {
155+
config.SetGlobal(gosec.NoSecAlternative, *flagAlternativeNoSec)
156+
}
151157
return config, nil
152158
}
153159

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
Nosec GlobalOption = "nosec"
2323
// Audit global option which indicates that gosec runs in audit mode
2424
Audit GlobalOption = "audit"
25+
// NoSecAlternative global option alternative for #nosec directive
26+
NoSecAlternative GlobalOption = "#nosec"
2527
)
2628

2729
// Config is used to provide configuration and customization to each of the rules.

0 commit comments

Comments
 (0)