From 5b42e4cba9e94517d2ff84c9d4aedf93b57282a8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 4 Oct 2026 01:53:00 +1000 Subject: [PATCH] feat(storepkg): fetch packages from any registry, plain HTTP on loopback only --- internal/app/statestore/server.go | 2 +- internal/infrastructure/storepkg/registry.go | 20 +++++++------- .../infrastructure/storepkg/registry_test.go | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 internal/infrastructure/storepkg/registry_test.go diff --git a/internal/app/statestore/server.go b/internal/app/statestore/server.go index 12d647602..4306e7c0a 100644 --- a/internal/app/statestore/server.go +++ b/internal/app/statestore/server.go @@ -266,7 +266,7 @@ func (b *server) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants} if b.pg != nil { packages := storepkg.Service{ - Registry: registry.LocalRegistry{}, + Registry: registry.OCIRegistry{}, Store: postgres.NewInstalledPackages(b.pg), } // A projection failure degrades the org resources a package would have diff --git a/internal/infrastructure/storepkg/registry.go b/internal/infrastructure/storepkg/registry.go index 200a8fe56..ee4d1fbf7 100644 --- a/internal/infrastructure/storepkg/registry.go +++ b/internal/infrastructure/storepkg/registry.go @@ -27,21 +27,20 @@ const ( maxPackageBytes = 64 << 20 ) -// LocalRegistry accepts a local OCI registry over HTTP and verifies the -// manifest and sole layer before returning declarative package content. -type LocalRegistry struct{} +// OCIRegistry fetches a package from an OCI registry and verifies the pinned +// manifest and sole layer before returning package content. A loopback +// registry is reached over plain HTTP; every other host over HTTPS. The digest +// pin, not the transport, is what makes the content trustworthy. +type OCIRegistry struct{} -var _ domain.Registry = LocalRegistry{} +var _ domain.Registry = OCIRegistry{} -func (LocalRegistry) Fetch(ctx context.Context, reference, pin string) (domain.Descriptor, []byte, error) { - if !localReference(reference) { - return domain.Descriptor{}, nil, errors.New("package registry must be local") - } +func (OCIRegistry) Fetch(ctx context.Context, reference, pin string) (domain.Descriptor, []byte, error) { repository, err := remote.NewRepository(reference) if err != nil { return domain.Descriptor{}, nil, err } - repository.PlainHTTP = true + repository.PlainHTTP = loopbackReference(reference) _, stream, err := repository.FetchReference(ctx, pin) if err != nil { return domain.Descriptor{}, nil, err @@ -68,7 +67,8 @@ func (LocalRegistry) Fetch(ctx context.Context, reference, pin string) (domain.D return decodeOCI(manifest, layer, pin) } -func localReference(reference string) bool { +// loopbackReference reports whether the reference names a registry on this host. +func loopbackReference(reference string) bool { host, _, ok := strings.Cut(reference, "/") if !ok { return false diff --git a/internal/infrastructure/storepkg/registry_test.go b/internal/infrastructure/storepkg/registry_test.go new file mode 100644 index 000000000..aa31286c8 --- /dev/null +++ b/internal/infrastructure/storepkg/registry_test.go @@ -0,0 +1,27 @@ +package storepkg + +import "testing" + +// Plain HTTP is for this host only: a reference that merely looks local must +// still go over HTTPS. +func TestLoopbackReference(t *testing.T) { + tests := []struct { + reference string + want bool + }{ + {"localhost:5000/bws:1.0.0", true}, + {"127.0.0.1:5001/bws", true}, + {"[::1]:5000/bws", true}, + {"ghcr.io/samcharles93/archipelago/bws:1.0.0", false}, + {"localhost.evil.example/bws", false}, + {"127.0.0.1.evil.example/bws", false}, + {"bws", false}, + } + for _, tt := range tests { + t.Run(tt.reference, func(t *testing.T) { + if got := loopbackReference(tt.reference); got != tt.want { + t.Fatalf("loopbackReference(%q) = %v, want %v", tt.reference, got, tt.want) + } + }) + } +}