@@ -165,6 +165,21 @@ var reGeneratedGoFile = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`
165
165
// of the files in fullPaths for the presence of generated Go headers to avoid
166
166
// reporting on generated code, per https://github.com/cosmos/gosec/issues/30
167
167
func filterOutGeneratedGoFiles (fullPaths []string ) (filtered []string ) {
168
+ if len (fullPaths ) == 0 {
169
+ return nil
170
+ }
171
+
172
+ if len (fullPaths ) == 1 {
173
+ blob , err := os .ReadFile (fullPaths [0 ])
174
+ if err != nil {
175
+ panic (err )
176
+ }
177
+ if ! reGeneratedGoFile .Match (blob ) {
178
+ filtered = append (filtered , fullPaths [0 ])
179
+ }
180
+ return
181
+ }
182
+
168
183
// position stores the order "pos" which will later be
169
184
// used to sort the paths to maintain original order
170
185
// despite the concurrent filtering that'll take place.
@@ -248,9 +263,9 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
248
263
}
249
264
}
250
265
251
- // step 1/4 create build context.
266
+ // step 1/3: create build context.
252
267
buildD := build .Default
253
- // step 2/4 : add build tags to get env dependent files into basePackage.
268
+ // step 2/3 : add build tags to get env dependent files into basePackage.
254
269
buildD .BuildTags = conf .BuildFlags
255
270
buildD .Dir = absGoModPath
256
271
basePackage , err := buildD .ImportDir (abspath , build .ImportComment )
@@ -276,12 +291,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
276
291
}
277
292
}
278
293
279
- // step 3/4: now filter out generated go files as we definitely don't
280
- // want to report on generated code, which is out of our direct control.
281
- // Please see: https://github.com/cosmos/gosec/issues/30
282
- packageFiles = filterOutGeneratedGoFiles (packageFiles )
283
-
284
- // step 4/4: remove build tags from conf to proceed build correctly.
294
+ // step 3/3: remove build tags from conf to proceed build correctly.
285
295
conf .BuildFlags = nil
286
296
conf .Dir = absGoModPath
287
297
pkgs , err := packages .Load (conf , packageFiles ... )
@@ -294,6 +304,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
294
304
// Check runs analysis on the given package
295
305
func (gosec * Analyzer ) Check (pkg * packages.Package ) {
296
306
gosec .logger .Println ("Checking package:" , pkg .Name )
307
+
297
308
for _ , file := range pkg .Syntax {
298
309
checkedFile := pkg .Fset .File (file .Pos ()).Name ()
299
310
// Skip the no-Go file from analysis (e.g. a Cgo files is expanded in 3 different files
@@ -312,7 +323,13 @@ func (gosec *Analyzer) Check(pkg *packages.Package) {
312
323
gosec .context .Imports = NewImportTracker ()
313
324
gosec .context .Imports .TrackFile (file )
314
325
gosec .context .PassedValues = make (map [string ]interface {})
315
- ast .Walk (gosec , file )
326
+
327
+ // Only walk non-generated Go files as we definitely don't
328
+ // want to report on generated code, which is out of our direct control.
329
+ // Please see: https://github.com/cosmos/gosec/issues/30
330
+ if filtered := filterOutGeneratedGoFiles ([]string {checkedFile }); len (filtered ) > 0 {
331
+ ast .Walk (gosec , file )
332
+ }
316
333
gosec .stats .NumFiles ++
317
334
gosec .stats .NumLines += pkg .Fset .File (file .Pos ()).LineCount ()
318
335
}
0 commit comments