Documentation
¶
Overview ¶
Package patterns provides security and framework pattern detection.
This package handles:
- Security pattern matching (SQL injection, XSS, etc.)
- Framework detection (Django, Flask, FastAPI)
- Code quality pattern detection
Pattern Matching ¶
registry := patterns.NewPatternRegistry()
registry.AddPattern(&patterns.Pattern{
ID: "SQL-INJECTION-001",
Name: "SQL Injection",
Type: patterns.PatternTypeMissingSanitizer,
Sources: []string{"request.GET", "request.POST"},
Sinks: []string{"execute", "executemany"},
Sanitizers: []string{"escape_sql"},
})
match := registry.MatchPattern(pattern, callGraph)
if match.Matched {
fmt.Printf("Found vulnerability: %s -> %s\n",
match.SourceFQN, match.SinkFQN)
}
Framework Detection ¶
framework := patterns.DetectFramework(importMap)
if framework != nil {
fmt.Printf("Using %s (%s)\n",
framework.Name, framework.Category)
}
Index ¶
- func GetFrameworkCategory(importPath string) string
- func GetFrameworkName(importPath string) string
- func IsKnownFramework(importPath string) bool
- type Framework
- type Pattern
- type PatternMatchDetails
- type PatternRegistry
- func (pr *PatternRegistry) AddPattern(pattern *Pattern)
- func (pr *PatternRegistry) GetPattern(id string) (*Pattern, bool)
- func (pr *PatternRegistry) GetPatternsByType(patternType PatternType) []*Pattern
- func (pr *PatternRegistry) LoadDefaultPatterns()
- func (pr *PatternRegistry) MatchPattern(pattern *Pattern, callGraph *core.CallGraph) *PatternMatchDetails
- type PatternType
- type Severity
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFrameworkCategory ¶
GetFrameworkCategory returns the category of a framework given its import path. Returns empty string if not a known framework.
func GetFrameworkName ¶
GetFrameworkName returns the name of a framework given its import path. Returns empty string if not a known framework.
func IsKnownFramework ¶
IsKnownFramework checks if import path is a known framework. This is a convenience wrapper around core.IsKnownFramework.
Types ¶
type Framework ¶
Framework represents a detected framework.
func DetectFramework ¶
DetectFramework detects which framework is used based on imports. Returns the first detected framework or nil if none found.
type Pattern ¶
type Pattern struct {
ID string // Unique identifier (e.g., "SQL-INJECTION-001")
Name string // Human-readable name
Description string // What this pattern detects
Type PatternType // Pattern category
Severity Severity // Risk level
// Sources are function names that introduce tainted data
Sources []string
// Sinks are function names that consume tainted data dangerously
Sinks []string
// Sanitizers are function names that clean tainted data
Sanitizers []string
// DangerousFunctions for PatternTypeDangerousFunction
DangerousFunctions []string
CWE string // Common Weakness Enumeration
OWASP string // OWASP Top 10 category
}
Pattern represents a security pattern to detect in the call graph.
type PatternMatchDetails ¶
type PatternMatchDetails struct {
Matched bool
IsIntraProcedural bool // true if source and sink are in the same function
SourceFQN string // Fully qualified name of function containing the source call
SourceCall string // The actual dangerous call (e.g., "input", "request.GET")
SinkFQN string // Fully qualified name of function containing the sink call
SinkCall string // The actual dangerous call (e.g., "eval", "exec")
DataFlowPath []string // Complete path from source to sink
}
PatternMatchDetails contains detailed information about a pattern match.
type PatternRegistry ¶
type PatternRegistry struct {
Patterns map[string]*Pattern // Pattern ID -> Pattern
PatternsByType map[PatternType][]*Pattern // Type -> Patterns
}
PatternRegistry manages security patterns.
func NewPatternRegistry ¶
func NewPatternRegistry() *PatternRegistry
NewPatternRegistry creates a new pattern registry.
func (*PatternRegistry) AddPattern ¶
func (pr *PatternRegistry) AddPattern(pattern *Pattern)
AddPattern registers a pattern in the registry.
func (*PatternRegistry) GetPattern ¶
func (pr *PatternRegistry) GetPattern(id string) (*Pattern, bool)
GetPattern retrieves a pattern by ID.
func (*PatternRegistry) GetPatternsByType ¶
func (pr *PatternRegistry) GetPatternsByType(patternType PatternType) []*Pattern
GetPatternsByType retrieves all patterns of a specific type.
func (*PatternRegistry) LoadDefaultPatterns ¶
func (pr *PatternRegistry) LoadDefaultPatterns()
LoadDefaultPatterns loads the hardcoded example pattern. Additional patterns will be loaded from queries in future PRs.
func (*PatternRegistry) MatchPattern ¶
func (pr *PatternRegistry) MatchPattern(pattern *Pattern, callGraph *core.CallGraph) *PatternMatchDetails
MatchPattern checks if a call graph matches a pattern. Returns detailed match information if a vulnerability is found.
type PatternType ¶
type PatternType string
PatternType categorizes security patterns for analysis.
const ( // PatternTypeSourceSink detects tainted data flow from source to sink. PatternTypeSourceSink PatternType = "source-sink" // PatternTypeMissingSanitizer detects missing sanitization between source and sink. PatternTypeMissingSanitizer PatternType = "missing-sanitizer" // PatternTypeDangerousFunction detects calls to dangerous functions. PatternTypeDangerousFunction PatternType = "dangerous-function" )