Security testing in Go combines the built-in toolchain with a few focused analysers. Use go test with the race detector and fuzzing to find bugs in your own code, govulncheck to find known vulnerabilities in your dependencies, and static analysers such as go vet, staticcheck and gosec to catch risky patterns before they ship.
The standard library gives you the test harness (testing, net/http/httptest) and native fuzzing; the rest is tooling you run in CI. This guide shows each layer and how to wire them together.
Write tests for security behaviour, not only for features: authentication failures, authorization boundaries, input limits and error handling. net/http/httptest lets you exercise handlers without a network.
Table-driven tests like this make it cheap to add a new abuse case every time you find one.
Native fuzzing has been part of go test since Go 1.18. It mutates inputs automatically and stores any failing input under testdata/fuzz, so it becomes a permanent regression test.
Run it with go test -fuzz=FuzzParseConfig -fuzztime=60s. Fuzz anything that handles untrusted input: decoders, protocol parsers, template inputs and file-format readers. Without -fuzz, go test just replays the saved corpus.
Races are both correctness and security bugs, since they can corrupt state in ways attackers exploit. Run the detector in CI:
It only reports races that actually occur during the run, so combine it with concurrent tests and fuzzing for better coverage. Expect slower execution and higher memory use.
| Tool | What it finds | Command |
|---|---|---|
govulncheck | Known vulnerabilities your code actually calls (official Go tool) | govulncheck ./... |
go vet | Suspicious constructs, bad format strings | go vet ./... |
staticcheck | Bugs and deprecated API use | staticcheck ./... |
gosec | Insecure patterns: weak hashes, hardcoded credentials, unsafe TLS | gosec ./... |
go mod verify | Modified module contents | go mod verify |
Install govulncheck with go install golang.org/x/vuln/cmd/govulncheck@latest. Unlike scanners that only match versions, it reports a finding only when a vulnerable function is reachable from your code, which keeps noise low. Static analysers produce false positives, so review findings and suppress them with an explanatory comment rather than blanket disabling.
A minimal job runs these steps on every pull request:
Add a scheduled run of govulncheck, because new advisories appear for code that has not changed. Pin the Go version in go.mod (toolchain directive) and update it promptly, since standard library fixes arrive in minor releases.
Unit tests cannot cover deployment mistakes. Complement them with:
Strict-Transport-Security, Content-Security-Policy) in integration tests.Run govulncheck ./.... It uses the Go vulnerability database and reports only vulnerabilities in code paths you actually call.
No. Unit tests check expected behaviour; fuzzing explores unexpected inputs to find panics and logic errors. Use both, and keep fuzz-found inputs as regression cases.
No. It is a pattern-based static analyser and misses logic flaws such as broken authorization. Pair it with targeted tests and review.
It is intended for testing because of its overhead. Run it in CI and in staging load tests instead.
Test security in Go in layers: table-driven tests for access control and input handling, native fuzzing for parsers, the race detector for concurrency, and govulncheck, go vet, staticcheck and gosec in CI. Add scheduled scans and dynamic testing of the deployed system for coverage that unit tests cannot provide.