Skip to content

Commit d382a0c

Browse files
committed
analyzer: move generated file filtering to ast.Walk phase not in package building
The prior code had filtering out of generated Go files at the wrong phase of building packages which unfortunately caused a panic. Instead move the filtering to the phase before walking the AST for the file, which is the correct place. Updates #30
1 parent 00d45f6 commit d382a0c

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

Diff for: analyzer.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ var reGeneratedGoFile = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`
165165
// of the files in fullPaths for the presence of generated Go headers to avoid
166166
// reporting on generated code, per https://github.com/cosmos/gosec/issues/30
167167
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+
168183
// position stores the order "pos" which will later be
169184
// used to sort the paths to maintain original order
170185
// despite the concurrent filtering that'll take place.
@@ -248,9 +263,9 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
248263
}
249264
}
250265

251-
// step 1/4 create build context.
266+
// step 1/3: create build context.
252267
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.
254269
buildD.BuildTags = conf.BuildFlags
255270
buildD.Dir = absGoModPath
256271
basePackage, err := buildD.ImportDir(abspath, build.ImportComment)
@@ -276,12 +291,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
276291
}
277292
}
278293

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.
285295
conf.BuildFlags = nil
286296
conf.Dir = absGoModPath
287297
pkgs, err := packages.Load(conf, packageFiles...)
@@ -294,6 +304,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
294304
// Check runs analysis on the given package
295305
func (gosec *Analyzer) Check(pkg *packages.Package) {
296306
gosec.logger.Println("Checking package:", pkg.Name)
307+
297308
for _, file := range pkg.Syntax {
298309
checkedFile := pkg.Fset.File(file.Pos()).Name()
299310
// 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) {
312323
gosec.context.Imports = NewImportTracker()
313324
gosec.context.Imports.TrackFile(file)
314325
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+
}
316333
gosec.stats.NumFiles++
317334
gosec.stats.NumLines += pkg.Fset.File(file.Pos()).LineCount()
318335
}

0 commit comments

Comments
 (0)