@@ -57,6 +57,8 @@ const externalSuppressionJustification = "Globally suppressed."
57
57
58
58
const aliasOfAllRules = "*"
59
59
60
+ var directiveRegexp = regexp .MustCompile ("^//gosec:disable(?: (.+))?$" )
61
+
60
62
type ignore struct {
61
63
start int
62
64
end int
@@ -582,53 +584,77 @@ func (gosec *Analyzer) ignore(n ast.Node) map[string]issue.SuppressionInfo {
582
584
}
583
585
584
586
for _ , group := range groups {
585
- comment := strings .TrimSpace (group .Text ())
586
- foundDefaultTag := strings .HasPrefix (comment , noSecDefaultTag ) || regexp .MustCompile ("\n *" + noSecDefaultTag ).MatchString (comment )
587
- foundAlternativeTag := strings .HasPrefix (comment , noSecAlternativeTag ) || regexp .MustCompile ("\n *" + noSecAlternativeTag ).MatchString (comment )
588
-
589
- if foundDefaultTag || foundAlternativeTag {
590
- gosec .stats .NumNosec ++
591
-
592
- // Discard what's in front of the nosec tag.
593
- if foundDefaultTag {
594
- comment = strings .SplitN (comment , noSecDefaultTag , 2 )[1 ]
595
- } else {
596
- comment = strings .SplitN (comment , noSecAlternativeTag , 2 )[1 ]
597
- }
587
+ found , args := findNoSecDirective (group , noSecDefaultTag , noSecAlternativeTag )
588
+ if ! found {
589
+ continue
590
+ }
598
591
599
- // Extract the directive and the justification.
600
- justification := ""
601
- commentParts := regexp .MustCompile (`-{2,}` ).Split (comment , 2 )
602
- directive := commentParts [0 ]
603
- if len (commentParts ) > 1 {
604
- justification = strings .TrimSpace (strings .TrimRight (commentParts [1 ], "\n " ))
605
- }
592
+ gosec .stats .NumNosec ++
606
593
607
- // Pull out the specific rules that are listed to be ignored.
608
- re := regexp .MustCompile (`(G\d{3})` )
609
- matches := re .FindAllStringSubmatch (directive , - 1 )
594
+ // Extract the directive and the justification.
595
+ justification := ""
596
+ commentParts := regexp .MustCompile (`-{2,}` ).Split (args , 2 )
597
+ directive := commentParts [0 ]
598
+ if len (commentParts ) > 1 {
599
+ justification = strings .TrimSpace (strings .TrimRight (commentParts [1 ], "\n " ))
600
+ }
610
601
611
- suppression := issue.SuppressionInfo {
612
- Kind : "inSource" ,
613
- Justification : justification ,
614
- }
602
+ // Pull out the specific rules that are listed to be ignored.
603
+ re := regexp .MustCompile (`(G\d{3})` )
604
+ matches := re .FindAllStringSubmatch (directive , - 1 )
615
605
616
- // Find the rule IDs to ignore.
617
- ignores := make (map [string ]issue.SuppressionInfo )
618
- for _ , v := range matches {
619
- ignores [v [1 ]] = suppression
620
- }
606
+ suppression := issue.SuppressionInfo {
607
+ Kind : "inSource" ,
608
+ Justification : justification ,
609
+ }
621
610
622
- // If no specific rules were given, ignore everything.
623
- if len (matches ) == 0 {
624
- ignores [aliasOfAllRules ] = suppression
625
- }
626
- return ignores
611
+ // Find the rule IDs to ignore.
612
+ ignores := make (map [string ]issue.SuppressionInfo )
613
+ for _ , v := range matches {
614
+ ignores [v [1 ]] = suppression
627
615
}
616
+
617
+ // If no specific rules were given, ignore everything.
618
+ if len (matches ) == 0 {
619
+ ignores [aliasOfAllRules ] = suppression
620
+ }
621
+ return ignores
628
622
}
629
623
return nil
630
624
}
631
625
626
+ // findNoSecDirective checks if the comment group contains `#nosec` or `//gosec:disable` directive.
627
+ // If found, it returns true and the directive's arguments.
628
+ func findNoSecDirective (group * ast.CommentGroup , noSecDefaultTag , noSecAlternativeTag string ) (bool , string ) {
629
+ // Check if the comment grounp has a nosec comment.
630
+ for _ , tag := range []string {noSecDefaultTag , noSecAlternativeTag } {
631
+ if found , args := findNoSecTag (group , tag ); found {
632
+ return true , args
633
+ }
634
+ }
635
+
636
+ // Check if the comment group has a directive comment.
637
+ for _ , c := range group .List {
638
+ match := directiveRegexp .FindStringSubmatch (c .Text )
639
+ if len (match ) > 0 {
640
+ return true , match [0 ]
641
+ }
642
+ }
643
+
644
+ return false , ""
645
+ }
646
+
647
+ func findNoSecTag (group * ast.CommentGroup , tag string ) (bool , string ) {
648
+ comment := strings .TrimSpace (group .Text ())
649
+
650
+ if strings .HasPrefix (comment , tag ) || regexp .MustCompile ("\n *" + tag ).MatchString (comment ) {
651
+ // Discard what's in front of the nosec tag.
652
+ return true , strings .SplitN (comment , tag , 2 )[1 ]
653
+ }
654
+
655
+ return false , ""
656
+ }
657
+
632
658
// Visit runs the gosec visitor logic over an AST created by parsing go code.
633
659
// Rule methods added with AddRule will be invoked as necessary.
634
660
func (gosec * Analyzer ) Visit (n ast.Node ) ast.Visitor {
0 commit comments