# Go Security Testing Tools
> The static, dynamic and instrumentation toolchain for assessing Go code. Linters find the low-hanging issues, custom `go/analysis` and 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:
```go
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/scanner` handles tokens, `go/parser` produces the AST (`*ast.File`), and `ast.Inspect` walks it depth-first. Calls like `fmt.Printf` appear as `*ast.SelectorExpr{X: fmt, Sel: Printf}`.
- `golang.org/x/tools/go/packages` loads whole packages, not single files.
- `golang.org/x/tools/go/ssa` and `go/pointer` add types, call graphs and pointer analysis. This is how sqlvet finds SQL strings reaching `Query`/`Exec`. Trail of Bits also suggests custom SSA tooling or CodeQL for depth.
## Dynamic analysis
- **`dvyukov/go-fuzz`** is coverage-guided mutational fuzzing with libFuzzer support and distributed mode. Point it at anything that parses input.
- **`google/gofuzz`** fills structs with random values. It has no harness or corpus management. Use it to stress struct validation.
- **`testing/quick`** is basic property testing (`quick.Check(f, nil)`).
- **`leanovate/gopter`** adds property testing with generators, shrinking and stateful `gopter/commands`. In Trail of Bits' example it found a divide-by-zero panic in `Divide` and shrank the inputs to `0`.
- **KRF (`trailofbits/krf`)** injects kernel-level syscall faults (Go binaries are static, so `LD_PRELOAD` doesn't work). Randomly faulting `read`/`write` found 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-edge`** piggybacks on it to report global state changed between function entry and a panic, which catches broken `defer`/`recover` handling. See [[Go Panics and Concurrency Bugs]].
- **`google/gops`** lists 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:linkname`** exposes 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 `int` vs `int32`/`int64` assumptions, as in TOB-K8S-015 (see [[Go Secure Coding Pitfalls]]).
## Dependencies and runtime (SCA/IAST)
- `snyk test` parses Go modules and reports known CVEs with fixed versions. OWASP DependencyCheck has an experimental `go mod` analyzer.
- 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
- [[raw/blog.trailofbits.com/security-assessment-techniques-for-go-projects-trail-of-bits-blog-1213|Security assessment techniques for Go projects]]
- [[raw/about.houqp.me/write-your-own-go-static-analysis-tool-houqp-1145|Write your own Go static analysis tool]]
- [[raw/nakabonne.dev/digging-deeper-into-the-analysis-of-go-code-1203|Digging deeper into the analysis of Go-code]]
- [[raw/contrastsecurity.com/secure-coding-with-go-1224|Secure Coding with Go]]
- [[raw/snyk.io/go-security-cheatsheet-snyk-blog-1225|Go Security cheatsheet]]
- [[raw/link.medium.com/the-top-10-most-common-mistakes-i-ve-seen-in-go-projects-1366|The Top 10 Most Common Mistakes I've Seen in Go Projects]]
- [[raw/devs.cloudimmunity.com/50-shades-of-go-traps-gotchas-and-common-mistakes-for-new-golang-devs-1102|50 Shades of Go: Traps, Gotchas, and Common Mistakes]]
- [[raw/deepsource.io/5-common-mistakes-in-go-1275|5 Common mistakes in Go]]
- [[raw/github.com/github-google-gops-a-tool-to-list-and-diagnose-go-processes-currently-1238|gops: list and diagnose Go processes]]