gosec

package module
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2020 License: Apache-2.0 Imports: 22 Imported by: 25

README

gosec - Golang Security Checker

Inspects source code for security problems by scanning the Go AST.

License

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 here.

Project status

CII Best Practices Build Status Coverage Status GoReport GoDoc Docs Downloads Docker Pulls Slack

Install

CI Installation
# binary will be $GOPATH/bin/gosec
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $GOPATH/bin vX.Y.Z

# or install it into ./bin/
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s vX.Y.Z

# In alpine linux (as it does not come with curl by default)
wget -O - -q https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s vX.Y.Z

# If you want to use the checksums provided on the "Releases" page
# then you will have to download a tar.gz file for your operating system instead of a binary file
wget https://github.com/securego/gosec/releases/download/vX.Y.Z/gosec_vX.Y.Z_OS.tar.gz

# The file will be in the current folder where you run the command 
# and you can check the checksum like this
echo "<check sum from the check sum file>  gosec_vX.Y.Z_OS.tar.gz" | sha256sum -c -

gosec --help
GitHub Action

You can run gosec as a GitHub action as follows:

name: Run Gosec
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  tests:
    runs-on: ubuntu-latest
    env:
      GO111MODULE: on
    steps:
      - name: Checkout Source 
        uses: actions/checkout@v2
      - name: Run Gosec Security Scanner
        uses: securego/gosec@master
        with:
          args: ./...
Local Installation
go get github.com/securego/gosec/cmd/gosec

Usage

Gosec can be configured to only run a subset of rules, to exclude certain file paths, and produce reports in different formats. By default all rules will be run against the supplied input files. To recursively scan from the current directory you can supply ./... as the input argument.

Available rules
  • G101: Look for hard coded credentials
  • G102: Bind to all interfaces
  • G103: Audit the use of unsafe block
  • G104: Audit errors not checked
  • G106: Audit the use of ssh.InsecureIgnoreHostKey
  • G107: Url provided to HTTP request as taint input
  • G108: Profiling endpoint automatically exposed on /debug/pprof
  • G109: Potential Integer overflow made by strconv.Atoi result conversion to int16/32
  • G110: Potential DoS vulnerability via decompression bomb
  • G201: SQL query construction using format string
  • G202: SQL query construction using string concatenation
  • G203: Use of unescaped data in HTML templates
  • G204: Audit use of command execution
  • G301: Poor file permissions used when creating a directory
  • G302: Poor file permissions used with chmod
  • G303: Creating tempfile using a predictable path
  • G304: File path provided as taint input
  • G305: File traversal when extracting zip archive
  • G306: Poor file permissions used when writing to a new file
  • G307: Deferring a method which returns an error
  • G401: Detect the usage of DES, RC4, MD5 or SHA1
  • G402: Look for bad TLS connection settings
  • G403: Ensure minimum RSA key length of 2048 bits
  • G404: Insecure random number source (rand)
  • G501: Import blacklist: crypto/md5
  • G502: Import blacklist: crypto/des
  • G503: Import blacklist: crypto/rc4
  • G504: Import blacklist: net/http/cgi
  • G505: Import blacklist: crypto/sha1
  • G601: Implicit memory aliasing of items from a range statement
Retired rules
Selecting rules

By default, gosec will run all rules against the supplied file paths. It is however possible to select a subset of rules to run via the -include= flag, or to specify a set of rules to explicitly exclude using the -exclude= flag.

# Run a specific set of rules
$ gosec -include=G101,G203,G401 ./...

# Run everything except for rule G303
$ gosec -exclude=G303 ./...
CWE Mapping

Every issue detected by gosec is mapped to a CWE (Common Weakness Enumeration) which describes in more generic terms the vulnerability. The exact mapping can be found here.

Configuration

A number of global settings can be provided in a configuration file as follows:

{
    "global": {
        "nosec": "enabled",
        "audit": "enabled"
    }
}
  • nosec: this setting will overwrite all #nosec directives defined throughout the code base
  • audit: runs in audit mode which enables addition checks that for normal code analysis might be too nosy
# Run with a global configuration file
$ gosec -conf config.json .

Also some rules accept configuration. For instance on rule G104, it is possible to define packages along with a list of functions which will be skipped when auditing the not checked errors:

{
    "G104": {
        "io/ioutil": ["WriteFile"]
    }
}

You can also configure the hard-coded credentials rule G101 with additional patters, or adjust the entropy threshold:

{
    "G101": {
        "pattern": "(?i)passwd|pass|password|pwd|secret|private_key|token",
         "ingnore_entropy": false,
         "entropy_threshold": "80.0",
         "per_char_threshold": "3.0",
         "trucate": "32"
    }
}
Dependencies

gosec will fetch automatically the dependencies of the code which is being analyzed when go module is turned on (e.g. GO111MODULE=on). If this is not the case, the dependencies need to be explicitly downloaded by running the go get -d command before the scan.

Excluding test files and folders

gosec will ignore test files across all packages and any dependencies in your vendor directory.

The scanning of test files can be enabled with the following flag:


gosec -tests ./...

Also additional folders can be excluded as follows:

 gosec -exclude-dir=rules -exclude-dir=cmd ./...
Annotating code

As with all automated detection tools, there will be cases of false positives. In cases where gosec reports a failure that has been manually verified as being safe, it is possible to annotate the code with a #nosec comment.

The annotation causes gosec to stop processing any further nodes within the AST so can apply to a whole block or more granularly to a single expression.


import "md5" // #nosec


func main(){

    /* #nosec */
    if x > y {
        h := md5.New() // this will also be ignored
    }

}

When a specific false positive has been identified and verified as safe, you may wish to suppress only that single rule (or a specific set of rules) within a section of code, while continuing to scan for other problems. To do this, you can list the rule(s) to be suppressed within the #nosec annotation, e.g: /* #nosec G401 */ or // #nosec G201 G202 G203

In some cases you may also want to revisit places where #nosec annotations have been used. To run the scanner and ignore any #nosec annotations you can do the following:

gosec -nosec=true ./...
Build tags

gosec is able to pass your Go build tags to the analyzer. They can be provided as a comma separated list as follows:

gosec -tag debug,ignore ./...
Output formats

gosec currently supports text, json, yaml, csv, sonarqube, JUnit XML, html and golint output formats. By default results will be reported to stdout, but can also be written to an output file. The output format is controlled by the -fmt flag, and the output file is controlled by the -out flag as follows:

# Write output in json format to results.json
$ gosec -fmt=json -out=results.json *.go

Development

Build

You can build the binary with:

make
Tests

You can run all unit tests using:

make test
Release

You can create a release by tagging the version as follows:

git tag v1.0.0 -m "Release version v1.0.0"
git push origin v1.0.0

The GitHub release workflow triggers immediately after the tag is pushed upstream. This flow will release the binaries using the goreleaser action and then it will build and publish the docker image into Docker Hub.

Docker image

You can also build locally the docker image by using the command:

make image

You can run the gosec tool in a container against your local Go project. You only have to mount the project into a volume as follows:

docker run -it -v <YOUR PROJECT PATH>/<PROJECT>:/<PROJECT> securego/gosec /<PROJECT>/...
Generate TLS rule

The configuration of TLS rule can be generated from Mozilla's TLS ciphers recommendation.

First you need to install the generator tool:

go get github.com/securego/gosec/cmd/tlsconfig/...

You can invoke now the go generate in the root of the project:

go generate ./...

This will generate the rules/tls_config.go file which will contain the current ciphers recommendation from Mozilla.

Documentation

Overview

Package gosec holds the central scanning logic used by gosec security scanner

Index

Constants

View Source
const (
	// Globals are applicable to all rules and used for general
	// configuration settings for gosec.
	Globals = "global"
)

LoadMode controls the amount of details to return when loading the packages

Variables

View Source
var IssueToCWE = map[string]Cwe{
	"G101": GetCwe("798"),
	"G102": GetCwe("200"),
	"G103": GetCwe("242"),
	"G104": GetCwe("703"),
	"G106": GetCwe("322"),
	"G107": GetCwe("88"),
	"G109": GetCwe("190"),
	"G110": GetCwe("409"),
	"G201": GetCwe("89"),
	"G202": GetCwe("89"),
	"G203": GetCwe("79"),
	"G204": GetCwe("78"),
	"G301": GetCwe("276"),
	"G302": GetCwe("276"),
	"G303": GetCwe("377"),
	"G304": GetCwe("22"),
	"G305": GetCwe("22"),
	"G401": GetCwe("326"),
	"G402": GetCwe("295"),
	"G403": GetCwe("310"),
	"G404": GetCwe("338"),
	"G501": GetCwe("327"),
	"G502": GetCwe("327"),
	"G503": GetCwe("327"),
	"G504": GetCwe("327"),
	"G505": GetCwe("327"),
}

IssueToCWE maps gosec rules to CWEs

Functions

func ConcatString

func ConcatString(n *ast.BinaryExpr) (string, bool)

ConcatString recursively concatenates strings from a binary expression

func ExcludedDirsRegExp

func ExcludedDirsRegExp(excludedDirs []string) []*regexp.Regexp

ExcludedDirsRegExp builds the regexps for a list of excluded dirs provided as strings

func FindVarIdentities

func FindVarIdentities(n *ast.BinaryExpr, c *Context) ([]*ast.Ident, bool)

FindVarIdentities returns array of all variable identities in a given binary expression

func GetCallInfo

func GetCallInfo(n ast.Node, ctx *Context) (string, string, error)

GetCallInfo returns the package or type and name associated with a call expression.

func GetCallObject

func GetCallObject(n ast.Node, ctx *Context) (*ast.CallExpr, types.Object)

GetCallObject returns the object and call expression and associated object for a given AST node. nil, nil will be returned if the object cannot be resolved.

func GetCallStringArgsValues

func GetCallStringArgsValues(n ast.Node, ctx *Context) []string

GetCallStringArgsValues returns the values of strings arguments if they can be resolved

func GetChar

func GetChar(n ast.Node) (byte, error)

GetChar will read and return a char value from an ast.BasicLit

func GetFloat

func GetFloat(n ast.Node) (float64, error)

GetFloat will read and return a float value from an ast.BasicLit

func GetIdentStringValues

func GetIdentStringValues(ident *ast.Ident) []string

GetIdentStringValues return the string values of an Ident if they can be resolved

func GetImportPath

func GetImportPath(name string, ctx *Context) (string, bool)

GetImportPath resolves the full import path of an identifier based on the imports in the current context.

func GetImportedName

func GetImportedName(path string, ctx *Context) (string, bool)

GetImportedName returns the name used for the package within the code. It will resolve aliases and ignores initialization only imports.

func GetInt

func GetInt(n ast.Node) (int64, error)

GetInt will read and return an integer value from an ast.BasicLit

func GetLocation

func GetLocation(n ast.Node, ctx *Context) (string, int)

GetLocation returns the filename and line number of an ast.Node

func GetPkgAbsPath

func GetPkgAbsPath(pkgPath string) (string, error)

GetPkgAbsPath returns the Go package absolute path derived from the given path

func GetPkgRelativePath

func GetPkgRelativePath(path string) (string, error)

GetPkgRelativePath returns the Go relative relative path derived form the given path

func GetString

func GetString(n ast.Node) (string, error)

GetString will read and return a string value from an ast.BasicLit

func Getenv

func Getenv(key, userDefault string) string

Getenv returns the values of the environment variable, otherwise returns the default if variable is not set

func Gopath

func Gopath() []string

Gopath returns all GOPATHs

func MatchCallByPackage

func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool)

MatchCallByPackage ensures that the specified package is imported, adjusts the name for any aliases and ignores cases that are initialization only imports.

Usage:

node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")

func MatchCompLit

func MatchCompLit(n ast.Node, ctx *Context, required string) *ast.CompositeLit

MatchCompLit will match an ast.CompositeLit based on the supplied type

func PackagePaths

func PackagePaths(root string, excludes []*regexp.Regexp) ([]string, error)

PackagePaths returns a slice with all packages path at given root directory

func RootPath

func RootPath(root string) (string, error)

RootPath returns the absolute root path of a scan

func TryResolve

func TryResolve(n ast.Node, c *Context) bool

TryResolve will attempt, given a subtree starting at some AST node, to resolve all values contained within to a known constant. It is used to check for any unknown values in compound expressions.

Types

type Analyzer

type Analyzer struct {
	// contains filtered or unexported fields
}

Analyzer object is the main object of gosec. It has methods traverse an AST and invoke the correct checking rules as on each node as required.

func NewAnalyzer

func NewAnalyzer(conf Config, tests bool, logger *log.Logger) *Analyzer

NewAnalyzer builds a new analyzer.

func (*Analyzer) AppendError

func (gosec *Analyzer) AppendError(file string, err error)

AppendError appends an error to the file errors

func (*Analyzer) Check

func (gosec *Analyzer) Check(pkg *packages.Package)

Check runs analysis on the given package

func (*Analyzer) Config

func (gosec *Analyzer) Config() Config

Config returns the current configuration

func (*Analyzer) LoadRules

func (gosec *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder)

LoadRules instantiates all the rules to be used when analyzing source packages

func (*Analyzer) ParseErrors

func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error

ParseErrors parses the errors from given package

func (*Analyzer) Process

func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error

Process kicks off the analysis process for a given package

func (*Analyzer) Report

func (gosec *Analyzer) Report() ([]*Issue, *Metrics, map[string][]Error)

Report returns the current issues discovered and the metrics about the scan

func (*Analyzer) Reset

func (gosec *Analyzer) Reset()

Reset clears state such as context, issues and metrics from the configured analyzer

func (*Analyzer) SetConfig

func (gosec *Analyzer) SetConfig(conf Config)

SetConfig upates the analyzer configuration

func (*Analyzer) Visit

func (gosec *Analyzer) Visit(n ast.Node) ast.Visitor

Visit runs the gosec visitor logic over an AST created by parsing go code. Rule methods added with AddRule will be invoked as necessary.

type CallList

type CallList map[string]set

CallList is used to check for usage of specific packages and functions.

func NewCallList

func NewCallList() CallList

NewCallList creates a new empty CallList

func (CallList) Add

func (c CallList) Add(selector, ident string)

Add a selector and call to the call list

func (CallList) AddAll

func (c CallList) AddAll(selector string, idents ...string)

AddAll will add several calls to the call list at once

func (CallList) Contains

func (c CallList) Contains(selector, ident string) bool

Contains returns true if the package and function are / members of this call list.

func (CallList) ContainsCallExpr

func (c CallList) ContainsCallExpr(n ast.Node, ctx *Context) *ast.CallExpr

ContainsCallExpr resolves the call expression name and type, and then determines if the call exists with the call list

func (CallList) ContainsPkgCallExpr

func (c CallList) ContainsPkgCallExpr(n ast.Node, ctx *Context, stripVendor bool) *ast.CallExpr

ContainsPkgCallExpr resolves the call expression name and type, and then further looks

up the package path for that type. Finally, it determines if the call exists within the call list

func (CallList) ContainsPointer

func (c CallList) ContainsPointer(selector, indent string) bool

ContainsPointer returns true if a pointer to the selector type or the type itself is a members of this call list.

type Config

type Config map[string]interface{}

Config is used to provide configuration and customization to each of the rules.

func NewConfig

func NewConfig() Config

NewConfig initializes a new configuration instance. The configuration data then needs to be loaded via c.ReadFrom(strings.NewReader("config data")) or from a *os.File.

func (Config) Get

func (c Config) Get(section string) (interface{}, error)

Get returns the configuration section for the supplied key

func (Config) GetGlobal

func (c Config) GetGlobal(option GlobalOption) (string, error)

GetGlobal returns value associated with global configuration option

func (Config) IsGlobalEnabled

func (c Config) IsGlobalEnabled(option GlobalOption) (bool, error)

IsGlobalEnabled checks if a global option is enabled

func (Config) ReadFrom

func (c Config) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements the io.ReaderFrom interface. This should be used with io.Reader to load configuration from file or from string etc.

func (Config) Set

func (c Config) Set(section string, value interface{})

Set section in the configuration to specified value

func (Config) SetGlobal

func (c Config) SetGlobal(option GlobalOption, value string)

SetGlobal associates a value with a global configuration option

func (Config) WriteTo

func (c Config) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriteTo interface. This should be used to save or print out the configuration information.

type Context

type Context struct {
	FileSet      *token.FileSet
	Comments     ast.CommentMap
	Info         *types.Info
	Pkg          *types.Package
	PkgFiles     []*ast.File
	Root         *ast.File
	Config       Config
	Imports      *ImportTracker
	Ignores      []map[string]bool
	PassedValues map[string]interface{}
}

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 encountered AST node.

type Cwe

type Cwe struct {
	ID  string
	URL string
}

Cwe id and url

func GetCwe

func GetCwe(id string) Cwe

GetCwe creates a cwe object for a given RuleID

type Error

type Error struct {
	Line   int    `json:"line"`
	Column int    `json:"column"`
	Err    string `json:"error"`
}

Error is used when there are golang errors while parsing the AST

func NewError

func NewError(line, column int, err string) *Error

NewError creates Error object

type GlobalOption

type GlobalOption string

GlobalOption defines the name of the global options

const (
	// Nosec global option for #nosec directive
	Nosec GlobalOption = "nosec"
	// Audit global option which indicates that gosec runs in audit mode
	Audit GlobalOption = "audit"
	// NoSecAlternative global option alternative for #nosec directive
	NoSecAlternative GlobalOption = "#nosec"
)

type ImportTracker

type ImportTracker struct {
	Imported map[string]string
	Aliased  map[string]string
	InitOnly map[string]bool
}

ImportTracker is used to normalize the packages that have been imported by a source file. It is able to differentiate between plain imports, aliased imports and init only imports.

func NewImportTracker

func NewImportTracker() *ImportTracker

NewImportTracker creates an empty Import tracker instance

func (*ImportTracker) TrackFile

func (t *ImportTracker) TrackFile(file *ast.File)

TrackFile track all the imports used by the supplied file

func (*ImportTracker) TrackImport

func (t *ImportTracker) TrackImport(n ast.Node)

TrackImport tracks imports and handles the 'unsafe' import

func (*ImportTracker) TrackPackages

func (t *ImportTracker) TrackPackages(pkgs ...*types.Package)

TrackPackages tracks all the imports used by the supplied packages

type Issue

type Issue struct {
	Severity   Score  `json:"severity"`   // issue severity (how problematic it is)
	Confidence Score  `json:"confidence"` // issue confidence (how sure we are we found it)
	Cwe        Cwe    `json:"cwe"`        // Cwe associated with RuleID
	RuleID     string `json:"rule_id"`    // Human readable explanation
	What       string `json:"details"`    // Human readable explanation
	File       string `json:"file"`       // File name we found it in
	Code       string `json:"code"`       // Impacted code line
	Line       string `json:"line"`       // Line number in file
	Col        string `json:"column"`     // Column number in line
}

Issue is returned by a gosec rule if it discovers an issue with the scanned code.

func NewIssue

func NewIssue(ctx *Context, node ast.Node, ruleID, desc string, severity Score, confidence Score) *Issue

NewIssue creates a new Issue

func (Issue) FileLocation

func (i Issue) FileLocation() string

FileLocation point out the file path and line number in file

type MetaData

type MetaData struct {
	ID         string
	Severity   Score
	Confidence Score
	What       string
}

MetaData is embedded in all gosec rules. The Severity, Confidence and What message will be passed through to reported issues.

type Metrics

type Metrics struct {
	NumFiles int `json:"files"`
	NumLines int `json:"lines"`
	NumNosec int `json:"nosec"`
	NumFound int `json:"found"`
}

Metrics used when reporting information about a scanning run.

type Rule

type Rule interface {
	ID() string
	Match(ast.Node, *Context) (*Issue, error)
}

The Rule interface used by all rules supported by gosec.

type RuleBuilder

type RuleBuilder func(id string, c Config) (Rule, []ast.Node)

RuleBuilder is used to register a rule definition with the analyzer

type RuleSet

type RuleSet map[reflect.Type][]Rule

A RuleSet maps lists of rules to the type of AST node they should be run on. The analyzer will only invoke rules contained in the list associated with the type of AST node it is currently visiting.

func NewRuleSet

func NewRuleSet() RuleSet

NewRuleSet constructs a new RuleSet

func (RuleSet) Register

func (r RuleSet) Register(rule Rule, nodes ...ast.Node)

Register adds a trigger for the supplied rule for the the specified ast nodes.

func (RuleSet) RegisteredFor

func (r RuleSet) RegisteredFor(n ast.Node) []Rule

RegisteredFor will return all rules that are registered for a specified ast node.

type Score

type Score int

Score type used by severity and confidence values

const (
	// Low severity or confidence
	Low Score = iota
	// Medium severity or confidence
	Medium
	// High severity or confidence
	High
)

func (Score) MarshalJSON

func (c Score) MarshalJSON() ([]byte, error)

MarshalJSON is used convert a Score object into a JSON representation

func (Score) String

func (c Score) String() string

String converts a Score into a string

Directories

Path Synopsis
cmd
tlsconfig
main
main

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL