Skip to content

Commit f01589d

Browse files
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>
1 parent daab941 commit f01589d

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

internal/version.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ var Commit = ""
88
func PrintableVersion() string {
99
return fmt.Sprintf("%s (%s)", Version, Commit)
1010
}
11+
12+
// UserAgent returns the version-aware client identity used for outbound requests.
13+
func UserAgent() string {
14+
if Version == "" {
15+
return "LocalAI"
16+
}
17+
return fmt.Sprintf("LocalAI/%s", Version)
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cosignverify
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/google/go-containerregistry/pkg/name"
10+
"github.com/google/go-containerregistry/pkg/v1/remote"
11+
"github.com/mudler/LocalAI/internal"
12+
. "github.com/onsi/ginkgo/v2"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
type roundTripFunc func(*http.Request) (*http.Response, error)
17+
18+
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
19+
return f(req)
20+
}
21+
22+
var _ = Describe("registry User-Agent", func() {
23+
It("identifies LocalAI during signature verification requests", func() {
24+
originalVersion := internal.Version
25+
internal.Version = "v-test"
26+
DeferCleanup(func() {
27+
internal.Version = originalVersion
28+
})
29+
30+
var userAgent string
31+
transport := roundTripFunc(func(req *http.Request) (*http.Response, error) {
32+
userAgent = req.Header.Get("User-Agent")
33+
return &http.Response{
34+
StatusCode: http.StatusOK,
35+
Header: http.Header{
36+
"Content-Type": []string{"application/vnd.oci.image.manifest.v1+json"},
37+
"Docker-Content-Digest": []string{"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
38+
},
39+
Body: io.NopCloser(strings.NewReader("")),
40+
Request: req,
41+
}, nil
42+
})
43+
44+
verifier, err := NewVerifier(Policy{
45+
Issuer: "https://token.actions.githubusercontent.com",
46+
IdentityRegex: `^https://github\.com/example/.*`,
47+
}, nil, transport)
48+
Expect(err).NotTo(HaveOccurred())
49+
50+
ref, err := name.ParseReference("registry.example.com/localai/backend:latest")
51+
Expect(err).NotTo(HaveOccurred())
52+
_, err = remote.Head(ref, verifier.remoteOptions(context.Background())...)
53+
Expect(err).NotTo(HaveOccurred())
54+
Expect(userAgent).To(ContainSubstring("LocalAI/v-test"))
55+
})
56+
})

pkg/oci/cosignverify/verify.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/google/go-containerregistry/pkg/v1/remote"
3131
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
3232

33+
"github.com/mudler/LocalAI/internal"
3334
"github.com/sigstore/sigstore-go/pkg/root"
3435
"github.com/sigstore/sigstore-go/pkg/tuf"
3536
"github.com/sigstore/sigstore-go/pkg/verify"
@@ -297,6 +298,7 @@ func (v *Verifier) remoteOptions(ctx context.Context) []remote.Option {
297298
opts := []remote.Option{
298299
remote.WithContext(ctx),
299300
remote.WithTransport(t),
301+
remote.WithUserAgent(internal.UserAgent()),
300302
}
301303
if v.auth != nil {
302304
opts = append(opts, remote.WithAuth(staticAuth{auth: v.auth}))

pkg/oci/useragent.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package oci
22

3-
import (
4-
"fmt"
5-
6-
"github.com/mudler/LocalAI/internal"
7-
)
3+
import "github.com/mudler/LocalAI/internal"
84

95
// UserAgent returns the User-Agent string LocalAI sends on outbound registry
106
// requests (OCI registries and Ollama). It identifies the client as LocalAI
117
// and, when the binary was built with a version stamp, appends it so registries
128
// can attribute client-side usage to LocalAI rather than to the generic
139
// User-Agent of the underlying transport library.
1410
func UserAgent() string {
15-
if internal.Version == "" {
16-
return "LocalAI"
17-
}
18-
return fmt.Sprintf("LocalAI/%s", internal.Version)
11+
return internal.UserAgent()
1912
}

0 commit comments

Comments
 (0)