2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
// Package gosec holds the central scanning logic used by gosec security scanner
|
|
|
|
package gosec
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
import (
|
2019-04-30 07:13:10 +01:00
|
|
|
"fmt"
|
2016-07-20 11:02:01 +01:00
|
|
|
"go/ast"
|
2017-05-10 05:26:53 +01:00
|
|
|
"go/build"
|
2016-07-20 11:02:01 +01:00
|
|
|
"go/token"
|
|
|
|
"go/types"
|
|
|
|
"log"
|
2017-07-19 22:17:00 +01:00
|
|
|
"os"
|
2016-12-02 18:20:23 +00:00
|
|
|
"path"
|
2016-07-20 11:02:01 +01:00
|
|
|
"reflect"
|
2017-10-05 22:32:03 +01:00
|
|
|
"regexp"
|
2019-02-26 22:24:06 +00:00
|
|
|
"strconv"
|
2019-04-25 08:25:32 +01:00
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
"strings"
|
2017-04-26 00:01:28 +01:00
|
|
|
|
2019-04-25 08:25:32 +01:00
|
|
|
"golang.org/x/tools/go/packages"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// The Context is populated with data parsed from the source code as it is scanned.
|
|
|
|
// It is passed through to all rule functions as they are called. Rules may use
|
2018-10-11 13:45:31 +01:00
|
|
|
// this data in conjunction withe the encountered AST node.
|
2016-07-20 11:02:01 +01:00
|
|
|
type Context struct {
|
|
|
|
FileSet *token.FileSet
|
|
|
|
Comments ast.CommentMap
|
|
|
|
Info *types.Info
|
|
|
|
Pkg *types.Package
|
2018-09-28 08:46:59 +01:00
|
|
|
PkgFiles []*ast.File
|
2016-08-05 11:04:06 +01:00
|
|
|
Root *ast.File
|
2019-01-14 11:37:40 +00:00
|
|
|
Config Config
|
2017-05-10 05:26:53 +01:00
|
|
|
Imports *ImportTracker
|
2017-10-05 22:32:03 +01:00
|
|
|
Ignores []map[string]bool
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// Metrics used when reporting information about a scanning run.
|
2016-07-20 11:02:01 +01:00
|
|
|
type Metrics struct {
|
2016-07-26 00:39:55 +01:00
|
|
|
NumFiles int `json:"files"`
|
|
|
|
NumLines int `json:"lines"`
|
|
|
|
NumNosec int `json:"nosec"`
|
|
|
|
NumFound int `json:"found"`
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
// Analyzer object is the main object of gosec. It has methods traverse an AST
|
2016-08-12 14:17:28 +01:00
|
|
|
// and invoke the correct checking rules as on each node as required.
|
2016-07-20 11:02:01 +01:00
|
|
|
type Analyzer struct {
|
2016-07-28 12:51:25 +01:00
|
|
|
ignoreNosec bool
|
2016-07-20 11:02:01 +01:00
|
|
|
ruleset RuleSet
|
2016-12-02 23:21:13 +00:00
|
|
|
context *Context
|
2017-04-28 22:46:26 +01:00
|
|
|
config Config
|
2016-07-20 11:02:01 +01:00
|
|
|
logger *log.Logger
|
2017-05-10 05:26:53 +01:00
|
|
|
issues []*Issue
|
|
|
|
stats *Metrics
|
2019-02-26 22:24:06 +00:00
|
|
|
errors map[string][]Error // keys are file paths; values are the golang errors in those files
|
2019-04-28 18:33:50 +01:00
|
|
|
tests bool
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2018-10-11 13:45:31 +01:00
|
|
|
// NewAnalyzer builds a new analyzer.
|
2019-04-28 18:33:50 +01:00
|
|
|
func NewAnalyzer(conf Config, tests bool, logger *log.Logger) *Analyzer {
|
2017-04-28 22:46:26 +01:00
|
|
|
ignoreNoSec := false
|
2019-01-14 11:37:40 +00:00
|
|
|
if enabled, err := conf.IsGlobalEnabled(Nosec); err == nil {
|
|
|
|
ignoreNoSec = enabled
|
2017-07-19 22:17:00 +01:00
|
|
|
}
|
|
|
|
if logger == nil {
|
2018-07-19 17:42:25 +01:00
|
|
|
logger = log.New(os.Stderr, "[gosec]", log.LstdFlags)
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
return &Analyzer{
|
2017-04-28 22:46:26 +01:00
|
|
|
ignoreNosec: ignoreNoSec,
|
2016-07-20 11:02:01 +01:00
|
|
|
ruleset: make(RuleSet),
|
2017-04-26 00:01:28 +01:00
|
|
|
context: &Context{},
|
2017-04-28 22:46:26 +01:00
|
|
|
config: conf,
|
2016-12-02 23:21:13 +00:00
|
|
|
logger: logger,
|
2017-05-10 05:26:53 +01:00
|
|
|
issues: make([]*Issue, 0, 16),
|
|
|
|
stats: &Metrics{},
|
2019-02-26 22:24:06 +00:00
|
|
|
errors: make(map[string][]Error),
|
2019-04-28 18:33:50 +01:00
|
|
|
tests: tests,
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:29:19 +01:00
|
|
|
// SetConfig upates the analyzer configuration
|
|
|
|
func (gosec *Analyzer) SetConfig(conf Config) {
|
|
|
|
gosec.config = conf
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// LoadRules instantiates all the rules to be used when analyzing source
|
|
|
|
// packages
|
2018-07-19 17:42:25 +01:00
|
|
|
func (gosec *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder) {
|
2017-10-05 22:32:03 +01:00
|
|
|
for id, def := range ruleDefinitions {
|
2018-07-19 17:42:25 +01:00
|
|
|
r, nodes := def(id, gosec.config)
|
|
|
|
gosec.ruleset.Register(r, nodes...)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// Process kicks off the analysis process for a given package
|
2018-07-19 17:42:25 +01:00
|
|
|
func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error {
|
2019-04-27 09:01:27 +01:00
|
|
|
config := gosec.pkgConfig(buildTags)
|
|
|
|
for _, pkgPath := range packagePaths {
|
|
|
|
pkgs, err := gosec.load(pkgPath, config)
|
2018-01-07 23:02:33 +00:00
|
|
|
if err != nil {
|
2019-04-30 15:57:32 +01:00
|
|
|
gosec.AppendError(pkgPath, err)
|
2018-01-07 23:02:33 +00:00
|
|
|
}
|
2019-04-27 09:01:27 +01:00
|
|
|
for _, pkg := range pkgs {
|
2019-04-30 07:13:10 +01:00
|
|
|
if pkg.Name != "" {
|
2019-04-30 12:53:22 +01:00
|
|
|
err := gosec.ParseErrors(pkg)
|
2019-04-30 07:13:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing errors in pkg %q: %v", pkg.Name, err)
|
|
|
|
}
|
|
|
|
gosec.check(pkg)
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
2018-01-07 23:02:33 +00:00
|
|
|
}
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
|
|
|
sortErrors(gosec.errors)
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-07 23:02:33 +00:00
|
|
|
|
2019-04-27 09:01:27 +01:00
|
|
|
func (gosec *Analyzer) pkgConfig(buildTags []string) *packages.Config {
|
2019-04-30 15:57:32 +01:00
|
|
|
flags := []string{}
|
|
|
|
if len(buildTags) > 0 {
|
|
|
|
tagsFlag := "-tags=" + strings.Join(buildTags, " ")
|
|
|
|
flags = append(flags, tagsFlag)
|
|
|
|
}
|
2019-04-27 09:01:27 +01:00
|
|
|
return &packages.Config{
|
|
|
|
Mode: packages.LoadSyntax,
|
2019-04-30 15:57:32 +01:00
|
|
|
BuildFlags: flags,
|
2019-04-28 18:33:50 +01:00
|
|
|
Tests: gosec.tests,
|
2017-05-10 05:26:53 +01:00
|
|
|
}
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
2017-04-26 00:01:28 +01:00
|
|
|
|
2019-04-27 09:01:27 +01:00
|
|
|
func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.Package, error) {
|
|
|
|
abspath, err := GetPkgAbsPath(pkgPath)
|
|
|
|
if err != nil {
|
|
|
|
gosec.logger.Printf("Skipping: %s. Path doesn't exist.", abspath)
|
|
|
|
return []*packages.Package{}, nil
|
2019-02-26 22:24:06 +00:00
|
|
|
}
|
2019-04-25 08:25:32 +01:00
|
|
|
|
2019-04-27 09:01:27 +01:00
|
|
|
gosec.logger.Println("Import directory:", abspath)
|
|
|
|
basePackage, err := build.Default.ImportDir(pkgPath, build.ImportComment)
|
|
|
|
if err != nil {
|
2019-04-30 15:57:32 +01:00
|
|
|
return []*packages.Package{}, fmt.Errorf("importing dir %q: %v", pkgPath, err)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
2019-04-25 08:25:32 +01:00
|
|
|
|
2019-04-27 09:01:27 +01:00
|
|
|
var packageFiles []string
|
|
|
|
for _, filename := range basePackage.GoFiles {
|
|
|
|
packageFiles = append(packageFiles, path.Join(pkgPath, filename))
|
|
|
|
}
|
|
|
|
|
2019-04-28 18:33:50 +01:00
|
|
|
if gosec.tests {
|
|
|
|
testsFiles := []string{}
|
|
|
|
testsFiles = append(testsFiles, basePackage.TestGoFiles...)
|
|
|
|
testsFiles = append(testsFiles, basePackage.XTestGoFiles...)
|
|
|
|
for _, filename := range testsFiles {
|
|
|
|
packageFiles = append(packageFiles, path.Join(pkgPath, filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 09:01:27 +01:00
|
|
|
pkgs, err := packages.Load(conf, packageFiles...)
|
|
|
|
if err != nil {
|
2019-04-30 15:57:32 +01:00
|
|
|
return []*packages.Package{}, fmt.Errorf("loading files from package %q: %v", pkgPath, err)
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
|
|
|
return pkgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gosec *Analyzer) check(pkg *packages.Package) {
|
|
|
|
gosec.logger.Println("Checking package:", pkg.Name)
|
|
|
|
for _, file := range pkg.Syntax {
|
|
|
|
gosec.logger.Println("Checking file:", pkg.Fset.File(file.Pos()).Name())
|
|
|
|
gosec.context.FileSet = pkg.Fset
|
|
|
|
gosec.context.Config = gosec.config
|
|
|
|
gosec.context.Comments = ast.NewCommentMap(gosec.context.FileSet, file, file.Comments)
|
|
|
|
gosec.context.Root = file
|
|
|
|
gosec.context.Info = pkg.TypesInfo
|
|
|
|
gosec.context.Pkg = pkg.Types
|
|
|
|
gosec.context.PkgFiles = pkg.Syntax
|
|
|
|
gosec.context.Imports = NewImportTracker()
|
2019-04-29 17:31:53 +01:00
|
|
|
gosec.context.Imports.TrackFile(file)
|
2019-04-27 09:01:27 +01:00
|
|
|
ast.Walk(gosec, file)
|
|
|
|
gosec.stats.NumFiles++
|
|
|
|
gosec.stats.NumLines += pkg.Fset.File(file.Pos()).LineCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 12:53:22 +01:00
|
|
|
// ParseErrors parses the errors from given package
|
|
|
|
func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error {
|
2019-04-27 09:01:27 +01:00
|
|
|
if len(pkg.Errors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, pkgErr := range pkg.Errors {
|
2019-04-30 12:53:22 +01:00
|
|
|
parts := strings.Split(pkgErr.Pos, ":")
|
|
|
|
file := parts[0]
|
2019-04-30 07:13:10 +01:00
|
|
|
var err error
|
2019-04-30 12:53:22 +01:00
|
|
|
var line int
|
|
|
|
if len(parts) > 1 {
|
|
|
|
if line, err = strconv.Atoi(parts[1]); err != nil {
|
2019-04-30 07:13:10 +01:00
|
|
|
return fmt.Errorf("parsing line: %v", err)
|
|
|
|
}
|
2019-04-30 12:53:22 +01:00
|
|
|
}
|
|
|
|
var column int
|
|
|
|
if len(parts) > 2 {
|
|
|
|
if column, err = strconv.Atoi(parts[2]); err != nil {
|
2019-04-30 07:13:10 +01:00
|
|
|
return fmt.Errorf("parsing column: %v", err)
|
|
|
|
}
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
2019-04-30 12:53:22 +01:00
|
|
|
msg := strings.TrimSpace(pkgErr.Msg)
|
|
|
|
newErr := NewError(line, column, msg)
|
|
|
|
if errSlice, ok := gosec.errors[file]; ok {
|
|
|
|
gosec.errors[file] = append(errSlice, *newErr)
|
2019-04-27 09:01:27 +01:00
|
|
|
} else {
|
2019-04-30 07:13:10 +01:00
|
|
|
errSlice = []Error{}
|
2019-04-30 12:53:22 +01:00
|
|
|
gosec.errors[file] = append(errSlice, *newErr)
|
2019-04-27 09:01:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
return nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:57:32 +01:00
|
|
|
// AppendError appends an error to the file errors
|
|
|
|
func (gosec *Analyzer) AppendError(file string, err error) {
|
2019-04-30 16:14:26 +01:00
|
|
|
// Do not report the error for empty packages (e.g. files excluded from build with a tag)
|
2019-04-30 15:57:32 +01:00
|
|
|
r := regexp.MustCompile(`no buildable Go source files in`)
|
|
|
|
if r.MatchString(err.Error()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errors := []Error{}
|
|
|
|
if ferrs, ok := gosec.errors[file]; ok {
|
|
|
|
errors = ferrs
|
|
|
|
}
|
|
|
|
ferr := NewError(0, 0, err.Error())
|
|
|
|
errors = append(errors, *ferr)
|
|
|
|
gosec.errors[file] = errors
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:45:59 +00:00
|
|
|
// ignore a node (and sub-tree) if it is tagged with a "#nosec" comment
|
2018-07-19 17:42:25 +01:00
|
|
|
func (gosec *Analyzer) ignore(n ast.Node) ([]string, bool) {
|
|
|
|
if groups, ok := gosec.context.Comments[n]; ok && !gosec.ignoreNosec {
|
2016-07-20 11:02:01 +01:00
|
|
|
for _, group := range groups {
|
2016-12-02 21:45:59 +00:00
|
|
|
if strings.Contains(group.Text(), "#nosec") {
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.stats.NumNosec++
|
2017-10-05 22:32:03 +01:00
|
|
|
|
|
|
|
// Pull out the specific rules that are listed to be ignored.
|
2019-04-30 10:32:06 +01:00
|
|
|
re := regexp.MustCompile(`(G\d{3})`)
|
2017-10-05 22:32:03 +01:00
|
|
|
matches := re.FindAllStringSubmatch(group.Text(), -1)
|
|
|
|
|
2018-03-08 19:01:00 +00:00
|
|
|
// If no specific rules were given, ignore everything.
|
2019-04-30 10:32:06 +01:00
|
|
|
if len(matches) == 0 {
|
2018-03-08 19:01:00 +00:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
// Find the rule IDs to ignore.
|
2018-03-03 00:04:48 +00:00
|
|
|
var ignores []string
|
2017-10-05 22:32:03 +01:00
|
|
|
for _, v := range matches {
|
|
|
|
ignores = append(ignores, v[1])
|
|
|
|
}
|
|
|
|
return ignores, false
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 22:32:03 +01:00
|
|
|
return nil, false
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
// Visit runs the gosec visitor logic over an AST created by parsing go code.
|
2016-08-12 14:17:28 +01:00
|
|
|
// Rule methods added with AddRule will be invoked as necessary.
|
2018-07-19 17:42:25 +01:00
|
|
|
func (gosec *Analyzer) Visit(n ast.Node) ast.Visitor {
|
2017-10-05 22:32:03 +01:00
|
|
|
// If we've reached the end of this branch, pop off the ignores stack.
|
|
|
|
if n == nil {
|
2018-07-19 17:42:25 +01:00
|
|
|
if len(gosec.context.Ignores) > 0 {
|
|
|
|
gosec.context.Ignores = gosec.context.Ignores[1:]
|
2017-10-05 22:32:03 +01:00
|
|
|
}
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec
|
2017-10-05 22:32:03 +01:00
|
|
|
}
|
2016-11-07 17:13:20 +00:00
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
// Get any new rule exclusions.
|
2018-07-19 17:42:25 +01:00
|
|
|
ignoredRules, ignoreAll := gosec.ignore(n)
|
2017-10-05 22:32:03 +01:00
|
|
|
if ignoreAll {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
// Now create the union of exclusions.
|
2019-04-30 10:32:06 +01:00
|
|
|
ignores := map[string]bool{}
|
2018-07-19 17:42:25 +01:00
|
|
|
if len(gosec.context.Ignores) > 0 {
|
|
|
|
for k, v := range gosec.context.Ignores[0] {
|
2017-10-05 22:32:03 +01:00
|
|
|
ignores[k] = v
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 22:32:03 +01:00
|
|
|
|
|
|
|
for _, v := range ignoredRules {
|
|
|
|
ignores[v] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the new set onto the stack.
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.context.Ignores = append([]map[string]bool{ignores}, gosec.context.Ignores...)
|
2017-10-05 22:32:03 +01:00
|
|
|
|
|
|
|
// Track aliased and initialization imports
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.context.Imports.TrackImport(n)
|
2017-10-05 22:32:03 +01:00
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
for _, rule := range gosec.ruleset.RegisteredFor(n) {
|
2017-10-05 22:32:03 +01:00
|
|
|
if _, ok := ignores[rule.ID()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-19 17:42:25 +01:00
|
|
|
issue, err := rule.Match(n, gosec.context)
|
2017-10-05 22:32:03 +01:00
|
|
|
if err != nil {
|
2018-07-19 17:42:25 +01:00
|
|
|
file, line := GetLocation(n, gosec.context)
|
2017-10-05 22:32:03 +01:00
|
|
|
file = path.Base(file)
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line)
|
2017-10-05 22:32:03 +01:00
|
|
|
}
|
|
|
|
if issue != nil {
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.issues = append(gosec.issues, issue)
|
|
|
|
gosec.stats.NumFound++
|
2017-10-05 22:32:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
|
|
|
|
// Report returns the current issues discovered and the metrics about the scan
|
2019-02-26 22:24:06 +00:00
|
|
|
func (gosec *Analyzer) Report() ([]*Issue, *Metrics, map[string][]Error) {
|
|
|
|
return gosec.issues, gosec.stats, gosec.errors
|
2017-05-10 05:26:53 +01:00
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
// Reset clears state such as context, issues and metrics from the configured analyzer
|
2018-07-19 17:42:25 +01:00
|
|
|
func (gosec *Analyzer) Reset() {
|
|
|
|
gosec.context = &Context{}
|
|
|
|
gosec.issues = make([]*Issue, 0, 16)
|
|
|
|
gosec.stats = &Metrics{}
|
2019-06-25 10:14:27 +01:00
|
|
|
gosec.ruleset = NewRuleSet()
|
2017-07-19 22:17:00 +01:00
|
|
|
}
|