go.sum & the Checksum Database
go.mod says which versions a build uses. go.sum says what those versions contain: one cryptographic hash per module version, recorded the first time the go command downloads it. Every later download, on any machine, must produce the same hash or the build stops. That is why go.sum is committed, and why Go needs no separate lockfile to make builds repeatable.
What a go.sum line is
mkdir s && cd s
go mod init example.com/s 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"rsc.io/quote"
)
func main() { fmt.Println(quote.Glass()) }
SRC
go mod tidy 2>/dev/null
cat go.sum
go mod download -json rsc.io/quote | grep -E '"(Sum|GoModSum)"'golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
"Sum": "h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=",
"GoModSum": "h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0="
Each module version can have two lines:
| Line | Hash of | Needed when |
|---|---|---|
path version h1:… |
the whole module zip (every file) | the build compiles a package from that module |
path version/go.mod h1:… |
only its go.mod |
the go command reads its requirements while building the module graph |
That is why some modules in a big graph have only a /go.mod line: their requirements were read, but none of their code is compiled. go.sum is not a list of what the build uses (that is go list -m all); it is a set of known-good hashes, and extra lines are harmless.
How the h1 hash is computed
h1: names hash algorithm 1: SHA-256 over a summary of the files, not over the zip bytes, so the same files always give the same hash no matter how the zip was compressed. For each file, sorted by name, the summary has a line <sha256 hex> <name>; the hash is the base64 SHA-256 of the whole summary. This model computes the /go.mod hash of rsc.io/quote v1.5.2 from the file's exact bytes and gets the value in the session above:
import base64
import hashlib
def h1(files):
"""files: {name: bytes}. Go's dirhash.Hash1."""
summary = "".join(
f"{hashlib.sha256(files[name]).hexdigest()} {name}\n" for name in sorted(files)
)
return "h1:" + base64.b64encode(hashlib.sha256(summary.encode()).digest()).decode()
# the go.mod of rsc.io/quote v1.5.2, byte for byte (an old, quoted style)
quote_gomod = b'module "rsc.io/quote"\n\nrequire "rsc.io/sampler" v1.3.0\n'
print("go.mod hash:", h1({"go.mod": quote_gomod}))
# a whole module: names are prefixed with module@version/, as inside the zip
module = {
"example.com/greet@v1.0.0/go.mod": b"module example.com/greet\n\ngo 1.27\n",
"example.com/greet@v1.0.0/greet.go": b"package greet\n",
}
print("module hash:", h1(module))
module["example.com/greet@v1.0.0/greet.go"] = b"package greet \n" # one extra space
print("after edit: ", h1(module))go.mod hash: h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
module hash: h1:DOlK6GgGEk0ju+OOznaJH9Z2PfWri2zPUecgDSQQYFk=
after edit: h1:0bg5m4g/FkU79TB6P5MY0xSE8LTUp90A3reOX5dTZlo=
A one-byte change anywhere gives an unrelated hash, so go.sum detects any modification of any file.
A mismatch stops the build
Here the session corrupts the recorded hash of uuid, empties the module cache so the module is downloaded again, and builds:
mkdir t && cd t
go mod init example.com/t 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() { fmt.Println(uuid.NewSHA1(uuid.NameSpaceDNS, []byte("go.dev"))) }
SRC
go mod tidy 2>/dev/null
go run .
go clean -modcache
sed -i '1s/h1:[^=]*=/h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=/' go.sum
go run .; echo "exit=$?"91da4ff0-bcdb-5fa4-b85d-7c2de6ebc8fe
go: downloading github.com/google/uuid v1.6.0
verifying github.com/google/uuid@v1.6.0: checksum mismatch
downloaded: h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
go.sum: h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
SECURITY ERROR
This download does NOT match an earlier download recorded in go.sum.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.
For more information, see 'go help module-auth'.
exit=1
In real life the side that is wrong is usually the download: a tag was moved, a proxy served different bits, or someone tampered with a mirror. Never "fix" this error by deleting the go.sum line; find out why the content changed.
go mod verify: is the cache still intact?
go.sum protects downloads. go mod verify checks the other half: that the extracted sources in the module cache still hash to what was downloaded. The cache is read-only on purpose, so changing it takes a deliberate chmod:
mkdir v && cd v
go mod init example.com/v 2>/dev/null
printf 'package main\n\nimport _ "github.com/google/uuid"\n\nfunc main() {}\n' > main.go
go mod tidy 2>/dev/null
go mod verify
dir=$(go list -m -f '{{.Dir}}' github.com/google/uuid)
chmod u+w "$dir/uuid.go" && echo "// patched in place" >> "$dir/uuid.go"
go mod verify; echo "exit=$?"all modules verified
github.com/google/uuid v1.6.0: dir has been modified (/home/learner/go/pkg/mod/github.com/google/uuid@v1.6.0)
exit=1
Builds don't re-verify the cache (it would be slow), so a hand-edited cache goes unnoticed until go mod verify or go clean -modcache. Run go mod verify in CI after downloading, and never edit files under ~/go/pkg/mod; use replace to work on a dependency.
The checksum database (not run here)
go.sum only protects you from changes after your first download. Who checks the first download? That is the job of the checksum database, sum.golang.org, which the go command consults by default (GOSUMDB=sum.golang.org):
- The first time anyone in the world asks for
module@version, the database records itsh1hashes in an append-only transparency log (a Merkle tree, like Certificate Transparency). - Before your go command trusts a module it has no
go.sumline for, it fetches the hashes from the database and checks a cryptographic proof that they are in the signed log. - The go command caches the signed tree heads it has seen (in
$GOMODCACHE/cache/download/sumdb) and checks that every new one extends the previous one. A database that showed different hashes to different users would have to fork its log, which clients detect.
So two developers who never share a go.sum still get bit-for-bit identical modules, and a compromised proxy can't serve modified code without the hashes failing. The database only knows public modules; for private ones, see Private Modules & Proxies (GONOSUMDB, GOPRIVATE).
Note: This course's container sets
GOSUMDB=offbecause it has no network.go.sumis still written and checked, as the sessions show; only step 2, the lookup of first downloads, is skipped. The image's module proxy was filled fromproxy.golang.organd verified againstsum.golang.orgwhen the image was built. Don't setGOSUMDB=offon a developer machine or in CI.
Test yourself
Why do some modules have only a /go.mod line in go.sum?
- They are corrupt
- Their go.mod was read to build the module graph, but none of their packages are compiled
- They are replaced
- They are indirect dependencies, which never get a full hash
What does the checksum database (sum.golang.org) protect that go.sum alone cannot?
- Nothing; it is a cache
- The first download of a module version, by comparing it with the hash the whole world saw
- Private modules
- The module cache on disk
go mod verify reports dir has been modified. What happened?
- go.sum has a wrong line
- Files in the extracted module cache no longer match the hashes of what was downloaded
- The proxy is down
- A newer version exists