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.
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// Package core holds the central scanning logic used by GAS
|
2016-07-20 11:02:01 +01:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/importer"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"go/types"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// this data in conjunction withe the encoutered 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
|
2016-08-05 11:04:06 +01:00
|
|
|
Root *ast.File
|
|
|
|
Config map[string]interface{}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// The Rule interface used by all rules supported by GAS.
|
2016-07-20 11:02:01 +01:00
|
|
|
type Rule interface {
|
|
|
|
Match(ast.Node, *Context) (*Issue, error)
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// A RuleSet maps lists of rules to the type of AST node they should be run on.
|
|
|
|
// The anaylzer will only invoke rules contained in the list associated with the
|
|
|
|
// type of AST node it is currently visiting.
|
2016-07-20 11:02:01 +01:00
|
|
|
type RuleSet map[reflect.Type][]Rule
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// The Analyzer object is the main object of GAS. It has methods traverse an AST
|
|
|
|
// 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
|
|
|
|
context Context
|
|
|
|
logger *log.Logger
|
2016-07-26 00:39:55 +01:00
|
|
|
Issues []Issue `json:"issues"`
|
|
|
|
Stats Metrics `json:"metrics"`
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// NewAnalyzer buildas a new anaylzer.
|
2016-08-05 14:27:21 +01:00
|
|
|
func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer {
|
2016-07-20 11:02:01 +01:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.New(os.Stdout, "[gas]", 0)
|
|
|
|
}
|
2016-07-29 11:19:50 +01:00
|
|
|
a := Analyzer{
|
2016-08-05 14:27:21 +01:00
|
|
|
ignoreNosec: conf["ignoreNosec"].(bool),
|
2016-07-20 11:02:01 +01:00
|
|
|
ruleset: make(RuleSet),
|
|
|
|
Issues: make([]Issue, 0),
|
2016-08-05 11:04:06 +01:00
|
|
|
context: Context{token.NewFileSet(), nil, nil, nil, nil, nil},
|
2016-07-20 11:02:01 +01:00
|
|
|
logger: logger,
|
|
|
|
}
|
2016-07-29 11:19:50 +01:00
|
|
|
|
2016-08-05 14:27:21 +01:00
|
|
|
// TODO(tkelsey): use the inc/exc lists
|
2016-07-29 11:19:50 +01:00
|
|
|
|
|
|
|
return a
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gas *Analyzer) process(filename string, source interface{}) error {
|
|
|
|
mode := parser.ParseComments
|
|
|
|
root, err := parser.ParseFile(gas.context.FileSet, filename, source, mode)
|
|
|
|
if err == nil {
|
|
|
|
gas.context.Comments = ast.NewCommentMap(gas.context.FileSet, root, root.Comments)
|
2016-08-05 11:04:06 +01:00
|
|
|
gas.context.Root = root
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
// here we get type info
|
|
|
|
gas.context.Info = &types.Info{
|
2016-08-03 14:54:17 +01:00
|
|
|
Types: make(map[ast.Expr]types.TypeAndValue),
|
|
|
|
Defs: make(map[*ast.Ident]types.Object),
|
|
|
|
Uses: make(map[*ast.Ident]types.Object),
|
|
|
|
Selections: make(map[*ast.SelectorExpr]*types.Selection),
|
|
|
|
Scopes: make(map[ast.Node]*types.Scope),
|
|
|
|
Implicits: make(map[ast.Node]types.Object),
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := types.Config{Importer: importer.Default()}
|
|
|
|
gas.context.Pkg, _ = conf.Check("pkg", gas.context.FileSet, []*ast.File{root}, gas.context.Info)
|
2016-07-25 21:47:40 +01:00
|
|
|
if err != nil {
|
|
|
|
gas.logger.Println("failed to check imports")
|
|
|
|
return err
|
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
ast.Walk(gas, root)
|
|
|
|
gas.Stats.NumFiles++
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// AddRule adds a rule into a rule set list mapped to the given AST node's type.
|
|
|
|
// The node is only needed for its type and is not otherwise used.
|
2016-07-20 11:02:01 +01:00
|
|
|
func (gas *Analyzer) AddRule(r Rule, n ast.Node) {
|
|
|
|
t := reflect.TypeOf(n)
|
|
|
|
if val, ok := gas.ruleset[t]; ok {
|
|
|
|
gas.ruleset[t] = append(val, r)
|
|
|
|
} else {
|
|
|
|
gas.ruleset[t] = []Rule{r}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// Process reads in a source file, convert it to an AST and traverse it.
|
|
|
|
// Rule methods added with AddRule will be invoked as necessary.
|
2016-07-20 11:02:01 +01:00
|
|
|
func (gas *Analyzer) Process(filename string) error {
|
|
|
|
err := gas.process(filename, nil)
|
|
|
|
fun := func(f *token.File) bool {
|
|
|
|
gas.Stats.NumLines += f.LineCount()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
gas.context.FileSet.Iterate(fun)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// ProcessSource will convert a source code string into an AST and traverse it.
|
|
|
|
// Rule methods added with AddRule will be invoked as necessary. The string is
|
|
|
|
// identified by the filename given but no file IO will be done.
|
2016-07-20 11:02:01 +01:00
|
|
|
func (gas *Analyzer) ProcessSource(filename string, source string) error {
|
|
|
|
err := gas.process(filename, source)
|
|
|
|
fun := func(f *token.File) bool {
|
|
|
|
gas.Stats.NumLines += f.LineCount()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
gas.context.FileSet.Iterate(fun)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// ignore a node (and sub-tree) if it is tagged with a "nosec" comment
|
|
|
|
func (gas *Analyzer) ignore(n ast.Node) bool {
|
2016-07-28 12:51:25 +01:00
|
|
|
if groups, ok := gas.context.Comments[n]; ok && !gas.ignoreNosec {
|
2016-07-20 11:02:01 +01:00
|
|
|
for _, group := range groups {
|
|
|
|
if strings.Contains(group.Text(), "nosec") {
|
|
|
|
gas.Stats.NumNosec++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// Visit runs the GAS visitor logic over an AST created by parsing go code.
|
|
|
|
// Rule methods added with AddRule will be invoked as necessary.
|
2016-07-20 11:02:01 +01:00
|
|
|
func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
|
2016-08-12 14:17:28 +01:00
|
|
|
if !gas.ignore(n) {
|
2016-07-20 11:02:01 +01:00
|
|
|
if val, ok := gas.ruleset[reflect.TypeOf(n)]; ok {
|
|
|
|
for _, rule := range val {
|
|
|
|
ret, err := rule.Match(n, &gas.context)
|
|
|
|
if err != nil {
|
|
|
|
// will want to give more info than this ...
|
|
|
|
gas.logger.Println("internal error running rule:", err)
|
|
|
|
}
|
|
|
|
if ret != nil {
|
|
|
|
gas.Issues = append(gas.Issues, *ret)
|
|
|
|
gas.Stats.NumFound++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 12:51:25 +01:00
|
|
|
return gas
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
2016-07-28 12:51:25 +01:00
|
|
|
return nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|