Secrets, cryptography and supply chain · 6 MIN READ
Secure the Go supply chain: reachable vulnerabilities, verified modules, pinned tools, auditable binaries
Run govulncheck in CI, rely on go.sum and the checksum database correctly, pin tools with tool directives, build with -trimpath, and audit a binary's embedded build information.
Most of the code in a Go binary was written by someone else. The toolchain provides strong defaults: go.sum pins exact module content, the checksum database exposes a module version that changed after publication, and every binary records the modules it was built from. Supply-chain security in Go is mostly about not weakening those defaults, and about reacting quickly and precisely when a dependency has a known vulnerability.
security · A MAP OF THE IDEA
- Pin modules and tools
- Verify sums, read-only
- Scan reachable vulns
- Audit binary build info
Useful when
- Reachability analysis keeps vulnerability reports short and actionable.
- Embedded build information makes every shipped binary auditable after the fact.
Watch out
- The checksum database proves consistency, not that code is benign.
- Tool directives add tool dependencies to the application's module graph.
Implementation & operation cost: A govulncheck step and a build-information gate in CI, plus reviewer time for every dependency update; keeping the defaults on costs nothing.
Report the vulnerabilities your code can reach
govulncheck compares your dependencies, and the Go version itself, against the Go vulnerability database. In source mode it builds a call graph and reports a vulnerability as affecting you only when your code can reach a vulnerable function, which turns a long list of advisories into the few that matter now. Vulnerable modules that you import but never call are summarized separately. Binary mode inspects a compiled executable, which is the right check for what you actually ship, but it cannot show call stacks. Standard-library vulnerabilities are fixed by moving to a newer Go patch release, so scan with the same Go version you release with.
# Pin the scanner in go.mod (Go 1.24+), so developers and CI run one version:
go get -tool golang.org/x/vuln/cmd/govulncheck@latest
go tool govulncheck ./... # source mode: reachable findings
go tool govulncheck -show verbose ./... # also list imported but unreached ones
go tool govulncheck -mode=binary ./bin/orders # audit what you actually shipRun these in your own module; govulncheck fetches advisories from the Go vulnerability database. go get -tool records the resolved version in go.mod and go.sum, and go tool runs exactly that version, building it on first use. In its default text output, govulncheck exits with a non-zero status when it finds vulnerabilities your code reaches, so a CI step fails on the findings that matter. Reachability is a static approximation: reflection and some indirect calls can hide a path, so schedule upgrades for unreached findings instead of ignoring them.
What go.sum and the checksum database prove
go.sum records a hash of each module version's file tree and of its go.mod file. When the go command first downloads a module version that go.sum does not list, it checks the hash against sum.golang.org, a transparent log that lets every user confirm they see the same content for the same version. That defends against a compromised proxy or origin serving you different code from everyone else, and against a version tag moved after publication. It does not say the code is benign: a malicious version published openly is logged faithfully. Private modules must not be looked up in public services, which is what GOPRIVATE controls.
go env -w GOPRIVATE='*.corp.example.com' # no public proxy or checksum DB for these
go mod verify # module cache still matches go.sum
go mod tidy -diff # non-zero exit if go.mod or go.sum would change
go build -mod=readonly -trimpath -o bin/orders ./cmd/orders
go version -m bin/orders # the embedded module and build recordRun these in your own environment. GOPRIVATE takes comma-separated glob patterns of module path prefixes and keeps those modules away from proxy.golang.org and sum.golang.org; GONOPROXY and GONOSUMDB override it for finer control. go mod verify detects tampering in the local module cache. go mod tidy -diff prints needed changes and exits non-zero, so CI catches a go.mod that drifted from the imports. -mod=readonly is already the default without a vendor directory, but stating it guards against a GOFLAGS=-mod=mod in the environment quietly rewriting requirements. -trimpath replaces file system paths with module paths, so binaries do not reveal the build machine's layout and are easier to reproduce.
Pin and review tools like code
Tools that run in CI or through go generate, such as linters, code generators and govulncheck, execute with your repository's credentials, so they deserve the same pinning as libraries. Since Go 1.24 a tool directive in go.mod records each one: go tool runs it at the recorded version, go get tool upgrades every declared tool, and go install tool installs them into GOBIN. Tool dependencies join your module graph, which can raise shared dependency versions for the application too; if that is unwanted, declare the tools in a separate go.mod and pass -modfile to go tool. Avoid go install pkg@latest and download-and-run installers in CI scripts: they execute whatever was published most recently, unreviewed.
Audit what a binary was built from
Every module-mode binary embeds its main module, its dependency versions with checksums, and build settings such as -trimpath, CGO_ENABLED and the VCS revision. go version -m prints that record, debug/buildinfo.ReadFile reads it from a file, and runtime/debug.ReadBuildInfo returns the running program's own copy. A release gate can read the candidate binary and refuse to ship it when the record shows a modified working tree or a dependency replaced by a local directory. The program applies such a policy to a fixed record and then prints only those fields of its own record that do not vary between environments.
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
// candidate is what debug/buildinfo.ReadFile returns for a release binary;
// `go version -m ./orders` prints the same record as text.
var candidate = &debug.BuildInfo{
Path: "example.com/orders/cmd/orders",
Main: debug.Module{Path: "example.com/orders", Version: "v1.8.0+dirty"},
Deps: []*debug.Module{
{Path: "example.com/auth", Version: "v0.3.1",
Replace: &debug.Module{Path: "../auth", Version: "(devel)"}},
{Path: "golang.org/x/crypto", Version: "v0.57.0", Sum: "h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M="},
{Path: "golang.org/x/sys", Version: "v0.48.0", Sum: "h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo="},
},
Settings: []debug.BuildSetting{
{Key: "-compiler", Value: "gc"},
{Key: "-trimpath", Value: "true"},
{Key: "CGO_ENABLED", Value: "0"},
{Key: "GOOS", Value: "linux"},
{Key: "vcs", Value: "git"},
{Key: "vcs.revision", Value: "4b825dc642cb6eb9a060e54bf8d69288fbee4904"},
{Key: "vcs.modified", Value: "true"},
},
}
// audit applies a release policy to embedded build information.
func audit(bi *debug.BuildInfo) []string {
settings := map[string]string{}
for _, s := range bi.Settings {
settings[s.Key] = s.Value
}
var problems []string
if settings["-trimpath"] != "true" {
problems = append(problems, "built without -trimpath")
}
if settings["vcs.revision"] == "" {
problems = append(problems, "no VCS revision stamped")
}
if settings["vcs.modified"] != "false" {
problems = append(problems, "built from a modified working tree")
}
for _, d := range bi.Deps { // a slice, so findings keep a stable order
switch {
case d.Replace != nil && d.Replace.Sum == "":
problems = append(problems, d.Path+" replaced by local directory "+d.Replace.Path)
case d.Replace == nil && d.Sum == "":
problems = append(problems, d.Path+" has no checksum")
}
}
return problems
}
func main() {
fmt.Println("candidate:", candidate.Main.Path, candidate.Main.Version, "deps:", len(candidate.Deps))
problems := audit(candidate)
for _, p := range problems {
fmt.Println("block:", p)
}
fmt.Println("release allowed:", len(problems) == 0)
self, ok := debug.ReadBuildInfo() // the record embedded in this very program
fmt.Println("self has build info:", ok)
if ok {
fmt.Println("self GoVersion == runtime.Version():", self.GoVersion == runtime.Version())
fmt.Println("self dependency modules:", len(self.Deps))
}
}candidate mirrors what the go command records for a build from a modified checkout: since Go 1.24, go build appends +dirty to the main module version and sets vcs.modified=true. The replaced dependency carries no checksum because a directory has no published content to hash, which is exactly why it must not reach a release. audit walks Settings and Deps as slices, so findings print in a stable order. For the running program, ReadBuildInfo fills GoVersion from runtime.Version, and a program that imports only the standard library lists no dependency modules. Paths, VCS fields and target platform vary by environment, so they are not printed; go run does not stamp VCS information at all, while go build does.
Review dependency updates as code changes
Each upgrade imports code that runs with your service's privileges. Update deliberately, one dependency or one related group per change, so a regression points at its cause. Read the go.mod and go.sum diff and look for modules that arrived transitively; go mod why -m explains why a module is needed and go mod graph shows who requires it. Skim the release notes and the size of the source diff in the upstream repository. Run the tests and govulncheck on the result. Automated update pull requests, such as Dependabot version updates for Go modules, are useful when a person reviews them with the same care as any other change. And prefer fewer dependencies: a small, well-understood helper is sometimes cheaper to own than a module that brings twenty more.
go list -m -u all # available upgrades appear in [brackets]
go get golang.org/x/net@v0.61.0 && go mod tidy
git diff go.mod go.sum # new transitive modules show up here
go mod why -m golang.org/x/text # which import path needs this module
go test ./... && go tool govulncheck ./...Run these in your own module; the module and version are examples. go list -m -u all shows each module with any newer version in brackets. go get changes one requirement and go mod tidy settles the graph, after which the git diff of go.mod and go.sum is the review surface: every new line in go.sum is code you have not seen before. go mod why -m prints the shortest import path that needs the module. The last line gates the change on tests and reachable vulnerabilities, the same checks CI repeats.
TAKE THIS WITH YOU
Keep the toolchain's defaults on (go.sum, the checksum database, read-only module mode), gate CI on reachable govulncheck findings, pin tools in go.mod, and refuse releases whose embedded build information shows a dirty tree or a local replacement.
Check your understanding
A new version of a dependency is listed in the Go checksum database. What does that establish?
Go to the source
Progress stays in this browser. No account needed.