Documentation
¶
Overview ¶
Package analysis provides a go/analysis.Analyzer that enforces symbol-level import restrictions on Go source files.
The analyzer checks that every imported symbol is in a given allowlist, that no permanently banned packages are imported, and that every symbol in the allowlist is actually used. It reports violations with file:line:col diagnostics.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var OpenFileCloseAnalyzer = &analysis.Analyzer{
Name: "openfileclose",
Doc: "checks that callCtx.OpenFile results are always closed within the same function",
Run: runOpenFileClose,
}
OpenFileCloseAnalyzer checks that every callCtx.OpenFile call result that is assigned to a variable has a corresponding .Close() call (direct or via defer) within the same function scope. Unclosed file handles exhaust file descriptors over repeated script executions.
var ScannerBufferAnalyzer = &analysis.Analyzer{
Name: "scannerbuffer",
Doc: "checks that bufio.NewScanner results have Buffer() called to set a bounded read buffer",
Run: runScannerBuffer,
}
ScannerBufferAnalyzer checks that every bufio.NewScanner call in the analyzed package has a corresponding .Buffer() call on the returned value within the same function scope. Without Buffer(), the scanner uses a fixed 64 KiB internal buffer and fails on lines longer than that — a reliability and DoS risk for builtins that must handle arbitrary input.
Functions ¶
func NewAnalyzer ¶
func NewAnalyzer(cfg AnalyzerConfig) *analysis.Analyzer
NewAnalyzer returns a go/analysis.Analyzer that enforces the symbol-level import restrictions described by cfg. Violations are reported via pass.Reportf and appear as diagnostics with proper file:line:col positions.
NewAnalyzer panics if any entry in cfg.Symbols is malformed (no dot separator), matching the behaviour of the test-harness variant.
NOTE: This analyzer only enforces symbol-level allowlist restrictions. For full static analysis coverage, callers should also register ScannerBufferAnalyzer and OpenFileCloseAnalyzer alongside this one. The test-harness path (checkAllowedSymbols) already applies all three checks automatically.
Types ¶
type AnalyzerConfig ¶
type AnalyzerConfig struct {
// Symbols is the allowlist to enforce (e.g. builtinAllowedSymbols).
// Each entry must be in "importpath.Symbol" form.
Symbols []string
// ExemptImport returns true for import paths that are auto-allowed and
// should not be checked against the allowlist (e.g. same-module imports).
ExemptImport func(importPath string) bool
// ListName is used in diagnostic messages (e.g. "builtinAllowedSymbols").
ListName string
}
AnalyzerConfig configures a single instance of the allowed-symbols analyzer.