12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- // Package gas holds the central scanning logic used by GAS
16
- package gas
15
+ // Package gosec holds the central scanning logic used by gosec security scanner
16
+ package gosec
17
17
18
18
import (
19
19
"go/ast"
@@ -55,7 +55,7 @@ type Metrics struct {
55
55
NumFound int `json:"found"`
56
56
}
57
57
58
- // Analyzer object is the main object of GAS . It has methods traverse an AST
58
+ // Analyzer object is the main object of gosec . It has methods traverse an AST
59
59
// and invoke the correct checking rules as on each node as required.
60
60
type Analyzer struct {
61
61
ignoreNosec bool
@@ -74,7 +74,7 @@ func NewAnalyzer(conf Config, logger *log.Logger) *Analyzer {
74
74
ignoreNoSec = setting == "true" || setting == "enabled"
75
75
}
76
76
if logger == nil {
77
- logger = log .New (os .Stderr , "[gas ]" , log .LstdFlags )
77
+ logger = log .New (os .Stderr , "[gosec ]" , log .LstdFlags )
78
78
}
79
79
return & Analyzer {
80
80
ignoreNosec : ignoreNoSec ,
@@ -89,15 +89,15 @@ func NewAnalyzer(conf Config, logger *log.Logger) *Analyzer {
89
89
90
90
// LoadRules instantiates all the rules to be used when analyzing source
91
91
// packages
92
- func (gas * Analyzer ) LoadRules (ruleDefinitions map [string ]RuleBuilder ) {
92
+ func (gosec * Analyzer ) LoadRules (ruleDefinitions map [string ]RuleBuilder ) {
93
93
for id , def := range ruleDefinitions {
94
- r , nodes := def (id , gas .config )
95
- gas .ruleset .Register (r , nodes ... )
94
+ r , nodes := def (id , gosec .config )
95
+ gosec .ruleset .Register (r , nodes ... )
96
96
}
97
97
}
98
98
99
99
// Process kicks off the analysis process for a given package
100
- func (gas * Analyzer ) Process (buildTags []string , packagePaths ... string ) error {
100
+ func (gosec * Analyzer ) Process (buildTags []string , packagePaths ... string ) error {
101
101
ctx := build .Default
102
102
ctx .BuildTags = append (ctx .BuildTags , buildTags ... )
103
103
packageConfig := loader.Config {
@@ -111,10 +111,10 @@ func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
111
111
return err
112
112
}
113
113
if _ , err := os .Stat (abspath ); os .IsNotExist (err ) {
114
- gas .logger .Printf ("Skipping: %s. Path doesn't exist." , abspath )
114
+ gosec .logger .Printf ("Skipping: %s. Path doesn't exist." , abspath )
115
115
continue
116
116
}
117
- gas .logger .Println ("Searching directory:" , abspath )
117
+ gosec .logger .Println ("Searching directory:" , abspath )
118
118
119
119
basePackage , err := build .Default .ImportDir (packagePath , build .ImportComment )
120
120
if err != nil {
@@ -135,31 +135,31 @@ func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
135
135
}
136
136
137
137
for _ , pkg := range builtPackage .Created {
138
- gas .logger .Println ("Checking package:" , pkg .String ())
138
+ gosec .logger .Println ("Checking package:" , pkg .String ())
139
139
for _ , file := range pkg .Files {
140
- gas .logger .Println ("Checking file:" , builtPackage .Fset .File (file .Pos ()).Name ())
141
- gas .context .FileSet = builtPackage .Fset
142
- gas .context .Config = gas .config
143
- gas .context .Comments = ast .NewCommentMap (gas .context .FileSet , file , file .Comments )
144
- gas .context .Root = file
145
- gas .context .Info = & pkg .Info
146
- gas .context .Pkg = pkg .Pkg
147
- gas .context .Imports = NewImportTracker ()
148
- gas .context .Imports .TrackPackages (gas .context .Pkg .Imports ()... )
149
- ast .Walk (gas , file )
150
- gas .stats .NumFiles ++
151
- gas .stats .NumLines += builtPackage .Fset .File (file .Pos ()).LineCount ()
140
+ gosec .logger .Println ("Checking file:" , builtPackage .Fset .File (file .Pos ()).Name ())
141
+ gosec .context .FileSet = builtPackage .Fset
142
+ gosec .context .Config = gosec .config
143
+ gosec .context .Comments = ast .NewCommentMap (gosec .context .FileSet , file , file .Comments )
144
+ gosec .context .Root = file
145
+ gosec .context .Info = & pkg .Info
146
+ gosec .context .Pkg = pkg .Pkg
147
+ gosec .context .Imports = NewImportTracker ()
148
+ gosec .context .Imports .TrackPackages (gosec .context .Pkg .Imports ()... )
149
+ ast .Walk (gosec , file )
150
+ gosec .stats .NumFiles ++
151
+ gosec .stats .NumLines += builtPackage .Fset .File (file .Pos ()).LineCount ()
152
152
}
153
153
}
154
154
return nil
155
155
}
156
156
157
157
// ignore a node (and sub-tree) if it is tagged with a "#nosec" comment
158
- func (gas * Analyzer ) ignore (n ast.Node ) ([]string , bool ) {
159
- if groups , ok := gas .context .Comments [n ]; ok && ! gas .ignoreNosec {
158
+ func (gosec * Analyzer ) ignore (n ast.Node ) ([]string , bool ) {
159
+ if groups , ok := gosec .context .Comments [n ]; ok && ! gosec .ignoreNosec {
160
160
for _ , group := range groups {
161
161
if strings .Contains (group .Text (), "#nosec" ) {
162
- gas .stats .NumNosec ++
162
+ gosec .stats .NumNosec ++
163
163
164
164
// Pull out the specific rules that are listed to be ignored.
165
165
re := regexp .MustCompile ("(G\\ d{3})" )
@@ -182,27 +182,27 @@ func (gas *Analyzer) ignore(n ast.Node) ([]string, bool) {
182
182
return nil , false
183
183
}
184
184
185
- // Visit runs the GAS visitor logic over an AST created by parsing go code.
185
+ // Visit runs the gosec visitor logic over an AST created by parsing go code.
186
186
// Rule methods added with AddRule will be invoked as necessary.
187
- func (gas * Analyzer ) Visit (n ast.Node ) ast.Visitor {
187
+ func (gosec * Analyzer ) Visit (n ast.Node ) ast.Visitor {
188
188
// If we've reached the end of this branch, pop off the ignores stack.
189
189
if n == nil {
190
- if len (gas .context .Ignores ) > 0 {
191
- gas .context .Ignores = gas .context .Ignores [1 :]
190
+ if len (gosec .context .Ignores ) > 0 {
191
+ gosec .context .Ignores = gosec .context .Ignores [1 :]
192
192
}
193
- return gas
193
+ return gosec
194
194
}
195
195
196
196
// Get any new rule exclusions.
197
- ignoredRules , ignoreAll := gas .ignore (n )
197
+ ignoredRules , ignoreAll := gosec .ignore (n )
198
198
if ignoreAll {
199
199
return nil
200
200
}
201
201
202
202
// Now create the union of exclusions.
203
203
ignores := make (map [string ]bool , 0 )
204
- if len (gas .context .Ignores ) > 0 {
205
- for k , v := range gas .context .Ignores [0 ] {
204
+ if len (gosec .context .Ignores ) > 0 {
205
+ for k , v := range gosec .context .Ignores [0 ] {
206
206
ignores [k ] = v
207
207
}
208
208
}
@@ -212,37 +212,37 @@ func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
212
212
}
213
213
214
214
// Push the new set onto the stack.
215
- gas .context .Ignores = append ([]map [string ]bool {ignores }, gas .context .Ignores ... )
215
+ gosec .context .Ignores = append ([]map [string ]bool {ignores }, gosec .context .Ignores ... )
216
216
217
217
// Track aliased and initialization imports
218
- gas .context .Imports .TrackImport (n )
218
+ gosec .context .Imports .TrackImport (n )
219
219
220
- for _ , rule := range gas .ruleset .RegisteredFor (n ) {
220
+ for _ , rule := range gosec .ruleset .RegisteredFor (n ) {
221
221
if _ , ok := ignores [rule .ID ()]; ok {
222
222
continue
223
223
}
224
- issue , err := rule .Match (n , gas .context )
224
+ issue , err := rule .Match (n , gosec .context )
225
225
if err != nil {
226
- file , line := GetLocation (n , gas .context )
226
+ file , line := GetLocation (n , gosec .context )
227
227
file = path .Base (file )
228
- gas .logger .Printf ("Rule error: %v => %s (%s:%d)\n " , reflect .TypeOf (rule ), err , file , line )
228
+ gosec .logger .Printf ("Rule error: %v => %s (%s:%d)\n " , reflect .TypeOf (rule ), err , file , line )
229
229
}
230
230
if issue != nil {
231
- gas .issues = append (gas .issues , issue )
232
- gas .stats .NumFound ++
231
+ gosec .issues = append (gosec .issues , issue )
232
+ gosec .stats .NumFound ++
233
233
}
234
234
}
235
- return gas
235
+ return gosec
236
236
}
237
237
238
238
// Report returns the current issues discovered and the metrics about the scan
239
- func (gas * Analyzer ) Report () ([]* Issue , * Metrics ) {
240
- return gas .issues , gas .stats
239
+ func (gosec * Analyzer ) Report () ([]* Issue , * Metrics ) {
240
+ return gosec .issues , gosec .stats
241
241
}
242
242
243
243
// Reset clears state such as context, issues and metrics from the configured analyzer
244
- func (gas * Analyzer ) Reset () {
245
- gas .context = & Context {}
246
- gas .issues = make ([]* Issue , 0 , 16 )
247
- gas .stats = & Metrics {}
244
+ func (gosec * Analyzer ) Reset () {
245
+ gosec .context = & Context {}
246
+ gosec .issues = make ([]* Issue , 0 , 16 )
247
+ gosec .stats = & Metrics {}
248
248
}
0 commit comments