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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-05-10 05:26:53 +01:00
|
|
|
"regexp"
|
2016-08-10 12:51:03 +01:00
|
|
|
"sort"
|
2016-07-20 11:02:01 +01:00
|
|
|
"strings"
|
|
|
|
|
2017-04-26 16:08:46 +01:00
|
|
|
"github.com/GoASTScanner/gas"
|
2017-04-26 01:57:12 +01:00
|
|
|
"github.com/GoASTScanner/gas/output"
|
2017-04-28 22:46:26 +01:00
|
|
|
"github.com/GoASTScanner/gas/rules"
|
2017-05-10 05:26:53 +01:00
|
|
|
"github.com/kisielk/gotool"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2016-12-02 18:40:36 +00:00
|
|
|
const (
|
|
|
|
usageText = `
|
2016-07-20 11:02:01 +01:00
|
|
|
GAS - Go AST Scanner
|
|
|
|
|
|
|
|
Gas analyzes Go source code to look for common programming mistakes that
|
|
|
|
can lead to security problems.
|
|
|
|
|
2018-03-12 22:57:10 +00:00
|
|
|
VERSION: %s
|
|
|
|
GIT TAG: %s
|
|
|
|
BUILD DATE: %s
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
USAGE:
|
|
|
|
|
2017-04-26 00:01:28 +01:00
|
|
|
# Check a single package
|
|
|
|
$ gas $GOPATH/src/github.com/example/project
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2017-04-26 00:01:28 +01:00
|
|
|
# Check all packages under the current directory and save results in
|
2016-07-20 11:02:01 +01:00
|
|
|
# json format.
|
|
|
|
$ gas -fmt=json -out=results.json ./...
|
|
|
|
|
|
|
|
# Run a specific set of rules (by default all rules will be run):
|
2017-12-13 06:35:54 +00:00
|
|
|
$ gas -include=G101,G203,G401 ./...
|
2016-08-11 13:14:19 +01:00
|
|
|
|
|
|
|
# Run all rules except the provided
|
2017-12-13 06:35:54 +00:00
|
|
|
$ gas -exclude=G101 $GOPATH/src/github.com/example/project/...
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
`
|
2016-12-02 18:40:36 +00:00
|
|
|
)
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
var (
|
|
|
|
// #nosec flag
|
|
|
|
flagIgnoreNoSec = flag.Bool("nosec", false, "Ignores #nosec comments when set")
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// format output
|
2018-03-05 12:20:24 +00:00
|
|
|
flagFormat = flag.String("fmt", "text", "Set output format. Valid options are: json, yaml, csv, junit-xml, html, or text")
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// output file
|
|
|
|
flagOutput = flag.String("out", "", "Set output file for results")
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// config file
|
|
|
|
flagConfig = flag.String("conf", "", "Path to optional config file")
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// quiet
|
|
|
|
flagQuiet = flag.Bool("quiet", false, "Only show output when errors are found")
|
|
|
|
|
|
|
|
// rules to explicitly include
|
|
|
|
flagRulesInclude = flag.String("include", "", "Comma separated list of rules IDs to include. (see rule list)")
|
|
|
|
|
|
|
|
// rules to explicitly exclude
|
|
|
|
flagRulesExclude = flag.String("exclude", "", "Comma separated list of rules IDs to exclude. (see rule list)")
|
|
|
|
|
|
|
|
// log to file or stderr
|
|
|
|
flagLogfile = flag.String("log", "", "Log messages to file rather than stderr")
|
|
|
|
|
2018-02-08 11:08:05 +00:00
|
|
|
// sort the issues by severity
|
|
|
|
flagSortIssues = flag.Bool("sort", true, "Sort issues by severity")
|
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
logger *log.Logger
|
|
|
|
)
|
2016-08-05 14:27:21 +01:00
|
|
|
|
2016-12-02 18:20:23 +00:00
|
|
|
// #nosec
|
2016-07-20 11:02:01 +01:00
|
|
|
func usage() {
|
2016-12-02 18:20:23 +00:00
|
|
|
|
2018-03-12 22:57:10 +00:00
|
|
|
usageText := fmt.Sprintf(usageText, Version, GitTag, BuildDate)
|
2016-07-20 11:02:01 +01:00
|
|
|
fmt.Fprintln(os.Stderr, usageText)
|
|
|
|
fmt.Fprint(os.Stderr, "OPTIONS:\n\n")
|
|
|
|
flag.PrintDefaults()
|
2016-08-10 12:51:03 +01:00
|
|
|
fmt.Fprint(os.Stderr, "\n\nRULES:\n\n")
|
|
|
|
|
2017-12-14 00:04:22 +00:00
|
|
|
// sorted rule list for ease of reading
|
2017-04-28 22:46:26 +01:00
|
|
|
rl := rules.Generate()
|
2016-08-10 12:51:03 +01:00
|
|
|
keys := make([]string, 0, len(rl))
|
|
|
|
for key := range rl {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
v := rl[k]
|
2017-04-28 22:46:26 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "\t%s: %s\n", k, v.Description)
|
2016-08-10 12:51:03 +01:00
|
|
|
}
|
|
|
|
fmt.Fprint(os.Stderr, "\n")
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
func loadConfig(configFile string) (gas.Config, error) {
|
|
|
|
config := gas.NewConfig()
|
|
|
|
if configFile != "" {
|
2018-03-08 23:23:27 +00:00
|
|
|
// #nosec
|
2017-05-10 05:26:53 +01:00
|
|
|
file, err := os.Open(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if _, err := config.ReadFrom(file); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
if *flagIgnoreNoSec {
|
|
|
|
config.SetGlobal("nosec", "true")
|
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
return config, nil
|
|
|
|
}
|
2017-04-26 00:01:28 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
func loadRules(include, exclude string) rules.RuleList {
|
2017-12-13 07:39:00 +00:00
|
|
|
var filters []rules.RuleFilter
|
2017-05-10 05:26:53 +01:00
|
|
|
if include != "" {
|
2018-01-23 00:02:20 +00:00
|
|
|
logger.Printf("including rules: %s", include)
|
2017-05-10 05:26:53 +01:00
|
|
|
including := strings.Split(include, ",")
|
|
|
|
filters = append(filters, rules.NewRuleFilter(false, including...))
|
2017-12-14 00:04:22 +00:00
|
|
|
} else {
|
2018-01-23 00:02:20 +00:00
|
|
|
logger.Println("including rules: default")
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
if exclude != "" {
|
2018-01-23 00:02:20 +00:00
|
|
|
logger.Printf("excluding rules: %s", exclude)
|
2017-05-10 05:26:53 +01:00
|
|
|
excluding := strings.Split(exclude, ",")
|
|
|
|
filters = append(filters, rules.NewRuleFilter(true, excluding...))
|
2017-12-14 00:04:22 +00:00
|
|
|
} else {
|
2018-01-23 00:02:20 +00:00
|
|
|
logger.Println("excluding rules: default")
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
return rules.Generate(filters...)
|
|
|
|
}
|
2017-04-26 00:01:28 +01:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
func saveOutput(filename, format string, issues []*gas.Issue, metrics *gas.Metrics) error {
|
|
|
|
if filename != "" {
|
|
|
|
outfile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
defer outfile.Close()
|
2018-02-07 13:07:24 +00:00
|
|
|
err = output.CreateReport(outfile, format, issues, metrics)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
} else {
|
2018-02-07 13:07:24 +00:00
|
|
|
err := output.CreateReport(os.Stdout, format, issues, metrics)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
return nil
|
2017-04-26 00:01:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Setup usage description
|
|
|
|
flag.Usage = usage
|
|
|
|
|
|
|
|
// Parse command line arguments
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Ensure at least one file was specified
|
|
|
|
if flag.NArg() == 0 {
|
2018-02-07 13:07:24 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\nError: FILE [FILE...] or './...' expected\n") // #nosec
|
2016-07-20 11:02:01 +01:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// Setup logging
|
|
|
|
logWriter := os.Stderr
|
|
|
|
if *flagLogfile != "" {
|
|
|
|
var e error
|
|
|
|
logWriter, e = os.Create(*flagLogfile)
|
|
|
|
if e != nil {
|
|
|
|
flag.Usage()
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
logger = log.New(logWriter, "[gas] ", log.LstdFlags)
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2017-04-28 22:46:26 +01:00
|
|
|
// Load config
|
2017-05-10 05:26:53 +01:00
|
|
|
config, err := loadConfig(*flagConfig)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load enabled rule definitions
|
|
|
|
ruleDefinitions := loadRules(*flagRulesInclude, *flagRulesExclude)
|
2017-12-14 00:04:22 +00:00
|
|
|
if len(ruleDefinitions) <= 0 {
|
2018-01-23 00:02:20 +00:00
|
|
|
logger.Fatal("cannot continue: no rules are configured.")
|
2017-12-14 00:04:22 +00:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
|
|
|
|
// Create the analyzer
|
|
|
|
analyzer := gas.NewAnalyzer(config, logger)
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(ruleDefinitions.Builders())
|
2017-05-10 05:26:53 +01:00
|
|
|
|
|
|
|
vendor := regexp.MustCompile(`[\\/]vendor([\\/]|$)`)
|
|
|
|
|
2018-01-07 23:02:33 +00:00
|
|
|
var packages []string
|
2017-05-10 05:26:53 +01:00
|
|
|
// Iterate over packages on the import paths
|
|
|
|
for _, pkg := range gotool.ImportPaths(flag.Args()) {
|
|
|
|
|
|
|
|
// Skip vendor directory
|
|
|
|
if vendor.MatchString(pkg) {
|
|
|
|
continue
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2018-01-07 23:02:33 +00:00
|
|
|
packages = append(packages, pkg)
|
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
|
2018-01-07 23:02:33 +00:00
|
|
|
if err := analyzer.Process(packages...); err != nil {
|
|
|
|
logger.Fatal(err)
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2017-05-10 05:26:53 +01:00
|
|
|
|
|
|
|
// Collect the results
|
|
|
|
issues, metrics := analyzer.Report()
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2017-04-26 00:01:28 +01:00
|
|
|
issuesFound := len(issues) > 0
|
2016-11-04 18:36:55 +00:00
|
|
|
// Exit quietly if nothing was found
|
|
|
|
if !issuesFound && *flagQuiet {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:08:05 +00:00
|
|
|
// Sort the issue by severity
|
|
|
|
if *flagSortIssues {
|
2018-02-10 18:45:04 +00:00
|
|
|
sortIssues(issues)
|
2018-02-08 11:08:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
// Create output report
|
2017-05-10 05:26:53 +01:00
|
|
|
if err := saveOutput(*flagOutput, *flagFormat, issues, metrics); err != nil {
|
|
|
|
logger.Fatal(err)
|
2017-04-26 01:57:12 +01:00
|
|
|
}
|
2016-12-02 18:40:36 +00:00
|
|
|
|
2017-05-10 05:26:53 +01:00
|
|
|
// Finialize logging
|
2018-02-07 13:07:24 +00:00
|
|
|
logWriter.Close() // #nosec
|
2017-05-10 05:26:53 +01:00
|
|
|
|
2017-04-26 00:01:28 +01:00
|
|
|
// Do we have an issue? If so exit 1
|
|
|
|
if issuesFound {
|
|
|
|
os.Exit(1)
|
2016-12-02 18:40:36 +00:00
|
|
|
}
|
|
|
|
}
|