taint

package
v2.29.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package taint provides a minimal taint analysis engine for gosec. It tracks data flow from sources (user input) to sinks (dangerous functions) using SSA form and call graph analysis.

This implementation uses only golang.org/x/tools packages which gosec already depends on - no external dependencies required.

Inspired by:

  • github.com/google/capslock (call graph traversal pattern)
  • gosec issue #1160 (requirements)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGosecAnalyzer

func NewGosecAnalyzer(rule *RuleInfo, config *Config) *analysis.Analyzer

NewGosecAnalyzer creates a golang.org/x/tools/go/analysis.Analyzer compatible with gosec's analyzer framework.

Types

type Analyzer

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

func New

func New(config *Config) *Analyzer

New creates a new taint analyzer with the given configuration.

func (*Analyzer) Analyze

func (a *Analyzer) Analyze(prog *ssa.Program, srcFuncs []*ssa.Function) []Result

Analyze performs taint analysis on the given SSA program. It returns all detected taint flows from sources to sinks.

func (*Analyzer) SetCallGraph added in v2.24.0

func (a *Analyzer) SetCallGraph(cg *callgraph.Graph)

SetCallGraph injects a precomputed call graph.

type Config

type Config struct {
	// Sources is the list of data origins that produce tainted values
	Sources []Source
	// Sinks is the list of dangerous functions that should not receive tainted data
	Sinks []Sink
	// Sanitizers is the list of functions that neutralize taint (optional)
	Sanitizers []Sanitizer
}

Config holds taint analysis configuration.

type Result

type Result struct {
	// Source is the origin of the tainted data
	Source Source
	// Sink is the dangerous function that receives the tainted data
	Sink Sink
	// SinkPos is the source code position of the sink call
	SinkPos token.Pos
	// Path is the sequence of functions from entry point to the sink
	Path []*ssa.Function
}

Result represents a detected taint flow from source to sink.

type RuleInfo

type RuleInfo struct {
	ID          string
	Description string
	Severity    string
	CWE         string
}

RuleInfo holds metadata about a taint analysis rule.

type Sanitizer added in v2.24.0

type Sanitizer struct {
	// Package is the import path (e.g., "path/filepath")
	Package string
	// Receiver is the type name for methods, or empty for package-level functions
	Receiver string
	// Method is the function or method name (e.g., "Clean")
	Method string
	// Pointer indicates whether the receiver is a pointer type
	Pointer bool
}

Sanitizer defines a function that neutralizes taint. When tainted data passes through a sanitizer, it is no longer considered tainted.

type Sink

type Sink struct {
	// Package is the import path of the package containing the sink (e.g., "database/sql")
	Package string
	// Receiver is the type name for methods (e.g., "DB"), or empty for package-level functions
	Receiver string
	// Method is the function or method name that represents the sink (e.g., "Query")
	Method string
	// Pointer indicates whether the receiver is a pointer type (true for *Type methods)
	Pointer bool
	// CheckArgs specifies which argument positions to check for taint (0-indexed).
	// For method calls, Args[0] is the receiver.
	// If nil or empty, all arguments are checked.
	// Examples:
	//   - SQL methods: [1] - only check query string (Args[1]), skip receiver
	//   - fmt.Fprintf: [1,2,3,...] - skip writer (Args[0]), check format and data
	CheckArgs []int

	// ArgTypeGuards constrains argument types before treating a call as a sink.
	// Key is the zero-based argument index; value is the required type expressed
	// as "import/path.TypeName" (e.g. "net/http.ResponseWriter").
	// The sink only fires when every guarded argument's type implements (or equals)
	// the named interface/type. When empty, no type constraint is applied.
	ArgTypeGuards map[int]string
}

Sink defines a dangerous function that should not receive tainted data. Format: "(*package/path.Type).Method" or "package/path.Func"

type Source

type Source struct {
	// Package is the import path of the package containing the source (e.g., "net/http")
	Package string
	// Name is the type or function name that produces tainted data (e.g., "Request" for type, "Get" for function)
	Name string
	// Pointer indicates whether the source is a pointer type (true for *Type)
	Pointer bool
	// IsFunc marks this source as a function/method that returns tainted data
	// (e.g., os.Getenv, os.ReadFile). When false, Source is treated as a type
	// that is only tainted when received as a function parameter from external callers.
	IsFunc bool
}

Source defines where tainted data originates. Format: "package/path.TypeOrFunc" or "*package/path.Type" for pointer types.

Jump to

Keyboard shortcuts

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