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

ToolWhat it catchesNotes
gosecSecurity-specific rulesTrail of Bits’ minimum set is gosec, go vet and staticcheck
go vetShadowing (-shadow), misuse of unsafe pointers, printf mismatchesMisses := error overwrites
staticcheckSA4006 unused value, ST1005 style, and moreCatches overwritten err
ineffassignIneffectual assignmentsCatches overwritten err
errcheckUnchecked error returnsMisses the := redeclare case
go-nyetMore aggressive shadow detectionRecommended by 50 Shades over vet -shadow
sqlvetValidates SQL strings in Go source at build timeBuilt on SSA
DeepSourceBug-risk rules: nil-map writes, loop-variable capture in goroutines, non-pointer receiver mutation, unbounded recursionHosted 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/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.

Sources