Skip to content

Commit 35af340

Browse files
authored
Fix #736 (#738)
1 parent 6c0b344 commit 35af340

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

analyzer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,16 @@ func (gosec *Analyzer) ignore(n ast.Node) map[string]SuppressionInfo {
319319
}
320320

321321
for _, group := range groups {
322-
323-
foundDefaultTag := strings.HasPrefix(group.Text(), noSecDefaultTag)
324-
foundAlternativeTag := strings.HasPrefix(group.Text(), noSecAlternativeTag)
322+
comment := strings.TrimSpace(group.Text())
323+
foundDefaultTag := strings.HasPrefix(comment, noSecDefaultTag)
324+
foundAlternativeTag := strings.HasPrefix(comment, noSecAlternativeTag)
325325

326326
if foundDefaultTag || foundAlternativeTag {
327327
gosec.stats.NumNosec++
328328

329329
// Extract the directive and the justification.
330330
justification := ""
331-
commentParts := regexp.MustCompile(`-{2,}`).Split(group.Text(), 2)
331+
commentParts := regexp.MustCompile(`-{2,}`).Split(comment, 2)
332332
directive := commentParts[0]
333333
if len(commentParts) > 1 {
334334
justification = strings.TrimSpace(strings.TrimRight(commentParts[1], "\n"))

analyzer_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var _ = Describe("Analyzer", func() {
139139
}
140140
})
141141

142-
It("should not report errors when a nosec comment is present", func() {
142+
It("should not report errors when a nosec line comment is present", func() {
143143
sample := testutils.SampleCodeG401[0]
144144
source := sample.Code[0]
145145
analyzer.LoadRules(rules.Generate(false, rules.NewRuleFilter(false, "G401")).RulesInfo())
@@ -156,6 +156,23 @@ var _ = Describe("Analyzer", func() {
156156
Expect(nosecIssues).Should(BeEmpty())
157157
})
158158

159+
It("should not report errors when a nosec block comment is present", func() {
160+
sample := testutils.SampleCodeG401[0]
161+
source := sample.Code[0]
162+
analyzer.LoadRules(rules.Generate(false, rules.NewRuleFilter(false, "G401")).RulesInfo())
163+
164+
nosecPackage := testutils.NewTestPackage()
165+
defer nosecPackage.Close()
166+
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() /* #nosec */", 1)
167+
nosecPackage.AddFile("md5.go", nosecSource)
168+
err := nosecPackage.Build()
169+
Expect(err).ShouldNot(HaveOccurred())
170+
err = analyzer.Process(buildTags, nosecPackage.Path)
171+
Expect(err).ShouldNot(HaveOccurred())
172+
nosecIssues, _, _ := analyzer.Report()
173+
Expect(nosecIssues).Should(BeEmpty())
174+
})
175+
159176
It("should not report errors when an exclude comment is present for the correct rule", func() {
160177
// Rule for MD5 weak crypto usage
161178
sample := testutils.SampleCodeG401[0]

0 commit comments

Comments
 (0)