@@ -32,6 +32,7 @@ import (
32
32
"sync"
33
33
34
34
"github.com/securego/gosec/v2/analyzers"
35
+ "github.com/securego/gosec/v2/issue"
35
36
"golang.org/x/tools/go/analysis"
36
37
"golang.org/x/tools/go/analysis/passes/buildssa"
37
38
"golang.org/x/tools/go/packages"
@@ -68,10 +69,21 @@ type Context struct {
68
69
Root * ast.File
69
70
Imports * ImportTracker
70
71
Config Config
71
- Ignores []map [string ][]SuppressionInfo
72
+ Ignores []map [string ][]issue. SuppressionInfo
72
73
PassedValues map [string ]interface {}
73
74
}
74
75
76
+ // getFileAtNodePos returns the file at the node position in the file set available in the context.
77
+ func (ctx * Context ) GetFileAtNodePos (node ast.Node ) * token.File {
78
+ return ctx .FileSet .File (node .Pos ())
79
+ }
80
+
81
+ // NewIssue creates a new issue
82
+ func (ctx * Context ) NewIssue (node ast.Node , ruleID , desc string ,
83
+ severity , confidence issue.Score ) * issue.Issue {
84
+ return issue .New (ctx .GetFileAtNodePos (node ), node , ruleID , desc , severity , confidence )
85
+ }
86
+
75
87
// Metrics used when reporting information about a scanning run.
76
88
type Metrics struct {
77
89
NumFiles int `json:"files"`
@@ -88,7 +100,7 @@ type Analyzer struct {
88
100
context * Context
89
101
config Config
90
102
logger * log.Logger
91
- issues []* Issue
103
+ issues []* issue. Issue
92
104
stats * Metrics
93
105
errors map [string ][]Error // keys are file paths; values are the golang errors in those files
94
106
tests bool
@@ -99,13 +111,6 @@ type Analyzer struct {
99
111
analyzerList []* analysis.Analyzer
100
112
}
101
113
102
- // SuppressionInfo object is to record the kind and the justification that used
103
- // to suppress violations.
104
- type SuppressionInfo struct {
105
- Kind string `json:"kind"`
106
- Justification string `json:"justification"`
107
- }
108
-
109
114
// NewAnalyzer builds a new analyzer.
110
115
func NewAnalyzer (conf Config , tests bool , excludeGenerated bool , trackSuppressions bool , concurrency int , logger * log.Logger ) * Analyzer {
111
116
ignoreNoSec := false
@@ -126,7 +131,7 @@ func NewAnalyzer(conf Config, tests bool, excludeGenerated bool, trackSuppressio
126
131
context : & Context {},
127
132
config : conf ,
128
133
logger : logger ,
129
- issues : make ([]* Issue , 0 , 16 ),
134
+ issues : make ([]* issue. Issue , 0 , 16 ),
130
135
stats : & Metrics {},
131
136
errors : make (map [string ][]Error ),
132
137
tests : tests ,
@@ -371,8 +376,8 @@ func (gosec *Analyzer) CheckAnalyzers(pkg *packages.Package) {
371
376
continue
372
377
}
373
378
if result != nil {
374
- if issue , ok := result .(* analyzers.Issue ); ok {
375
- gosec .updateIssues (toGosecIssue (issue ), false , []SuppressionInfo {})
379
+ if aissue , ok := result .(* analyzers.Issue ); ok {
380
+ gosec .updateIssues (toGosecIssue (aissue ), false , []issue. SuppressionInfo {})
376
381
}
377
382
}
378
383
}
@@ -439,7 +444,7 @@ func (gosec *Analyzer) AppendError(file string, err error) {
439
444
}
440
445
441
446
// ignore a node (and sub-tree) if it is tagged with a nosec tag comment
442
- func (gosec * Analyzer ) ignore (n ast.Node ) map [string ]SuppressionInfo {
447
+ func (gosec * Analyzer ) ignore (n ast.Node ) map [string ]issue. SuppressionInfo {
443
448
if groups , ok := gosec .context .Comments [n ]; ok && ! gosec .ignoreNosec {
444
449
445
450
// Checks if an alternative for #nosec is set and, if not, uses the default.
@@ -476,13 +481,13 @@ func (gosec *Analyzer) ignore(n ast.Node) map[string]SuppressionInfo {
476
481
re := regexp .MustCompile (`(G\d{3})` )
477
482
matches := re .FindAllStringSubmatch (directive , - 1 )
478
483
479
- suppression := SuppressionInfo {
484
+ suppression := issue. SuppressionInfo {
480
485
Kind : "inSource" ,
481
486
Justification : justification ,
482
487
}
483
488
484
489
// Find the rule IDs to ignore.
485
- ignores := make (map [string ]SuppressionInfo )
490
+ ignores := make (map [string ]issue. SuppressionInfo )
486
491
for _ , v := range matches {
487
492
ignores [v [1 ]] = suppression
488
493
}
@@ -525,7 +530,7 @@ func (gosec *Analyzer) Visit(n ast.Node) ast.Visitor {
525
530
return gosec
526
531
}
527
532
528
- func (gosec * Analyzer ) updateIgnoredRules (n ast.Node ) (map [string ][]SuppressionInfo , bool ) {
533
+ func (gosec * Analyzer ) updateIgnoredRules (n ast.Node ) (map [string ][]issue. SuppressionInfo , bool ) {
529
534
if n == nil {
530
535
if len (gosec .context .Ignores ) > 0 {
531
536
gosec .context .Ignores = gosec .context .Ignores [1 :]
@@ -536,7 +541,7 @@ func (gosec *Analyzer) updateIgnoredRules(n ast.Node) (map[string][]SuppressionI
536
541
ignoredRules := gosec .ignore (n )
537
542
538
543
// Now create the union of exclusions.
539
- ignores := map [string ][]SuppressionInfo {}
544
+ ignores := map [string ][]issue. SuppressionInfo {}
540
545
if len (gosec .context .Ignores ) > 0 {
541
546
for k , v := range gosec .context .Ignores [0 ] {
542
547
ignores [k ] = v
@@ -548,12 +553,12 @@ func (gosec *Analyzer) updateIgnoredRules(n ast.Node) (map[string][]SuppressionI
548
553
}
549
554
550
555
// Push the new set onto the stack.
551
- gosec .context .Ignores = append ([]map [string ][]SuppressionInfo {ignores }, gosec .context .Ignores ... )
556
+ gosec .context .Ignores = append ([]map [string ][]issue. SuppressionInfo {ignores }, gosec .context .Ignores ... )
552
557
553
558
return ignores , true
554
559
}
555
560
556
- func (gosec * Analyzer ) updateSuppressions (id string , ignores map [string ][]SuppressionInfo ) ([]SuppressionInfo , bool ) {
561
+ func (gosec * Analyzer ) updateSuppressions (id string , ignores map [string ][]issue. SuppressionInfo ) ([]issue. SuppressionInfo , bool ) {
557
562
// Check if all rules are ignored.
558
563
generalSuppressions , generalIgnored := ignores [aliasOfAllRules ]
559
564
// Check if the specific rule is ignored
@@ -565,15 +570,15 @@ func (gosec *Analyzer) updateSuppressions(id string, ignores map[string][]Suppre
565
570
// Track external suppressions.
566
571
if gosec .ruleset .IsRuleSuppressed (id ) {
567
572
ignored = true
568
- suppressions = append (suppressions , SuppressionInfo {
573
+ suppressions = append (suppressions , issue. SuppressionInfo {
569
574
Kind : "external" ,
570
575
Justification : externalSuppressionJustification ,
571
576
})
572
577
}
573
578
return suppressions , ignored
574
579
}
575
580
576
- func (gosec * Analyzer ) updateIssues (issue * Issue , ignored bool , suppressions []SuppressionInfo ) {
581
+ func (gosec * Analyzer ) updateIssues (issue * issue. Issue , ignored bool , suppressions []issue. SuppressionInfo ) {
577
582
if issue != nil {
578
583
if gosec .showIgnored {
579
584
issue .NoSec = ignored
@@ -590,27 +595,27 @@ func (gosec *Analyzer) updateIssues(issue *Issue, ignored bool, suppressions []S
590
595
}
591
596
}
592
597
593
- func toGosecIssue (issue * analyzers.Issue ) * Issue {
594
- return & Issue {
595
- File : issue .File ,
596
- Line : issue .Line ,
597
- Col : issue .Col ,
598
- RuleID : issue .AnalyzerID ,
599
- What : issue .What ,
600
- Confidence : Score (issue .Confidence ),
601
- Severity : Score (issue .Severity ),
598
+ func toGosecIssue (aissue * analyzers.Issue ) * issue. Issue {
599
+ return & issue. Issue {
600
+ File : aissue .File ,
601
+ Line : aissue .Line ,
602
+ Col : aissue .Col ,
603
+ RuleID : aissue .AnalyzerID ,
604
+ What : aissue .What ,
605
+ Confidence : issue . Score (aissue .Confidence ),
606
+ Severity : issue . Score (aissue .Severity ),
602
607
}
603
608
}
604
609
605
610
// Report returns the current issues discovered and the metrics about the scan
606
- func (gosec * Analyzer ) Report () ([]* Issue , * Metrics , map [string ][]Error ) {
611
+ func (gosec * Analyzer ) Report () ([]* issue. Issue , * Metrics , map [string ][]Error ) {
607
612
return gosec .issues , gosec .stats , gosec .errors
608
613
}
609
614
610
615
// Reset clears state such as context, issues and metrics from the configured analyzer
611
616
func (gosec * Analyzer ) Reset () {
612
617
gosec .context = & Context {}
613
- gosec .issues = make ([]* Issue , 0 , 16 )
618
+ gosec .issues = make ([]* issue. Issue , 0 , 16 )
614
619
gosec .stats = & Metrics {}
615
620
gosec .ruleset = NewRuleSet ()
616
621
}
0 commit comments