hex

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package hex validates Hex tarballs and extracts metadata needed by the signed registry v2 protocol. It never executes metadata.config as Erlang: a bounded parser reads only the term forms the Hex package metadata specification prescribes. It also encodes and verifies the registry's signed resources (names, versions and packages/NAME), so a repository can serve its own signed view and trust an upstream registry's only once its signature verifies.

Role

artifacts module, ecosystem layer: the Hex tarball validation, metadata and registry index rules of the Hex slice. Its Handler plugs into the item service through the format hooks (decision 0032); the slice's http package (artifacts/ecosystem/hex/http) serves the signed registry from the tarballs it keeps.

Inputs

  • Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for hex repositories to Handler, and the work queue, which runs every format's reindex through it; and the protocol, which lists a repository's files (Files), reads stored and upstream tarballs (InspectTarball) and builds or verifies the signed resources.
  • Driven: the format hooks' environment (format.Env), whose blob store Handler reads every tarball from and through whose store it keeps the package index (artifacts/core/pkgindex); and the generic deploy policy (artifacts/ecosystem/generic), which Handler embeds and applies first.
  • Settings: none.

Outputs

  • Handler: the hooks that admit only a valid tarball at the path its own name and version give (Inspect, Admit), refuse a deploy of the generated registry resources, index each stored tarball (Deployed, Reindex) and class each path (Classify).
  • The package index: a package per Hex package name, with a version per stored release.
  • The rules the protocol shares: Package, Requirement, InspectTarball, TarballPath, IsTarballPath, DerivedPath, ValidName, ValidVersion and Files; and the registry codec: RegistryPackage, EncodePackage, EncodeNames, EncodeVersions, DecodePackage, DecodeNames, DecodeVersions, ParsePublicKey and PublicKeyPEM.

Invariants

  • A tarball is admitted only when both tar layers, its inner checksum, its contents' paths and links and its metadata verify, and its name and version give exactly the path it is written to.
  • Every signed resource is a gzip-compressed Signed message of at most 16 MiB whose signature is RSA PKCS #1 v1.5 over the payload's SHA-512, made or checked with a key of at least 2048 bits; a decoder reads no field of the payload before its signature verifies.
  • Every size, count and nesting depth is bounded before it is used.

References

Status

Implemented P9c, which delivered the plan's P30 group (issues #762 to #768). Moved here in P9d.

Testing

  • package_test.go and terms_test.go: the public decimal and ecto tarballs, traversal, duplicate and link refusals in the nested archive, and the metadata.config parser's accepted forms and bounds. testdata/README.md records where every fixture came from; the acceptance steps also serve its telemetry tarball.
  • registry_test.go: hex.pm's signed packages/decimal resource and public key, round trips of every signed resource, and malformed fields.
  • coverage_test.go and security_coverage_test.go: admission and reindex, hostile and truncated wire fields, forged signature envelopes and crafted outer archives.

Index

Constants

View Source
const (
	// MaxTarballBytes is the largest package tarball, 64 MiB: InspectTarball
	// refuses a longer one before it parses anything, and the protocol
	// applies the same bound to an upstream tarball.
	MaxTarballBytes = 64 << 20
)

Variables

This section is empty.

Functions

func DecodeNames

func DecodeNames(body []byte, key *rsa.PublicKey) (string, []string, error)

DecodeNames verifies a signed upstream /names resource before it is used to build a new resource under this repository's own signing key.

func DerivedPath

func DerivedPath(p string) bool

DerivedPath reports whether p is a registry resource the protocol generates: names, versions, public_key or anything under packages/.

func EncodeNames

func EncodeNames(repository string, names []string, private *rsa.PrivateKey) ([]byte, error)

EncodeNames builds the signed names resource of repository: each name once, sorted, in a registry v2 Names message, wrapped in a Signed message whose signature is RSA PKCS #1 v1.5 over the payload's SHA-512, and gzipped, as Mix verifies every registry resource. It sorts names in place. It refuses an empty repository, an invalid or repeated name, a nil key or one shorter than 2048 bits, and a payload over 16 MiB.

func EncodePackage

func EncodePackage(reg RegistryPackage, private *rsa.PrivateKey) ([]byte, error)

EncodePackage builds the signed packages/NAME resource of reg: the registry v2 Package message, its releases in the given order, gzipped and signed with private as EncodeNames signs. A zero OuterChecksum is left out, as older signed records leave it. It refuses an invalid name or version, an empty repository, a release of another package or without an inner checksum, a malformed requirement, retirement, publication time or advisory, an advisory index past reg.Advisories, and the keys and payloads EncodeNames refuses.

func EncodeVersions

func EncodeVersions(
	repository string,
	packages []RegistryPackage,
	private *rsa.PrivateKey,
) ([]byte, error)

EncodeVersions builds the signed versions resource of repository: each package once, sorted by name (in place), with its releases' versions in the given order, gzipped and signed as EncodeNames signs. It refuses an empty repository, an invalid or repeated name, an invalid version, and the keys and payloads EncodeNames refuses.

func Files

func Files(ctx context.Context, st format.Store, c repo.Config) ([]model.Item, error)

Files lists the files repository c's package index reads (pkgindex.Stored): a remote's cached files, or a hosted repository's own, in no promised order. A search failure is returned wrapped.

func IsTarballPath

func IsTarballPath(p string) bool

IsTarballPath reports whether p has the shape of a tarball path: one file name ending .tar under tarballs/, with no traversal, backslash or NUL. It does not check the name and version, so a write is also compared with the TarballPath its tarball's own metadata gives.

func ParsePublicKey

func ParsePublicKey(value string) (*rsa.PublicKey, error)

ParsePublicKey reads the RSA public key an upstream registry signs with (a remote repository's hexPublicKey): one PEM block of type PUBLIC KEY (PKIX) or RSA PUBLIC KEY (PKCS #1), with nothing after it but white space. Any other block or key type, a malformed key and a key shorter than 2048 bits are refused.

func PublicKeyPEM

func PublicKeyPEM(key *rsa.PrivateKey) string

PublicKeyPEM is the public half of key as a PKIX PEM block of type PUBLIC KEY, the form a repository's public_key resource serves and mix hex.repo add reads.

func Serves

func Serves(t repo.PackageType) bool

Serves reports whether t is the Hex package type.

func TarballPath

func TarballPath(name, version string) (string, bool)

TarballPath is the canonical path of a release's tarball, tarballs/NAME-VERSION.tar, as Mix requests it; the bool is false, with an empty path, when name or version is invalid.

func ValidName

func ValidName(name string) bool

ValidName reports whether name can name a Hex package, an OTP application or a registry here: an ASCII letter followed by up to 99 ASCII letters, digits, underscores and hyphens.

func ValidVersion

func ValidVersion(version string) bool

ValidVersion reports whether version is a release version: three dot-separated numbers, then an optional -pre-release and +build, each of ASCII letters, digits, dots and hyphens.

Types

type Handler

type Handler struct{ generic.Handler }

Handler is the format of Hex repositories, which the item service calls on every write and delete in them, whichever API carried it. It admits only package tarballs, each at the path its own metadata gives, and keeps the package index of each name's versions. The registry resources (names, versions, packages/NAME and public_key) are generated and signed by the protocol on each read, so no deploy may supply them.

func (Handler) Admit

func (Handler) Admit(
	ctx context.Context, env format.Env, c repo.Config, w format.Write,
) (format.Write, error)

Admit refuses every write to a federated repository (400), since Hex has no federated class, then applies the generic deploy policy, and passes a checksum write. Any other write must be a tarball at its canonical path: a write of a registry resource that is not cached is refused with 409, and any other path, or a tarball whose name and version do not give its path, with 400. A remote's copy at any other path is kept as a plain file (format.RemoteCopy). It reuses Inspect's result for the same content at the same path, and reads the tarball again otherwise.

func (Handler) Classify

func (Handler) Classify(_ repo.Config, p string) format.Class

Classify reports the registry resources as derived, since every repository generates and signs its own, and every other path as unique.

func (Handler) Deployed

func (Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it model.Item) error

Deployed reads a stored tarball again and links its name and version into the package index. Folders and other paths are not indexed. A tarball that no longer validates at its path fails the hook, which rolls the change back.

func (Handler) Inspect

func (Handler) Inspect(
	ctx context.Context, env format.Env, _ repo.Config, w format.Write,
) (any, error)

Inspect reads a tarball write's content before the write's transaction, so Admit decides without parsing under the write lock. It answers nil for a checksum write and for any path that is not a tarball path. A tarball that does not validate, or whose name and version do not give its path, is refused with 400; a blob that cannot be opened is an error.

func (Handler) Reindex

func (Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error

Reindex rebuilds the package index of repository c from its stored tarballs (Files), reading each again: a version's files are the tarballs of its name and version, and each version and package takes its oldest file's creation time. A stored tarball that no longer validates fails the reindex instead of being dropped from the index.

type Package

type Package struct {
	Name          string
	Version       string
	Requirements  []Requirement
	InnerChecksum [32]byte
	OuterChecksum [32]byte
	// These signed registry fields are retained when a remote view is
	// permission-filtered and re-signed. Local tarballs do not carry them.
	Retirement      []byte
	AdvisoryIndexes []uint64
	PublishedAt     []byte
}

Package is one release of a Hex package: its name, version and requirements, and the two checksums the signed registry carries for it. InnerChecksum is the SHA-256 of the tarball's VERSION, metadata.config and contents.tar.gz, which its CHECKSUM file must state; OuterChecksum is the SHA-256 of the whole tarball, and is zero where an older signed registry record leaves it out.

func InspectTarball

func InspectTarball(r io.Reader) (Package, error)

InspectTarball validates both tar layers and metadata before the item can be admitted to a repository. The source bytes remain unchanged.

type RegistryPackage

type RegistryPackage struct {
	Name       string
	Repository string
	Releases   []Package
	Advisories [][]byte
}

RegistryPackage is the verified content of /packages/NAME. Release checksums are binary SHA-256 digests, exactly as Hex registry v2 prescribes.

func DecodePackage

func DecodePackage(body []byte, key *rsa.PublicKey) (RegistryPackage, error)

DecodePackage verifies a signed packages/NAME resource against key, then reads it: every release takes the package's name, and fields it does not know are skipped. It refuses a bad envelope or signature, a missing or repeated name or repository, an invalid name or version, a repeated version, a release without a version or inner checksum, a malformed requirement or message, an advisory index past the advisories, and more than 10,000 releases.

func DecodeVersions

func DecodeVersions(body []byte, key *rsa.PublicKey) (string, []RegistryPackage, error)

DecodeVersions verifies a signed upstream /versions resource.

type Requirement

type Requirement struct {
	Package     string
	Requirement string
	App         string
	Repository  string
	Optional    bool
}

Requirement is one dependency of a release, as a tarball's metadata.config and the registry's dependency records carry it: the package required, its version requirement, the OTP application that provides it, the registry it comes from (empty for the release's own registry) and whether it is optional.

Directories

Path Synopsis
Package hexhttp serves the signed Hex registry v2 protocol under /artifactory/api/hex/{repoKey}: the repository's public key, the signed names, versions and packages/NAME resources, and package tarballs, each for GET and HEAD.
Package hexhttp serves the signed Hex registry v2 protocol under /artifactory/api/hex/{repoKey}: the repository's public key, the signed names, versions and packages/NAME resources, and package tarballs, each for GET and HEAD.

Jump to

Keyboard shortcuts

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