Go Security Testing Tools
The static, dynamic and instrumentation toolchain for assessing Go code. Linters find the low-hanging issues, custom
go/analysisand SSA passes find project-specific ones, and fuzzing, property tests, fault injection and the race detector find what only shows up at runtime.
Static analysis
| Tool | What it catches | Notes |
|---|---|---|
gosec | Security-specific rules | Trail of Bits’ minimum set is gosec, go vet and staticcheck |
go vet | Shadowing (-shadow), misuse of unsafe pointers, printf mismatches | Misses := error overwrites |
staticcheck | SA4006 unused value, ST1005 style, and more | Catches overwritten err |
ineffassign | Ineffectual assignments | Catches overwritten err |
errcheck | Unchecked error returns | Misses the := redeclare case |
go-nyet | More aggressive shadow detection | Recommended by 50 Shades over vet -shadow |
sqlvet | Validates SQL strings in Go source at build time | Built on SSA |
| DeepSource | Bug-risk rules: nil-map writes, loop-variable capture in goroutines, non-pointer receiver mutation, unbounded recursion | Hosted analyzer |
Caveats:
- Most of these tools need a buildable tree, which slows third-party assessments when dependencies don’t resolve.
- Tools report the same bug differently or not at all. Run several and treat each warning location as a lead: Trail of Bits found that warning hotspots “typically” lead to exploitable functionality.
- Contrast argues gosec and staticcheck results are largely quality-level, with high false positives, and that interface-heavy code breaks data-flow analysis. That is the case they make for runtime instrumentation (IAST). Note the vendor bias.
Writing custom checks
For project-specific sinks, such as a bespoke query builder or an internal auth helper, write an analyzer:
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, path, nil, parser.ParseComments)
ast.Inspect(f, func(n ast.Node) bool {
if call, ok := n.(*ast.CallExpr); ok {
if sel, ok := call.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "Sprintf" { /* flag */ }
}
return true
})go/scannerhandles tokens,go/parserproduces the AST (*ast.File), andast.Inspectwalks it depth-first. Calls likefmt.Printfappear as*ast.SelectorExpr{X: fmt, Sel: Printf}.golang.org/x/tools/go/packagesloads whole packages, not single files.golang.org/x/tools/go/ssaandgo/pointeradd types, call graphs and pointer analysis. This is how sqlvet finds SQL strings reachingQuery/Exec. Trail of Bits also suggests custom SSA tooling or CodeQL for depth.
Dynamic analysis
-
dvyukov/go-fuzzis coverage-guided mutational fuzzing with libFuzzer support and distributed mode. Point it at anything that parses input. -
google/gofuzzfills structs with random values. It has no harness or corpus management. Use it to stress struct validation. -
testing/quickis basic property testing (quick.Check(f, nil)). -
leanovate/gopteradds property testing with generators, shrinking and statefulgopter/commands. In Trail of Bits’ example it found a divide-by-zero panic inDivideand shrank the inputs to0. -
KRF (
trailofbits/krf) injects kernel-level syscall faults (Go binaries are static, soLD_PRELOADdoesn’t work). Randomly faultingread/writefound the Kubelet/cAdvisor index-out-of-range panic in a vendored dependency (Kubernetes report, Appendix G). -
Race detector (
-race) should be on for every test run. Trail of Bits’on-edgepiggybacks on it to report global state changed between function entry and a panic, which catches brokendefer/recoverhandling. See Go Panics and Concurrency Bugs. -
google/gopslists running Go processes and their versions. Against a process that starts the gops agent, it pulls stack traces, memory stats and CPU/heap pprof profiles. It is useful for triaging hangs found while fuzzing. An agent left listening is itself exposure (see Go Build and Deployment Hardening).
Compiler tricks for assessors
//go:linknameexposes unexported functions to harnesses without renaming them (see Go unsafe and cgo).- Coverage maps (
go test -cover) measure fuzz and property-test reach. Prioritize review of untested areas. - Cross-width builds: compile and test for 32-bit as well as 64-bit targets to surface
intvsint32/int64assumptions, as in TOB-K8S-015 (see Go Secure Coding Pitfalls).
Dependencies and runtime (SCA/IAST)
snyk testparses Go modules and reports known CVEs with fixed versions. OWASP DependencyCheck has an experimentalgo modanalyzer.- In 2019 Trail of Bits noted that Go had no mature first-party dependency auditor. Vendored and modified dependencies still defeat version-based scanners. See Go Build and Deployment Hardening.
- Contrast Go replaces the compiler in the test pipeline to embed IAST sensors that trace tainted data to SQL, file and path sinks at runtime, and also reports vulnerable libraries.
Related
- Go Security (hub) · Go Secure Coding Pitfalls · Go Panics and Concurrency Bugs · Go unsafe and cgo
- Python SAST Tools (Python equivalent) · API Fuzzing · SBOM and Provenance
Sources
- Security assessment techniques for Go projects · original
- Write your own Go static analysis tool · original
- Digging deeper into the analysis of Go-code · original
- Secure Coding with Go · original
- Go Security cheatsheet · original
- The Top 10 Most Common Mistakes I've Seen in Go Projects · original
- 50 Shades of Go: Traps, Gotchas, and Common Mistakes · original
- 5 Common mistakes in Go · original
- gops: list and diagnose Go processes · original