Create taint analysis rules
masterTaint analyzers track data flow from untrusted Sources to dangerous Sinks.
Implementation Steps
- Create an analyzer file in
analyzers/containing ataint.Config(sources, sinks, and optional sanitizers) and a constructor that returnstaint.NewGosecAnalyzer(...). - Register the analyzer in
analyzers/analyzerslist.go. - Add sample programs in
testutils/. - Add the analyzer test in
analyzers/analyzers_test.gousing therunnerfunction.
Taint Configuration Reference
Sources
Package: import path (e.g.,"net/http")Name: type or function name (e.g.,"Request","Getenv")Pointer:truefor pointer types (e.g.,*http.Request)IsFunc:trueif the source is a function returning tainted data
Sinks
Package: import pathReceiver: method receiver type (empty for package functions)Method: method namePointer: whether receiver is a pointerCheckArgs: optional integer slice of argument indexes to inspect. If omitted, all args are inspected.
Sanitizers
Sanitizers break taint flow. They use the same configuration fields as Sinks (Package, Receiver, Method, Pointer).
package analyzers
import (
"golang.org/x/tools/go/analysis"
"github.com/securego/gosec/v2/taint"
)
func NewVulnerability() taint.Config {
return taint.Config{
Sources: []taint.Source{
{Package: "net/http", Name: "Request", Pointer: true},
{Package: "os", Name: "Args", IsFunc: true},
},
Sinks: []taint.Sink{
{Package: "dangerous/package", Method: "DangerousFunc"},
},
}
}
func newNewVulnAnalyzer(id string, description string) *analysis.Analyzer {
config := NewVulnerability()
rule := NewVulnerabilityRule
rule.ID = id
rule.Description = description
return taint.NewGosecAnalyzer(&rule, &config)
}