Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
server/images: prevent skipVerify map collision with duplicate digests (#15504)
Vignesh4138e853d57c
diff --git a/server/images.go b/server/images.goindex c5ff13380..7e265ea11 100644--- a/server/images.go+++ b/server/images.go@@ -1043,7 +1043,16 @@ func PullModel(ctx context.Context, name string, regOpts *registryOptions, fn fuif err != nil {return err}- skipVerify[layer.Digest] = cacheHit+ // If any download of a given digest was not a cache hit,+ // always verify it. Without this guard, a config entry+ // sharing a digest with a layer can overwrite the layer's+ // false (needs verification) with true (cache hit), since+ // the blob now exists on disk from the first download.+ if existing, ok := skipVerify[layer.Digest]; !ok {+ skipVerify[layer.Digest] = cacheHit+ } else {+ skipVerify[layer.Digest] = existing && cacheHit+ }delete(deleteMap, layer.Digest)}diff --git a/server/images_test.go b/server/images_test.goindex ea10106d0..a3de7fc0f 100644--- a/server/images_test.go+++ b/server/images_test.go@@ -2,14 +2,17 @@ package serverimport ("crypto/sha256"+ "errors""fmt""net/http""net/http/httptest"+ "net/url""os""strings""testing""time"+ "github.com/ollama/ollama/api""github.com/ollama/ollama/fs/ggml""github.com/ollama/ollama/manifest""github.com/ollama/ollama/template"@@ -796,3 +799,56 @@ func TestPullModelManifest(t *testing.T) {})}}++// TestPullModelDuplicateDigestVerifiesBlob pulls a manifest whose config and+// layer share a digest. The registry redirects blob downloads via Location to+// an "internal" path serving bytes that don't match the digest, so PullModel+// must reject the pull with errDigestMismatch.+func TestPullModelDuplicateDigestVerifiesBlob(t *testing.T) {+ t.Setenv("OLLAMA_MODELS", t.TempDir())++ const bogusDigest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"++ backendURL := ""+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch {+ case strings.Contains(r.URL.Path, "/manifests/"):+ w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")+ fmt.Fprintf(w, `{+ "schemaVersion": 2,+ "mediaType": "application/vnd.docker.distribution.manifest.v2+json",+ "config": {+ "mediaType": "application/vnd.ollama.image.config",+ "digest": %q,+ "size": 5+ },+ "layers": [{+ "mediaType": "application/vnd.ollama.image.model",+ "digest": %q,+ "size": 5+ }]+ }`, bogusDigest, bogusDigest)+ case strings.Contains(r.URL.Path, "/internal/blobs/"):+ w.Write([]byte("attacker-controlled-bytes"))+ case strings.Contains(r.URL.Path, "/blobs/"):+ w.Header().Set("Location", backendURL+"/internal"+r.URL.Path)+ w.WriteHeader(http.StatusOK)+ default:+ http.NotFound(w, r)+ }+ }))+ defer ts.Close()+ backendURL = ts.URL++ u, err := url.Parse(ts.URL)+ if err != nil {+ t.Fatal(err)+ }+ n := model.ParseName(u.Host + "/test/attack")+ n.ProtocolScheme = "http"++ err = PullModel(t.Context(), n.String(), ®istryOptions{Insecure: true}, func(api.ProgressResponse) {})+ if !errors.Is(err, errDigestMismatch) {+ t.Fatalf("PullModel = %v, want errDigestMismatch (unverified blob would persist)", err)+ }+}