sumverify

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sumverify answers one question about an installed Go binary: was every module compiled into it checked against sum.golang.org?

The answer comes from the RESULT, not from how the binary was installed. A Go binary records, for its main module and every dependency, the h1: hash of the module zip it was built from (runtime/debug.BuildInfo, read here with debug/buildinfo). The checksum database is an append-only, signed Merkle tree (https://go.dev/design/25530-sumdb) holding the h1: hash of every public module version. If the two agree the binary was built from the bytes the whole world sees; if they disagree it was built from something else. An install made with GONOSUMDB, GOSUMDB=off, GOFLAGS=-insecure, a replace directive or a working tree cannot produce a VERIFIED binary, however the environment was set or hidden.

Lookups go through golang.org/x/mod/sumdb.Client, which verifies the signed tree head with the built-in key and the inclusion of the record; a plain HTTP GET of the record would be believing the transport.

Index

Constants

View Source
const (
	DefaultBase = "https://sum.golang.org"
	DefaultKey  = "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8"
)

DefaultBase and DefaultKey are the checksum database the Go toolchain trusts. The key is cmd/go/internal/modfetch/key.go, copied from the go1.27.4 source tree; every record and tree head is verified against it by golang.org/x/mod/sumdb, so the transport (and any proxy in between) is not trusted.

View Source
const MaxFile = 512 << 20

MaxFile bounds what is hashed.

Variables

This section is empty.

Functions

func ParseHookInput

func ParseHookInput(r io.Reader) (string, error)

ParseHookInput reads a Claude Code PreToolUse payload and returns the Bash command, or "" when it is not a Bash call. It never panics, whatever it is given (fuzzed).

func Programs

func Programs(command, binDir, home string) []string

Programs returns the files under binDir that the command would run: the first word of every simple command, looking through env assignments, launchers (env, sudo, command, exec, nohup, time, nice, xargs, sh -c ...), pipes, lists and $( ) / backtick substitutions. No shell is run.

It is a best-effort reading of what the command LINE runs. It cannot see a program started by another program; the cache, not this parser, is what makes an installed binary trustworthy.

Types

type DBSource

type DBSource struct {
	// contains filtered or unexported fields
}

DBSource is the production Source: a tlog-verifying sumdb.Client.

func NewDBSource

func NewDBSource(ops *Ops) *DBSource

NewDBSource builds a Source over ops.

func (*DBSource) Lookup

func (s *DBSource) Lookup(path, vers string) ([]string, Status, error)

Lookup implements Source.

type Decision

type Decision struct {
	Deny   bool
	Reason string   // shown to the model when Deny
	Notes  []string // for stderr: NOSUM reports, cannot-read allowances
}

Decision is a hook answer.

type Ops

type Ops struct {
	Base string // no trailing slash
	Key  string // verifier key
	Dir  string // durable directory (config/ and cache/)
	HTTP *http.Client
	// contains filtered or unexported fields
}

Ops implements sumdb.ClientOps over HTTP with durable files under Dir.

It is deliberately NOT configurable from the environment: GOPROXY, GOSUMDB, GONOSUMDB and friends are exactly the knobs this package exists to be immune to.

func NewOps

func NewOps(dir string) *Ops

NewOps returns the production configuration rooted at dir.

func (*Ops) Log

func (o *Ops) Log(string)

Log implements sumdb.ClientOps.

func (*Ops) ReadCache

func (o *Ops) ReadCache(name string) ([]byte, error)

ReadCache implements sumdb.ClientOps.

func (*Ops) ReadConfig

func (o *Ops) ReadConfig(name string) ([]byte, error)

ReadConfig implements sumdb.ClientOps: the key, and the latest tree head.

func (*Ops) ReadRemote

func (o *Ops) ReadRemote(path string) ([]byte, error)

ReadRemote implements sumdb.ClientOps.

func (*Ops) SecurityError

func (o *Ops) SecurityError(msg string)

SecurityError implements sumdb.ClientOps: the database lied, or a cached tree head contradicts it. Recorded, so the verdict can say so.

func (*Ops) WriteCache

func (o *Ops) WriteCache(name string, data []byte)

WriteCache implements sumdb.ClientOps. A failure only costs a re-fetch.

func (*Ops) WriteConfig

func (o *Ops) WriteConfig(name string, old, new []byte) error

WriteConfig implements sumdb.ClientOps with compare-then-rename.

type Result

type Result struct {
	Path    string
	Verdict Verdict
	// SHA is the SHA-256 of the file content (empty for NotGo).
	SHA string
	// Main is "module@version" of the main module, when known.
	Main string
	// Unreachable is set when Unverified because the database could not be
	// asked at all (as opposed to answering "no").
	Unreachable bool
	// Cached is set when the answer came from the verdict cache.
	Cached bool
	// Detail is one line of explanation; Bad names the modules at fault.
	Detail string
	Bad    []string
}

Result is the verdict for one file.

type Source

type Source interface {
	Lookup(path, vers string) ([]string, Status, error)
}

Source looks a module version up in the checksum database. The returned lines are go.sum lines ("path version h1:...").

type Status

type Status int

Status classifies a failed lookup.

const (
	// OK: the database answered with a verified record.
	OK Status = iota
	// NotFound: HTTP 404/410, the version is not (yet) in the database.
	NotFound
	// Unreachable: the database could not be asked (transport error, 5xx...).
	Unreachable
	// Failed: the database answered and the answer did not verify.
	Failed
)

type StatusError

type StatusError struct {
	Path string
	Code int
}

StatusError is a non-200 answer from the database.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) NotYetPublished

func (e *StatusError) NotYetPublished() bool

NotYetPublished reports the answers that mean "the database does not know this module version (yet)", as opposed to "the database could not be asked".

type Verdict

type Verdict string

Verdict is the answer for one file.

const (
	// Verified: every module in the binary carries an h1: hash that
	// sum.golang.org confirms.
	Verified Verdict = "VERIFIED"
	// Mismatch: the database knows a module version with a DIFFERENT hash. The
	// binary was built from other bytes than the published ones: tampered.
	Mismatch Verdict = "MISMATCH"
	// Unverified: the database could not confirm it (not yet published, 404,
	// unreachable, misbehaving, or a dependency without a recorded hash). An
	// unreadable answer is never Verified.
	Unverified Verdict = "UNVERIFIED"
	// NoSum: built from a working tree (main module "(devel)"): there is no
	// published hash to compare with.
	NoSum Verdict = "NOSUM"
	// NotGo: not a Go binary.
	NotGo Verdict = "NOTGO"
)

type Verifier

type Verifier struct {
	Source   Source
	CacheDir string
	// Info reads the build info; nil means debug/buildinfo.Read. A seam for
	// tests, which cannot make the go tool stamp arbitrary dependency hashes.
	Info func(io.ReaderAt) (*debug.BuildInfo, error)
	// MaxSize bounds what is hashed; 0 means MaxFile.
	MaxSize int64
}

Verifier verifies binaries. CacheDir holds VERIFIED verdicts keyed by the SHA-256 of the file content; nothing else is cached, so an unverified binary is asked again every time.

func (*Verifier) Decide

func (v *Verifier) Decide(command, binDir, home string) Decision

Decide applies the policy to every program the command runs from binDir.

MISMATCH   deny
UNVERIFIED deny (unless the database simply could not be reached: then the
           hook could not read, and a refusal must be a verdict)
NOSUM      allow, noted
error      allow, noted

func (*Verifier) Verify

func (v *Verifier) Verify(path string) (Result, error)

Verify returns the verdict for the file at path (symlinks are followed: what runs is the target).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL