fix(oci): identify signature verification requests (#11244)

Signed backend verification performs separate registry requests for manifests and referrers. Reuse LocalAI's version-aware User-Agent there so the full install flow is attributable to LocalAI.

Assisted-by: Codex:gpt-5

Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
This commit is contained in:
localai-org-maint-bot
2026-07-31 09:20:26 +02:00
committed by GitHub
parent daab94134c
commit f01589d98b
4 changed files with 68 additions and 9 deletions

View File

@@ -8,3 +8,11 @@ var Commit = ""
func PrintableVersion() string {
return fmt.Sprintf("%s (%s)", Version, Commit)
}
// UserAgent returns the version-aware client identity used for outbound requests.
func UserAgent() string {
if Version == "" {
return "LocalAI"
}
return fmt.Sprintf("LocalAI/%s", Version)
}

View File

@@ -0,0 +1,56 @@
package cosignverify
import (
"context"
"io"
"net/http"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/mudler/LocalAI/internal"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
var _ = Describe("registry User-Agent", func() {
It("identifies LocalAI during signature verification requests", func() {
originalVersion := internal.Version
internal.Version = "v-test"
DeferCleanup(func() {
internal.Version = originalVersion
})
var userAgent string
transport := roundTripFunc(func(req *http.Request) (*http.Response, error) {
userAgent = req.Header.Get("User-Agent")
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/vnd.oci.image.manifest.v1+json"},
"Docker-Content-Digest": []string{"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
},
Body: io.NopCloser(strings.NewReader("")),
Request: req,
}, nil
})
verifier, err := NewVerifier(Policy{
Issuer: "https://token.actions.githubusercontent.com",
IdentityRegex: `^https://github\.com/example/.*`,
}, nil, transport)
Expect(err).NotTo(HaveOccurred())
ref, err := name.ParseReference("registry.example.com/localai/backend:latest")
Expect(err).NotTo(HaveOccurred())
_, err = remote.Head(ref, verifier.remoteOptions(context.Background())...)
Expect(err).NotTo(HaveOccurred())
Expect(userAgent).To(ContainSubstring("LocalAI/v-test"))
})
})

View File

@@ -30,6 +30,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/mudler/LocalAI/internal"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/tuf"
"github.com/sigstore/sigstore-go/pkg/verify"
@@ -297,6 +298,7 @@ func (v *Verifier) remoteOptions(ctx context.Context) []remote.Option {
opts := []remote.Option{
remote.WithContext(ctx),
remote.WithTransport(t),
remote.WithUserAgent(internal.UserAgent()),
}
if v.auth != nil {
opts = append(opts, remote.WithAuth(staticAuth{auth: v.auth}))

View File

@@ -1,10 +1,6 @@
package oci
import (
"fmt"
"github.com/mudler/LocalAI/internal"
)
import "github.com/mudler/LocalAI/internal"
// UserAgent returns the User-Agent string LocalAI sends on outbound registry
// requests (OCI registries and Ollama). It identifies the client as LocalAI
@@ -12,8 +8,5 @@ import (
// can attribute client-side usage to LocalAI rather than to the generic
// User-Agent of the underlying transport library.
func UserAgent() string {
if internal.Version == "" {
return "LocalAI"
}
return fmt.Sprintf("LocalAI/%s", internal.Version)
return internal.UserAgent()
}