taint

package
v2.23.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
}

Analyzer performs taint analysis on SSA programs.

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.

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
}

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 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
}

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
}

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