@@ -265,6 +265,54 @@ var _ = Describe("Analyzer", func() {
265
265
266
266
})
267
267
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
+
268
316
It ("should be able to analyze Go test package" , func () {
269
317
customAnalyzer := gosec .NewAnalyzer (nil , true , logger )
270
318
customAnalyzer .LoadRules (rules .Generate ().Builders ())
0 commit comments